Process statistics and threads using prtstat

Lets hit straight to the point – what does prtstat command in linux provide ?

Well, in that case lets dwell into it by running the command and understanding all the crucial information it gives.

prtstat would print the below statistics about the process:

  • Process and its associated ID’s, state of the process and process thread count ( we will see with the support of other commands, what are those threads )
  • Page Faults information of the process & child
  • CPU times of process and its child
  • Memory stats for process
  • process Scheduling (we will look at process scheduling policy & priority – a bit more on it)

Before we start, lets see how to get this prtstat command. Basically, prtstat command is by default provided on most flavours of linux by package psmisc.
If you dont find it, just go ahead and install the psmisc package. In case of Redhat systems use below command to install

# yum install psmisc

Now the real stuff..!! Let us pick any process. In our case, we just chose below goferd process since it has got some associated threads which would in turn help us to illustrate more.

[root@linuxminion ~]# ps -ef | grep gofer
root  20242 20225  0 Jul13 pts/0   00:00:01 /usr/bin/python /usr/bin/goferd --foreground
root  21245 20276  0 02:07 pts/1   00:00:00 grep --color=auto gofer
[root@linuxminion ~]#

We will now run the prtstat command against the goferd process (PID:20242) as below to get the process statistics. Have a look at the command output which shows some really great information.

[root@linuxminion ~]# prtstat 20242
 Process: python                 State: S (sleeping)
   CPU#:  0              TTY: 136:0      Threads: 5
 Process, Group and Session IDs
   Process ID: 20242               Parent ID: 20225
     Group ID: 20242              Session ID: 20204
   T Group ID: 20242
 Page Faults
   This Process    (minor major):     4153         0
   Child Processes (minor major):     1265         0
 CPU Times
   This Process    (user system guest blkio):   0.39   0.77   0.00   0.00
   Child processes (user system guest):         0.00   0.00   0.00
 Memory
   Vsize:       522 MB
   RSS:         13 MB                     RSS Limit: 18446744073709 MB
   Code Start:  0x400000                  Code Stop:  0x4009a4
   Stack Start: 0x7ffcf1718d70
   Stack Pointer (ESP): 0x7ffcf1717e70    Inst Pointer (EIP): 0x7f64d219e790
 Scheduling
   Policy: normal
   Nice:   0              RT Priority: 0 (non RT)
[root@linuxminion ~]#

Let us now understand the above output by splitting into various sections.

------------------------------------------------------------
Process: python                 State: S (sleeping)
   CPU#:  0              TTY: 136:0      Threads: 5
 Process, Group and Session IDs
   Process ID: 20242               Parent ID: 20225
     Group ID: 20242              Session ID: 20204
   T Group ID: 20242
------------------------------------------------------------

Where: 

Process - Name of the process
State - Process State and the values can be:
         D    uninterruptible sleep (usually IO)            
         R    running or runnable (on run queue)            
         S    interruptible sleep (waiting for an event to complete)
         T    stopped by job control signal            
         t    stopped by debugger during the tracing            
         W    paging (not valid since the 2.6.xx kernel)            
         X    dead (should never be seen)            
         Z    defunct ("zombie") process, terminated but not reaped by its parent
CPU - Process running on which CPU (This can vary in case of multiple cpu's)
TTY - the device number of the controlling tty
Threads - Number of associated threads
Process ID - Process ID of this process
Parent ID - Parent ID of this process
Group ID - Group ID of the process
Session ID - Session ID of the process
T GID - Thread Group ID

Note: A session contains a number of process groups, and a process group contains a number of processes, and a process contains a number of threads. A session can have a controlling tty.
------------------------------------------------------------
Page Faults
   This Process    (minor major):     4153         0
   Child Processes (minor major):     1265         0
------------------------------------------------------------

Where:

minor and major - are the number of page faults of main process and its child processes respectively.
 
major page faults (I/O-requiring, page faults that occurred while the process was running. These are faults where the page has actually migrated out of primary memory) - from man page of time command
minor page faults (recoverable page faults. These are pages that are not valid (so they fault) but which have not yet been claimed by other virtual pages. 
Thus the data in the page is still valid but the system tables must be updated) - from manual page of time command)
------------------------------------------------------------
CPU Times
   This Process    (user system guest blkio):   0.39   0.77   0.00   0.00
   Child processes (user system guest):         0.00   0.00   0.00
------------------------------------------------------------

Where:

Every attribute represents the seconds the CPU has spent in the given mode.
user: time spent by processes executing in user mode
system: time spent by processes executing in kernel mode
guest: time spent running a virtual CPU for guest operating systems under the control of the Linux kernel
------------------------------------------------------------
Memory
   Vsize:       522 MB
   RSS:         13 MB                     RSS Limit: 18446744073709 MB
   Code Start:  0x400000                  Code Stop:  0x4009a4
   Stack Start: 0x7ffcf1718d70
   Stack Pointer (ESP): 0x7ffcf1717e70    Inst Pointer (EIP): 0x7f64d219e790
------------------------------------------------------------

Where:

Vsize: The total amount of virtual memory used by the process. It includes all code, data and shared libraries plus pages that have been swapped out and pages that have been mapped but not used.
RSS: RSS (resident set size) is the amount of main (RAM) memory used by a process; it does not include swapped out memory used by a process.
Code Start/Stop: virtual memory address space, for a 64 bit ELF executable
Stack Start: linear addresses of stack depending upon the underlying architecture. 
Note : The stack contains all the local variables used in the program considering their respective local scopes. Whenever a local variable is defined, it is pushed into the stack and whenever the scope for that local variable ends, it is popped out.
Stack Pointer: stack pointer, which points to the last value pushed onto the stack
Inst Pointer: the instruction pointer, which points to the next instruction it will execute

Note: In x86, the instruction pointer is the register %eip and the stack pointer is in the register %esp.
------------------------------------------------------------
Scheduling
   Policy: normal
   Nice:   0              RT Priority: 0 (non RT)
------------------------------------------------------------

Where:

Policy: Scheduling policy of the process.
         The Linux provides 6 policies: 
         SCHED_FIFO (realtime) - first in/first out realtime policy
         SCHED_RR (realtime) - A round-robin realtime policy
         SCHED_DEADLINE
         SCHED_OTHER (normal) - The default policy
         SCHED_BATCH - Similar to SCHED_OTHER, but with a throughput orientation
         SCHED_IDLE (normal) - The default policy
In Linux, a scheduler controls which thread will be executed and how long it will be. 
The order of execution is based on scheduling policy & scheduling priority of each thread.
The scheduling policy was introduced in the Linux on version 2.2.
These policies, except SCHED_DEADLINE, are divided into 2 groups - SCHED_OTHER, SCHED_IDLE and SCHED_BATCH belong to a normal policy and SCHED_FIFO, SCHED_RR belong to a real-time policy. SCHED_DEADLINE threads are special and of highest priority threads.

Nice: nice value of the process and the values range from -20 (most favorable to the process) to 19 (least favorable to the process)
The nice value is an attribute that can be used to influence the CPU scheduler to favor or disfavor a process in scheduling decisions.

RT Priority: Real-time priority which is mainly used to decide execution order of threads  with a real-time policy.

Additional Information on Threads & Scheduling policy /Priority:

  • Find the threads of the process and what scheduling policy they are into. In our case from the output of prtstat, it shows 5 threads. Let’s see what are those.

To quickly get the process threads, simply run below command which would give Thread ID’s

[root@linuxminion ~]# pstree -p 20242
 python(20242)─┬─{python}(20247)
               ├─{python}(20248)
               ├─{python}(20249)
               └─{python}(20250)
 [root@linuxminion ~]#

For the detailed output on threads, run below command.

[root@linuxminion ~]# ps -T -clp 20242 -f
 F S UID        PID  SPID  PPID CLS PRI ADDR SZ WCHAN  STIME TTY          TIME CMD
 0 S root     20242 20242 20225 TS   19 - 127683 futex_ Jul13 pts/0   00:00:00 /usr/bin/python /usr/bin/goferd --foreground
 1 S root     20242 20247 20225 TS   19 - 127683 futex_ Jul13 pts/0   00:00:00 /usr/bin/python /usr/bin/goferd --foreground
 1 S root     20242 20248 20225 TS   19 - 127683 poll_s Jul13 pts/0   00:00:00 /usr/bin/python /usr/bin/goferd --foreground
 1 S root     20242 20249 20225 TS   19 - 127683 poll_s Jul13 pts/0   00:00:00 /usr/bin/python /usr/bin/goferd --foreground
 1 S root     20242 20250 20225 TS   19 - 127683 futex_ Jul13 pts/0   00:00:00 /usr/bin/python /usr/bin/goferd --foreground
[root@linuxminion ~]

In the above output, we can see the SPID (Thread ID) and the corresponding Scheduling policy (CLS) they are into.

CLS – Scheduling class of the process(alias policy). Field’s possible values are:

 - not reported
 TS  SCHED_OTHER
 FF  SCHED_FIFO
 RR  SCHED_RR
 B   SCHED_BATCH
 ISO SCHED_ISO
 IDL SCHED_IDLE
 ?   unknown value 

I believe now you have a better understanding of the prtstat command and how to interpret the output.

Leave a Reply

Your email address will not be published.