一、基本信息
1. 特性
pidstat: 像topd、vmstat命令对于单个进程的状态并不是非常的直观,而pidstat命令可以更方便的获取到各进程的状态。
2. 获取源码
[ ](sysstat/sysstat: Performance monitoring tools for Linux (github.com))
二、编译
# mkdir obj
# ./configure \
--prefix=/home/liujun/workspace/ztemp/tool/sysstat-master/obj \
--host=x86_64 \
CFLAGS="-g -O2 -static" \
CC=gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc \
# make
编译后会在顶层目录内生成可执行文件pidstat、iostat、sysstat、tapestat等
三、pidstat工具的使用
1. 选项
-w:查看每个进程上下文切换情况
-t:查看每个进程线程相关的信息,默认是进程的;常与-w结合使用(cpu的上下文切换包括进程的切换、线程的切换、中断的切换)
-d:展示每个进程磁盘 I/O 统计数据
-p:指明进程号
-r:查看内存
2. 使用示例
liujun@admin:~/workspace/project$ pidstat
Linux 4.15.0-142-generic (admin) 2022年06月16日 _x86_64_ (4 CPU)
20时59分27秒 UID PID %usr %system %guest %CPU CPU Command
20时59分27秒 0 1 0.00 0.17 0.00 0.18 2 systemd
20时59分27秒 0 2 0.00 0.00 0.00 0.00 0 kthreadd
20时59分27秒 0 7 0.00 0.00 0.00 0.00 0 ksoftirqd/0
20时59分27秒 0 8 0.00 0.02 0.00 0.02 0 rcu_sched
20时59分27秒 0 15 0.00 0.01 0.00 0.01 1 migration/1
20时59分27秒 0 16 0.00 0.00 0.00 0.00 1 ksoftirqd/1
---->
%usr --- 用户空间占用CPU百分比
%system --- 内核空间占用CPU百分比
%CPU --- 总的CPU使用百分比
------------------------------------------------------------------------------------------
# 每隔1秒输出一组数据(需要 Ctrl+C 才结束)
# -wt 参数表示输出线程的上下文切换指标
liujun@admin:~$ pidstat -wt 1
Linux 4.15.0-142-generic (admin) 2022年06月16日 _x86_64_ (4 CPU)
21时14分20秒 UID PID cswch/s nvcswch/s Command
21时14分20秒 0 1 0.66 0.47 systemd
21时14分20秒 0 2 0.13 0.00 kthreadd
21时14分20秒 0 4 0.00 0.00 kworker/0:0H
21时14分20秒 0 6 0.00 0.00 mm_percpu_wq
----->
cswch/s:表示每秒自愿上下文切换(voluntary context switches)的次数
ncswch/s:表示每秒非自愿上下文切换(non voluntary context switches)的次数
其它工具使用到了再记录吧