前言
NAME
pidstat - Report statistics for Linux tasks.
vmstat用来观测系统整体的性能情况,并不能观测单个进程,使用pidstat来观测单个进程的性能情况。
一、pidstat简介
pidstat命令用于监视当前由Linux内核管理的各个任务,使用 选项 -p 将指定的任务的信息输出到标准输出中,或者使用选项 -p ALL将Linux内核管理的每个任务的信息输出到标准输出中。若不指定任何任务等同于使用选项 -p ALL,但只有活动任务(统计值非零的任务)将出现在报告中。
pidstat命令还可以用于监视所选任务的子进程,参考选项 -T。
interval参数指定每个报告之间以秒为单位的时间间隔。值为0(或者根本没有参数)表示将报告自系统启动(boot)以来的一段时间内的任务统计信息。如果没有将interval参数设置为零,则可以将count参数与interval参数一起指定。count的值决定了间隔几秒生成的报告的数量。如果指定interval参数而不带count参数,pidstat命令将连续生成报表。
pidstat ...... [ interval[ count ] ]
每隔2秒生成一份任务统计信息,总共生成5次任务统计信息,同时还会产生一个该时间段内的平均任务统计信息。
[root@localhost ~]# pidstat 2 5
Linux 3.10.0-957.el7.x86\_64 (localhost.localdomain) 11/24/2022 \_x86\_64\_ (4 CPU)
03:45:25 PM UID PID %usr %system %guest %CPU CPU Command
03:45:27 PM 0 4524 5.97 1.99 0.00 7.96 0 accounts-daemon
03:45:27 PM 998 4533 6.97 2.99 0.00 9.95 0 polkitd
03:45:27 PM 81 4541 6.97 1.49 0.00 8.46 1 dbus-daemon
03:45:27 PM 0 14329 0.00 0.50 0.00 0.50 3 bash
03:45:27 PM 0 15031 0.00 0.50 0.00 0.50 0 pidstat
03:45:27 PM 0 21424 1.99 0.50 0.00 2.49 3 gsd-account
03:45:27 PM UID PID %usr %system %guest %CPU CPU Command
03:45:29 PM 0 482 0.50 0.00 0.00 0.50 2 sshd
03:45:29 PM 0 4524 5.00 2.00 0.00 7.00 2 accounts-daemon
03:45:29 PM 998 4533 8.50 2.50 0.00 11.00 3 polkitd
03:45:29 PM 81 4541 7.00 2.00 0.00 9.00 0 dbus-daemon
03:45:29 PM 0 15031 0.00 0.50 0.00 0.50 0 pidstat
03:45:29 PM 0 21424 2.00 1.00 0.00 3.00 3 gsd-account
03:45:29 PM UID PID %usr %system %guest %CPU CPU Command
03:45:31 PM 0 9 0.00 0.50 0.00 0.50 3 rcu_sched
03:45:31 PM 0 4524 6.50 1.00 0.00 7.50 0 accounts-daemon
03:45:31 PM 998 4533 7.00 3.50 0.00 10.50 3 polkitd
03:45:31 PM 81 4541 7.00 1.00 0.00 8.00 1 dbus-daemon
03:45:31 PM 0 15031 0.50 0.50 0.00 1.00 0 pidstat
03:45:31 PM 0 21424 2.50 0.00 0.00 2.50 3 gsd-account
03:45:31 PM UID PID %usr %system %guest %CPU CPU Command
03:45:33 PM 0 482 0.00 0.50 0.00 0.50 2 sshd
03:45:33 PM 0 4524 5.47 1.99 0.00 7.46 0 accounts-daemon
03:45:33 PM 998 4533 7.46 3.48 0.00 10.95 0 polkitd
03:45:33 PM 81 4541 6.47 1.99 0.00 8.46 1 dbus-daemon
03:45:33 PM 0 14329 0.50 0.50 0.00 1.00 3 bash
03:45:33 PM 0 15031 0.00 0.50 0.00 0.50 0 pidstat
03:45:33 PM 0 21086 0.50 0.00 0.00 0.50 1 gvfsd-fuse
03:45:33 PM 0 21424 2.49 1.00 0.00 3.48 3 gsd-account
03:45:33 PM 0 21440 0.50 0.00 0.00 0.50 2 gsd-color
03:45:33 PM UID PID %usr %system %guest %CPU CPU Command
03:45:35 PM 0 4524 6.00 1.00 0.00 7.00 3 accounts-daemon
03:45:35 PM 998 4533 8.50 2.00 0.00 10.50 1 polkitd
03:45:35 PM 81 4541 6.50 1.50 0.00 8.00 2 dbus-daemon
03:45:35 PM 0 6183 0.00 0.50 0.00 0.50 3 pcscd
03:45:35 PM 0 15031 0.50 0.50 0.00 1.00 0 pidstat
03:45:35 PM 0 21423 0.00 0.50 0.00 0.50 1 gsd-smartcard
03:45:35 PM 0 21424 2.00 0.50 0.00 2.50 0 gsd-account
03:45:35 PM 0 21805 0.50 0.00 0.00 0.50 3 fwupd
Average: UID PID %usr %system %guest %CPU CPU Command
Average: 0 9 0.00 0.10 0.00 0.10 - rcu_sched
Average: 0 482 0.10 0.10 0.00 0.20 - sshd
Average: 0 4524 5.79 1.60 0.00 7.39 - accounts-daemon
Average: 998 4533 7.68 2.89 0.00 10.58 - polkitd
Average: 81 4541 6.79 1.60 0.00 8.38 - dbus-daemon
Average: 0 6183 0.00 0.10 0.00 0.10 - pcscd
Average: 0 14329 0.10 0.20 0.00 0.30 - bash
Average: 0 15031 0.20 0.50 0.00 0.70 - pidstat
Average: 0 21086 0.10 0.00 0.00 0.10 - gvfsd-fuse
Average: 0 21423 0.00 0.10 0.00 0.10 - gsd-smartcard
Average: 0 21424 2.20 0.60 0.00 2.79 - gsd-account
Average: 0 21440 0.10 0.00 0.00 0.10 - gsd-color
Average: 0 21805 0.10 0.00 0.00 0.10 - fwupd
二、pidstat 的使用
2.1 pidstat -C comm
只显示命令名中包含字符串comm的任务。该字符串可以是正则表达式。
[root@localhost ~]# pidstat -C sshd
Linux 3.10.0-957.el7.x86\_64 (localhost.localdomain) 11/24/2022 \_x86\_64\_ (4 CPU)
03:54:27 PM UID PID %usr %system %guest %CPU CPU Command
03:54:27 PM 0 356 0.00 0.00 0.00 0.00 0 sshd
03:54:27 PM 0 360 0.00 0.00 0.00 0.00 2 sshd
03:54:27 PM 0 482 0.00 0.00 0.00 0.00 3 sshd
03:54:27 PM 0 486 0.00 0.00 0.00 0.00 0 sshd
03:54:27 PM 0 5174 0.00 0.00 0.00 0.00 1 sshd
2.2 pidstat -d
-d Report I/O statistics
Linux 3.10.0-957.el7.x86\_64 (localhost.localdomain) 11/24/2022 \_x86\_64\_ (4 CPU)
04:02:11 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
04:02:11 PM 0 1 0.96 1.64 0.13 systemd
04:02:11 PM 0 365 0.00 0.00 0.00 bash
04:02:11 PM 0 490 0.00 0.00 0.00 bash
04:02:11 PM 0 2173 0.00 0.00 0.00 xfsaild/dm-0
04:02:11 PM 0 2254 0.00 0.00 0.00 systemd-journal
04:02:11 PM 0 2276 0.00 0.00 0.00 lvmetad
04:02:11 PM 0 2297 0.02 0.00 0.00 systemd-udevd
04:02:11 PM 0 4496 0.00 0.02 0.00 auditd
04:02:11 PM 0 4524 0.00 0.00 0.00 accounts-daemon
04:02:11 PM 172 4527 0.00 0.00 0.00 rtkit-daemon
04:02:11 PM 0 4528 0.00 0.00 0.00 alsactl
04:02:11 PM 998 4533 0.00 0.00 0.00 polkitd
04:02:11 PM 70 4539 0.00 0.00 0.00 avahi-daemon
04:02:11 PM 0 4540 0.00 0.00 0.00 bluetoothd
04:02:11 PM 81 4541 0.00 0.00 0.00 dbus-daemon
04:02:11 PM 0 4549 0.00 0.00 0.00 udisksd
04:02:11 PM 0 4551 0.00 0.00 0.00 abrtd
04:02:11 PM 0 4554 0.00 0.00 0.00 abrt-watch-log
04:02:11 PM 994 4561 0.00 0.00 0.00 lsmd
04:02:11 PM 0 4591 0.00 0.00 0.00 gssproxy
04:02:11 PM 0 4635 0.00 0.00 0.00 firewalld
......
UID:正在监视的任务的真实用户标识号。
PID:正在监视的任务的标识号。
kB_rd/s:任务每秒从磁盘读取的 kilobytes。
kB_wr/s:任务每秒已写入或将写入磁盘的 kilobytes。
kB_ccwr/s:任务已经取消写入磁盘的 kilobytes,这可能发生在任务截断某些脏页缓存时(dirty pagecache:缓存中的数据还没有写入磁盘)。
Command:任务的命令名称。
2.3 pidstat -l
-l Display the process command name and all its arguments.
显示任务的完整路径和任务执行时的参数,和从/proc/fd/cmdline 中的值一样
04:25:21 PM UID PID %usr %system %guest %CPU CPU Command
......
04:25:21 PM 0 614 0.00 0.00 0.00 0.00 1 kworker/u8:0
04:25:21 PM 0 1463 0.00 0.00 0.00 0.00 2 i915/signal:0
04:25:21 PM 0 2173 0.00 0.01 0.00 0.01 3 xfsaild/dm-0
04:25:21 PM 0 2174 0.00 0.00 0.00 0.00 0 kworker/0:1H
04:25:21 PM 0 2254 0.00 0.00 0.00 0.00 1 /usr/lib/systemd/systemd-journald
04:25:21 PM 0 2276 0.00 0.00 0.00 0.00 2 /usr/sbin/lvmetad -f
04:25:21 PM 0 2295 0.00 0.00 0.00 0.00 3 kworker/3:1H
04:25:21 PM 0 2297 0.00 0.00 0.00 0.00 3 /usr/lib/systemd/systemd-udevd
04:25:21 PM 0 4428 0.00 0.00 0.00 0.00 1 xfsaild/sda2
04:25:21 PM 0 4496 0.00 0.00 0.00 0.00 2 /sbin/auditd
04:25:21 PM 0 4498 0.00 0.00 0.00 0.00 0 /sbin/audispd
04:25:21 PM 0 4499 0.00 0.00 0.00 0.00 2 /usr/sbin/sedispatch
04:25:21 PM 32 4522 0.00 0.00 0.00 0.00 3 /sbin/rpcbind -w
04:25:21 PM 0 4523 0.00 0.00 0.00 0.00 1 /usr/lib/systemd/systemd-logind
04:25:21 PM 0 4524 2.26 0.60 0.00 2.86 0 /usr/libexec/accounts-daemon
04:25:21 PM 172 4527 0.00 0.00 0.00 0.00 3 /usr/libexec/rtkit-daemon
04:25:21 PM 0 4528 0.00 0.00 0.00 0.00 0 /usr/sbin/alsactl -s -n 19 -c -E ALSA_CONFIG_PATH=/etc/alsa/alsactl.conf --initfile=/lib/alsa/init/00main rdaemon
04:25:21 PM 0 4529 0.00 0.00 0.00 0.00 0 /sbin/rngd -f
.......
数据来源就是 /proc/fd/stat,/proc/fd/cmdline,/proc/fd/status:
[root@localhost ~]# strace -e trace=open pidstat -l
......
open("/proc/1/stat", O_RDONLY) = 4
open("/proc/1/cmdline", O_RDONLY) = 4
open("/proc/1/status", O_RDONLY) = 4
open("/proc/2/stat", O_RDONLY) = 4
open("/proc/2/cmdline", O_RDONLY) = 4
open("/proc/2/status", O_RDONLY) = 4
open("/proc/3/stat", O_RDONLY) = 4
open("/proc/3/cmdline", O_RDONLY) = 4
open("/proc/3/status", O_RDONLY) = 4
open("/proc/5/stat", O_RDONLY) = 4
open("/proc/5/cmdline", O_RDONLY) = 4
open("/proc/5/status", O_RDONLY) = 4
open("/proc/7/stat", O_RDONLY) = 4
open("/proc/7/cmdline", O_RDONLY) = 4
open("/proc/7/status", O_RDONLY) = 4
open("/proc/8/stat", O_RDONLY) = 4
open("/proc/8/cmdline", O_RDONLY) = 4
open("/proc/8/status", O_RDONLY) = 4
......
2.4 pidstat -p
查看指定进程的信息统计:
[root@localhost ~]# pidstat -p `pidof top`
Linux 3.10.0-957.el7.x86\_64 (localhost.localdomain) 11/24/2022 \_x86\_64\_ (4 CPU)
04:36:10 PM UID PID %usr %system %guest %CPU CPU Command
04:36:10 PM 0 4121 0.00 0.00 0.00 0.00 2 top
查看 pidstat 进程本身信息统计:
[root@localhost ~]# pidstat -p SELF
Linux 3.10.0-957.el7.x86\_64 (localhost.localdomain) 11/24/2022 \_x86\_64\_ (4 CPU)
04:36:41 PM UID PID %usr %system %guest %CPU CPU Command
04:36:41 PM 0 5340 0.00 0.00 0.00 0.00 3 pidstat
查看所有进程信息统计:
[root@localhost ~]# pidstat -p ALL
Linux 3.10.0-957.el7.x86\_64 (localhost.localdomain) 11/24/2022 \_x86\_64\_ (4 CPU)
04:37:28 PM UID PID %usr %system %guest %CPU CPU Command
04:37:28 PM 0 1 0.01 0.01 0.00 0.01 2 systemd
04:37:28 PM 0 2 0.00 0.00 0.00 0.00 0 kthreadd
04:37:28 PM 0 3 0.00 0.00 0.00 0.00 0 ksoftirqd/0
04:37:28 PM 0 5 0.00 0.00 0.00 0.00 0 kworker/0:0H
04:37:28 PM 0 7 0.00 0.00 0.00 0.00 0 migration/0
04:37:28 PM 0 8 0.00 0.00 0.00 0.00 0 rcu_bh
04:37:28 PM 0 9 0.00 0.07 0.00 0.07 1 rcu_sched
04:37:28 PM 0 10 0.00 0.00 0.00 0.00 0 lru-add-drain
04:37:28 PM 0 11 0.00 0.00 0.00 0.00 0 watchdog/0
04:37:28 PM 0 12 0.00 0.00 0.00 0.00 1 watchdog/1
04:37:28 PM 0 13 0.00 0.00 0.00 0.00 1 migration/1
04:37:28 PM 0 14 0.00 0.00 0.00 0.00 1 ksoftirqd/1
......
2.5 pidstat -r
-r Report page faults and memory utilization.
Linux 3.10.0-957.el7.x86\_64 (localhost.localdomain) 11/24/2022 \_x86\_64\_ (4 CPU)
04:41:20 PM UID PID minflt/s majflt/s VSZ RSS %MEM Command
04:41:20 PM 0 1 0.23 0.00 193952 7080 0.09 systemd
04:41:20 PM 0 356 0.00 0.00 165512 6560 0.08 sshd
04:41:20 PM 0 360 0.00 0.00 165208 5936 0.08 sshd
04:41:20 PM 0 365 0.00 0.00 116784 3432 0.04 bash
04:41:20 PM 0 411 0.00 0.00 72092 2872 0.04 sftp-server
04:41:20 PM 0 482 0.00 0.00 165512 6544 0.08 sshd
04:41:20 PM 0 486 0.00 0.00 165208 5936 0.08 sshd
04:41:20 PM 0 490 0.00 0.00 116916 3660 0.05 bash
04:41:20 PM 0 531 0.00 0.00 72092 2876 0.04 sftp-server
04:41:20 PM 0 2254 0.09 0.00 68504 28784 0.36 systemd-journal
04:41:20 PM 0 2276 0.00 0.00 201084 4180 0.05 lvmetad
04:41:20 PM 0 2297 0.00 0.00 48476 5880 0.07 systemd-udevd
04:41:20 PM 0 4496 0.00 0.00 62044 1292 0.02 auditd
04:41:20 PM 0 4498 0.00 0.00 84552 888 0.01 audispd
04:41:20 PM 0 4499 0.00 0.00 55632 1664 0.02 sedispatch
04:41:20 PM 32 4522 0.00 0.00 73648 1376 0.02 rpcbind
04:41:20 PM 0 4523 0.05 0.00 26460 1840 0.02 systemd-logind
04:41:20 PM 0 4524 0.00 0.00 396624 4604 0.06 accounts-daemon
......
minflt/s
Total number of minor faults the task has made per second, those which have not required loading a memory page from disk.
majflt/s
Total number of major faults the task has made per second, those which have required loading a memory page from disk.
VSZ
Virtual Size: The virtual memory usage of entire task in kilobytes.
RSS
Resident Set Size: The non-swapped physical memory used by the task in kilobytes.
%MEM
The tasks's currently used share of available physical memory.
2.6 pidstat -s
![img](https://img-blog.csdnimg.cn/img_convert/417acc8254ee85e26aea3e47ef17e049.png)
![img](https://img-blog.csdnimg.cn/img_convert/fd971055850c9828c6df2cd65117a417.png)
![img](https://img-blog.csdnimg.cn/img_convert/d345bdd910d09e9821df4fe0116f455d.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
详情docs.qq.com/doc/DSmdCdUNwcEJDTXFK
the task in kilobytes.
%MEM
The tasks's currently used share of available physical memory.
2.6 pidstat -s
[外链图片转存中...(img-f5QXx0GV-1724272208471)]
[外链图片转存中...(img-3MmZEsWa-1724272208471)]
[外链图片转存中...(img-oath4nID-1724272208472)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
详情docs.qq.com/doc/DSmdCdUNwcEJDTXFK