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:
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.
Limiting System Load with CPUTool
The -l option is used to specify the maximum load the system may go through 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/rootadminz /backup/`date +%Y-%m-%d`/
For more information and usage, view the CPUTool man page:
# man cputool