pidstat 查看 cpu 进程 cpu 使用率

通常定位问题时我们可能会使用到 iostat 查看硬盘利用率, sar 查看网络流量等,而查看 cpu 使用率会使用 top,实际上 iostat 和 sar 都是 sysstat 这个软件包中的一个组件,而 sysstat 里面还有 pidstat 和 mpstat 这两个工具可以用来观察 cpu 使用率(其他的如 cifsiostat, nfsiostat-sysstat 组件我们可能一般不会用到)。

其中 mpstat 用于查看节点整体的 cpu 使用率:

~]# mpstat 1
Linux 3.10.0-693.17.1.el7.x86_64 (ceph20)      08/23/2018      _x86_64_        (16 CPU)

07:49:57 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
07:49:58 PM  all   33.90    0.00    4.00    0.00    0.00    0.00    0.00    0.00    0.00   62.10
07:49:59 PM  all   33.15    0.00    4.82    0.81    0.00    0.00    0.00    0.00    0.00   61.22
07:50:00 PM  all   34.83    0.00    4.25    0.00    0.00    0.00    0.00    0.00    0.00   60.91
07:50:01 PM  all   33.94    0.00    4.32    1.13    0.00    0.06    0.00    0.00    0.00   60.55
07:50:02 PM  all   35.04    0.00    2.75    0.00    0.00    0.00    0.00    0.00    0.00   62.20

而 pidstat 用于查看单个进程的 cpu 使用率:

~]# pidstat -p 61363 -dus 1
Linux 3.10.0-693.17.1.el7.x86_64 (ceph20)      08/23/2018      _x86_64_        (16 CPU)

07:48:45 PM   UID       PID    %usr %system  %guest    %CPU   CPU  Command
07:48:46 PM     0     61363    0.00    0.00    0.00    0.00     3  ceph-osd

07:48:45 PM   UID       PID StkSize  StkRef  Command
07:48:46 PM     0     61363    656      96  ceph-osd

07:48:45 PM   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
07:48:46 PM     0     61363      0.00      0.00      0.00  ceph-osd

其中 -d 显示进程的硬盘使用率,-u 显示 cpu 的使用率,-s 显示栈的使用率,另外还可以使用 -t 显示进程中所有线程的使用率,可以独立使用这些指定的显示参数,如:

~]# pidstat -p 61363 -u 1
Linux 3.10.0-693.17.1.el7.x86_64 (ceph20) 08/23/2018 _x86_64_ (16 CPU)
07:56:42 PM UID PID %usr %system %guest %CPU CPU Command
07:56:43 PM 0 61363 0.99 0.00 0.00 0.99 3 ceph-osd
07:56:44 PM 0 61363 0.00 0.00 0.00 0.00 3 ceph-osd
07:56:45 PM 0 61363 0.00 0.00 0.00 0.00 3 ceph-osd
07:56:46 PM 0 61363 0.00 0.00 0.00 0.00 3 ceph-osd
07:56:47 PM 0 61363 0.00 0.00 0.00 0.00 3 ceph-osd

-u 是默认选项,如果不指定任何显示选项的话会默认显示 cpu 使用率。

此外 pidstat 的 -p 选项也可以指定线程 id,从而观察到的是单个线程的使用率,如:

~]# pidstat -p 61365 1
Linux 3.10.0-693.17.1.el7.x86_64 (ceph20) 08/23/2018 _x86_64_ (16 CPU)
07:58:36 PM UID PID %usr %system %guest %CPU CPU Command
07:58:37 PM 0 61365 0.00 0.00 0.00 0.00 7 msgr-worker-0
07:58:38 PM 0 61365 0.00 0.00 0.00 0.00 5 msgr-worker-0
07:58:39 PM 0 61365 0.00 0.00 0.00 0.00 5 msgr-worker-0
07:58:40 PM 0 61365 0.00 0.00 0.00 0.00 7 msgr-worker-0

最后修改于 2019-01-23