Jan 192013
 

When something goes wrong on your Linux server or desktop, is important to understand which process is taking all the resources, in the past I’ve published some information about top and htop, two great tools to have a general overview of your system with just a glance, but sometimes you need to gather more information of a particular process and for this task the command pidstat is perfect.

Pidstat is a statistics report tool for Linux which is part of the sysstat utilities. The pidstat command is used for monitoring individual tasks currently being managed by the Linux kernel. It writes to standard output activities for every task selected with option -p or for every task managed by the Linux kernel if option -p ALL has been used. Not selecting any tasks is equivalent to specifying -p ALL but only active tasks (tasks with non-zero statistics values) will appear in the report.
You can use PID’s and even regular expressions on the process names to filter them out.


Basic usage

The easiest way to use pidstat is with the option -p ALL, this will print on the screen a list of all the processes running and the resources that they are using:

#pidstat -p ALL
Linux 3.6.5   	01/19/13 	_i686_	(4 CPU)
 
19:03:10          PID    %usr %system  %guest    %CPU   CPU  Command
19:03:10            1    0.00    0.00    0.00    0.00     1  init
19:03:10            2    0.00    0.00    0.00    0.00     1  kthreadd
19:03:10            3    0.00    0.00    0.00    0.00     0  ksoftirqd/0
19:03:10            4    0.00    0.00    0.00    0.00     0  kworker/0:0
19:03:10            5    0.00    0.00    0.00    0.00     0  kworker/0:0H
19:03:10            6    0.00    0.00    0.00    0.00     1  kworker/u:0
19:03:10            7    0.00    0.00    0.00    0.00     0  kworker/u:0H
19:03:10            8    0.00    0.00    0.00    0.00     0  migration/0
19:03:10            9    0.00    0.00    0.00    0.00     1  migration/1
19:03:10           10    0.00    0.00    0.00    0.00     1  kworker/1:0
19:03:10           11    0.00    0.00    0.00    0.00     1  kworker/1:0H
19:03:10           12    0.00    0.00    0.00    0.00     1  ksoftirqd/1
19:03:10           13    0.00    0.00    0.00    0.00     2  migration/2
19:03:10           14    0.00    0.00    0.00    0.00     2  kworker/2:0
19:03:10           15    0.00    0.00    0.00    0.00     2  kworker/2:0H
19:03:10           16    0.00    0.00    0.00    0.00     2  ksoftirqd/2
19:03:10           17    0.00    0.00    0.00    0.00     3  migration/3
19:03:10           18    0.00    0.00    0.00    0.00     3  kworker/3:0
19:03:10           19    0.00    0.00    0.00    0.00     3  kworker/3:0H
.....
19:03:10          293    0.00    0.00    0.00    0.00     1  nfsiod
19:03:10          294    0.00    0.00    0.00    0.00     1  cifsiod
19:03:10          297    0.00    0.00    0.00    0.00     1  jfsIO
19:03:10          298    0.00    0.00    0.00    0.00     1  jfsCommit
19:03:10          302    0.00    0.00    0.00    0.00     1  jfsSync
19:03:10          303    0.00    0.00    0.00    0.00     1  xfsalloc
19:03:10          304    0.00    0.00    0.00    0.00     1  xfs_mru_cache
19:03:10          305    0.00    0.00    0.00    0.00     1  xfslogd
19:03:10          306    0.00    0.00    0.00    0.00     1  glock_workqueue
19:03:10          307    0.00    0.00    0.00    0.00     1  delete_workqueu
19:03:10          308    0.00    0.00    0.00    0.00     1  gfs_recovery
19:03:10          309    0.00    0.00    0.00    0.00     1  crypto
19:03:10          873    0.00    0.00    0.00    0.00     2  khvcd
19:03:10          965    0.00    0.01    0.00    0.01     0  kworker/0:1H
19:03:10          992    0.00    0.00    0.00    0.00     3  kpsmoused
19:03:10         1030    0.00    0.00    0.00    0.00     3  deferwq
19:03:10         1033    0.00    0.00    0.00    0.00     0  kjournald
19:03:10         1036    0.00    0.00    0.00    0.00     0  kworker/0:1
19:03:10         1069    0.00    0.00    0.00    0.00     0  udevd
19:03:10         1106    0.00    0.00    0.00    0.00     3  kworker/3:1
19:03:10         2039    0.00    0.00    0.00    0.00     0  dhcpcd-bin
19:03:10         2149    0.00    0.00    0.00    0.00     0  rsyslogd
19:03:10         2174    0.00    0.00    0.00    0.00     0  atd
19:03:10         2188    0.00    0.00    0.00    0.00     1  monit
19:03:10         2206    0.00    0.00    0.00    0.00     0  cron
19:03:10         2215    0.00    0.00    0.00    0.00     1  dropbear
19:03:10         2260    0.00    0.00    0.00    0.00     1  mysqld_safe
19:03:10         2416    0.16    0.08    0.00    0.23     2  mysqld
19:03:10         2417    0.00    0.00    0.00    0.00     3  logger
19:03:10         2419    0.00    0.00    0.00    0.00     2  php5-fpm
19:03:10         2425    0.00    0.09    0.00    0.09     2  flush-202:0
19:03:10         2471    0.00    0.00    0.00    0.01     1  ntpd
19:03:10         2523    0.00    0.00    0.00    0.00     1  mingetty
19:03:10         2694    0.00    0.00    0.00    0.00     2  kworker/2:1H
19:03:10         2734    0.00    0.00    0.00    0.00     3  kworker/3:1H
19:03:10         2736    0.00    0.00    0.00    0.00     1  kworker/1:1H
19:03:10         2776    0.00    0.00    0.00    0.00     0  jbd2/xvdc1-8
19:03:10         2777    0.00    0.00    0.00    0.00     3  ext4-dio-unwrit
19:03:10         3526    0.00    0.00    0.00    0.00     2  varnishd
19:03:10         3527    0.00    0.00    0.00    0.01     1  varnishd
19:03:10         4972    0.02    0.00    0.00    0.03     3  php5-fpm
19:03:10         4977    0.03    0.00    0.00    0.03     0  php5-fpm
19:03:10         4992    0.02    0.00    0.00    0.02     1  php5-fpm
19:03:10         5087    0.01    0.00    0.00    0.01     0  php5-fpm
19:03:10         5091    0.01    0.00    0.00    0.02     3  php5-fpm

Time Interval

As you can see the former command prints a very long list with all your processes, useful sometimes but perhaps you just want to know which processes are using resources in this moment and for this task is much better to run pidstat with a number as option, this number specifies the amount of time in seconds between each report.

# pidstat 1
Linux 3.6.5 	01/19/13 	_i686_	(4 CPU)
 
19:22:28          PID    %usr %system  %guest    %CPU   CPU  Command
19:22:29         5725    0.00    0.99    0.00    0.99     0  pidstat
19:22:29        30634    1.98    0.00    0.00    1.98     0  nginx
 
19:22:29          PID    %usr %system  %guest    %CPU   CPU  Command
19:22:30        30634    4.00    0.00    0.00    4.00     0  nginx
 
19:22:30          PID    %usr %system  %guest    %CPU   CPU  Command
19:22:31         2416    3.00    1.00    0.00    4.00     2  mysqld
19:22:31         3527    0.00    1.00    0.00    1.00     2  varnishd
19:22:31         4977   14.00    2.00    0.00   16.00     3  php5-fpm
19:22:31         4992   26.00    3.00    0.00   29.00     1  php5-fpm
19:22:31         5685   13.00    2.00    0.00   15.00     0  php5-fpm
19:22:31         5725    1.00    1.00    0.00    2.00     1  pidstat
19:22:31        30634    1.00    0.00    0.00    1.00     0  nginx
 
19:22:31          PID    %usr %system  %guest    %CPU   CPU  Command
19:22:32          965    0.00    1.00    0.00    1.00     0  kworker/0:1H
19:22:32         2416    1.00    1.00    0.00    2.00     2  mysqld
19:22:32         4977    1.00    0.00    0.00    1.00     3  php5-fpm
19:22:32         5093   34.00    3.00    0.00   37.00     0  php5-fpm
19:22:32         5685   15.00    2.00    0.00   17.00     2  php5-fpm



This report is much better as it shows only process whose use of resources changes between the time interval you have specified.
You can also specify a second number as count parameter. The value of count determines the number of reports generated at interval seconds apart. If the interval parameter is specified without the count parameter, the pidstat command generates reports continuously.

Specify which kind of resources you want to monitor

So far we haven’t specified which kind of resource we are interested in, and so as default pidstat show information about CPU usage, but this software is capable of give us much more information using these options:

  • -d Report I/O statistics.
  • -r Report page faults and memory utilization.
  • -u Report CPU utilization

You can also specify multiple resources, so to display information about CPU and memory you can use the command:

# pidstat 2 2 -u -r
Linux 3.6.5	01/19/13 	_i686_	(4 CPU)
 
19:51:08          PID    %usr %system  %guest    %CPU   CPU  Command
19:51:10         2416    1.00    1.00    0.00    1.99     0  mysqld
19:51:10         5091   35.32    0.00    0.00   35.32     2  php5-fpm
19:51:10         5937    0.00    0.50    0.00    0.50     0  pidstat
 
19:51:08          PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
19:51:10         2416    193.53      0.00   89296  32156   6.33  mysqld
19:51:10         3527      2.99      0.00  314384  29084   5.72  varnishd
19:51:10         5091    191.04      0.00  166464  59596  11.73  php5-fpm
19:51:10         5937    101.00      0.00    1768    748   0.15  pidstat
 
19:51:10          PID    %usr %system  %guest    %CPU   CPU  Command
19:51:12         3527    0.00    0.50    0.00    0.50     1  varnishd
19:51:12         5728    4.50    0.00    0.00    4.50     0  php5-fpm
19:51:12         5937    0.00    0.50    0.00    0.50     0  pidstat
 
19:51:10          PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
19:51:12         3527      4.00      0.50  314384  29084   5.72  varnishd
19:51:12         5728     16.00      0.00  165664  59260  11.66  php5-fpm
19:51:12         5937    104.50      0.00    1768    784   0.15  pidstat
 
Average:          PID    %usr %system  %guest    %CPU   CPU  Command
Average:         2416    0.50    0.50    0.00    1.00     -  mysqld
Average:         3527    0.00    0.25    0.00    0.25     -  varnishd
Average:         5091   17.71    0.00    0.00   17.71     -  php5-fpm
Average:         5728    2.24    0.00    0.00    2.24     -  php5-fpm
Average:         5937    0.00    0.50    0.00    0.50     -  pidstat
 
Average:          PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
Average:         2416     97.01      0.00   89296  32156   6.33  mysqld
Average:         3527      3.49      0.25  314384  29084   5.72  varnishd
Average:         5091     95.76      0.00  166464  59596  11.73  php5-fpm
Average:         5728      7.98      0.00  165664  59222  11.65  php5-fpm
Average:         5937    102.74      0.00    1768    766   0.15  pidstat

Show only certain processes

So far we have analysed all the running processes, but perhaps you want to monitor just a specific process, to do this you can use the flag -p with a PID number or use the option -C with a string, this display only tasks whose command name includes the string you have specified.

# pidstat -C mysql        
Linux 3.6.5	01/19/13 	_i686_	(4 CPU)
 
20:12:40          PID    %usr %system  %guest    %CPU   CPU  Command
20:12:40         2260    0.00    0.00    0.00    0.00     1  mysqld_safe
20:12:40         2416    0.16    0.08    0.00    0.23     3  mysqld

You can mix the -C parameter with all the others, so you can choose a time interval or which kind of resource

Popular Posts:

Flattr this!

  One Response to “Linux Terminal: pidstat, know everything about your processes”

  1. Thank you for this nice article, but why doesn’t the htop link to this article? – http://linuxaria.com/article/monitoring-process-top-htop

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*