How to use the command 'pidstat' (with examples)
- Linux
- December 25, 2023
The pidstat
command is used to show system resource usage, including CPU, memory, IO, etc. It provides detailed information about the utilization of resources by processes on a system.
Use case 1: Show CPU statistics at a 2 second interval for 10 times
Code:
pidstat 2 10
Motivation: This command is useful when you want to monitor CPU usage over a specific time period. By specifying a 2-second interval and running it for 10 times, you can get a detailed view of how the CPU is being utilized.
Explanation: The pidstat
command is used without any options, followed by the interval (in seconds) and the number of times to display the statistics. In this case, the CPU statistics are displayed at a 2-second interval for a total of 10 times.
Example output:
Linux 5.4.0-90-generic (hostname) 11/14/2021 _x86_64_ (4 CPU)
12:22:12 AM UID PID %usr %system %guest %wait %CPU CPU Command
12:22:14 AM 1000 2211 0.00 0.50 0.00 0.00 0.50 2 chrome
12:22:14 AM 1000 2286 6.00 0.50 0.00 0.00 6.50 1 top
...
Use case 2: Show page faults and memory utilization
Code:
pidstat -r
Motivation: Monitoring page faults and memory utilization is essential for identifying potential bottlenecks in a system. By using this command, you can obtain information about the memory usage of processes and the number of page faults that occurred.
Explanation: The -r
option is used to display page fault and memory utilization statistics. This command will provide valuable insights into the memory usage patterns of processes running on the system.
Example output:
Linux 5.4.0-90-generic (hostname) 11/14/2021 _x86_64_ (4 CPU)
12:24:07 AM UID PID minflt/s majflt/s VSZ RSS %MEM Command
12:24:07 AM 1000 2211 0.00 0.00 960416 37232 0.97 chrome
12:24:07 AM 1000 2286 1.00 0.00 430560 22584 0.58 top
...
Use case 3: Show input/output usage per process id
Code:
pidstat -d
Motivation: Monitoring input/output (I/O) usage can be helpful in identifying processes that are causing high IO load on a system. By using this command, you can easily determine the I/O usage of individual processes.
Explanation: The -d
option is used to display input/output statistics. This command will provide information about the number of input/output operations performed by each process, giving insights into disk activity.
Example output:
Linux 5.4.0-90-generic (hostname) 11/14/2021 _x86_64_ (4 CPU)
12:25:13 AM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
12:25:13 AM 1000 2211 0.00 0.00 0.00 chrome
12:25:13 AM 1000 2286 0.00 0.00 0.00 top
...
Use case 4: Show information on a specific PID
Code:
pidstat -p PID
Motivation: Sometimes, it is necessary to gather specific information about a particular process identified by its unique Process ID (PID). This command allows you to focus on a specific PID and obtain detailed statistics about its resource usage.
Explanation: The -p
option is used to specify a specific PID for which detailed information is required. By replacing PID
with the actual process ID, you can obtain accurate resource usage statistics for that process.
Example output:
Linux 5.4.0-90-generic (hostname) 11/14/2021 _x86_64_ (4 CPU)
12:27:04 AM UID PID %usr %system %guest %wait %CPU CPU Command
12:27:04 AM 1000 2211 0.00 0.50 0.00 0.00 0.50 2 chrome
Use case 5: Show memory statistics for specific command names
Code:
pidstat -C "fox|bird" -r -p ALL
Motivation: When you want to monitor memory statistics for specific processes with command names containing certain keywords, this command allows you to filter the output based on those command names and retrieve relevant memory statistics.
Explanation: The -C
option is used to specify a regular expression pattern for command names to include in the output. In this case, the pattern is "fox|bird"
, which would include the command names that contain either “fox” or “bird”. The -r
option is used to display memory statistics, and the -p ALL
option shows the statistics for all matching processes.
Example output:
Linux 5.4.0-90-generic (hostname) 11/14/2021 _x86_64_ (4 CPU)
12:28:18 AM UID PID minflt/s majflt/s VSZ RSS %MEM Command
12:28:18 AM 1000 2211 0.00 0.00 960416 37232 0.97 chrome
...
Conclusion:
The pidstat
command is a powerful utility for monitoring system resource usage. By using its various options, you can gather detailed statistics about CPU usage, memory utilization, input/output activity, and more. These examples provide a starting point for utilizing pidstat
to gain valuable insights into the resource utilization of processes on your system.