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.



How it works

Note that you don't need to read this paragraph in order to use cpulimit, you can safely skip it if you are not interested in tweaking cpulimit.

So, you are curious to know the secrets of cpulimit 🙂 Of course there is no secret if you are a C developer, but I will try to explain to everyone. The work of cpulimit is done all in userspace, so it doesn't interfere with the Linux scheduler. Basically, the target process, which you can specify by pid, name, or command line, is continuosly paused and resumed by sending it SIGSTOP and SIGCONT signals. Signals are sent by cpulimit in appropriate moments, based on the limit specified by user and the process statistics read from /proc.

System Requirements

cpulimit should run on every Linux 2.2 or greater. It has been reported by several users that cpulimit works fine even on SMP hardware, but consider that if you have more than one cpu there is a little difference in the meaning of cpu usage .

If you can modify the source code of cpulimit to make it run in another OS, please notify me, so I can publish your code. I think that the only non-portable code is to iterate through the process list and get process statistics.

Install cpulimit in debian

#apt-get install cpulimit

Examples of use

Limit the process ‘bigloop' by executable name to 40% CPU:

cpulimit -- exe bigloop -- limit 40

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

Limit a process by PID to 55% CPU: cpulimit -- pid 2960 -- limit 55 Launch a process by command line and limit it to 40% (in development version only!):

cpulimit -- limit 40 /etc/rc.d/rc.boinc start

cpulimit manpage

cpulimit -- limits the CPU usage of a process

Synopsis

cpulimit TARGET [OPTIONS...]

Description

TARGET must be exactly one of these:

-p, -- pid=N pid of the process

-e, -- exe=FILE name of the executable program file

-P, -- path=PATH absolute path name of the executable program file

OPTIONS

-l, -- limit=N percentage of CPU allowed from 0 to 100 (mandatory)

-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

Examples

Assuming you have started ‘foo --bar‘ and you find out with top or ps that this process uses all your CPU time you can either

# cpulimit -e foo -l 50

limits the CPU usage of the process by acting on the executable program file (note: the argument “ -- bar” is omitted)

# cpulimit -p 1234 -l 50

limits the CPU usage of the process by acting on its PID, as shown by ps

# cpulimit -P /usr/bin/foo -l 50

same as -e but uses the absolute path name

