通常我們都認(rèn)為CPU越快越好,真的是這樣嗎?不盡然,高頻的CPU固然能做更多的事情,但是對(duì)于消耗較小的系統(tǒng),或者服務(wù)器系統(tǒng)而言,為了保證CPU的高效能就必須從減少平常的系統(tǒng)開(kāi)銷做起了。在本文博遠(yuǎn)電子將帶給您3種linux平臺(tái)下限制CPU使用率的方法,進(jìn)而提高服務(wù)器的效能。
1,apache本身的限制功能(RLimitCPU)
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.
這個(gè)限制將施于Apache子服務(wù)請(qǐng)求衍生出的進(jìn)程,而不是Apache子進(jìn)程本身。這個(gè)范圍包括CGI腳本和SSI執(zhí)行命令,但不包括所有從Apache父進(jìn)程衍生出的進(jìn)程。比如說(shuō)管道日志。
如果不子進(jìn)程不衍生進(jìn)程,或非常占資源的這個(gè)進(jìn)程不屬于衍生出來(lái)的進(jìn)程,那不就是沒(méi)辦法了嗎?所以呀,這個(gè)辦法不行地.
2,通過(guò)系統(tǒng)的 ulimit命令限制資源的使用
設(shè)置文件在:
/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
這種方式限制是可以的,他是基于用戶來(lái)限制的,可以限制內(nèi)存,CPU占用時(shí)間,打開(kāi)的文件數(shù)等等.前提是你要登陸到這個(gè)系統(tǒng)上.如果只是以這個(gè)用戶來(lái)單獨(dú)運(yùn)行文件的話,就不行…….所以我還是要放棄.
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的百分比,而不是時(shí)間.很好,很舒服.
開(kāi)始安裝吧.
官方地址:http://cpulimit.sourceforge.net/
cd /root/install/
svn checkouthttps://cpulimit.svn.sourceforge.net/svnroot/cpulimit/trunk cpulimit
cd cpulimit
make
cp ./cpulimit /usr/sbin
如此簡(jiǎn)單,很愉快吧?
限制方法:
如果限制進(jìn)程名,比如將httpd這個(gè)進(jìn)程名的CPU限制在40%
cpulimit --exe httpd --limit 40
cpulimit --exe /usr/local/bin/httpd --limit 40
將pid為2960的進(jìn)程的CPU限制在55%
cpulimit --pid 2960 --limit 55
慢慢對(duì)比top 命令那里的CPU變化.嘿嘿,有效果了吧?.
再來(lái)個(gè)完美的
自動(dòng)限制當(dāng)前進(jìn)程使用CPU超過(guò)20%的進(jìn)程,將他限制為10%
cpulimit --pid `ps aux|awk '{if($3 > 20) print $2}'` --limit 10
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
來(lái)源:博遠(yuǎn)電子(軟件定制),如涉及版權(quán)問(wèn)題請(qǐng)與我們聯(lián)系。
TAG
軟件定制,軟件開(kāi)發(fā),瀚森HANSEN,遼寧,沈陽(yáng),撫順