pidstat主要用于监控全部或指定进程占用系统资源的情况,如CPU,内存、设备IO、任务切换、线程等。pidstat首次运行时显示自系统启动开始的各项统计信息,之后运行pidstat将显示自上次运行该命令以后的统计信息。用户可以通过指定统计的次数和时间来获得所需的统计信息。
执行pidstat,将输出系统启动后所有活动进程的cpu统计信息:
[root@localhost ~]# pidstat
Linux 3.10.0-1062.el7.x86_64 (192.168.32.130) 03/08/2020 _x86_64_ (4 CPU)
04:27:31 AM UID PID %usr %system %guest %CPU CPU Command
04:27:31 AM 0 1 0.00 0.05 0.00 0.05 1 systemd
04:27:31 AM 0 2 0.00 0.00 0.00 0.00 1 kthreadd
04:27:31 AM 0 741 0.00 0.00 0.00 0.00 1 auditd
04:27:31 AM 0 764 0.00 0.01 0.00 0.01 2 irqbalance
04:27:31 AM 999 766 0.00 0.00 0.00 0.00 2 polkitd
04:27:31 AM 0 767 0.00 0.00 0.00 0.00 0 systemd-logind
04:27:31 AM 81 768 0.00 0.01 0.00 0.01 1 dbus-daemon
04:27:31 AM 0 774 0.00 0.00 0.00 0.00 2 VGAuthService
04:27:31 AM 0 775 0.07 0.09 0.00 0.16 2 vmtoolsd
04:27:31 AM 998 784 0.00 0.00 0.00 0.00 3 chronyd
04:27:31 AM 0 794 0.00 0.01 0.00 0.01 1 crond
04:27:31 AM 0 806 0.00 0.01 0.00 0.02 3 firewalld
...........
pidstat指定采样周期和采样次数
pidstat命令指定采样周期和采样次数,命令形式为”pidstat [option] interval [count]”,以下pidstat输出以2秒为采样周期,输出2次cpu使用统计信息:
[root@localhost ~]# pidstat 2 2
Linux 3.10.0-1062.el7.x86_64 (192.168.32.130) 03/08/2020 _x86_64_ (4 CPU)
04:30:48 AM UID PID %usr %system %guest %CPU CPU Command
04:30:50 AM 0 1636 0.50 0.00 0.00 0.50 1 java
04:30:50 AM 0 2103 0.00 0.50 0.00 0.50 1 pidstat
04:30:50 AM UID PID %usr %system %guest %CPU CPU Command
Average: UID PID %usr %system %guest %CPU CPU Command
Average: 0 1636 0.25 0.00 0.00 0.25 - java
Average: 0 2103 0.00 0.25 0.00 0.25 - pidstat
pidstat查看进程的cpu上下文切换情况
-w:查看每个进程上下文切换情况
#每隔5秒输出1组数据
[root@localhost ~]# pidstat -w 5 2
Linux 3.10.0-1062.el7.x86_64 (192.168.32.130) 03/08/2020 _x86_64_ (4 CPU)
04:35:33 AM UID PID cswch/s nvcswch/s Command
04:35:38 AM 0 9 2.99 0.00 rcu_sched
04:35:38 AM 0 11 0.20 0.00 watchdog/0
04:35:38 AM 0 12 0.20 0.00 watchdog/1
04:35:38 AM 0 13 0.20 0.00 migration/1
04:35:38 AM 0 17 0.20 0.00 watchdog/2
04:35:38 AM 0 19 0.20 0.00 ksoftirqd/2
04:35:38 AM 0 22 0.20 0.00 watchdog/3
04:35:38 AM 0 24 0.20 0.00 ksoftirqd/3
04:35:38 AM 0 40 0.20 0.00 kworker/3:1
04:35:38 AM 0 41 1.00 0.00 kworker/2:1
04:35:38 AM 0 48 0.20 0.00 khugepaged
04:35:38 AM 0 65 0.40 0.00 kworker/1:1
04:35:38 AM 0 463 20.16 0.00 xfsaild/dm-0
04:35:38 AM 0 764 0.20 0.00 irqbalance
04:35:38 AM 0 775 10.38 0.00 vmtoolsd
04:35:38 AM 0 945 0.20 0.00 kworker/0:1H
04:35:38 AM 0 2066 1.20 0.00 kworker/u256:0
04:35:38 AM 0 2086 2.40 0.20 kworker/0:3
04:35:38 AM 0 2094 0.60 0.00 kworker/0:1
04:35:38 AM 0 2359 0.20 0.00 pidstat
cswch/s:表示每秒自愿上下文切换(voluntary context switches)的次数
ncswch/s:表示每秒非自愿上下文切换(non voluntary context switches)的次数 这两个概念一定要牢牢记住,因为它们意味着不同的性能问题: 所谓自愿上下文切换,是指进程无法获取所需资源,导致的上下文切换。比如说, I/O、内存等系统资源不足时,就会发生自愿上下文切换。
而非自愿上下文切换,则是指进程由于时间片已到等原因,被系统强制调度,进而发生的上下文切换。比如说,大量进程都在争抢 CPU 时,就容易发生非自愿上下文切换。
查看线程的cpu上下文切换情况
#-wt参数表示输出线程的上下文切换指标
#每隔2秒输出一组数据,共输出2组
[root@localhost ~]# pidstat -wt 2 2
Linux 3.10.0-1062.el7.x86_64 (192.168.32.130) 03/08/2020 _x86_64_ (4 CPU)
04:42:50 AM UID TGID TID cswch/s nvcswch/s Command
04:42:52 AM 0 9 - 6.40 0.00 rcu_sched
04:42:52 AM 0 - 9 6.40 0.00 |__rcu_sched
04:42:52 AM 0 11 - 0.49 0.00 watchdog/0
04:42:52 AM 0 - 11 0.49 0.00 |__watchdog/0
04:42:52 AM 0 12 - 0.49 0.00 watchdog/1
04:42:52 AM 0 - 12 0.49 0.00 |__watchdog/1
04:42:52 AM 0 17 - 0.49 0.00 watchdog/2
04:42:52 AM 0 - 17 0.49 0.00 |__watchdog/2
04:42:52 AM 0 18 - 0.49 0.00 migration/2
04:42:52 AM 0 - 18 0.49 0.00 |__migration/2
04:42:52 AM 0 19 - 0.49 0.00 ksoftirqd/2
04:42:52 AM 0 - 19 0.49 0.00 |__ksoftirqd/2
.........
每秒上下文切换多少次才算正常呢? 这个数值其实取决于系统本身的 CPU 性能。
如果系统的上下文切换次数比较稳定,那么从数百到一万以内,都应该算是正常的。 但当上下文切换次数超过一万次,或者切换次数出现数量级的增长时,就很可能已经出现了性能问题。 这时,你还需要根据上下文切换的类型,再做具体分析。比方说:
-
自愿上下文切换变多了,说明进程都在等待资源,有可能发生了I/O等其他问题;
-
非自愿上下文切换变多了,说明进程都在被强制调度,也就是都在争抢cpu,说明cpu的确成了瓶颈;
-
中断次数变多了,说明cpu被中断处理程序占用,还需要通过查看/proc/interrupts文件来分析具体的中断类型;
pidstat进程内存使用情况统计
使用-r选项,pidstat将显示各活动进程的内存使用统计
#使用-p选项指定进程1636 每隔2秒执行一次,共执行2次
[root@localhost ~]# pidstat -r -p 1636 2 2
Linux 3.10.0-1062.el7.x86_64 (192.168.32.130) 03/08/2020 _x86_64_ (4 CPU)
04:54:42 AM UID PID minflt/s majflt/s VSZ RSS %MEM Command
04:54:44 AM 0 1636 0.00 0.00 4135676 431228 23.15 java
04:54:46 AM 0 1636 1.00 0.00 4135676 431228 23.15 java
Average: 0 1636 0.50 0.00 4135676 431228 23.15 java
以上各列输出的含义如下:
- minflt/s: 每秒次缺页错误次数(minor page faults),次缺页错误次数意即虚拟内存地址映射成物理内存地址产生的page fault次数
- majflt/s: 每秒主缺页错误次数(major page faults),当虚拟内存地址映射成物理内存地址时,相应的page在swap中,这样的page fault为major page fault,一般在内存使用紧张时产生
- VSZ: 该进程使用的虚拟内存(以kB为单位)
- RSS: 该进程使用的物理内存(以kB为单位)
- %MEM: 该进程使用内存的百分比
- Command: 拉起进程对应的命令
pidstat IO情况统计
使用-d选项,可以查看到进程IO的统计信息
[root@localhost ~]# pidstat -d -p 1636 1 2
Linux 3.10.0-1062.el7.x86_64 (192.168.32.130) 03/08/2020 _x86_64_ (4 CPU)
05:02:21 AM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
05:02:22 AM 0 1636 0.00 8.00 0.00 java
05:02:23 AM 0 1636 0.00 0.00 0.00 java
Average: 0 1636 0.00 4.00 0.00 java
输出信息含义:
- kB_rd/s:每秒进程从磁盘读取的数据量(以KB为单位)
- kB_wr/s:每秒进程向磁盘写的数据量(以KB为单位)
- Command: 拉起进程对应的命令
pidstat常用命令
#pidstat -u 1
#pidstat -r 1
#pidstat -d 1
以上命令以1秒为信息采集周期,分别获取cpu,内存和磁盘io的统计信息