pidstat 使用

介绍

pidstat 是一个用来监控报告被linux 系统管理的任务的统计的数据。

 

man pidstat 查看介绍

PIDSTAT(1)                                                                              Linux User's Manual                                                                              PIDSTAT(1)

NAME
       pidstat - Report statistics for Linux tasks.

SYNOPSIS
       pidstat  [ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U [ username ] ] [ -u ] [ -V ] [ -w ] [ -C comm ] [ -p { pid [,...] | SELF | ALL } ] [ -T { TASK | CHILD | ALL } ] [ interval [
       count ] ]

DESCRIPTION
       The pidstat command is used for monitoring individual tasks currently being managed by the Linux kernel.  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. 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.

       The pidstat command can also be used for monitoring the child processes of selected tasks.  Read about option -T below.

       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).  The count parameter can be specified in conjunction with the interval parameter if this one is not set to zero. 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.

       You can select information about specific task activities using flags.  Not specifying any flags selects only CPU activity.

解释:关于描述中的几句话的解释如下

1、pidstat 被用来监控当前正在被内核管理的任务,它会输出信息到std ,如果命令后面的参数是-p pid 那么就输出的是指定的pid 的进程的信息;如果-p ALL或者不加任何参数那么输出的就是所有的任务的数据。

2、pidstat 也可以被用来监控所选任务的子进程,详情请查看选项-T下面的介绍

3、频率参数定义了报告之间的以秒为单位的时间间隔,如果为0或者不写那么就是系统启动以来这段时间的统计数据。具体可以看后面的使用例子

 

 

使用介绍

 

注意pidstat  可以查看进程的很多信息,包括cpu使用、内存、io 等等,所以要查看不同性能指标就要指定对应的参数,而不指定默认就是cpu 情况报告,如下命令

pidstat  2  3    #   每隔2秒出一个报告数据,一共出具3次

Average: UID PID %usr %system %guest %CPU CPU Command
Average: 0 9 0.00 0.49 0.00 0.49 - rcu_sched
Average: 0 848 0.98 0.00 0.00 0.98 - systemd-journal
Average: 0 1633 59.02 0.98 0.00 60.00 - dockerd
Average: 0 1792 0.49 0.00 0.00 0.49 - containerd
Average: 0 2275 0.98 0.00 0.00 0.98 - rancher
Average: 0 2875 1.95 0.98 0.00 2.93 - etcd
Average: 0 4663 0.49 0.49 0.00 0.98 - kube-scheduler
Average: 0 4696 2.93 1.95 0.00 4.88 - kube-proxy

查看cpu 和上下文切换情况

# 每隔1秒输出1组数据(需要 Ctrl+C 才结束)
# -w参数表示输出进程切换指标,而-u参数则表示输出CPU使用指标
$ pidstat -w -u 1
08:06:33      UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
08:06:34        0     10488   30.00  100.00    0.00    0.00  100.00     0  sysbench
08:06:34        0     26326    0.00    1.00    0.00    0.00    1.00     0  kworker/u4:2

08:06:33      UID       PID   cswch/s nvcswch/s  Command
08:06:34        0         8     11.00      0.00  rcu_sched
08:06:34        0        16      1.00      0.00  ksoftirqd/1
08:06:34        0       471      1.00      0.00  hv_balloon
08:06:34        0      1230      1.00      0.00  iscsid
08:06:34        0      4089      1.00      0.00  kworker/1:5
08:06:34        0      4333      1.00      0.00  kworker/0:3
08:06:34        0     10499      1.00    224.00  pidstat
08:06:34        0     26326    236.00      0.00  kworker/u4:2
08:06:34     1000     26784    223.00      0.00  sshd

pidstat 默认显示的是进程的信息,如果要显示线程信息加上t参数

# 每隔1秒输出一组数据(需要 Ctrl+C 才结束)
# -wt 参数表示输出线程的上下文切换指标
$ pidstat -wt 1
08:14:05      UID      TGID       TID   cswch/s nvcswch/s  Command
...
08:14:05        0     10551         -      6.00      0.00  sysbench
08:14:05        0         -     10551      6.00      0.00  |__sysbench
08:14:05        0         -     10552  18911.00 103740.00  |__sysbench
08:14:05        0         -     10553  18915.00 100955.00  |__sysbench
08:14:05        0         -     10554  18827.00 103954.00  |__sysbench
...

 

版本问题

旧版本的sysstat 中的pidstat 命令输出没有%wait 指标,只有在systat 11.5.5 版本以后才引进的这个指标。注意这个指标表示的是进程等待cpu 的时间百分比也就是进程处于就绪队列中的状态,而不是等待io 的状态。

posted @ 2021-01-03 20:05  fanggege  阅读(966)  评论(0编辑  收藏  举报