By:
Bulat Khamitov
We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Limit CPU Usage On Ubuntu 12.10

PostedJune 8, 2013 31.9k views System Tools Ubuntu

Limiting CPU usage can be important to prevent your server from crashing.

This is especially useful for any custom scripts you might be running in a crontab.

First, we will spin up a Ubuntu 12.10 x64 droplet:

Install cpulimit

apt-get -y install cpulimit

Usage Syntax

Usage: cpulimit TARGET [OPTIONS...]
   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
      -b  --background   run in background
      -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

Benchmark CPU Usage

Lets benchmark our CPU usage without cpulimit.

Here is an example of how to utilize your CPU with an application:

md5sum /dev/zero &

This 'forks' the md5sum process into background. You can now see CPU usage with top:

As you can see it is consuming almost 100% of CPU resources (given that we have one CPU core on this droplet).

We can bring up this process to foreground using fg and cancel it with CTRL+C:

Limit CPU usage with cpulimit

Now we can test cpulimit to see if it actually does what it is supposed to.

Lets limit our CPU usage to 40% and run same command:

cpulimit -l 40 md5sum /dev/zero &

Sure enough, it is limited to 40%:

Multi-core Droplets

On droplets with multiple processors, you would need to limit CPU usage on each process.

Here is a script that forks 4 processes without any restrictions and lets them run concurrently on your server:

for j in `seq 1 4`; do md5sum /dev/zero & done

Each CPU core is using almost 100% of resources:

top - 23:29:28 up 7 days, 13:54,  1 user,  load average: 0.80, 1.08, 0.53
Tasks:  77 total,   5 running,  72 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.0 st
%Cpu1  : 93.2 us,  6.8 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 95.0 us,  5.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 98.3 us,  1.7 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
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      20   0  7172  612  520 R 101.7  0.0   0:03.10 md5sum                                                                                                                                                                         
 8401 root      20   0  7172  612  520 R 101.7  0.0   0:03.10 md5sum                                                                                                                                                                         
 8399 root      20   0  7172  616  520 R  98.4  0.0   0:03.06 md5sum                                                                                                                                                                         
 8402 root      20   0  7172  612  520 R  98.4  0.0   0:03.09 md5sum                                                                                                                     

To use cpulimit for each process, place it in front of the command:

for j in `seq 1 4`; do cpulimit -l 40 md5sum /dev/zero & done

Now each process uses at most 40% of each thread, and does not overload the server:

top - 23:31:03 up 7 days, 13:55,  1 user,  load average: 2.68, 1.72, 0.82
Tasks:  81 total,   5 running,  76 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.3 st
%Cpu1  : 38.7 us,  1.7 sy,  0.0 ni, 59.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 39.4 us,  1.3 sy,  0.0 ni, 59.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 39.4 us,  1.7 sy,  0.0 ni, 58.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
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      20   0  7172  616  520 R  40.4  0.0   0:06.10 md5sum                                                                                                                                                                         
 8440 root      20   0  7172  612  520 R  40.0  0.0   0:06.09 md5sum                                                                                                                                                                         
 8435 root      20   0  7172  616  520 R  39.7  0.0   0:06.09 md5sum                                                                                                                                                                         
 8436 root      20   0  7172  612  520 R  39.7  0.0   0:06.10 md5sum                                                                                          

And you are all done!

6 Comments

Creative Commons License