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

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

The Prstat is a command available in Unix-based operating systems that allows users to monitor and report active process statistics. It provides information about CPU usage, memory usage, process accounting, and much more. This article will illustrate various use cases of the ‘prstat’ command and explain how each use case can be beneficial.

Use case 1: Examine all processes and report statistics sorted by CPU usage

Code:

prstat

Motivation: By using the ‘prstat’ command without any arguments, users can examine all active processes and get detailed statistics about their CPU usage. This can be useful for identifying processes that are consuming high CPU resources and may be causing performance issues on the system. It allows system administrators to monitor and troubleshoot the CPU usage of various processes.

Explanation: No additional arguments are required with the ‘prstat’ command to obtain CPU usage statistics. By default, the ‘prstat’ command displays a list of all active processes along with their CPU usage percentage, process ID, username, and other relevant information. The processes are sorted based on their CPU usage, with the highest CPU usage displayed at the top.

Example output:

   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP
  1946 root      3424K 1688K sleep   59    0   0:00:00 7.9% prstat/1
 64122 john      4988K  864K sleep   59    0   0:00:00 3.5% sshd/1
  3183 john      4944K 1460K sleep   59    0   0:00:00 2.3% bash/1
  3179 john      4944K 1460K sleep   58    0   0:00:00 1.8% bash/1
 30052 john      4976K  928K sleep   58    0   0:00:00 1.7% sshd/1
 ...

Use case 2: Examine all processes and report statistics sorted by memory usage

Code:

prstat -s rss

Motivation: The memory usage of processes can significantly impact system performance and stability. By sorting the process statistics by memory usage, users can identify memory-intensive processes and take necessary actions to optimize memory allocation or investigate memory leaks. This helps in efficient resource management and prevents memory-related issues.

Explanation: The ‘-s rss’ argument is used with the ‘prstat’ command to sort the process statistics by memory usage (RSS - Resident Set Size). When this argument is provided, the ‘prstat’ command will display all active processes sorted based on their memory usage, with the highest memory usage displayed at the top.

Example output:

   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP
  1946 root      3424K 1688K sleep   59    0   0:00:00 7.9% prstat/1
 64122 john      4988K  864K sleep   59    0   0:00:00 3.5% sshd/1
  3183 john      4944K 1460K sleep   59    0   0:00:00 2.3% bash/1
  3179 john      4944K 1460K sleep   58    0   0:00:00 1.8% bash/1
 30052 john      4976K  928K sleep   58    0   0:00:00 1.7% sshd/1
 ...

Use case 3: Report total usage summary for each user

Code:

prstat -t

Motivation: Understanding the resource consumption of each user on a system can be crucial for monitoring user behavior, managing system resources, and identifying any potential resource abuse. By using the ‘-t’ argument with the ‘prstat’ command, users can obtain a summary of resource usage for each individual user.

Explanation: The ‘-t’ argument is used with the ‘prstat’ command to generate a summary of the total resource usage for each user present on the system. The summary includes statistics such as the number of processes running, CPU usage, memory usage, the amount of data transferred, and much more. It provides an overview of the resource utilization for each user.

Example output:

   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP
  1946 root      3424K 1688K sleep   59    0   0:00:00 7.9% prstat/1
 64122 john      4988K  864K sleep   59    0   0:00:00 3.5% sshd/1
  3183 john      4944K 1460K sleep   59    0   0:00:00 2.3% bash/1
  3179 john      4944K 1460K sleep   58    0   0:00:00 1.8% bash/1
 30052 john      4976K  928K sleep   58    0   0:00:00 1.7% sshd/1
 ...
 
Total: 5 processes, 25 lwps, load averages: 0.10, 0.12, 0.08

Use case 4: Report microstate process accounting information

Code:

prstat -m

Motivation: Microstate process accounting provides information about the execution state of processes at a microstate level. This can be helpful in understanding the process behavior and performance by analyzing how much time the processes spend in different execution states. The ‘-m’ argument with the ‘prstat’ command allows users to retrieve microstate process accounting information.

Explanation: The ‘-m’ argument is used with the ‘prstat’ command to obtain microstate process accounting information. Microstate accounting measures the CPU usage of processes at a more detailed level, including the time spent in various microstates such as user, system, wait, sleep, and much more. It provides valuable insights into the execution state of processes.

Example output:

PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROCESS/NLWP
1946 root      125  33   0   0   0   0  42   0  0.2 0.1 0.2   0 prstat/1
64122 john      37  14   0   0   0   0  49   0  0.1   0   0   0 sshd/1
 3183 john      26   8   0   0   0   0  64   0  0.0   0   0   0 bash/1
 3179 john      19   4   0   0   0   0  77   0  0.0   0   0   0 bash/1
30052 john      19   5   0   0   0   0  76   0  0.0   0   0   0 sshd/1
...

Use case 5: Print out a list of top 5 CPU using processes every second

Code:

prstat -c -n 5 -s cpu 1

Motivation: In scenarios where users need real-time insights into the CPU usage of processes, the ‘prstat’ command can be used with specific arguments to continuously monitor and display the top CPU using processes. This can help identify processes that are causing high CPU load and might require optimization or investigation.

Explanation: The ‘-c’ argument is used with the ‘prstat’ command to display the top CPU using processes. The ‘-n 5’ argument ensures that only the top 5 processes are displayed. The ‘-s cpu’ argument sorts the processes based on CPU usage. By specifying an interval of ‘1’, the output is continuously refreshed every second to provide real-time statistics.

Example output:

 PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP
28641 john      4924K 1164K sleep   59    0   0:00:01  40% php/1
30502 john      5004K 1312K sleep   59    0   0:00:00  20% node/1
  254 root      3568K 1656K sleep   59    0   0:00:00  10% cron/1
16982 john      4968K  788K sleep   59    0   0:00:00   5% apache/8
  102 root      4708K 1856K sleep   59    0   0:00:00   2% syseventd/15
...

Conclusion:

The ‘prstat’ command is a powerful tool for monitoring and reporting active process statistics in Unix-based operating systems. It offers various use cases, such as examining processes by CPU and memory usage, generating user-specific resource summaries, retrieving microstate process accounting information, and monitoring real-time CPU usage. By leveraging the ‘prstat’ command, users can effectively manage system resources, troubleshoot performance issues, and optimize process utilization.

Related Posts

qutebrowser (with examples)

qutebrowser (with examples)

1: Open qutebrowser with a specified storage directory qutebrowser --basedir path/to/directory Motivation: This command allows the user to specify a custom directory for storing qutebrowser data.

Read More
How to use the command kscreen-doctor (with examples)

How to use the command kscreen-doctor (with examples)

The command kscreen-doctor is a utility command in KDE Plasma that can be used to change and manipulate the screen setup.

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

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

The drupal command is a powerful tool that allows developers to generate boilerplate code, interact with, and debug Drupal projects.

Read More