pidstat 统计进程、线程、进程组的资源消耗

本文涉及的产品
云原生数据库 PolarDB 分布式版,标准版 2核8GB
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:

标签

PostgreSQL , Linux , pidstat


背景

如果我们要查看Linux下面进程、进程组、线程的资源消耗的统计信息,可以使用pidstat,它可以收集并报告进程的统计信息。

pidstat实际上也是将/proc/pid下的统计信息统筹后展现给用户。

pidstat例子

1. 列出IO统计信息:-d

2. 列出内存使用统计,page fault:-r

3. CPU统计信息:-u

4. 上下文切换统计信息:-w

5. 指定输出的维度

指定命令: -C command

指定进程号:-p { pid [,...] | SELF | ALL }

将所有列输出到单行、便于导入单张表中:-h

按每个CPU核的统计:-I

按所有CPU核统计:默认

列出命令的完整内容,包括参数:-l

列出线程统计信息:-t

按进程维度单独统计、按进程全局+子进程单独统计、按进程全局、单独统计同时按子任务单独统计:-T { TASK | CHILD | ALL }

实例,统计5秒的值并输出

#pidstat -d -r -u -w -l -h -p ALL 5 1|less  


#      Time       PID    %usr %system  %guest    %CPU   CPU  minflt/s  majflt/s     VSZ    RSS   %MEM   kB_rd/s   kB_wr/s kB_ccwr/s   cswch/s nvcswch/s  Command  
 1488541252         1    0.00    0.00    0.00    0.00     0      0.00      0.00   19348   1512   0.00      0.00      0.00      0.00      0.00      0.00  /sbin/init   
 1488541252         2    0.00    0.00    0.00    0.00    50      0.00      0.00       0      0   0.00      0.00      0.00      0.00      0.00      0.00  kthreadd  
 1488541252         3    0.00    0.00    0.00    0.00     0      0.00      0.00       0      0   0.00      0.00      0.00      0.00      0.59      0.00  migration/0  
 1488541252         4    0.00    0.00    0.00    0.00     0      0.00      0.00       0      0   0.00      0.00      0.00      0.00      0.00      0.00  ksoftirqd/0  
 1488541252         5    0.00    0.00    0.00    0.00     0      0.00      0.00       0      0   0.00      0.00      0.00      0.00      0.00      0.00  migration/0  
 1488541252         6    0.00    0.00    0.00    0.00     0      0.00      0.00       0      0   0.00      0.00      0.00      0.00      0.00      0.00  watchdog/0  

 ......  

 1488541264     18523    0.00    0.20    0.00    0.20     5     10.78      0.00 137774940 3002232   0.57      0.00      0.00      0.00      1.96      0.20  /home/digoal/pgsql9.6/bin/postgres   
 1488541264     18549    0.00    0.00    0.00    0.00    61      0.00      0.00  140892   2028   0.00      0.00      0.00      0.00      0.00      0.00  postgres: logger process               
 1488541264     18552    0.00    0.00    0.00    0.00     0      0.00      0.00 137779116 5509520   1.04      0.00      0.00      0.00      0.00      0.00  postgres: checkpointer process         
 1488541264     18554    0.00    0.00    0.00    0.00    20      0.00      0.00 137776328 1058572   0.20      0.00      0.00      0.00      3.92      0.00  postgres: writer process               
 1488541264     18556    0.00    0.00    0.00    0.00    15      0.00      0.00 137774940  19120   0.00      0.00      0.00      0.00      3.92      0.00  postgres: wal writer process           
 1488541264     18557    0.00    0.00    0.00    0.00    21      2.94      0.00 137779092   3404   0.00      0.00      0.00      0.00      3.92      0.00  postgres: autovacuum launcher process     
 1488541264     18559    0.00    0.00    0.00    0.00    11      0.00      0.00  142988   2092   0.00      0.00      0.00      0.00      0.00      0.00  postgres: archiver process   last was 0000000100000030000000B2  
 1488541264     18561    0.00    0.00    0.00    0.00    53      4.90      0.00  143556   2624   0.00      0.00    317.65    317.65      4.12      0.20  postgres: stats collector process      

参考

man pidstat

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

NAME  
       pidstat - Report statistics for Linux tasks.  

SYNOPSIS  
       pidstat [ -C comm ] [ -d ] [ -h ] [ -I ] [ -l ] [ -p { pid [,...] | SELF | ALL } ] [ -r ] [ -t ] [ -T { TASK | CHILD | ALL } ] [ -u ] [ -V ] [ -w ] [ 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.  

OPTIONS  
       -C comm  
              Display only tasks whose command name includes the string comm.  

       -d     Report I/O statistics (kernels 2.6.20 and later only).  The following values are displayed:  

              PID  
                     The identification number of the task being monitored.  

              kB_rd/s  
                     Number of kilobytes the task has caused to be read from disk per second.  

              kB_wr/s  
                     Number of kilobytes the task has caused, or shall cause to be written to disk per second.  

              kB_ccwr/s  
                     Number  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.  

              Command  
                     The command name of the task.  

       -h     Display all activities horizontally on a single line. This is intended to make it easier to be parsed by other programs.  

       -I     In an SMP environment, indicate that tasks CPU usage (as displayed by option -u ) should be divided by the total number of processors.  

       -l     Display the process command name and all its arguments.  

       -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.  

       -r     Report page faults and memory utilization.  

              When reporting statistics for individual tasks, the following values are displayed:  

              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.  

              Command  
                     The command name of the task.  

              When reporting global statistics for tasks and all their children, the following values are displayed:  

              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.  

       -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 { 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 indicates 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.  

              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.  

       -u     Report CPU utilization.  

              When reporting statistics for individual tasks, the following values are displayed:  

              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.  

              %system  
                     Percentage of CPU used by the task while executing at the system level (kernel).  

              %guest  
                     Percentage of CPU spent by the task in virtual machine (running a virtual processor).  

              %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  
                     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 are displayed:  

              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.  

       -V     Print version number then exit.  

       -w     Report task switching activity (kernels 2.6.23 and later only).  The following values are displayed:  

              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.  

ENVIRONMENT  
       The pidstat command takes into account the following environment variable:  

       S_TIME_FORMAT  
              If  this  variable  exists  and  its  value  is ISO then the current locale will be ignored when printing the date in the report header.  The pidstat command will use the ISO 8601 format (YYYY-MM-DD)  
              instead.  

EXAMPLES  
       pidstat 2 5  
              Display five reports of CPU statistics for every active task in the system at two second intervals.  

       pidstat -r -p 1643 2 5  
              Display five reports of page faults and memory statistics for PID 1643 at two second intervals.  

       pidstat -T CHILD -r 2 5  
              Display 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.  

BUGS  
       /proc filesystem must be mounted for the pidstat command to work.  

FILES  
       /proc contains various files with system statistics.  

AUTHOR  
       Sebastien Godard (sysstat <at> orange.fr)  

SEE ALSO  
       sar(1), top(1), ps(1), mpstat(1), iostat(1), vmstat(8)  

       http://pagesperso-orange.fr/sebastien.godard/  

Linux                            DECEMBER 2008                      PIDSTAT(1)  

http://linoxide.com/linux-command/linux-pidstat-monitor-statistics-procesess/

http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858874.html

http://www.2cto.com/os/201306/217190.html

其他

mpstat  

vmstat  

top  

sar  

iostat  

dstat  

ps  
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
21天前
|
消息中间件 并行计算 安全
进程、线程、协程
【10月更文挑战第16天】进程、线程和协程是计算机程序执行的三种基本形式。进程是操作系统资源分配和调度的基本单位,具有独立的内存空间,稳定性高但资源消耗大。线程是进程内的执行单元,共享内存,轻量级且并发性好,但同步复杂。协程是用户态的轻量级调度单位,适用于高并发和IO密集型任务,资源消耗最小,但不支持多核并行。
38 1
|
4天前
|
Linux 调度 C语言
深入理解操作系统:进程和线程的管理
【10月更文挑战第32天】本文旨在通过浅显易懂的语言和实际代码示例,带领读者探索操作系统中进程与线程的奥秘。我们将从基础知识出发,逐步深入到它们在操作系统中的实现和管理机制,最终通过实践加深对这一核心概念的理解。无论你是编程新手还是希望复习相关知识的资深开发者,这篇文章都将为你提供有价值的见解。
|
1天前
|
Java
java小知识—进程和线程
进程 进程是程序的一次执行过程,是系统运行的基本单位,因此进程是动态的。系统运行一个程序即是一个进程从创建,运行到消亡的过程。简单来说,一个进程就是一个执行中的程序,它在计算机中一个指令接着一个指令地执行着,同时,每个进程还占有某些系统资源如CPU时间,内存空间,文件,文件,输入输出设备的使用权等等。换句话说,当程序在执行时,将会被操作系统载入内存中。 线程 线程,与进程相似,但线程是一个比进程更小的执行单位。一个进程在其执行的过程中产生多个线程。与进程不同的是同类的多个线程共享同一块内存空间和一组系统资源,所以系统在产生一个线程,或是在各个线程之间做切换工作时,负担要比
7 1
|
7天前
深入理解操作系统:进程与线程的管理
【10月更文挑战第30天】操作系统是计算机系统的核心,它负责管理计算机硬件资源,为应用程序提供基础服务。本文将深入探讨操作系统中进程和线程的概念、区别以及它们在资源管理中的作用。通过本文的学习,读者将能够更好地理解操作系统的工作原理,并掌握进程和线程的管理技巧。
16 2
|
8天前
|
调度 Python
深入浅出操作系统:进程与线程的奥秘
【10月更文挑战第28天】在数字世界的幕后,操作系统悄无声息地扮演着关键角色。本文将拨开迷雾,深入探讨操作系统中的两个基本概念——进程和线程。我们将通过生动的比喻和直观的解释,揭示它们之间的差异与联系,并展示如何在实际应用中灵活运用这些知识。准备好了吗?让我们开始这段揭秘之旅!
|
1月前
|
存储 消息中间件 人工智能
进程,线程,协程 - 你了解多少?
本故事采用简洁明了的对话方式,尽洪荒之力让你在轻松无负担的氛围中,稍微深入地理解进程、线程和协程的相关原理知识
40 2
进程,线程,协程 - 你了解多少?
|
19天前
|
Python
Python中的多线程与多进程
本文将探讨Python中多线程和多进程的基本概念、使用场景以及实现方式。通过对比分析,我们将了解何时使用多线程或多进程更为合适,并提供一些实用的代码示例来帮助读者更好地理解这两种并发编程技术。
|
21天前
|
消息中间件 并行计算 安全
进程、线程、协程
【10月更文挑战第15天】进程、线程和协程是操作系统中三种不同的执行单元。进程是资源分配和调度的基本单位,每个进程有独立的内存空间;线程是进程内的执行路径,共享进程资源,切换成本较低;协程则更轻量,由用户态调度,适合处理高并发和IO密集型任务。进程提供高隔离性和安全性,线程支持高并发,协程则在资源消耗和调度灵活性方面表现优异。
44 2
|
2月前
|
存储 消息中间件 资源调度
「offer来了」进程线程有啥关系?10个知识点带你巩固操作系统基础知识
该文章总结了操作系统基础知识中的十个关键知识点,涵盖了进程与线程的概念及区别、进程间通信方式、线程同步机制、死锁现象及其预防方法、进程状态等内容,并通过具体实例帮助理解这些概念。
「offer来了」进程线程有啥关系?10个知识点带你巩固操作系统基础知识
|
27天前
|
算法 安全 调度
深入理解操作系统:进程与线程的管理
【10月更文挑战第9天】在数字世界的心脏跳动着的,不是别的,正是操作系统。它如同一位无形的指挥家,协调着硬件与软件的和谐合作。本文将揭开操作系统中进程与线程管理的神秘面纱,通过浅显易懂的语言和生动的比喻,带你走进这一复杂而又精妙的世界。我们将从进程的诞生讲起,探索线程的微妙关系,直至深入内核,理解调度算法的智慧。让我们一起跟随代码的脚步,解锁操作系统的更多秘密。
34 1