{"id":11319,"date":"2019-03-13T00:45:01","date_gmt":"2019-03-13T00:45:01","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11319"},"modified":"2019-03-13T00:45:01","modified_gmt":"2019-03-13T00:45:01","slug":"cputool-limit-and-control-cpu-utilization-of-any-process-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/cputool-limit-and-control-cpu-utilization-of-any-process-in-linux\/","title":{"rendered":"CPUTool \u2013 Limit and Control CPU Utilization of Any Process in Linux"},"content":{"rendered":"<p>One of the critical areas under\u00a0<a href=\"https:\/\/www.tecmint.com\/command-line-tools-to-monitor-linux-performance\/\" target=\"_blank\" rel=\"noopener noreferrer\">Linux performance monitoring<\/a>\u00a0has to be CPU usage and system load. There are several\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-performance-monitoring-tools\/\" target=\"_blank\" rel=\"noopener noreferrer\">Linux performance monitoring tools<\/a>\u00a0to keep an eye on how things are unfolding on a system.<\/p>\n<p>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\u00a0<strong>CPUTool<\/strong>.<\/p>\n<p><strong>CPUTool<\/strong>\u00a0is 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.<\/p>\n<h4>How Does CPUTool Work?<\/h4>\n<p>In order to limit CPU usage, cputool sends the\u00a0<strong>SIGSTOP<\/strong>\u00a0and\u00a0<strong>SIGCONT<\/strong>\u00a0signals to processes and this is determined by the system load. It relies on the\u00a0<a href=\"https:\/\/www.tecmint.com\/exploring-proc-file-system-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">\/proc pseudo-filesystem<\/a>\u00a0to read\u00a0<strong>PIDs<\/strong>\u00a0and their CPU usage measures.<\/p>\n<p>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.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/understand-linux-load-averages-and-monitor-performance\/\" target=\"_blank\" rel=\"noopener noreferrer\">Understand Linux Load Averages and Monitor Performance of Linux<\/a><\/p>\n<h3>Install CPUTool to Limit CPU Usage and Load Average<\/h3>\n<p>A\u00a0<strong>CPUTool<\/strong>\u00a0is only available to install on Debian\/Ubuntu and its derivatives from the default system repositories using package management tool.<\/p>\n<pre>$ sudo apt install cputool\r\n<\/pre>\n<h4>Limiting Process CPU Usage With CUPTool<\/h4>\n<p>Now lets look at how cputool really works. To demonstrate it all, we will run a\u00a0<strong>dd command<\/strong>\u00a0which should result into a high CPU percentage, in the background and display its PID.<\/p>\n<pre># dd if=\/dev\/zero of=\/dev\/null &amp;\r\n<\/pre>\n<p>To monitor CPU usage we can use the\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">top<\/a>\u00a0or\u00a0<a href=\"https:\/\/www.tecmint.com\/glances-an-advanced-real-time-system-monitoring-tool-for-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">glances<\/a>\u00a0tools that allow us to view a real-time regularly updated state of a running Linux system processes:<\/p>\n<pre># top\r\n<\/pre>\n<div id=\"attachment_25833\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25833\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Monitor-dd-Command-CPU-Usage.png\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Monitor-dd-Command-CPU-Usage.png 805w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Monitor-dd-Command-CPU-Usage-768x437.png 768w\" alt=\"Monitor dd Command CPU Usage\" width=\"805\" height=\"458\" data-lazy-loaded=\"true\" \/><\/p>\n<p class=\"wp-caption-text\">Monitor dd Command CPU Usage<\/p>\n<\/div>\n<p>From the output above, we can see that\u00a0<strong>dd command<\/strong>\u00a0is having the highest percentage of CPU time\u00a0<code>99.7%)<\/code>Now we can limit this using cputool as shown below.<\/p>\n<p>The\u00a0<code>--cpu-limit<\/code>\u00a0or\u00a0<code>-c<\/code>\u00a0flag is used to set a usage percentage for a process or group of processes and\u00a0<code>-p<\/code>\u00a0to specify a PID. The following command will limit the dd command (PID 8275) to\u00a0<strong>50%<\/strong>\u00a0use of one CPU core:<\/p>\n<pre># cputool --cpu-limit 50 -p 8275 \r\n<\/pre>\n<p>After running cputool, we can check the new CPU usage for the process (PID 8275) once more. Now the CPU usage for\u00a0<strong>dd<\/strong>\u00a0process should range from (<strong>49.0%-52.0%<\/strong>).<\/p>\n<pre># top\r\n<\/pre>\n<div id=\"attachment_25834\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25834\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-CPU-Usage-on-Linux-Process.png\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-CPU-Usage-on-Linux-Process.png 805w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-CPU-Usage-on-Linux-Process-768x437.png 768w\" alt=\"Limit Process CPU to 50% Usage\" width=\"805\" height=\"458\" data-lazy-loaded=\"true\" \/><\/p>\n<p class=\"wp-caption-text\">Limit Process CPU to 50% Usage<\/p>\n<\/div>\n<p>To further limit dd\u2019s CPU usage to\u00a0<strong>20%<\/strong>, we can run cputool for a second time:<\/p>\n<pre># cputool --cpu-limit 20 -p 8275 \r\n<\/pre>\n<p>Then immediately check using tools such as\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">top<\/a>\u00a0or\u00a0<a href=\"https:\/\/www.tecmint.com\/glances-an-advanced-real-time-system-monitoring-tool-for-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">glances<\/a>\u00a0like this (the CPU usage for\u00a0<strong>dd<\/strong>\u00a0should now range from\u00a0<strong>19.0%-22.0%<\/strong>\u00a0or slightly beyond this):<\/p>\n<pre># top\r\n<\/pre>\n<div id=\"attachment_25835\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25835\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-Process-CPU-Usage-in-Linux.png\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-Process-CPU-Usage-in-Linux.png 802w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-Process-CPU-Usage-in-Linux-768x439.png 768w\" alt=\"Limit Process CPU Usage to 20%\" width=\"802\" height=\"458\" data-lazy-loaded=\"true\" \/><\/p>\n<p class=\"wp-caption-text\">Limit Process CPU Usage to 20%<\/p>\n<\/div>\n<p>Note that the shell doesn\u2019t expect any user input while cputool is running; therefore becomes unresponsive. To kill it (this will terminate the CPU usage limitation operation), press\u00a0<code>Ctrl + C<\/code>.<\/p>\n<p>Importantly, to specify a process group (one program with several running instances each with a distinct PID) for instance HTTP web server:<\/p>\n<pre># pidof apache2\r\n9592 3643 3642 3641 3640 3638 3637 1780\r\n<\/pre>\n<p>Use the\u00a0<code>-P<\/code>\u00a0flag like this:<\/p>\n<pre># cputool --cpu-limit 20 -P 1780\r\n<\/pre>\n<h4>Limiting System Load with CUPTool<\/h4>\n<p>The\u00a0<code>-l<\/code>\u00a0option 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.\u00a0<strong>2.5<\/strong>).<\/p>\n<p>The example below means run\u00a0<a href=\"https:\/\/www.tecmint.com\/rsync-local-remote-file-synchronization-commands\/\" target=\"_blank\" rel=\"noopener noreferrer\">rsync<\/a>\u00a0for a local backup only when the system load does not exceed\u00a0<strong>3.5<\/strong>:<\/p>\n<pre># cputool --load-limit 3.5 --rsync -av \/home\/tecmint \/backup\/`date +%Y-%m-%d`\/\r\n<\/pre>\n<p>For more information and usage, view the CPUTool man page:<\/p>\n<pre># man cputool\r\n<\/pre>\n<p>Do check out following useful guides for finding CPU info and CPU performance monitoring:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/check-linux-cpu-information\/\" target=\"_blank\" rel=\"noopener noreferrer\">9 Useful Commands to Get CPU Information on Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/cpustat-monitors-cpu-utilization-by-processes-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">Cpustat \u2013 Monitors CPU Utilization by Running Processes in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/corefreq-linux-cpu-monitoring-tool\/\" target=\"_blank\" rel=\"noopener noreferrer\">CoreFreq \u2013 A Powerful CPU Monitoring Tool for Linux Systems<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/find-linux-processes-memory-ram-cpu-usage\/\" target=\"_blank\" rel=\"noopener noreferrer\">Find Top Running Processes by Highest Memory and CPU Usage in Linux<\/a><\/li>\n<\/ol>\n<p>In conclusion,\u00a0<strong>CPUTool<\/strong>\u00a0really comes in handy for Linux performance management.<\/p>\n<p>&nbsp;<br \/>\n<a href=\"https:\/\/www.tecmint.com\/cputool-limit-linux-process-cpu-usage-load\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the critical areas under\u00a0Linux performance monitoring\u00a0has to be CPU usage and system load. There are several\u00a0Linux performance monitoring tools\u00a0to 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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/cputool-limit-and-control-cpu-utilization-of-any-process-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;CPUTool \u2013 Limit and Control CPU Utilization of Any Process in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11319","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11319"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11319\/revisions"}],"predecessor-version":[{"id":11320,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11319\/revisions\/11320"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}