We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsOperating SystemsLinuxAdministration

How to limit CPU usage on Ubuntu

Windows Cloud Server
Ubuntu VPS
USA VPS
Linux Cloud Server
Germany Cloud Servers
OpenVZ VPS
Linux KVM Hosting
cPanel KVM
Brazil VPS
KVM VPS

In this tutorial we will describe how to limit CPU usage in Ubuntu 14.04. We will be using CPU-limit utility for this purpose. Cpulimit is a tool which limits the CPU usage of a process (expressed in percentage, not in CPU time). It is useful to control batch jobs when you don’t want them to eat too many CPU cycles. The goal of cpulimit is to prevent a process from running for more than a specified time ratio. It does not change scheduling priority settings, but the real CPU usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

Follow These Steps to limit CPU usage on Ubuntu

First, we need to install cpulimit:

apt-get update
apt-get install cpulimit

Now we will check the utility for limiting the CPU usage. For this we will first check the CPU usage without cpulimit and then implement the cpulimit to evaluate the same.

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

dd if=/dev/zero of=/dev/null &


root@server1:~# dd if=/dev/zero of=/dev/null &
[1] 1850
root@server1:~#

Then we will check the CPU usage with command:

top


top – 11:24:18 up 49 min, 1 user, load average: 0.94, 1.02, 1.79
Tasks: 249 total, 2 running, 247 sleeping, 0 stopped, 0 zombie
%Cpu(s): 13.4 us, 11.6 sy, 0.0 ni, 74.9 id, 0.0 wa, 0.1 hi, 0.0 si, 0.0 st
KiB Mem: 1010540 total, 271652 used, 738888 free, 21760 buffers
KiB Swap: 1048572 total, 0 used, 1048572 free. 158204 cached Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1850 root 20 0 7224 616 520 R 100.0 0.1 1:20.33 dd
1851 root 20 0 24952 1756 1180 R 0.3 0.2 0:00.03 top
1 root 20 0 33480 2776 1456 S 0.0 0.3 0:05.31 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd

As we can see that CPU usage havs gone 100%, now we will use the cpulimit to limit the CPU usage. We can bring up this process to foreground using fg and cancel it with CTRL+C

fg


root@server1:~# fg
dd if=/dev/zero of=/dev/null
^C222182151+0 records in
222182150+0 records out
113757260800 bytes (114 GB) copied, 259.084 s, 439 MB/s

root@server1:~#

Now we can test cpulimit to see if it actually does what it is supposed to. Let test it as follows:

cpulimit -l 30 dd if=/dev/zero of=/dev/null &


root@server1:~# cpulimit -l 30 dd if=/dev/zero of=/dev/null &
[1] 1852
root@server1:~# Process 1853 detected

[1]+ Done cpulimit -l 30 dd if=/dev/zero of=/dev/null
root@server1:~#

Now we will check the CPU usage with top command:

top


top – 11:30:54 up 55 min, 1 user, load average: 0.20, 0.58, 1.34
Tasks: 250 total, 2 running, 247 sleeping, 1 stopped, 0 zombie
%Cpu(s): 4.5 us, 4.1 sy, 0.0 ni, 91.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 1010540 total, 271944 used, 738596 free, 21816 buffers
KiB Swap: 1048572 total, 0 used, 1048572 free. 158212 cached Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1853 root 20 0 7224 612 520 T 33.8 0.1 0:35.53 dd
1 root 20 0 33480 2776 1456 S 0.0 0.3 0:05.37 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0

Now you can see that the CPU usage have decreased from 100% to 33.8% almost 30%. So we have successfully checked the utility cpulimit which is able to limit the usage of CPU consumption in a single core Ubuntu distro.

Congratulations! We have successfully tested the cpulimit for limiting the CPU usage in Ubuntu 14.04.

cPanel KVM
Brazil VPS
Windows Cloud Server
USA VPS
OpenVZ VPS
KVM VPS
Ubuntu VPS
Linux Cloud Server
Germany Cloud Servers
Linux KVM Hosting
Rate this Tutorial:
No Comments Leave a Comment
Leave a Comment

Other (3)

Popular Keywords