It is also important to limit the use of the CPU to prevent server crashes. This is especially useful for any custom scripts you might run in a crontab. First, we're going to spin up an Ubuntu 12.10 x64 droplet:
Install CPULimit
Apt-get-y Install CPULimit
Usage Syntax
Usage:cpulimit TARGET [OPTIONS ...]
TARGET must is 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
-B--background run in background
-L,--limit=n percentage of CPU allowed from 0 to (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
Benchmark CPU Usage
Let our benchmark test CPU usage without cpulimit.
Here is an example of how you can use your CPU to have an application:
Md5sum/dev/zero &
This ' forks ' md5sum and then processed into the background.
Now you can see the CPU usage at the top:
As you can see it consumes almost 100% of the CPU resources (as we drop a CPU at this core).
We can take this process to the front desk using FG and CTRL + C to cancel it:
Limit CPU usage and please cpulimit
Now we can test it please will cpulimit and see what it actually does what it should be.
Let's limit CPU usage to 40% and run the same command:
Cpulimit-l Md5sum/dev/zero &
Sure enough, this is limited to 40%:
Multi-nuclear liquid droplets
In droplets with multiple processors, you need to limit the CPU utilization of each process.
Here is the 4 process with no restrictions on the script and let them run on your server at the same time:
For j in ' seq 1 4 '; Do Md5sum/dev/zero & Done
Each CPU core is resource utilization up to 100%:
Top-23:29:28 up to 7 days, 13:54, 1 user, load average:0.80, 1.08, 0.53
Tasks:77 Total, 5 running, sleeping, 0 stopped, 0 zombie
%cpu0:100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0th
%cpu1:93.2 us, 6.8 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0th
%cpu2:95.0 us, 5.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0th
%cpu3:98.3 us, 1.7 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0th
KiB mem:8178228 Total, 380196 used, 7798032 free, 28136 buffers
KiB swap:0 Total, 0 used, 0 free, 251708 cached
PID USER PR NI virt RES SHR S%cpu%mem time+ COMMAND
8400 Root 0 7172 612 520 R 101.7 0.0 0:03.10 md5sum 8401 Root 0 7172 612 520 R 101.7 0.0 0 : 03.10 md5sum 8399 Root 0 7172 616 520 r 98.4 0.0 0:03.06 md5sum 8402 Root 0 7172 612 520 R 98.4 0.0 0:03.09 md5sum
To use, place the cpulimit in front of the command for each process:
For j in ' seq 1 4 '; Do cpulimit-l Md5sum/dev/zero & Done
Each process now uses up to 40% of each thread and does not overload the server:
Top-23:31:03 up to 7 days, 13:55, 1 user, load average:2.68, 1.72, 0.82
tasks:81 Total, 5 running, sleeping, 0 stopped, 0 zombie
%cpu0:39.4 us, 0.7 sy, 0.0 ni, 59.6 ID, 0.0 wa, 0.0 hi, 0.0 si, 0.3rd
%cpu1:38.7 us, 1.7 sy, 0.0 ni, 59.6 ID, 0.0 wa, 0.0 hi, 0.0 si, 0.0th
%cpu2:39.4 us, 1.3 sy, 0.0 ni, 59.3 ID, 0.0 wa, 0.0 hi, 0.0 si, 0.0th
%cpu3:39.4 us, 1.7 sy, 0.0 ni, 58.9 ID, 0.0 wa, 0.0 hi, 0.0 si, 0.0th
KiB mem:8178228 Total, 380452 used, 7797776 free, 28144 buffers
KiB swap:0 Total, 0 used, 0 free, 251708 cached
PID USER PR NI virt RES SHR S%cpu%mem time+ COMMAND
8442 Root 0 7172 616 520 r 40.4 0.0 0:06.10 md5sum 8440 Root 0 7172 612 520 r 40.0 0.0 0:06.09 md5sum 8435 Root 0 7172 616 520 R 39.7 0.0 0:06.09 md5sum
8436 Root 0 7172 612 520 R 39.7 0.0 0:06.10 md5sum
Limit Firefox use 30% CPU utilization
# CPULIMIT-E Firefox-l 30 The program that restricts process number 1313 uses 30% CPU utilization
# cpulimit-p 1313-l 30 Find the PID method according to the process name
# PS aux | Less
# PS aux | grep Firefox
# pgrep-u Vivek PHP-CGI
# pgrep LIGHTTPD can limit the CPU utilization of a process using absolute paths
# cpulimit-p/opt/firefox/firebox-l 30 single-core CPUs and multi-core CPUs
The single core CPU utilization range is 0%-100%, the Quad core CPU utilization rate range is 0%-400%.