One of the critical areas under Linux performance monitoring has to be CPU usage and system load. There are several Linux performance monitoring tools to keep an eye on how things are unfolding on a system.
A number of these tools simply output the system state/statistics while a few others provide you means of managing system performance. One such tool called CPUTool.
CPUTool is a simple yet powerful command-line tool for limiting and controlling CPU utilization of any process to a given limit and allows the interruption of process execution if the system load overreach a defined threshold.
How Does CPUTool Work?
In order to limit CPU usage, cputool sends the SIGSTOP and SIGCONT signals to processes and this is determined by the system load. It relies on the /proc pseudo-filesystem to read PIDs and their CPU usage measures.
It may be used to limit the CPU usage or system load influenced by a single process or a group of processes to a given limit and/or suspend processes if the system load goes beyond a threshold.
Suggested Read: Understand Linux Load Averages and Monitor Performance of Linux
Install CPUTool to Limit CPU Usage and Load Average
A CPUTool is only available to install on Debian/Ubuntu and its derivatives from the default system repositories using package management tool.
$ sudo apt install cputool
Limiting Process CPU Usage With CUPTool
Now lets look at how cputool really works. To demonstrate it all, we will run a dd command which should result into a high CPU percentage, in the background and display its PID.
# dd if=/dev/zero of=/dev/null &
To monitor CPU usage we can use the top or glances tools that allow us to view a real-time regularly updated state of a running Linux system processes:
# top
From the output above, we can see that dd command is having the highest percentage of CPU time 99.7%)
Now we can limit this using cputool as shown below.
The --cpu-limit
or -c
flag is used to set a usage percentage for a process or group of processes and -p
to specify a PID. The following command will limit the dd command (PID 8275) to 50% use of one CPU core:
# cputool --cpu-limit 50 -p 8275
After running cputool, we can check the new CPU usage for the process (PID 8275) once more. Now the CPU usage for dd process should range from (49.0%-52.0%).
# top
To further limit dd’s CPU usage to 20%, we can run cputool for a second time:
# cputool --cpu-limit 20 -p 8275
Then immediately check using tools such as top or glances like this (the CPU usage for dd should now range from 19.0%-22.0% or slightly beyond this):
# top
Note that the shell doesn’t expect any user input while cputool is running; therefore becomes unresponsive. To kill it (this will terminate the CPU usage limitation operation), press Ctrl + C
.
Importantly, to specify a process group (one program with several running instances each with a distinct PID) for instance HTTP web server:
# pidof apache2 9592 3643 3642 3641 3640 3638 3637 1780
Use the -P
flag like this:
# cputool --cpu-limit 20 -P 1780
Limiting System Load with CUPTool
The -l
option is used to specify the maximum load the system may go though for the process or process group to continue running. We may use a fractional value (e.g. 2.5).
The example below means run rsync for a local backup only when the system load does not exceed 3.5:
# cputool --load-limit 3.5 --rsync -av /home/tecmint /backup/`date +%Y-%m-%d`/
For more information and usage, view the CPUTool man page:
# man cputool
Do check out following useful guides for finding CPU info and CPU performance monitoring:
- 9 Useful Commands to Get CPU Information on Linux
- Cpustat – Monitors CPU Utilization by Running Processes in Linux
- CoreFreq – A Powerful CPU Monitoring Tool for Linux Systems
- Find Top Running Processes by Highest Memory and CPU Usage in Linux
In conclusion, CPUTool really comes in handy for Linux performance management. Do share your thoughts about this article via the feedback form below.
The cputool is probably fine for some processes, but not for all of them. The
'dd'
command is stopped when operated with ‘cputool‘ and this can be seen if the ‘dd‘ command is being run in the background and then issued a ‘jobs‘ command.Cputool stops the execution of some of the processes and therefore is not a good limiter of CPU utilization. The ‘cpulimit‘ package does the same.
@emil12334
True, the cputool has some limitations. We will look at this further. Thanks for sharing your experience with it.
yum install cputool
has an error it says no package available for CentOS.cputool --cpu-limit 50 -p 6006
not working, the percentage remains same (100%)What should I do?
@Himanshi,
What error you getting when running the command? could you share the output?
@Himanshi
Which program/process is that whose CPU usage you are trying to limit?
I have downloaded cputool and cpulimit but this command “cputool –cpu-limit 20 -P 1780” is not working on my Ubuntu.
Any reason why? It keeps bringing to me the
--help
page.@speedoelement
The correct command is:
#cputool – -cpu-limit 20 -p 1780
Check the – -cpu-limit option, that is why it’s showing you the help page.
Hello, TecMint Team,
Thanks for writing a wonderful article, I have a doubt. Can we limit uses of CPU in range?
As I want to limit from 50-80% of CPU uses for dd command.
@joe
Good concern, we stated it as a range because after limiting CPU usage to a given percentage, the value will not just remain exactly at what you have set. It keeps on changing but remains either slightly lower or higher that the set value.
Try out the examples provided in the guide, and watch using top or glances what happens after setting a certain percentage. Thanks.