ps进程检查工具¶
ps 输出字段¶
Linux/Unix 通常采用 System V ( ps -elf ) 或 BSD ( ps alx ) 风格 ps
检查进程启动时间
lstart:
检查进程启动时间,例如
qemu-system-x86¶ps -eo pid,ppid,lstart,cmd | grep qemu-system-x86_64
输出显示案例类似如下:
检查进程启动时间,例如
qemu-system-x86 可以看到详细的启动时间¶ 7300 Sun Aug 6 11:10:56 2023 /usr/bin/qemu-system-x86_64 -name guest=z-b-data-3,...
7354 Sun Aug 6 11:11:03 2023 /usr/bin/qemu-system-x86_64 -name guest=z-b-data-1,...
7405 Sun Aug 6 11:11:09 2023 /usr/bin/qemu-system-x86_64 -name guest=z-b-data-2,...
备注
在 ps 检查进程启动时间的 field 格式参数可以有3种:
start启动时间类似09:46:10,但是超过24小时的进程会显示为日期,类似Aug 31stime启动时间类似09:10,但是超过24小时的进程也是显示为日期,类似Aug31lstart启动时间最详尽和标准化,非常方便对比检查,类似Thu Aug 31 23:58:01 2023强烈推荐
ps 检查线程¶
ps 命令的 -T 参数表示输出线程, -p 可以指定进程,结合上文的输出字段,我们可以构建一个检查某个进程所有线程的CPU使用率以及运行在哪些cpu core上,以便进一步排查进程异常。
举例,进程 qemu-system-x86 的 pid 是 7354 ,当前 top 可以看到使用的CPU百分比大约是 10+% :
检查
qemu-system-x86 进程的线程负载¶top - 16:30:30 up 4 days, 5:28, 11 users, load average: 0.72, 0.55, 0.40
Tasks: 630 total, 1 running, 629 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.6 us, 0.3 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 386813.0 total, 327264.1 free, 51822.8 used, 7726.1 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 332638.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7354 libvirt+ 20 0 16.8g 16.0g 19768 S 10.2 4.2 635:51.79 qemu-system-x86
7300 libvirt+ 20 0 16.7g 16.0g 19668 S 6.6 4.2 459:39.34 qemu-system-x86
7405 libvirt+ 20 0 16.8g 16.0g 19732 S 6.6 4.2 442:22.15 qemu-system-x86
2014 prometh+ 20 0 726984 19972 12036 S 4.3 0.0 50:29.36 node_exporter
现在来解析这个进程的线程:
检查进程的所有线程使用的cpu资源以及调度的cpu core¶
ps -T -o pid,tid,c,pcpu,comm -p 7354
输出信息:
检查进程的所有线程使用的cpu资源以及调度的cpu core¶
PID TID C %CPU COMMAND
7354 7354 0 0.2 qemu-system-x86
7354 7358 0 0.0 qemu-system-x86
7354 7361 0 0.0 IO mon_iothread
7354 7363 2 2.1 CPU 0/KVM
7354 7364 2 2.6 CPU 1/KVM
7354 7365 2 2.5 CPU 2/KVM
7354 7366 2 2.7 CPU 3/KVM
7354 7470 0 0.0 worker
7354 210749 0 0.0 worker
这里可以看到4个kvm线程分别消耗了大约 2.5% 的CPU资源
参考¶
About the output fields of the ps command in Unix 非常清晰的
ps -o参数字段快速查询,建议参考