(转载) cpu limit的使用

linux下限制CPU使用率的3种方法 (转)
原创woodcol 最后发布于2017-11-04 21:58:30 阅读数 6701 收藏
展开
在租的云服务器上架了一个获取BTCC实时交易数据的服务器程序,因为用了一个while死循环处理数据信息,所以云服务器上的处理器占用率一直在90%多,这两天邮箱里一直收到服务器CPU占用率过高的邮件,在网上找到了下边的这种方法来限制CPU占用率。我用的是下边的cuplimit这个方法,使用apt-get安装时出现了一个python编译器的错误,所以干脆下载了cpulimit源码,自已直接make了。

转自:http://blog.163.com/song_0803/blog/static/4609759720091014106684/

linux下限制CPU使用率的3种方法

1,apache本身的限制功能(RLimitCPU)

http://www.wrensoft.com/forum/archive/index.php/t-12.html

引用国外这个帖子

Ray03-19-2008, 05:20 AM
The PHP test can be used to show that the problem is evident, but it is not conclusive to prove that there is no limit put in place.

The reason is that it depends on how PHP is configured on the server. If the PHP scripting engine is setup as a CGI application which is forked by Apache, then the RLimit* operatives will apply and the PHP script can prove this. If they run within httpd however, the limits would not apply to the PHP engine.
这个限制将施于Apache子服务请求衍生出的进程,而不是Apache子进程本身。这个范围包括CGI脚本和SSI执行命令,但不包括所有从Apache父进程衍生出的进程。比如说管道日志。

如果不子进程不衍生进程,或非常占资源的这个进程不属于衍生出来的进程,那不就是没办法了吗?所以呀,这个办法不行地.

2,通过系统的 ulimit命令限制资源的使用

设置文件在:

/etc/security/limits.conf

比如:

xok_la           hard    cpu             1 
xok_la           hard    fsize           50000 
xok_la           hard    memlock         1000 
xok_la           hard    nofile          50 
xok_la           hard    nproc           50

这种方式限制是可以的,他是基于用户来限制的,可以限制内存,CPU占用时间,打开的文件数等等.前提是你要登陆到这个系统上.如果只是以这个用户来单独运行文件的话,就不行…….所以我还是要放弃.

3,安装CPU Usage Limiter for Linux

What is it?
cpulimit is a simple program that attempts to limit the cpu usage of a process (expressed in percentage, not in cpu time). This is useful to control batch jobs, when you don't want them to eat too much cpu. It does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.
可以限制程序使用CPU的百分比,而不是时间.很好,很舒服.

开始安装吧.

官方地址:http://cpulimit.sourceforge.net/

cd /root/install/ 
svn checkout https://cpulimit.svn.sourceforge.net/svnroot/cpulimit/trunkcpulimit 

cd cpulimit 
make 

cp ./cpulimit /usr/sbin

如此简单,很愉快吧?

限制方法:

如果限制进程名,比如将httpd这个进程名的CPU限制在40%

cpulimit --exe httpd --limit 40 
cpulimit --exe /usr/local/bin/httpd --limit 40

将pid为2960的进程的CPU限制在55%

cpulimit --pid 2960 --limit 55

慢慢对比top 命令那里的CPU变化.嘿嘿,有效果了吧?.

再来个完美的

自动限制当前进程使用CPU超过200%的进程,将他限制为100%

for x in `ps -aux|awk '{if($3 > 20) print $2}'`; do cpulimit --pid $x --limit 100; done

 

cpulimit命令:

Error: You must specify a target process, by name or by PID
Usage: cpulimit TARGET [OPTIONS...]
   TARGET must be exactly one of these:
      -p, --pid=N        pid of the process (implies -z)
      -e, --exe=FILE     name of the executable program file or absolute path name
   OPTIONS
      -l, --limit=N      percentage of cpu allowed from 0 to 100 (required)
      -v, --verbose      show control statistics
      -z, --lazy         exit if there is no suitable target process, or if it dies
      -h, --help         display this help and exit

posted @ 2020-04-10 22:47  仰望高端玩家的小清新  阅读(2030)  评论(0编辑  收藏  举报