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.
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:
Monitor dd Command CPU Usage
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.
-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%).
Limit Process CPU to 50% Usage
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):
Limit Process CPU Usage to 20%
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
-P flag like this:
# cputool --cpu-limit 20 -P 1780
Limiting System Load with CUPTool
-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.