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.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注