nfsiostat用于显示NFS(网络文件系统)客户端每个挂载点的I/O统计信息

功能说明:nfsiostat是一个用于显示NFS客户端每个挂载点I/O统计信息的工具,类似于iostat。

语  法:nfsiostat [ interval [ count ] ] [ options ] [ <mount point> ]

补充说明:Ubuntu系统可以通过以下命令安装nfsiostat:

sudo apt install nfs-common

   项:

-a, –attr        显示与属性缓存(attribute cache)相关的统计信息

-d, –dir         显示与目录操作(directory operations)相关的统计信息

-p, –page     显示与页面缓存(page cache)相关的统计信息

-s, –sort        按每秒操作数(ops/second)对 NFS 挂载点进行排序,用于识别哪些挂载点 I/O 活动最多

-l LIST, –list=LIST   只打印前 LIST 个挂载点的统计信息。这个选项适用于只查看几个主要挂载点的统计信息。

参  数:

interval                设置每次报告的时间间隔(以秒为单位)。命令会持续输出,直到手动终止或者报告了count次

count                   总共报告count次统计信息,然后终止命令

mount point         网络文件系统(NFS)的挂载点。你可以指定一个或多个 NFS 挂载点来查看这些挂载点的特定统计信息,而不是默认地统计所有的NFS挂载点。

   例:

每 5 秒输出一次统计信息,持续 10 次:

nfsiostat 5 10

哪些挂载点 I/O 活动最多:

nfsiostat -s

打印前 2 个挂载点的统计信息:

nfsiostat -l 2

查看 /mnt/nfs 挂载点的统计信息:

nfsiostat /mnt/nfs

Monitoring System Resources with pidstat: CPU, Memory, Threads, and Device I/O Usage

pidstat is a versatile command-line tool designed to monitor system resource usage, including CPU, memory, threads, and device I/O, across all or selected processes.

Overview:

pidstat is used to monitor the usage of CPU, memory, threads, device I/O, and other system resources for all or specific processes.

Syntax:

pidstat [options] [<interval> [<count>]]

Description:

When pidstat runs for the first time, it displays statistics from system startup. Subsequent executions will show data since the last run. Users can specify the interval and the number of times statistics should be displayed. It is part of the sysstat package, a performance monitoring toolkit, and can be accessed after installing sysstat.

Options:

  • -u – Display CPU usage of each process.
  • -r – Display memory usage of each process.
  • -d – Display I/O usage of each process.
  • -p <pid> – Specify process ID to monitor.
  • -w – Display context switch details for each process.
  • -t – Show additional thread statistics.
  • -V – Display the version of the tool.
  • -h – Display header in a more compact format to fit narrower terminal windows.
  • -I – On SMP systems, displays CPU usage per core.
  • -l – Show command name and all parameters.
  • -T {TASK | CHILD | ALL} – Scope of reported statistics:
    • TASK: Report stats for the specified task (process).
    • CHILD: Report stats only for child processes, useful for performance monitoring of a process’s descendants.
    • ALL: Comprehensive stats for both the task and all its child processes.
  • -C <command> – Monitor the status of processes associated with a specific command.

Parameters:

  • interval: Time between displays (in seconds).
  • count: Number of times to display, default is continuous.

Example Usage:

Basic Usage:

$ pidstat

Example output:

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)

Time       UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
16:03:44   0         1      0.00    0.00    0.00    0.00    0.00     0  systemd
16:03:44   0         2      0.00    0.00    0.00    0.00    0.00     0  kthreadd
16:03:44   0        16      0.00    0.00    0.00    0.00    0.00     0  ksoftirqd/0

Display CPU Usage for All Processes:

$ pidstat -u -p ALL

Example output:

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)

Time       UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
16:17:49   0         1      0.00    0.00    0.00    0.00    0.00     0  systemd
16:17:49   0         2      0.00    0.00    0.00    0.00    0.00     0  kthreadd

Display CPU Usage for a Specific Process:

$ pidstat -u -p 1

Example output:

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)

Time       UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
16:18:07   0         1      0.00    0.00    0.00    0.00    0.00     1  systemd

Display I/O Usage:

$ pidstat -d

Example output:

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)

Time       UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command
16:26:23   1000      1606    0.23      0.02      0.02       0  systemd

Display Context Switches:

$ pidstat -w

Example output:

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)

Time       UID       PID   cswch/s nvcswch/s  Command
16:29:11   0         1     0.19      0.04  systemd

Display Thread Statistics:

$ pidstat -t

Example output:

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)

Time       UID      TGID       TID    %usr %system  %guest   %wait    %CPU   CPU  Command
16:31:15   0         1         -    0.00    0.00    0.00    0.00    0.00     0  systemd

Field Explanations:

  • PID: Process ID
  • %usr: CPU percentage used in user space
  • %system: CPU percentage used in kernel space
  • %guest: CPU percentage used in virtual machine
  • %CPU: Total CPU percentage used by the process
  • CPU: CPU core number
  • Command: Command associated with the process

pidstat provides an in-depth look at resource usage, helping users understand how individual processes consume system resources, making it invaluable for performance monitoring and troubleshooting.

pidstat用于监控进程的CPU、内存、线程、设备 IO 等系统资源的使用情况

功能说明:用于监控全部或指定进程的CPU、内存、线程、设备 IO 等系统资源的使用情况。

语  法:pidstat [ options ] [ <interval> [ <count> ] ]

补充说明:pidstat 首次运行时显示自系统启动开始的各项统计信息,之后运行 pidstat 将显示自上次运行该命令以后的统计信息。用户可以通过指定统计的次数和时间来获得所需的统计信息。pidstat是sysstat性能监控工具包的工具之一,安装sysstat即可得到pidstat。

   项: 

-u           显示各个进程的cpu使用情况

-r           显示各个进程的内存使用情况

-d           显示各个进程的IO使用情况

-p           指定进程号

-w          显示每个进程的上下文切换情况

-t            显示选择任务的线程的统计信息外的额外信息

-V          版本号

-h           以更简洁的方式显示输出数据的表头,以便适应更窄的显示屏幕或终端窗口

-I           在SMP环境,表示任务的CPU使用率/内核数量

-l            显示命令名和所有参数

-T { TASK | CHILD | ALL }        指定报告统计信息的进程的范围:

  • TASK:仅报告特定任务(任务即进程)。
  • CHILD:仅报告子进程的统计信息。这通常用于查看某个进程派生的所有子进程的相关性能数据。
  • ALL:报告所有相关信息,即当前任务及其所有子进程的综合统计信息。这可以用于全面了解进程及其子进程的资源使用情况。

-C <command>         查看对应command进程的状态

   数:

interval         显示间隔,单位s

count            显示次数,默认一直显示

   例:

$ pidstat

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月19日   _x86_64_  (2 CPU)

16时03分44秒   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command

16时03分44秒     0         1    0.00    0.00    0.00    0.00    0.00     0  systemd

16时03分44秒     0         2    0.00    0.00    0.00    0.00    0.00     0  kthreadd

16时03分44秒     0        16    0.00    0.00    0.00    0.00    0.00     0  ksoftirqd/0

16时03分44秒     0        17    0.00    0.00    0.00    0.01    0.00     1  rcu_preempt

对输出信息中的字段的解释:

PID        进程ID

%usr      进程在用户空间占用cpu的百分比

%system       进程在内核空间占用cpu的百分比

%gues          进程在虚拟机占用cpu的百分比

%CPU          进程占用cpu的百分比

CPU             运行当前进程的cpu的编号

Command    当前进程对应的命令

显示所有进程使用cpu的情况:

$ pidstat -u -p ALL

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月19日   _x86_64_  (2 CPU)

16时17分49秒   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command

16时17分49秒     0         1    0.00    0.00    0.00    0.00    0.00     0  systemd

16时17分49秒     0         2    0.00    0.00    0.00    0.00    0.00     0  kthreadd

16时17分49秒     0         3    0.00    0.00    0.00    0.00    0.00     0  pool_workqueue_release

显示pid为1的进程的CPU的使用情况:

$ pidstat -u -p 1

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月19日   _x86_64_  (2 CPU)

16时18分07秒   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command

16时18分07秒     0         1    0.00    0.00    0.00    0.00    0.00     1  systemd

显示各个进程的IO使用情况:

$ pidstat -d

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月19日   _x86_64_  (2 CPU)

16时26分23秒   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command

16时26分23秒  1000      1606      0.23      0.02      0.02       0  systemd

16时26分23秒  1000      1614      0.00      0.00      0.00       0  pipewire-media-

16时26分23秒  1000      1615      0.00      0.00      0.00       0  pulseaudio

对输出信息中的的字段的解释:

PID               进程id

kB_rd/s        每秒从磁盘读取的KB

kB_wr/s        每秒写入磁盘KB

kB_ccwr/s           任务取消的写入磁盘的KB

COMMAND       当前进程对应的命令

显示每个进程的上下文切换情况:

$ pidstat -w

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024年10月19日         _x86_64_        (2 CPU)

16时29分11秒   UID       PID   cswch/s nvcswch/s  Command

16时29分11秒     0         1      0.19      0.04  systemd

16时29分11秒     0         2      0.00      0.00  kthreadd

16时29分11秒     0         3      0.00      0.00  pool_workqueue_release

对输出信息中的的字段的解释:

PID               进程id

Cswch/s        每秒主动任务上下文切换数量

Nvcswch/s    每秒被动任务上下文切换数量

Command           当前进程对应的命令

显示线程的统计信息外的其它信息:

$  pidstat -t

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox) 2024年10月19日          _x86_64_     (2 CPU)

16时31分15秒   UID      TGID       TID    %usr %system  %guest   %wait    %CPU   CPU  Command

16时31分15秒     0         1         –    0.00    0.00    0.00    0.00    0.00     0  systemd

16时31分15秒     0         –         1    0.00    0.00    0.00    0.00    0.00     0  |__systemd

16时31分15秒  1000      2722         –    0.04    0.05    0.00    0.00    0.09     0  VBoxClient

16时31分15秒  1000         –      3008    0.00    0.00    0.00    0.00    0.00     1  |__dndHGCM

16时31分15秒  1000         –      3009    0.04    0.04    0.00    0.11    0.09     0  |__dndX11

对输出信息中的的字段的解释:

TGID            主线程的表示

TID              线程id

%usr             进程在用户空间占用cpu的百分比

%system       进程在内核空间占用cpu的百分比

%guest         进程在虚拟机占用cpu的百分比

%CPU          进程占用cpu的百分比

CPU             执行当前进程的cpu的编号

Command    当前进程对应的命令

iostat: A Tool for Monitoring System I/O Statistics

Functionality:
iostat is a utility used to gather and report system I/O statistics, commonly employed for analyzing disk performance.

Syntax:
iostat [options]

Overview:
In addition to I/O statistics, iostat can also display CPU usage information.

Common Options:

  • -c: Display CPU usage only.
  • -d: Show device utilization statistics only.
  • -k: Display statistics in kilobytes per second instead of blocks per second.
  • -m: Display statistics in megabytes per second.
  • -p: Display statistics for block devices and all utilized partitions.
  • -t: Display the time each report is generated.
  • -V: Display version information and exit.
  • -x: Display extended I/O statistics.

Examples:

To display the usage of all devices at the current time:

$ iostat -x
Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)  2024年10月18日  _x86_64_  (2 CPU)

avg-cpu:  %user  %nice  %system  %iowait  %steal  %idle
           0.10   0.02    0.27     0.01    0.00   99.60

Device      r/s  rkB/s  rrqm/s  %rrqm  r_await  rareq-sz  w/s  wkB/s  wrqm/s  %wrqm  w_await  wareq-sz  %util
loop0      0.00   0.00    0.00   0.00    0.00     1.21   0.00    0.00   0.00   0.00    0.00     0.00    0.00
...
sda        0.30  10.96    0.10  24.42    0.35    36.60   0.51   23.23   0.64  55.59    0.41    45.13    0.02

Field Descriptions:

  • Device: Device name.
  • r/s: Number of read requests per second.
  • w/s: Number of write requests per second.
  • rkB/s: Kilobytes read per second.
  • wkB/s: Kilobytes written per second.
  • %util: Percentage of time the device was busy with I/O requests.

To display statistics for the device sda:

$ iostat -x /dev/sda
Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)  2024年10月18日  _x86_64_  (2 CPU)

avg-cpu:  %user  %nice  %system  %iowait  %steal  %idle
           0.10   0.02    0.27     0.01    0.00   99.60

Device      r/s  rkB/s  rrqm/s  %rrqm  r_await  rareq-sz  w/s  wkB/s  wrqm/s  %wrqm  w_await  wareq-sz  %util
sda        0.30  10.95    0.10  24.42    0.35    36.60   0.51   23.21   0.64  55.57    0.41    45.09    0.02

To display only device usage without CPU statistics:

$ iostat -xd /dev/sda

For overall system I/O status:

$ iostat
Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)  2024年10月18日  _x86_64_  (2 CPU)

avg-cpu:  %user  %nice  %system  %iowait  %steal  %idle
           0.10   0.02    0.27     0.01    0.00   99.60

Device     tps  kB_read/s  kB_wrtn/s  kB_dscd/s  kB_read  kB_wrtn  kB_dscd
loop0      0.00      0.00      0.00      0.00        17        0        0
...
sda        0.81     10.95     23.21      0.00   4791860  10157237       0

To display CPU I/O statistics only:

$ iostat -c

To display statistics in megabytes per second:

$ iostat -m

iostat用于统计系统I/O状态信息

功能说明:用于统计系统I/O状态信息,常用于分析磁盘性能。

语  法:iostat [options]

补充说明:也能显示CPU使用情况。

   项:

-c    仅显示CPU使用情况

-d    仅显示设备利用率

-k    显示状态以千字节每秒为单位,而不使用块每秒

-m   显示状态以兆字节每秒为单位

-p    仅显示块设备和所有被使用的其他分区的状态

-t     显示每个报告产生时的时间

-V   显示版号并退出

-x    显示扩展状态

参  数:

   例:

显示当前时刻所有设备使用情况:

$ iostat -x

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月18日   _x86_64_  (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.10    0.02    0.27    0.01    0.00   99.60

Device            r/s     rkB/s   rrqm/s  %rrqm r_await rareq-sz     w/s     wkB/s   wrqm/s  %wrqm w_await wareq-sz     d/s     dkB/s   drqm/s  %drqm d_await dareq-sz     f/s f_await  aqu-sz  %util

loop0            0.00      0.00     0.00   0.00    0.00     1.21    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop1            0.00      0.00     0.00   0.00    0.07     2.88    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop10           0.02      0.05     0.00   0.00    0.02     3.20    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop11           0.00      0.00     0.00   0.00    0.29     4.80    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop12           0.00      0.00     0.00   0.00    0.24     7.20    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop13           0.00      0.01     0.00   0.00    0.27    13.64    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop14           0.00      0.00     0.00   0.00    0.46    12.07    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop15           0.00      0.16     0.00   0.00    0.04    50.89    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop16           0.00      0.00     0.00   0.00    0.32    18.27    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop17           0.00      0.00     0.00   0.00    0.06     8.36    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop2            0.00      0.00     0.00   0.00    0.05     2.88    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop3            0.00      0.00     0.00   0.00    0.06     4.50    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop4            0.00      0.03     0.00   0.00    0.20    13.73    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop5            0.00      0.00     0.00   0.00    0.23    14.72    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop6            0.00      0.23     0.00   0.00    0.55    52.20    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop7            0.00      0.01     0.00   0.00    0.03     3.79    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop8            0.00      0.01     0.00   0.00    0.05     3.89    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

loop9            0.02      0.36     0.00   0.00    0.16    19.87    0.00      0.00     0.00   0.00    0.00     0.00    0.00      0.00     0.00   0.00    0.00     0.00    0.00    0.00    0.00   0.00

sda              0.30     10.96     0.10  24.42    0.35    36.60    0.51     23.23     0.64  55.59    0.41    45.13    0.00      0.00     0.00   0.00    0.00     0.00    0.13    0.72    0.00   0.02

输出结果中的字段解释:

Device   设备名称

rrqm/s    每秒需要读取需求的数量

wrqm/s  每秒需要写入需求的数量

r/s          每秒实际读取需求的数量

w/s         每秒实际写入需求的数量

rsec/s     每秒读取区段的数量

wsec/s   每秒写入区段的数量

rkB/s     每秒实际读取的大小,单位为KB

wkB/s    每秒实际写入的大小,单位为KB

avgrq-sz       需求的平均大小区段

avgqu-sz       需求的平均队列长度

await     等待I/O平均的时间(milliseconds)

svctm    I/O需求完成的平均时间

%util     被I/O需求消耗的CPU百分比

显示当前时刻设备sda使用情况:

$ iostat -x /dev/sda

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月18日   _x86_64_  (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.10    0.02    0.27    0.01    0.00   99.60

Device            r/s     rkB/s   rrqm/s  %rrqm r_await rareq-sz     w/s     wkB/s   wrqm/s  %wrqm w_await wareq-sz     d/s     dkB/s   drqm/s  %drqm d_await dareq-sz     f/s f_await  aqu-sz  %util

sda              0.30     10.95     0.10  24.42    0.35    36.60    0.51     23.21     0.64  55.57    0.41    45.09    0.00      0.00     0.00   0.00    0.00     0.00    0.13    0.72    0.00   0.02

可以使用-d选项不显示cpu使用情况,只显示设备的使用情况:

$ iostat -xd /dev/sda

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月18日   _x86_64_  (2 CPU)

Device            r/s     rkB/s   rrqm/s  %rrqm r_await rareq-sz     w/s     wkB/s   wrqm/s  %wrqm w_await wareq-sz     d/s     dkB/s   drqm/s  %drqm d_await dareq-sz     f/s f_await  aqu-sz  %util

sda              0.30     10.95     0.10  24.42    0.35    36.60    0.51     23.21     0.64  55.57    0.41    45.09    0.00      0.00     0.00   0.00    0.00     0.00    0.13    0.72    0.00   0.02

显示系统整体I/O状态信息:

$ iostat

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月18日   _x86_64_  (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.10    0.02    0.27    0.01    0.00   99.60

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd

loop0             0.00         0.00         0.00         0.00         17          0          0

loop1             0.00         0.00         0.00         0.00       1000          0          0

loop10            0.02         0.05         0.00         0.00      22978          0          0

loop11            0.00         0.00         0.00         0.00        456          0          0

loop12            0.00         0.00         0.00         0.00        367          0          0

loop13            0.00         0.01         0.00         0.00       4052          0          0

loop14            0.00         0.00         0.00         0.00        495          0          0

loop15            0.00         0.16         0.00         0.00      68346          0          0

loop16            0.00         0.00         0.00         0.00       1681          0          0

loop17            0.00         0.00         0.00         0.00        301          0          0

loop2             0.00         0.00         0.00         0.00        991          0          0

loop3             0.00         0.00         0.00         0.00       1786          0          0

loop4             0.00         0.03         0.00         0.00      15142          0          0

loop5             0.00         0.00         0.00         0.00       1148          0          0

loop6             0.00         0.23         0.00         0.00     102514          0          0

loop7             0.00         0.01         0.00         0.00       2567          0          0

loop8             0.00         0.01         0.00         0.00       2434          0          0

loop9             0.02         0.36         0.00         0.00     155861          0          0

sda               0.81        10.95        23.21         0.00    4791860   10157237          0

只显示CPU的I/O状态:

$ iostat -c

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月18日 _x86_64_     (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.10    0.02    0.27    0.01    0.00   99.60

以兆字节每秒为单位显示:

$ iostat -m

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月18日   _x86_64_  (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

           0.10    0.02    0.27    0.01    0.00   99.60

Device             tps    MB_read/s    MB_wrtn/s    MB_dscd/s    MB_read    MB_wrtn    MB_dscd

loop0             0.00         0.00         0.00         0.00          0          0          0

loop1             0.00         0.00         0.00         0.00          0          0          0

loop10            0.02         0.00         0.00         0.00         22          0          0

loop11            0.00         0.00         0.00         0.00          0          0          0

loop12            0.00         0.00         0.00         0.00          0          0          0

loop13            0.00         0.00         0.00         0.00          3          0          0

loop14            0.00         0.00         0.00         0.00          0          0          0

loop15            0.00         0.00         0.00         0.00         66          0          0

loop16            0.00         0.00         0.00         0.00          1          0          0

loop17            0.00         0.00         0.00         0.00          0          0          0

loop2             0.00         0.00         0.00         0.00          0          0          0

loop3             0.00         0.00         0.00         0.00          1          0          0

loop4             0.00         0.00         0.00         0.00         14          0          0

loop5             0.00         0.00         0.00         0.00          1          0          0

loop6             0.00         0.00         0.00         0.00        100          0          0

loop7             0.00         0.00         0.00         0.00          2          0          0

loop8             0.00         0.00         0.00         0.00          2          0          0

loop9             0.02         0.00         0.00         0.00        152          0          0

sda               0.81         0.01         0.02         0.00       4679       9920          0

Linux工具软件sar (System Activity Report)收集和报告系统活动信息

sar(System Activity Report)是 sysstat 工具包中的核心工具之一,用于收集和报告系统活动信息,包括CPU利用率、内存使用、I/O活动、网络流量等。可以从历史数据中生成报告,帮助分析系统性能随时间的变化。

常用选项

(1) CPU 相关选项

-u:显示 CPU 利用率。

示例:

sar -u 1 3

每 1 秒采样一次,连续采样 3 次,报告 CPU 的用户态、系统态、空闲时间等利用率。

-P:指定处理器,显示单个或所有 CPU 核心的利用率。

示例:

sar -P ALL 1 5

每 1 秒采样一次,连续采样 5 次,显示每个 CPU 核心的利用率。

(2) 内存相关选项

-r:显示内存和交换分区的使用情况。

示例:

sar -r 1 3

每 1 秒采样一次,连续采样 3 次,报告内存的使用情况(包括空闲内存、已用内存、交换空间的使用情况等)。

(3) 网络相关选项

-n:显示网络活动信息。

-n DEV:显示每个网络接口的传输情况(发送/接收的数据包和字节数)。

示例:

sar -n DEV 1 3

每 1 秒采样一次,连续采样 3 次,报告各个网络接口的流量信息。

-n TCP:显示 TCP 连接的活动状态。

示例:

sar -n TCP 1 3

每 1 秒采样一次,连续采样 3 次,报告 TCP 连接的状态,例如主动连接、被动连接、错误等。

(4) I/O 相关选项

-b:显示块设备的 I/O 传输统计信息。

示例:

sar -b 1 3

每 1 秒采样一次,连续采样 3 次,报告块设备的传输率、读写次数等信息。

-d:显示具体块设备的 I/O 活动。

示例:

sar -d 1 3

每 1 秒采样一次,连续采样 3 次,报告每个块设备的 I/O 活动情况(包括读写次数、等待时间等)。

(5) 上下文切换和中断

-w:显示系统的上下文切换和进程创建的统计信息。

示例:

sar -w 1 3

每 1 秒采样一次,连续采样 3 次,报告每秒的上下文切换和进程创建情况。

-I:显示中断统计信息。

示例:

sar -I SUM 1 3

每 1 秒采样一次,连续采样 3 次,显示所有中断的总和。

(6) 文件系统相关选项

-v:显示文件句柄的使用情况。

示例:

sar -v 1 3

每 1 秒采样一次,连续采样 3 次,报告文件句柄、inode 和其他资源的使用情况。

(7) 历史数据查看

-f:从保存的日志文件中读取历史数据。

示例:

sar -u -f /var/log/sysstat/sa01

从日志文件 /var/log/sysstat/sa01 中读取 CPU 使用率数据,显示历史的 CPU 利用率。

常用场景

(1) 监控 CPU 使用情况

当系统变得响应缓慢或 CPU 负载过高时,你可以使用 sar -u 或 sar -P ALL 来监控 CPU 的使用情况,以确定 CPU 是否成为瓶颈,是否有特定的 CPU 核心超载。

sar -u 1 5

sar -P ALL 1 5

(2) 分析内存使用

如果系统内存耗尽或交换空间使用过高,导致性能下降,可以使用 sar -r 来检查物理内存和交换空间的使用情况,帮助分析是否需要增加内存或调整交换空间的大小。

sar -r 1 5

(3) 网络性能监控

在分析网络问题时,可以使用 sar -n DEV 来查看各个网络接口的数据包收发情况,帮助判断网络负载,排查网络瓶颈和接口故障。

sar -n DEV 1 5

(4) 磁盘 I/O 性能分析

如果磁盘 I/O 性能下降,可能影响系统整体响应时间。可以使用 sar -b 和 sar -d 查看系统的磁盘 I/O 活动,判断是否是磁盘瓶颈或特定设备读写过多。

sar -b 1 5

sar -d 1 5

(5) 上下文切换和中断监控

如果系统中断频繁或上下文切换过多,可能会影响系统性能。可以使用 sar -w 和 sar -I 监控上下文切换和中断次数,帮助确定这些问题是否是性能瓶颈的原因。

sar -w 1 5

sar -I SUM 1 5

(6) 历史数据分析

sar 可以保留系统性能的历史数据,方便管理员在问题出现后回顾过去的系统状态。通过 sar -f 可以读取过去的数据文件,分析系统在某一时段的性能情况。例如,分析1号的 CPU 使用历史记录: sar -u -f /var/log/sysstat/sa01

What’s the Difference Between TSV and CSV Files?

Both TSV and CSV files are plain text formats for representing tabular data.

TSV (Tab-separated values): In a TSV file, each row uses a tab character (\t) as the delimiter between fields. You can think of it as pressing the “Tab” key to separate data values.

CSV (Comma-separated values): In a CSV file, each row uses a comma (,) as the delimiter between fields. Each value is separated by a half-width comma.

Though both formats serve similar purposes, the choice between TSV and CSV often depends on the data and the specific application you’re using.

mpstat: Displaying CPU Statistics for Each Available CPU

Functionality:
The mpstat command is used to display statistics for each available CPU.

Syntax:
mpstat [ options ]

Overview:
mpstat (Multi-Processor Statistics) is a tool for displaying statistics on the performance of individual CPUs, functioning as a real-time monitoring utility. While similar to vmstat, mpstat focuses solely on CPU performance statistics. To install mpstat on Ubuntu or CentOS, you can use the following commands:

For Ubuntu:

sudo apt install sysstat

For CentOS:

sudo yum install sysstat

Options:

  • -P: Specify a CPU number or use ALL to display statistics for all CPUs.

Examples:

Running mpstat without any options will display overall performance statistics for all CPUs:

$ mpstat
Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024年10月15日   _x86_64_   (2 CPU)

13:48:03     CPU   %usr   %nice   %sys   %iowait   %irq   %soft   %steal   %guest   %gnice   %idle
13:48:03     all   0.11   0.02    0.10     0.01    0.00    0.17     0.00     0.00     0.00   99.60

Using the -P ALL option provides both overall CPU performance statistics as well as detailed statistics for each individual CPU:

$ mpstat -P ALL
Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024年10月15日   _x86_64_   (2 CPU)

13:59:40     CPU   %usr   %nice   %sys   %iowait   %irq   %soft   %steal   %guest   %gnice   %idle
13:59:40     all   0.11   0.02    0.10     0.01    0.00    0.17     0.00     0.00     0.00   99.60
13:59:40       0   0.11   0.02    0.10     0.01    0.00    0.31     0.00     0.00     0.00   99.45
13:59:40       1   0.10   0.02    0.10     0.01    0.00    0.03     0.00     0.00     0.00   99.75

You can also specify a particular CPU by using the -P n option, where n represents the CPU number starting from 0:

$ mpstat -P 0
Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024年10月15日   _x86_64_   (2 CPU)

14:18:14     CPU   %usr   %nice   %sys   %iowait   %irq   %soft   %steal   %guest   %gnice   %idle
14:18:14       0   0.11   0.02    0.10     0.01    0.00    0.31     0.00     0.00     0.00   99.45

$ mpstat -P 1
Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024年10月15日   _x86_64_   (2 CPU)

14:18:17     CPU   %usr   %nice   %sys   %iowait   %irq   %soft   %steal   %guest   %gnice   %idle
14:18:17       1   0.10   0.02    0.10     0.01    0.00    0.03     0.00     0.00     0.00   99.75

Field Descriptions:

  • %usr: Percentage of CPU time spent in user mode (excluding processes with a negative nice value). Calculation: (usr/total)*100
  • %nice: Percentage of CPU time spent on processes with a negative nice value. Calculation: (nice/total)*100
  • %sys: Percentage of CPU time spent in kernel mode. Calculation: (system/total)*100
  • %iowait: Percentage of time spent waiting for I/O operations. Calculation: (iowait/total)*100
  • %irq: Percentage of CPU time spent handling hardware interrupts. Calculation: (irq/total)*100
  • %soft: Percentage of CPU time spent handling software interrupts. Calculation: (softirq/total)*100
  • %idle: Percentage of CPU time spent idle, excluding time waiting for I/O operations. Calculation: (idle/total)*100

mpstat显示各个可用CPU的状态统计

功能说明:mpstat显示各个可用CPU的状态统计

语  法:mpstat [ options ]

补充说明:mpstat(Multi-Processor Statistics)工具软件用于显示各个可用CPU的状态统计,是一个实时监控工具,与vmstat类似,但只能监控CPU的整体性能状态。Ubuntu系统可以通过以下命令安装mpstat:

sudo apt install sysstat

CentOS系统可以通过以下命令安装mpstat:

sudo yum install sysstat

   项:

-P           指定CPU编号或ALL值表示统计所有CPU的整体性能信息

参  数:

   例:

不加任何选项直接运行mpstat统计的是所有CPU的整体性能信息:

$ mpstat

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月15日 _x86_64_     (2 CPU)

13时48分03秒  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle

13时48分03秒  all    0.11    0.02    0.10    0.01    0.00    0.17    0.00    0.00    0.00   99.60

使用-P ALL选项既能统计所有CPU的整体性能信息,又能单独统计每一个CPU的性能信息:

$ mpstat -P ALL

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月15日 _x86_64_     (2 CPU)

13时59分40秒  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle

13时59分40秒  all    0.11    0.02    0.10    0.01    0.00    0.17    0.00    0.00    0.00   99.60

13时59分40秒    0    0.11    0.02    0.10    0.01    0.00    0.31    0.00    0.00    0.00   99.45

13时59分40秒    1    0.10    0.02    0.10    0.01    0.00    0.03    0.00    0.00    0.00   99.75

使用-P n选项指定CPU编号n单独统计某一个CPU的性能信息,其中n从0开始:

$ mpstat -P 0

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月15日 _x86_64_     (2 CPU)

14时18分14秒  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle

14时18分14秒    0    0.11    0.02    0.10    0.01    0.00    0.31    0.00    0.00    0.00   99.45

$ mpstat -P 1

Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)       2024年10月15日 _x86_64_     (2 CPU)

14时18分17秒  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle

14时18分17秒    1    0.10    0.02    0.10    0.01    0.00    0.03    0.00    0.00    0.00   99.75

统计字段说明:

%user    在internal时间段里,用户态的CPU时间(%),不包含nice值为负的进程  (usr/total)*100

%nice    在internal时间段里,nice值为负进程的CPU时间(%)(nice/total)*100

%sys      在internal时间段里,内核时间(%)(system/total)*100

%iowait 在internal时间段里,硬盘IO等待时间(%) (iowait/total)*100
%irq      在internal时间段里,硬中断时间(%)(irq/total)*100
%soft     在internal时间段里,软中断时间(%)(softirq/total)*100
%idle     在internal时间段里,CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%) (idle/total)*100

AWK: A Powerful Text Processing Tool and Programming Language

Overview:
AWK is a robust text processing tool and programming language, primarily used for formatting, analyzing, and processing text on Unix and Linux systems. It excels at handling structured text like tables, CSV files, and logs.

Syntax:

awk -f 'scripts' -v var=value filename
awk 'BEGIN{ print "start" } pattern{ commands } END{ print "end" }' filename

Explanation:
AWK reads files or input streams (including stdin) line by line, processing text data based on user-specified patterns and actions. It is particularly useful for structured text. While AWK can be used directly from the command line, it is more often employed through scripts. As a programming language, AWK shares many features with C, such as arrays and functions.

Options:

  • -F: Specifies the field separator (can be a string or regular expression).
  • -f 'scripts': Reads AWK commands from the script file 'scripts'.
  • -v var=value: Assigns a value to a variable, passing external variables to AWK.

AWK Script Structure:

  • pattern: Matches specific lines.
  • { commands }: Executes actions on matching lines.
  • filename: The file to be processed by AWK.

An AWK script typically consists of three optional parts: a BEGIN block, pattern matching, and an END block. The workflow proceeds as follows:

  1. Execute the BEGIN statement.
  2. Process each line from the file or standard input, executing the pattern matching.
  3. Execute the END statement.

AWK Built-in Variables:

  • $0: The current record (line).
  • $n: The nth field (column) of the current record, where $1 is the first column and $n is the nth column.
  • FS: The field separator (default is a space or tab), can be customized with the -F option.
  • OFS: Output field separator (used for formatted output).
  • RS: Record separator (default is newline).
  • ORS: Output record separator (default is newline).
  • NR: Current line number (starting from 1).
  • NF: Number of fields (columns) in the current line.

AWK Operators:

  • Arithmetic Operators:
    +, -, *, /, %, ^
    Increment and decrement operators (++, --) can be used as prefixes or suffixes.
  • Assignment Operators:
    =, +=, -=, *=, /=, %=, ^=
  • Regular Expression Operators:
    ~: Matches regular expression.
    !~: Does not match regular expression.
  • Logical Operators:
    ||: Logical OR
    &&: Logical AND
  • Relational Operators:
    <, <=, >, >=, !=, ==
  • Other Operators:
    $: Refers to a field by its number.
    Space: Concatenates strings.
    ?:: Ternary operator.
    in: Checks if a key exists in an array.

AWK Regular Expression Syntax:

  • ^: Matches the start of a line.
  • $: Matches the end of a line.
  • .: Matches any single character.
  • *: Matches zero or more occurrences of the preceding character.
  • +: Matches one or more occurrences of the preceding character.
  • ?: Matches zero or one occurrence of the preceding character.
  • []: Matches any character in the specified range.
  • [^]: Matches any character not in the specified range.
  • () and |: Subexpressions and alternations.
  • \: Escape character.
  • {m}: Matches exactly m occurrences of a character.
  • {m,}: Matches at least m occurrences.
  • {m,n}: Matches between m and n occurrences.

AWK Built-in Functions:

  • toupper(): Converts all lowercase letters to uppercase.
  • length(): Returns the length of a string.

Custom Functions in AWK: AWK scripts can include user-defined functions. For example:

function square(x) {
  return x * x;
}

To use the function:

awk '{ print square($1) }' file.txt

AWK Control Flow Statements:

  • if-else: Conditional statements.
  • while and do-while: Loops.
  • for: Standard loops, including array traversal with for-in.
  • break and continue: Loop control.
  • exit: Terminates the script execution.
  • next: Skips the remaining commands for the current line.
  • return: Returns a value from a function.
  • ?:: Ternary operator for conditional expressions.

AWK Arrays: AWK supports associative arrays, meaning array indexes can be strings as well as numbers. Arrays in AWK don’t need to be declared or sized; they are created as soon as you assign a value to an index.

Examples:

1. Basic Example:

$ echo "hello" | awk 'BEGIN{ print "start" } END{ print "end" }'
start
end

2. Using Built-in Variables: To print the first and third columns of a file:

awk '{ print $1, $3 }' test.txt

3. Using External Variables:

$ a=100
$ b=100
$ echo | awk '{ print v1 * v2 }' v1=$a v2=$b
10000

4. Using Regular Expressions: To print the second column of lines starting with “a”:

awk '/^a/ { print $2 }' test.txt

5. Using Built-in Functions: Convert all lowercase letters to uppercase:

awk '{ print toupper($0) }' test.txt

6. Handling Different Delimiters: For CSV files with comma-separated values:

awk -F ',' '{ print $1, $2 }' test.csv

7. Writing and Running AWK Scripts: Save an AWK script to a file (e.g., script.awk):

BEGIN { FS=","; OFS=" - " }
{ print $1, $3 }

Run the script:

awk -f script.awk test.csv

Conclusion:

AWK is a versatile and powerful tool for text processing, offering rich features like pattern matching, regular expressions, and scripting capabilities. From simple one-liners to complex data analysis scripts, AWK excels at processing structured text efficiently and flexibly.