(Reproduced) The use of cpu limit

Three methods (transfer) to limit CPU usage under linux
original woodcol finally released on 2017-11-04 21:58:30 read the number 6701 collection
launched
in the cloud server rented shelves BTCC get a real-time trading data server program, Because a while loop is used to process data information, the processor usage on the cloud server has been more than 90%. I have received emails with high CPU usage in the mailbox for two days. I found the following on the Internet. Ways to limit CPU usage. I used the cuplimit method below. There was a python compiler error when installing using apt-get, so I simply downloaded the cpulimit source code and made it myself.

From: http://blog.163.com/song_0803/blog/static/4609759720091014106684/

3 ways to limit CPU usage under Linux

1, Limit function of apache itself (RLimitCPU)

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

Quote this post abroad

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.
This limit will be applied to the process derived from the Apache subservice request, not the Apache subprocess itself. This scope includes CGI scripts and SSI execution commands, but does not include all processes derived from the Apache parent process. For example, pipeline logs.

If no child process does not spawn a process, or this process that occupies a lot of resources does not belong to a spawned process, isn't that no way? So, this method does not work.

2. Limit the use of resources through the system's ulimit command

The settings file is at:

/etc/security/limits.conf

such as:

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

It is possible to limit in this way, it is based on the user to limit, you can limit the memory, CPU time, the number of open files, and so on. The premise is that you have to log in to this system. If you only run files as this user If it doesn't work, then ... so I still have to give up.

3, Aso 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的百分比,而不是时间.很好,很舒服.

Start the installation.

Official address: http://cpulimit.sourceforge.net/

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

cd cpulimit 
make 

cp ./cpulimit /usr/sbin

So simple, is it pleasant?

Restriction method:

If you limit the process name, for example, limit the CPU of the process name httpd to 40%

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

Limit the CPU of the process with pid 2960 to 55%

cpulimit --pid 2960 --limit 55

Slowly compare the CPU changes in the top command. Hey, is it effective?

Another perfect

Automatically limit the current process to use processes with more than 200% CPU, limit him to 100%

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

 

cpulimit command:

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

Guess you like

Origin www.cnblogs.com/luruiyuan/p/12676758.html