{"id":11692,"date":"2019-03-15T22:27:19","date_gmt":"2019-03-15T22:27:19","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11692"},"modified":"2019-03-15T22:27:19","modified_gmt":"2019-03-15T22:27:19","slug":"cpustat-monitors-cpu-utilization-by-running-processes-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/cpustat-monitors-cpu-utilization-by-running-processes-in-linux\/","title":{"rendered":"Cpustat \u2013 Monitors CPU Utilization by Running Processes in Linux"},"content":{"rendered":"<p><strong>Cpustat<\/strong>\u00a0is a powerful system performance measure program for Linux, written using\u00a0<a href=\"https:\/\/www.tecmint.com\/install-go-in-linux\/\" target=\"_blank\" rel=\"noopener\">Go programming language<\/a>. It attempts to reveal CPU utilization and saturation in an effective way, using The Utilization Saturation and Errors (USE) Method (a methodology for analyzing the performance of any system).<\/p>\n<p>It extracts higher frequency samples of every process being executed on the system and then summarizes these samples at a lower frequency. For instance, it can measure every process every\u00a0<strong>200ms<\/strong>\u00a0and summarize these samples every\u00a0<strong>5<\/strong>\u00a0seconds, including min\/average\/max values for certain metrics.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/command-line-tools-to-monitor-linux-performance\/\" target=\"_blank\" rel=\"noopener\">20 Command Line Tools to Monitor Linux Performance<\/a><\/p>\n<p><strong>Cpustat<\/strong>\u00a0outputs data in two possible ways: a pure text list of the summary interval and a colorful scrolling dashboard of each sample.<\/p>\n<h3>How to Install Cpustat in Linux<\/h3>\n<p>You must have\u00a0<strong>Go (GoLang)<\/strong>\u00a0installed on your Linux system in order to use\u00a0<strong>cpustat<\/strong>, click on the link below to follow the GoLang installation steps that is if you do not have it installed:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/install-go-in-linux\/\" target=\"_blank\" rel=\"noopener\">Install GoLang (Go Programming Language) in Linux<\/a><\/li>\n<\/ol>\n<article class=\"post-25072 post type-post status-publish format-standard has-post-thumbnail hentry category-monitoring-tools tag-commandline-tools tag-linux-monitoring\">\n<div class=\"post-inner group\">\n<div class=\"entry share\">\n<div class=\"entry-inner\">\n<p>Once you have installed\u00a0<strong>Go<\/strong>, type the\u00a0<strong>go get<\/strong>\u00a0command below to install it, this command will install the\u00a0<strong>cpustat<\/strong>binary in your\u00a0<strong>GOBIN<\/strong>\u00a0variable:<\/p>\n<pre># go get github.com\/uber-common\/cpustat\r\n<\/pre>\n<h3>How to Use Cpustat in Linux<\/h3>\n<p>When the installation process completes, run\u00a0<strong>cpustat<\/strong>\u00a0as follows with root privileges using the sudo command that is if your controlling the system as a non-root user, otherwise you\u2019ll get the error as shown:<\/p>\n<pre>$ <strong>$GOBIN\/cpustat<\/strong>\r\nThis program uses the netlink taskstats interface, so it must be run as root.\r\n<\/pre>\n<p><strong>Note<\/strong>: To run\u00a0<strong>cpustat<\/strong>\u00a0as well as all other Go programs you have installed on your system like any other commands, include\u00a0<strong>GOBIN<\/strong>\u00a0variable in your\u00a0<strong>PATH<\/strong>\u00a0environment variable. Open the link below to learn how to set the PATH variable in Linux.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/set-path-variable-linux-permanently\/\" target=\"_blank\" rel=\"noopener\">Learn How to Set Your $PATH Variables Permanently in Linux<\/a><\/li>\n<\/ol>\n<p>This is how\u00a0<strong>cpustat<\/strong>\u00a0works; the\u00a0<code>\/proc<\/code>\u00a0directory is queried to get the current\u00a0<a href=\"https:\/\/www.tecmint.com\/find-process-name-pid-number-linux\/\" target=\"_blank\" rel=\"noopener\">list of process IDs<\/a>\u00a0for every interval, and:<\/p>\n<ul>\n<li>for each PID, read\u00a0<strong>\/proc\/pid\/stat<\/strong>, then compute difference from previous sample.<\/li>\n<li>in case it\u2019s a new PID, read\u00a0<strong>\/proc\/pid\/cmdline<\/strong>.<\/li>\n<li>for each PID, send a netlink message to fetch the taskstats, compute difference from previous sample.<\/li>\n<li>fetch\u00a0<strong>\/proc\/stat<\/strong>\u00a0to get the overall system stats.<\/li>\n<\/ul>\n<p>Again, each sleep interval is adjusted to account for the amount of time consumed fetching all of these stats. Furthermore, each sample also records the time it took to scale each measurement by the actual elapsed time between samples. This attempts to account for delays in cpustat itself.<\/p>\n<p>When run without any arguments,\u00a0<strong>cpustat<\/strong>\u00a0will display the following by default: sampling interval: 200ms, summary interval: 2s (10 samples),\u00a0<a href=\"https:\/\/www.tecmint.com\/find-linux-processes-memory-ram-cpu-usage\/\" target=\"_blank\" rel=\"noopener\">showing top 10 procs<\/a>, user filter: all, pid filter: all as shown in the screenshot below:<\/p>\n<pre>$ sudo $GOBIN\/cpustat \r\n<\/pre>\n<div id=\"attachment_25073\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Cpustat-Monitor-Linux-CPU-Usage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25073\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Cpustat-Monitor-Linux-CPU-Usage.png\" sizes=\"auto, (max-width: 1334px) 100vw, 1334px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Cpustat-Monitor-Linux-CPU-Usage.png 1334w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Cpustat-Monitor-Linux-CPU-Usage-768x410.png 768w\" alt=\"Cpustat - Monitor Linux CPU Usage\" width=\"1334\" height=\"713\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Cpustat \u2013 Monitor Linux CPU Usage<\/p>\n<\/div>\n<p>From the output above, the following are the meanings of the system-wide summary metrics displayed before the fields:<\/p>\n<ul>\n<li><strong>usr<\/strong>\u00a0\u2013 min\/avg\/max user mode run time as a percentage of a CPU.<\/li>\n<li><strong>sys<\/strong>\u00a0\u2013 min\/avg\/max system mode run time as a percentage of a CPU.<\/li>\n<li><strong>nice<\/strong>\u00a0\u2013 min\/avg\/max user mode low priority run time as a percentage of a CPU.<\/li>\n<li><strong>idle<\/strong>\u00a0\u2013 min\/avg\/max user mode run time as a percentage of a CPU.<\/li>\n<li><strong>iowait<\/strong>\u00a0\u2013 min\/avg\/max delay time waiting for disk IO.<\/li>\n<li><strong>prun<\/strong>\u00a0\u2013 min\/avg\/max count of processes in a runnable state (same as load average).<\/li>\n<li><strong>pblock<\/strong>\u00a0\u2013 min\/avg\/max count of processes blocked on disk IO.<\/li>\n<li><strong>pstart<\/strong>\u00a0\u2013 number of processes\/threads started in this summary interval.<\/li>\n<\/ul>\n<p>Still from the output above, for a given process, the different columns mean:<\/p>\n<ul>\n<li><strong>name<\/strong>\u00a0\u2013 common process name from \/proc\/pid\/stat or \/proc\/pid\/cmdline.<\/li>\n<li><strong>pid<\/strong>\u00a0\u2013 process id, also referred to as \u201ctgid\u201d.<\/li>\n<li><strong>min<\/strong>\u00a0\u2013 lowest sample of user+system time for the pid, measured from \/proc\/pid\/stat. Scale is a percentage of a CPU.<\/li>\n<li><strong>max<\/strong>\u00a0\u2013 highest sample of user+system time for this pid, also measured from \/proc\/pid\/stat.<\/li>\n<li><strong>usr<\/strong>\u00a0\u2013 average user time for the pid over the summary period, measured from \/proc\/pid\/stat.<\/li>\n<li><strong>sys<\/strong>\u00a0\u2013 average system time for the pid over the summary period, measured from \/proc\/pid\/stat.<\/li>\n<li><strong>nice<\/strong>\u00a0\u2013 indicates current \u201cnice\u201d value for the process, measured from \/proc\/pid\/stat. Higher means \u201cnicer\u201d.<\/li>\n<li><strong>runq<\/strong>\u00a0\u2013 time the process and all of its threads spent runnable but waiting to run, measured from taskstats via netlink. Scale is a percentage of a CPU.<\/li>\n<li><strong>iow<\/strong>\u00a0\u2013 time the process and all of its threads spent blocked by disk IO, measured from taskstats via netlink. Scale is a percentage of a CPU, averaged over the summary interval.<\/li>\n<li><strong>swap<\/strong>\u00a0\u2013 time the process and all of its threads spent waiting to be swapped in, measured from taskstats via netlink. Scale is a percentage of a CPU, averaged over the summary interval.<\/li>\n<li><strong>vcx and icx<\/strong>\u00a0\u2013 total number of voluntary context switches by the process and all of its threads over the summary interval, measured from taskstats via netlink.<\/li>\n<li><strong>rss<\/strong>\u00a0\u2013 current RSS value fetched from \/proc\/pid\/stat. It is the amount of memory this process is using.<\/li>\n<li><strong>ctime<\/strong>\u00a0\u2013 sum of user+sys CPU time consumed by waited for children that exited during this summary interval, measured from \/proc\/pid\/stat.<\/li>\n<\/ul>\n<p>Note that long running child processes can often confuse this measurement, because the time is reported only when the child process exits. However, this is useful for measuring the impact of frequent cron jobs and health checks where the CPU time is often consumed by many child processes.<\/p>\n<ul>\n<li><strong>thrd<\/strong>\u00a0\u2013 number of threads at the end of the summary interval, measured from \/proc\/pid\/stat.<\/li>\n<li><strong>sam<\/strong>\u00a0\u2013 number of samples for this process included in the summary interval. Processes that have recently started or exited may have been visible for fewer samples than the summary interval.<\/li>\n<\/ul>\n<p>The following command displays the top 10 root user processes running on the system:<\/p>\n<pre>$ sudo $GOBIN\/cpustat -u root\r\n<\/pre>\n<div id=\"attachment_25074\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/show-root-user-processes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25074\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/show-root-user-processes.png\" sizes=\"auto, (max-width: 1326px) 100vw, 1326px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/show-root-user-processes.png 1326w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/show-root-user-processes-768x413.png 768w\" alt=\"Find Root User Running Processes\" width=\"1326\" height=\"713\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Find Root User Running Processes<\/p>\n<\/div>\n<p>To display output in a fancy terminal mode, use the\u00a0<code>-t<\/code>\u00a0flag as follows:<\/p>\n<pre>$ sudo $GOBIN\/cpustat -u roo -t\r\n<\/pre>\n<div id=\"attachment_25075\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Root-User-Runnng-Processes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25075\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Root-User-Runnng-Processes.png\" sizes=\"auto, (max-width: 1348px) 100vw, 1348px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Root-User-Runnng-Processes.png 1348w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Root-User-Runnng-Processes-768x330.png 768w\" alt=\"Running Process Usage of Root User\" width=\"1348\" height=\"580\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Running Process Usage of Root User<\/p>\n<\/div>\n<p>To view the\u00a0<a href=\"https:\/\/www.tecmint.com\/find-processes-by-memory-usage-top-batch-mode\/\" target=\"_blank\" rel=\"noopener\">top x number of processes<\/a>\u00a0(the default is 10), you can use the\u00a0<code>-n<\/code>\u00a0flag, the following command shows the\u00a0<a href=\"https:\/\/www.tecmint.com\/install-htop-linux-process-monitoring-for-rhel-centos-fedora\/\" target=\"_blank\" rel=\"noopener\">top 20 Linux processes running<\/a>\u00a0on the system:<\/p>\n<pre>$ sudo $GOBIN\/cpustat -n 20 \r\n<\/pre>\n<p>You can also write CPU profile to a file using the\u00a0<code>-cpuprofile<\/code>\u00a0option as follows and then use the\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>\u00a0to view the file:<\/p>\n<pre>$ sudo $GOBIN\/cpustat -cpuprofile cpuprof.txt\r\n$ cat cpuprof.txt\r\n<\/pre>\n<p>To display help info, use the\u00a0<code>-h<\/code>\u00a0flag as follows:<\/p>\n<pre>$ sudo $GOBIN\/cpustat -h\r\n<\/pre>\n<p>Find additional info from the cpustat Github Repository:\u00a0<a href=\"https:\/\/github.com\/uber-common\/cpustat\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/github.com\/uber-common\/cpustat<\/a><\/p>\n<p>That\u2019s all! In this article, we showed you how to install and use cpustat, a useful system performance measure tool for Linux. Share your thoughts with us via the comment section below.<\/p>\n<p><a style=\"font-size: 1rem;\" href=\"https:\/\/www.tecmint.com\/cpustat-monitors-cpu-utilization-by-processes-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Cpustat\u00a0is a powerful system performance measure program for Linux, written using\u00a0Go programming language. It attempts to reveal CPU utilization and saturation in an effective way, using The Utilization Saturation and Errors (USE) Method (a methodology for analyzing the performance of any system). It extracts higher frequency samples of every process being executed on the system &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/cpustat-monitors-cpu-utilization-by-running-processes-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cpustat \u2013 Monitors CPU Utilization by Running Processes 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-11692","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\/11692","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=11692"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11692\/revisions"}],"predecessor-version":[{"id":11693,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11692\/revisions\/11693"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}