How to Use the Command 'pidstat' (with Examples)

How to Use the Command 'pidstat' (with Examples)

The pidstat command is a powerful tool used in Linux systems to monitor and display the activities of processes and threads. It provides detailed statistics regarding system resources usage, such as CPU, memory, and input/output resources per process or threads, either system-wide or for a specific process ID (PID). This command is essential for diagnosing performance issues or understanding workload distribution on a system.

Use case 1: Show CPU Statistics at a 2 Second Interval for 10 Times

Code:

pidstat 2 10

Motivation:

Monitoring CPU usage over time can help identify processes that are consuming too many resources, causing the system to slow down or become unresponsive. By examining CPU statistics at regular intervals, system administrators and developers can determine whether specific processes are responsible for performance bottlenecks and take corrective actions.

Explanation:

  • pidstat is the command used to gather statistics on process activities.
  • 2 is the interval in seconds, specifying how often pidstat should update its CPU statistics.
  • 10 indicates the number of times the statistics should be displayed.

Example Output:

Linux 5.4.0-77-generic (hostname) 	09/21/23 	_x86_64_	(4 CPU)

#      Time   UID       PID    %usr %system  %guest    %CPU   CPU  Command
02:40:01     0       1123    1.04    0.26    0.00    1.30     0  Process1
02:40:01     0       2481    0.50    0.16    0.00    0.66     1  Process2
...

Use Case 2: Show Page Faults and Memory Utilization

Code:

pidstat -r

Motivation:

Understanding memory usage and page faults is crucial for optimizing the performance of applications. Page faults occur when a process tries to access a portion of memory that is not currently mapped to the process’s address space. High numbers of page faults can significantly degrade performance.

Explanation:

  • pidstat is the command used to monitor process activities.
  • -r is the option that displays memory-related statistics, such as page faults and resident memory usage.

Example Output:

Linux 5.4.0-77-generic (hostname) 	09/21/23 	_x86_64_	(4 CPU)

#      Time   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
02:45:01     0       2170     0.00     0.00   27712   2212   0.05  Process3
02:45:01     0       2504     0.01     0.02   11368   5848   0.14  Process4
...

Use Case 3: Show Input/Output Usage per Process ID

Code:

pidstat -d

Motivation:

Monitoring input/output (I/O) usage is essential for identifying I/O bottlenecks that might affect system performance. Systems engineer can analyze the I/O performance of different processes to understand how disk and other input/output resources are being utilized, ensuring that these resources are efficiently managed.

Explanation:

  • pidstat is the command for monitoring process statistics.
  • -d is the option to display I/O statistics, allowing users to see how much I/O each process is generating.

Example Output:

Linux 5.4.0-77-generic (hostname) 	09/21/23 	_x86_64_	(4 CPU)

#      Time   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
02:50:01     0       3120    0.00      0.20     0.00     Process5
02:50:01     0       2143    0.00      1.00     0.00     Process6
...

Use Case 4: Show Information on a Specific PID

Code:

pidstat -p PID

Motivation:

Sometimes, specific processes may appear to be consuming higher system resources than expected, leading to performance issues. By focusing monitoring efforts on a single, suspicious process using its PID, administrators can gain a more profound understanding of how that process behaves over time and the resources it demands.

Explanation:

  • pidstat is the relevant command for retrieving process statistics.
  • -p specifies the option to monitor a particular process.
  • PID is the placeholder for the specific Process ID you are interested in.

Example Output:

Linux 5.4.0-77-generic (hostname) 	09/21/23 	_x86_64_	(4 CPU)

#      Time   UID       PID    %usr %system  %guest    %CPU   CPU  Command
02:55:01     0       1789    2.08    0.52    0.00    2.60     0  TargetProcess
...

Use Case 5: Show Memory Statistics for Processes with Command Names Including “fox” or “bird”

Code:

pidstat -C "fox|bird" -r -p ALL

Motivation:

Filtering process statistics based on command names allows users to monitor specific sets of applications, particularly if these applications might share a family name but have different instances or roles. This is particularly useful when working with processes created by similar service names and tracking their collective resource usage.

Explanation:

  • pidstat is the command for displaying process statistics.
  • -C "fox|bird" uses regular expressions to filter and display only processes with command names containing the strings “fox” or “bird”.
  • -r indicates the request to show memory statistics.
  • -p ALL specifies to include all PIDs that match the filtered command names, rather than default behavior that might select only significant ones.

Example Output:

Linux 5.4.0-77-generic (hostname) 	09/21/23 	_x86_64_	(4 CPU)

#      Time   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
03:00:01   1000      3131     0.10     0.00   41268   2345   0.42  firefox
03:00:01   1000      4021     0.02     0.01   21560   2950   0.39  usbird
...

Conclusion:

The pidstat command is a versatile tool for system administrators and developers in diagnosing and monitoring process-level resource usage. By providing insights into CPU, memory, and I/O activities per process, pidstat helps maintain a well-optimized and efficient system performance. By using specific options and intervals, users can tailor pidstat outputs to meet particular monitoring needs, maximizing their understanding of system behavior.

Related Posts

How to Use the Command 'trayer' (with Examples)

How to Use the Command 'trayer' (with Examples)

Trayer is a lightweight GTK-2 based system tray utility designed mainly for Unix-like operating systems.

Read More
How to use the command 'printenv' (with examples)

How to use the command 'printenv' (with examples)

The printenv command is a useful tool for interacting with environment variables in Unix-based systems.

Read More
How to play nsnake in the terminal (with examples)

How to play nsnake in the terminal (with examples)

nsnake is a command-line based version of the classic Snake game.

Read More