本文主要是介绍sysstat系列:pidstat,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
sysstat系列:pidstat
说明
pidstat
是 Linux 系统中的一个性能监控工具,它是 sysstat
包的一部分;
和sysstat
系列的sar
相比,主要用于收集、报告和保存系统活动信息;
pidstat
提供的是进程级别的性能数据,专门用于监控和报告特定进程的性能数据,包括 CPU、内存、I/O 等资源的使用情况。
本篇主要目的如下:
- 整理pidstat使用方式,了解pidstat返回内容
- pidstat场景使用
链接参考
官方说明
sysstat (GODARD Sebastien) · GitHub](https://github.com/sysstat)
使用说明
以下信息是通过man pidstat
返回内容获取
说明
NAMEpidstat - Report statistics for Linux tasks. # 报告Linux任务的统计信息。
简介
SYNOPSIS # 简介pidstat [ -d ] [ -H ] [ -h ] [ -I ] [ -l ] [ -R ] [ -r ] [ -s ] [ -t ] [ -U [ username ] ] [ -u ] [ -V ] [ -v ] [ -w ] [ -C comm ] [ -G process_name ] [ --human ] [ -p { pid [,...] | SELF | ALL } ] [ -T { TASK | CHILD | ALL } ] [ interval [ count ] ] [ -e program args ]
该内容可能和结合pidstat --help
分析
Usage: pidstat [ options ] [ <interval> [ <count> ] ] [ -e <program> <args> ]
Options are:
[ -d ] [ -H ] [ -h ] [ -I ] [ -l ] [ -R ] [ -r ] [ -s ] [ -t ] [ -U [ <username> ] ]
[ -u ] [ -V ] [ -v ] [ -w ] [ -C <command> ] [ -G <process_name> ] [ --human ]
[ -p { <pid> [,...] | SELF | ALL } ] [ -T { TASK | CHILD | ALL } ]
主要分为3部分内容,主要是[ options ]
、[ <interval> [ <count> ] ]
和 [ -e <program> <args> ]
- [ options ]:选项参数,参数较多,每个参数对应对应功能;具体见选项说明
- [ <interval> [ <count> ] ]:间隔参数 和计数参数, 间隔参数可以设置为任意0或者正整数;如果设置为0,表示将报告自系统启动(引导)以来的任务统计数据;否则表示间隔。计数参数表示设置间隔秒数生成的报告数量;如果不设置,表示无限制输出
- [ -e <program> <args> ]:
另外,对于pidstat --help
返回内容,应该知道以下规则:
- []:表示非必输项;即可输入或不输入,如[ options ],即表示options可输入也可不输入
- {}:表示必输项,如
[ -T { TASK | CHILD | ALL } ]
,表示选项-T可填或不填,使用-T时,必须选择TASK、CHILD或ALL之一 - |:表示或,如
{ TASK | CHILD | ALL }
,表示选项-T,可以选择TASK、CHILD或ALL - <>:表示输入变量,如<interval>,<pid>,<count>,<process_name>等
描述
DESCRIPTIONThe pidstat command is used for monitoring individual tasks currently being managed by the Linux kernel. # pidstat 命令用于监控当前由 Linux 内核管理的各个任务
It writes to standard output activities for every task selected with option -p or for every task managed by the Linux kernel if option -p ALL has been used.
# 它会将选项 -p 选中的每个任务或选项 -p ALL 选中的由 Linux 内核管理的每个任务的活动写入标准输出
Not selecting any tasks is equivalent to specifying -p ALL but only active tasks (tasks with non-zero statistics values) will appear in the report.
# 不选择任何任务等同于指定 -p ALL,但只有活动任务(统计值不为零的任务)才会出现在报告中 (说明选项 -p 的作用)The pidstat command can also be used for monitoring the child processes of selected tasks. Read about option -T below.
# pidstat 命令还可用于监控选定任务的子进程。请阅读下面的选项 -T(说明选项 -T 的作用)The interval parameter specifies the amount of time in seconds between each report.
A value of 0 (or no parameters at all) indicates that tasks statistics are to be reported for the time since system startup (boot).
# 间隔参数指定每次报告之间的时间间隔(以秒为单位)。如果值为 0(或根本没有参数),则表示将报告自系统启动(引导)以来的任务统计数据。([ <interval> [ <count> ] ]的作用)
The count parameter can be specified in conjunction with the interval parameter if this one is not set to zero.
# 如果时间间隔参数未设置为 0,则可同时指定计数参数(可以通过设置count限制输出次数)。
The value of count determines the number of reports generated at interval seconds apart. If the interval parameter is specified without the count parameter, the pidstat command generates reports continuously.
# 计数的值决定了间隔秒数生成的报告数量。如果只指定 interval 参数而不指定 count 参数,pidstat 命令将连续生成报告。(如果不指定count,则持续输出)You can select information about specific task activities using flags. Not specifying any flags selects only CPU activity.
# 您可以使用标记选择特定任务活动的信息。不指定任何标记,则只选择 CPU 活动。
选项说明(man)
选项比较多,可以分为3类,分别是:匹配指定类、父子任务显示类,资源选择类
匹配指定类
指定被监控者是谁,如哪个程序、哪个进程或者哪个用户
-C <comm> 根据名称模糊筛选命令
Display only tasks whose command name includes the string comm. This string can be a regular expression.
仅仅展示命令名中包含comm字符串的任务,这个comm字符串可以是一个正则表达式
示例
# 存在一个脚本
[root@host01 tmp]# cat test
while true; do echo 1 > test.log sleep 5; done# 多次挂起到后台
nohup ./test &# 启动后
root 2493258 2209771 88 23:34 pts/1 00:00:06 /bin/sh ./test
root 2493321 2209771 91 23:34 pts/1 00:00:04 /bin/sh ./test
root 2493384 2209771 74 23:34 pts/1 00:00:02 /bin/sh ./test# 示例与返回
[root@host01 tmp]# pidstat -C sh
Linux 5.10.134-15.al8.x86_64 (host01) 04/05/2024 _x86_64_ (2 CPU)11:37:33 PM UID PID %usr %system %guest %wait %CPU CPU Command
11:37:33 PM 0 2493258 0.03 0.02 0.00 0.03 0.05 1 sh
11:37:33 PM 0 2493321 0.03 0.02 0.00 0.03 0.05 1 sh
11:37:33 PM 0 2493384 0.03 0.02 0.00 0.03 0.05 1 sh
-G <process_name> 根据名称模糊筛选进程
Display only processes whose command name includes the string process_name. This string can be a regular expression. If option -t is used together with option -G then the threads belonging to that process are also displayed (even if their command name doesn’t include the string process_name).
只显示命令名称包含 process_name 字符串的进程。该字符串可以是正则表达式。如果选项 -t 与选项 -G 同时使用,则也会显示属于该进程的线程(即使其命令名不包含 process_name 字符串)。
示例
同-C comm
-U [ username ] 指定用户
Display the real user name of the tasks being monitored instead of the UID. If username is specified, then only tasks belonging to the specified user are displayed.
显示被监控任务的真实用户名,而不是 UID。如果指定了用户名,则只显示属于指定用户的任务。
示例
# 未使用-U:显示UID,而不是具体用户名
[root@host01 ~]# pidstat
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
01:12:23 AM UID PID %usr %system %guest %wait %CPU CPU Command
01:12:23 AM 0 1 0.00 0.00 0.00 0.00 0.00 1 systemd
...# 使用-U:显示具体用户名
[root@host01 ~]# pidstat -U
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
01:12:45 AM USER PID %usr %system %guest %wait %CPU CPU Command
01:12:45 AM root 1 0.00 0.00 0.00 0.00 0.00 1 systemd
...# 使用—U 且指定用户:选择指定用户
[root@host01 ~]# pidstat -U root
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
12:53:20 AM USER PID %usr %system %guest %wait %CPU CPU Command
12:53:20 AM root 1 0.00 0.00 0.00 0.00 0.00 0 systemd
...
-p { pid [,…] | SELF | ALL } 指定进程名
Select tasks (processes) for which statistics are to be reported. pid is the process identification number. The SELF keyword indicates that statistics are to be reported for the pidstat process itself, whereas the ALL keyword indicates that statistics are to be reported for all the tasks managed by the system.
选择要报告统计数据的任务(进程)。pid 是进程标识号。SELF 关键字表示要报告 pidstat 进程本身的统计数据,而 ALL 关键字表示要报告系统管理的所有任务的统计数据。
示例
# 获取指定进程的统计数据
[root@host01 ~]# pidstat -p 2493384
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
01:34:54 AM UID PID %usr %system %guest %wait %CPU CPU Command
01:34:54 AM 0 2493384 1.07 0.90 0.00 1.10 1.98 0 sh
...
# 获取pidstat进程本身的统计数据
[root@host01 ~]# pidstat -p SELF
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
01:32:46 AM UID PID %usr %system %guest %wait %CPU CPU Command
01:32:46 AM 0 2665128 0.00 0.00 0.00 0.00 0.00 0 pidstat
...
# 报告系统管理的所有任务的统计数据
[root@host01 ~]# pidstat -p ALL
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
01:33:19 AM UID PID %usr %system %guest %wait %CPU CPU Command
01:33:19 AM 0 1 0.00 0.00 0.00 0.00 0.00 0 systemd
...
父子任务显示类
控制是否返回父子任务的返回情况
-T { TASK | CHILD | ALL } 指定获取任务的类型
This option specifies what has to be monitored by the pidstat command. The TASK keyword indicates that statistics are to be reported for individual tasks (this is the default option) whereas the CHILD keyword indi‐cates that statistics are to be globally reported for the selected tasks and all their children. The ALL keyword indicates that statistics are to be reported for individual tasks and globally for the selected tasks and their children. 该选项指定 pidstat 命令需要监控的内容。 - TASK 关键字:表示要报告单个任务的统计数据(这是默认选项), - CHILD 关键字:表示要全局报告所选任务及其所有子任务的统计数据。 - ALL 关键字:表示报告单个任务的统计数据,以及所选任务及其子任务的全局统计数据。Note: Global statistics for tasks and all their children are not available for all options of pidstat. Also these statistics are not necessarily relevant to current time interval: The statistics of a child process are collected only when it finishes or it is killed. 注意:对于 pidstat 的所有选项,任务及其所有子任务的全局统计数据都不可用。此外,这些统计数据也不一定与当前时间间隔相关: 只有当子进程结束或被杀死时,才会收集其统计数据。
示例
# 未使用-T
[root@host01 ~]# pidstat
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
11:44:01 AM UID PID %usr %system %guest %wait %CPU CPU Command
11:44:01 AM 0 1 0.00 0.00 0.00 0.00 0.00 0 systemd
...
# 使用-T 指定TASK
[root@host01 ~]# pidstat -T TASK
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
11:46:07 AM UID PID %usr %system %guest %wait %CPU CPU Command
11:46:07 AM 0 1 0.00 0.00 0.00 0.00 0.00 0 systemd# 使用-T 指定CHILD
[root@host01 ~]# pidstat -T CHILD
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
11:46:15 AM UID PID usr-ms system-ms guest-ms Command
11:46:15 AM 0 1 178900 102410 0 systemd# 使用-T 指定ALL
[root@host01 ~]# pidstat -T ALL
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
11:46:19 AM UID PID %usr %system %guest %wait %CPU CPU Command
11:46:19 AM 0 1 0.00 0.00 0.00 0.00 0.00 0 systemd11:46:19 AM UID PID usr-ms system-ms guest-ms Command
11:46:19 AM 0 1 178900 102410 0 systemd
-t 展示跟此选择的任务相关的子线程
Also display statistics for threads associated with selected tasks. This option adds the following values to the reports:
还可显示与选定任务相关的线程统计信息。该选项会在报告中添加以下值:
TGID
The identification number of the thread group leader. 线程组组长的标识号TID
The identification number of the thread being monitored. 被监控线程的标识号
示例
# 未使用 -t
[root@host01 ~]# pidstat
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
12:05:09 PM UID PID %usr %system %guest %wait %CPU CPU Command
12:05:09 PM 0 1 0.00 0.00 0.00 0.00 0.00 1 systemd# 使用 -t
[root@host01 ~]# pidstat -t
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
12:05:11 PM UID TGID TID %usr %system %guest %wait %CPU CPU Command
12:05:11 PM 0 1 - 0.00 0.00 0.00 0.00 0.00 1 systemd
12:05:11 PM 0 - 1 0.00 0.00 0.00 0.00 0.00 1 |__systemd
资源选择类
指定要展示的内容,比如I/O,内存,CPU等
-d 报告I/O统计信息
返回说明
Report I/O statistics (kernels 2.6.20 and later only). The following values may be displayed:
# 报告IO统计信息,会展示下面的值UIDThe real user identification number of the task being monitored. # 被监控任务的实际用户idUSERThe name of the real user owning the task being monitored. # 被监控任务的实际用户名称PIDThe identification number of the task being monitored.# 被监控任务的idkB_rd/sNumber of kilobytes the task has caused to be read from disk per second.# 此任务每秒从disk上读取的KiBkB_wr/sNumber of kilobytes the task has caused, or shall cause to be written to disk per second.# 此任务每秒已经写入的或者引起写入到disk的KiBkB_ccwr/sNumber of kilobytes whose writing to disk has been cancelled by the task. This may occur when the task truncates some dirty pagecache. In this case, some IO which another task has been accounted for will not be happening. # 此任务取消掉的写入到磁盘的KiB数。当任务截断了一些脏页缓存的时候可能会出现。iodelayBlock I/O delay of the task being monitored, measured in clock ticks. This metric includes the delays spent waiting for sync block I/O completion and for swapin block I/O completion. # 被监视任务的块I/O延迟,以时钟周期为单位。此度量标准包括等待同步块I/O完成和交换模块I/O完成所花费的延迟。CommandThe command name of the task. # 任务的命令名
示例
[root@host01 ~]# pidstat -d
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)12:12:28 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
12:12:28 PM 0 1 10.14 0.78 0.00 119 systemd
-R 报告实时优先级和调度策略信息
Report realtime priority and scheduling policy information. The following values may be displayed:
报告实时优先级和调度策略信息。可显示以下值:
UID
The real user identification number of the task being monitored. 被监控任务的真实用户标识号
USER
The name of the real user owning the task being monitored. 监控任务的真实用户名
PID
The identification number of the task being monitored. 被监控任务的标识号
prio
The realtime priority of the task being monitored. 被监控任务的实时优先级
policy
The scheduling policy of the task being monitored. 被监控任务的调度策略
Command
The command name of the task.
示例
[root@host01 ~]# pidstat -R
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
01:37:21 PM UID PID prio policy Command
01:37:21 PM 0 13 99 FIFO migration/0
01:37:21 PM 0 16 99 FIFO migration/1
01:37:21 PM 0 88 50 FIFO watchdogd
01:37:21 PM 0 124 0 IDLE kidled
-r 报告内存使用情况
Report page faults and memory utilization. When reporting statistics for individual tasks, the following values may be displayed:
报告页面故障和内存使用情况。报告单个任务的统计数据时,可能会显示以下值:
UID
The real user identification number of the task being monitored. 被监控任务的真实用户标识号
USER
The name of the real user owning the task being monitored. 监控任务的真实用户名
PID
The identification number of the task being monitored.被监控任务的标识号
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.
任务当前使用的可用物理内存份额。
Command
The command name of the task.
When reporting global statistics for tasks and all their children, the following values may be displayed:
报告任务及其所有子任务的全局统计数据时(就是-T),可能会显示以下值:
UID
The real user identification number of the task which is being monitored together with its children.
被监控任务及其子任务的真实用户识别号。
USER
The name of the real user owning the task which is being monitored together with its children.
拥有被监控任务及其子任务的真实用户的名称。
PID
The identification number of the task which is being monitored together with its children.
被监控任务及其子任务的标识号。
minflt-nr
Total number of minor faults made by the task and all its children, and collected during the interval of time.
任务及其所有子任务在时间间隔内发生的小故障总数。
majflt-nr
Total number of major faults made by the task and all its children, and collected during the interval of time.
任务及其所有子任务在时间间隔内发生的主要故障总数。
Command
The command name of the task which is being monitored together with its children.
示例
[root@host01 ~]# pidstat -r
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
12:51:15 PM UID PID minflt/s majflt/s VSZ RSS %MEM Command
12:51:15 PM 0 1 0.36 0.00 103200 6408 0.33 systemd[root@host01 ~]# pidstat -r -T ALL
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
12:54:34 PM UID PID minflt/s majflt/s VSZ RSS %MEM Command
12:54:34 PM 0 1 0.36 0.00 103200 6408 0.33 systemd12:54:34 PM UID PID minflt-nr majflt-nr Command
12:54:34 PM 0 1 3318165 12512 systemd
-u 报告 CPU 使用率
When reporting statistics for individual tasks, the following values may be displayed:
报告单个任务的统计数据时,可能会显示以下值:
UID
The real user identification number of the task being monitored.
USER
The name of the real user owning the task being monitored.
PID
The identification number of the task being monitored.
%usr
Percentage of CPU used by the task while executing at the user level (application), with or without nice priority. Note that this field does NOT include time spent running a virtual processor.任务在用户级(应用程序)执行时使用的 CPU 百分比,无论是否具有漂亮的优先级。请注意,该字段不包括运行虚拟处理器的时间。
%system
Percentage of CPU used by the task while executing at the system level (kernel).任务在系统级(内核)执行时占用 CPU 的百分比。
%guest
Percentage of CPU spent by the task in virtual machine (running a virtual processor).虚拟机(运行虚拟处理器)中任务占用 CPU 的百分比。
%wait
Percentage of CPU spent by the task while waiting to run.任务在等待运行时占用 CPU 的百分比。
%CPU
Total percentage of CPU time used by the task. In an SMP environment, the task’s CPU usage will be divided by the total number of CPU’s if option -I has been entered on the command line.任务使用的 CPU 总时间百分比。在 SMP 环境中,如果在命令行中输入了选项 -I,则任务的 CPU 占用率将除以 CPU 总数。
CPU
Processor number to which the task is attached.任务所连接的处理器编号
Command
The command name of the task.
When reporting global statistics for tasks and all their children, the following values may be displayed:报告任务及其所有子任务的全局统计数据时,可能会显示以下值:
UID
The real user identification number of the task which is being monitored together with its children.USER
The name of the real user owning the task which is being monitored together with its children.
PID
The identification number of the task which is being monitored together with its children.
usr-ms
Total number of milliseconds spent by the task and all its children while executing at the user level (application), with or without nice priority, and collected during the interval of time. Note that this field does NOT include time spent running a virtual processor.任务及其所有子任务在用户级(应用程序)执行时花费的毫秒总数,无论是否具有漂亮的优先级,并在时间间隔内收集。请注意,该字段不包括运行虚拟处理器所花费的时间。
system-ms
Total number of milliseconds spent by the task and all its children while executing at the system level (kernel), and collected during the interval of time.任务及其所有子任务在系统级(内核)执行时花费的毫秒总数,在时间间隔内收集。
guest-ms
Total number of milliseconds spent by the task and all its children in virtual machine (running a virtual processor).任务及其所有子任务在虚拟机(运行虚拟处理器)中花费的总毫秒数。
Command
The command name of the task which is being monitored together with its children.
示例
[root@host01 ~]# pidstat -u
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)01:39:23 PM UID PID %usr %system %guest %wait %CPU CPU Command
01:39:23 PM 0 1 0.00 0.00 0.00 0.00 0.00 1 systemd
-s 报告堆栈利用率
Report stack utilization. The following values may be displayed:
报告堆栈利用率。可显示以下值:
UID
The real user identification number of the task being monitored.USER
The name of the real user owning the task being monitored.PID
The identification number of the task being monitored.StkSize
The amount of memory in kilobytes reserved for the task as stack, but not necessarily used.为任务保留的堆栈内存量(千字节),但不一定使用。
StkRef
The amount of memory in kilobytes used as stack, referenced by the task.任务引用的堆栈内存量(千字节)。
Command
The command name of the task.
示例
[root@host01 ~]# pidstat -s
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)
04:11:08 PM UID PID StkSize StkRef Command
04:11:08 PM 0 1 132 52 systemd
-v 报告某些内核表的值
Report values of some kernel tables. The following values may be displayed:
报告某些内核表的值。可显示以下值
UID
The real user identification number of the task being monitored.USER
The name of the real user owning the task being monitored.PID
The identification number of the task being monitored.threads
Number of threads associated with current task.与当前任务相关的线程数。
fd-nr
Number of file descriptors associated with current task.与当前任务相关的文件描述符数量。
Command
The command name of the task.
示例
[root@host01 ~]# pidstat -v
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)01:57:01 PM UID PID threads fd-nr Command
01:57:01 PM 0 1 1 59 systemd
01:57:01 PM 0 2 1 0 kthreadd
01:57:01 PM 0 3 1 0 rcu_gp
-w 报告任务切换活动
Report task switching activity (kernels 2.6.23 and later only). The following values may be displayed:
报告任务切换活动(仅限内核 2.6.23 及更高版本)。可显示以下值:
UID
The real user identification number of the task being monitored.
USER
The name of the real user owning the task being monitored.
PID
The identification number of the task being monitored.
cswch/s
Total number of voluntary context switches the task made per second. A voluntary context switch occurs when a task blocks because it requires a resource that is unavailable.任务每秒进行的自愿上下文切换总数。当任务需要的资源不可用而阻塞时,就会发生自愿上下文切换。
nvcswch/s
Total number of non voluntary context switches the task made per second. A involuntary context switch takes place when a task executes for the duration of its time slice and then is forced to relinquish the processor.任务每秒进行的非自愿上下文切换总数。非自愿上下文切换是指任务在其时间片内执行完毕后,被迫放弃处理器。
Command
The command name of the task.
示例
[root@host01 ~]# pidstat -w
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)02:05:07 PM UID PID cswch/s nvcswch/s Command
02:05:07 PM 0 1 0.04 0.00 systemd
其它
-e program args Execute program with given arguments args and monitor it with pidstat. pidstat stops when program terminates. # 使用给定参数 args 执行程序,并使用 pidstat 监控程序。-H Display timestamp in seconds since the epoch. # 以秒为单位显示自纪元以来的时间戳。-h Display all activities horizontally on a single line, with no average statistics at the end of the report. This is intended to make it easier to be parsed by other programs. # 在一行中水平显示所有活动,报告末尾不显示平均统计数据。这样做的目的是便于其他程序解析--humanPrint sizes in human readable format (e.g. 1.0k, 1.2M, etc.) The units displayed with this option supersede any other default units (e.g. kilobytes, sectors...) associated with the metrics. # 以人类可读格式打印大小(如 1.0k、1.2M 等)。该选项显示的单位取代与指标相关的任何其他默认单位(如千字节、扇区......)-I In an SMP environment, indicate that tasks CPU usage (as displayed by option -u ) should be divided by the total number of processors. # 在 SMP 环境中,表示任务 CPU 使用量(如选项 -u 所示)应除以处理器总数-l Display the process command name and all its arguments. # 显示进程命令名称及其所有参数-V Print version number then exit. # 获取版本信息
示例说明(man)
pidstat 2 5Display five reports of CPU statistics for every active task in the system at two second intervals.
# 以两秒为间隔,为系统中的每个活动任务显示五份 CPU 统计报告。
pidstat -r -p 1643 2 5Display five reports of page faults and memory statistics for PID 1643 at two second intervals.
# 以两秒为间隔,为 PID 1643 显示五份页面故障和内存统计报告。
pidstat -C "fox|bird" -r -p ALLDisplay global page faults and memory statistics for all the processes whose command name includes the string "fox" or "bird".
# 显示命令名称包含 "fox "或 "bird "字符串的所有进程的全局页面故障和内存统计信息。
pidstat -T CHILD -r 2 5Display five reports of page faults statistics at two second intervals for the child processes of all tasks in the system. Only child processes with non-zero statistics values are displayed.
# 以两秒为间隔,为系统中所有任务的子进程显示五份页面故障统计报告。只显示统计值不为零的子进程。
场景示例
dd测试I/O资源分析
以下将使用dd命令,不仅可以通过dd命令获取测试磁盘I/O性能,并使用pidstat获取资源情况
dd磁盘写
[root@host01 data]# time dd if=/dev/zero of=/data/test.log bs=4k count=1000000
1000000+0 records in
1000000+0 records out
4096000000 bytes (4.1 GB, 3.8 GiB) copied, 34.603 s, 118 MB/sreal 0m34.604s
user 0m0.747s
sys 0m2.850s
pidstat获取I/O情况
[root@host01 ~]# pidstat -C dd -d 1 1
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)05:50:33 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
05:50:34 PM 0 110269 0.00 111000.00 0.00 90 ddAverage: UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
Average: 0 110269 0.00 111000.00 0.00 90 dd
可以看到,dd的测试结果111MB/s
和pidstat的结果101012kB_wr/s
相差不大
dd磁盘读
[root@host01 data]# time dd if=/data/test.log of=/dev/null bs=4k
1000000+0 records in
1000000+0 records out
4096000000 bytes (4.1 GB, 3.8 GiB) copied, 36.8481 s, 111 MB/sreal 0m36.849s
user 0m0.664s
sys 0m1.432s
pidstat获取I/O情况
[root@host01 ~]# pidstat -C dd -d 1 1
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)05:52:12 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
05:52:13 PM 0 113347 110592.00 0.00 0.00 95 ddAverage: UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
Average: 0 113347 110592.00 0.00 0.00 95 dd
可以看到,dd的测试结果111MB/s
和pidstat的结果110592kB_wr/s
相差不大
磁盘读写
[root@host01 data]# time dd if=/data/test.log of=/data/test2.log bs=4k
1000000+0 records in
1000000+0 records out
4096000000 bytes (4.1 GB, 3.8 GiB) copied, 71.2047 s, 57.5 MB/sreal 1m11.206s
user 0m0.695s
sys 0m3.576s
pidstat获取I/O情况
[root@host01 ~]# pidstat -C dd -d 1 1
Linux 5.10.134-15.al8.x86_64 (host01) 04/06/2024 _x86_64_ (2 CPU)05:55:02 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
05:55:03 PM 0 117876 53248.00 56708.00 0.00 96 ddAverage: UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
Average: 0 117876 53248.00 56708.00 0.00 96 dd
极限耗尽CPU资源
# 耗尽CPU命令
for i in `seq $(cat /proc/cpuinfo |grep "processor" |wc -l)`; do sha512sum /dev/zero & done
pidstat结果
pidstat -u 1 10 -t
# 分析结果
06:35:34 PM UID TGID TID %usr %system %guest %wait %CPU CPU Command
06:35:35 PM 0 - 2717 1.00 0.00 0.00 0.00 1.00 1 |__AliYunDunMonito
06:35:35 PM 0 196788 - 93.00 3.00 0.00 3.00 96.00 1 sha512sum
06:35:35 PM 0 - 196788 93.00 3.00 0.00 3.00 96.00 1 |__sha512sum
06:35:35 PM 0 196789 - 96.00 2.00 0.00 1.00 98.00 0 sha512sum
06:35:35 PM 0 - 196789 96.00 2.00 0.00 1.00 98.00 0 |__sha512sum
06:35:35 PM 0 197568 - 1.00 1.00 0.00 0.00 2.00 1 pidstat
06:35:35 PM 0 - 197568 1.00 1.00 0.00 0.00 2.00 1 |__pidstat
这篇关于sysstat系列:pidstat的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!