Centos6下通过pidstat查看各进程资源(CPU、Memory、Disk)占用情况

本文介绍如何在CentOS 6环境下利用pidstat工具监控进程对CPU、内存及磁盘的使用情况。详细解释了pidstat常用参数-u、-r、-d的用法及其输出结果的各项指标含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境说明

Centos6.8 x86_64

使用说明

很多时候我们需要查看各进行对于硬件资源的占用情况,譬如说CPU、Memory、Disk,在Centos6下可以通过pidstat可以查看进行对各资源的占用情况。
[root@ovirt-host-2 ~]# pidstat --help
Usage: pidstat [ options ] [ <interval> [ <count> ] ]
Options are:
[ -C <command> ] [ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -t ] [ -u ] [ -V ] [ -w ]
[ -p { <pid> [,...] | SELF | ALL } ] [ -T { TASK | CHILD | ALL } ]

      pidstat后面可以跟很多参数,但是最常用的分别是:
 -u:监控cpu
 -r:监控内存
 -d:监控硬盘

    我们先来通过pidstat查看各进行占用cpu资源情况,后面的参数“1”,为显示列表的时间间隔,为1秒。
[root@ovirt-host-2 ~]# pidstat -u 1
Linux 2.6.32-642.4.2.el6.x86_64 (ovirt-host-2)  10/13/2016      _x86_64_        (8 CPU)

11:04:20 AM       PID    %usr %system  %guest    %CPU   CPU  Command
11:04:21 AM      3428    5.77    0.96    0.00    6.73     3  vdsm
11:04:21 AM      4637   13.46   10.58    0.00   24.04     1  sshd
11:04:21 AM      4639    1.92    3.85    0.00    5.77     3  virt-p2v-server
11:04:21 AM      4864    1.92    1.92    0.00    3.85     2  pidstat
11:04:21 AM     12080    0.96    2.88    0.00    3.85     2  pidstat

11:04:21 AM       PID    %usr %system  %guest    %CPU   CPU  Command
11:04:22 AM      4637   15.00    9.00    0.00   24.00     1  sshd
11:04:22 AM      4639    3.00    3.00    0.00    6.00     3  virt-p2v-server
11:04:22 AM      4864    0.00    2.00    0.00    2.00     2  pidstat
11:04:22 AM     12080    2.00    2.00    0.00    4.00     6  pidstat

11:04:22 AM       PID    %usr %system  %guest    %CPU   CPU  Command
11:04:23 AM       125    0.00    1.00    0.00    1.00     2  kswapd0
11:04:23 AM      2863    1.00    1.00    0.00    2.00     2  libvirtd
11:04:23 AM      3428    1.00    0.00    0.00    1.00     3  vdsm
11:04:23 AM      4637   14.00   10.00    0.00   24.00     1  sshd
11:04:23 AM      4639    2.00    4.00    0.00    6.00     3  virt-p2v-server
11:04:23 AM      4864    2.00    2.00    0.00    4.00     4  pidstat
11:04:23 AM     12080    1.00    2.00    0.00    3.00     2  pidstat

可以通过man pidstat中-u的内容,来了解上述列表中各参数的含义
   -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鈙 CPU usage will be divided by the total number of CPU鈙 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.

了解查看cpu后,我们再来看看查看内存

Linux 2.6.32-642.4.2.el6.x86_64 (ovirt-host-2)  10/13/2016      _x86_64_        (8 CPU)

11:07:57 AM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
11:07:58 AM      3428     12.62      0.00 2030068  42408   0.26  vdsm
11:07:58 AM      4864    619.42      0.00  101380   1216   0.01  pidstat
11:07:58 AM     12143    621.36      0.00  101384   1192   0.01  pidstat

11:07:58 AM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
11:07:59 AM      1860      9.00      0.00   18372    800   0.00  irqbalance
11:07:59 AM      2863      2.00      0.00  933520  10356   0.06  libvirtd
11:07:59 AM      3428     76.00      0.00 2030068  42408   0.26  vdsm
11:07:59 AM      4864    638.00      0.00  101380   1216   0.01  pidstat
11:07:59 AM     12143    646.00      0.00  101384   1224   0.01  pidstat

一样,通过man pidstat来查看各参数的含义

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

下面我们来查看监控disk

[root@ovirt-host-2 ~]# pidstat -d 1
Linux 2.6.32-642.4.2.el6.x86_64 (ovirt-host-2)  10/13/2016      _x86_64_        (8 CPU)

11:12:29 AM       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
11:12:31 AM      4639      0.00  11592.45      0.00  virt-p2v-server

11:12:31 AM       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
11:12:32 AM      4639      0.00   8192.00      0.00  virt-p2v-server

11:12:32 AM       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
11:12:33 AM      4639      0.00  12288.00      0.00  virt-p2v-server

11:12:33 AM       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
11:12:34 AM      4639      0.00  12288.00      0.00  virt-p2v-server

11:12:34 AM       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
11:12:35 AM      4639      0.00  12288.00      0.00  virt-p2v-server

查看各参数的含义

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



<think>好的,我需要帮助用户了解如何使用pidstat命令来监控CPU占用进程或线程。首先,我应该回顾一下用户提供的引用内容,看看里面有哪些关键点。引用中提到了pidstat是sysstat工具的一部分,可以监控进程CPU、内存、I/O等,并且可以指定进程或线程。用户需要具体的教程,所以步骤要详细。 首先,我得确认用户是否已经安装了sysstat包,因为pidstat是其中的一部分。如果没有安装,可能需要先指导安装方法,比如在Ubuntu中使用apt-get或者在CentOS中使用yum。不过用户的问题主要是如何使用,可能已经安装好了,但为了全面,还是应该提到安装步骤。 接下来,监控进程CPU使用情况。根据引用[1]和[5],基本命令是pidstat -p PID,但用户可能需要更动态的数据,比如实时监控。引用中提到pidstat 1 3这样的用法,间隔1秒,共3次。所以应该建议使用pidstat -u 1来持续监控所有进程CPU使用情况,或者结合-p指定进程ID。 然后,用户可能想监控特定进程的线程。引用[3]提到了使用-wt选项来查看线程的上下文切换,但用户关心的是CPU占用。这时候应该使用-t选项来显示线程信息。例如pidstat -t -p PID或者直接pidstat -t 1来实时查看所有线程的CPU使用情况。 另外,用户可能需要了解各个参数的含义,比如%usr表示用户态CPU使用率,%system是内核态,%CPU是总体使用率。需要解释这些指标,帮助用户理解输出结果。引用[3]和[5]都提到了这些指标,所以应该包括进去。 用户还可能想知道如何根据CPU使用率排序进程,但pidstat本身可能不支持排序,这时候可以结合其他工具如top,如引用[4]提到的,但用户的问题是关于pidstat,所以可能需要说明如何通过其他方式处理输出,比如使用grep过滤或者结合sort命令。 最后,常见问题解答部分应该包括权限问题(可能需要sudo)、如何持续监控、如何导出数据等。引用[5]提到pidstat首次运行显示系统启动以来的统计,之后显示上次运行后的数据,所以需要提醒用户根据需要调整间隔和次数。 总结步骤:安装sysstat,监控所有进程,监控特定进程,监控线程,解释输出指标,常见问题。需要确保每个步骤都有示例命令和说明,并引用相关引用来源。同时,语言要简洁明了,符合中文习惯,避免使用专业术语过多,但必要的术语需要保留并解释。</think>### 如何使用 pidstat 监控 CPU 占用进程或线程 #### 1. 安装 sysstat 工具 pidstat 属于 sysstat 工具包,需先安装: - **Ubuntu/Debian**:`sudo apt-get install sysstat` - **CentOS/RHEL**:`sudo yum install sysstat` 安装后,通过 `pidstat -h` 验证是否成功[^5]。 --- #### 2. 监控所有进程CPU 使用率 **命令格式**: ```bash pidstat -u [间隔时间] [次数] ``` - **示例**:每秒刷新一次,共显示 3 次 ```bash pidstat -u 1 3 ``` **输出说明**: - `%usr`:用户态 CPU 使用率 - `%system`:内核态 CPU 使用率 - `%CPU`:总 CPU 使用率[^3][^5] --- #### 3. 监控特定进程CPU 使用率 **命令格式**: ```bash pidstat -u -p [进程ID] [间隔时间] [次数] ``` - **示例**:监控进程 ID 为 1234 的 CPU 使用,每秒刷新 ```bash pidstat -u -p 1234 1 ``` --- #### 4. 监控线程级 CPU 使用率 添加 `-t` 参数可显示线程信息: ```bash pidstat -u -t -p [进程ID] [间隔时间] ``` - **示例**:显示进程 1234 的线程 CPU 使用,每秒刷新 ```bash pidstat -u -t -p 1234 1 ``` **输出说明**: - `TID`:线程 ID - `%usr`:线程用户态 CPU 使用率[^3] --- #### 5. 结合其他参数优化监控 - **显示命令名称**:加 `-C [进程名]`,如 `pidstat -C nginx` - **统计上下文切换**:加 `-w`,查看线程切换频率 - **导出数据**:使用 `> output.txt` 保存结果 --- #### 常见问题 1. **权限不足**:部分操作需 `sudo` 权限(如监控其他用户进程) 2. **持续监控**:省略次数参数(如 `pidstat -u 1`)可持续运行 3. **查看历史数据**:首次运行会显示系统启动以来的统计,后续运行仅显示增量数据[^5] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值