Pages

Monday, September 3, 2012

Resource Management : Top


Top: The ‘Top’ command provides a real time view of the running system. It can display system summary information as well as list of tasks running currently.

A basic execution of ‘top’ command gives,

localhost: root-~ $ top
top - 23:24:28 up 100 days, 23:30,  1 users,  load average: 3.64, 4.27, 3.94
Tasks:  97 total,   1 running,  96 sleeping,   0 stopped,   0 zombie
Cpu(s): 45.9%us,  0.0%sy,  0.0%ni, 53.9%id,  0.2%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4194304k total,  4116216k used,    78088k free,   666688k buffers
Swap:  4194296k total,       52k used,  4194244k free,   719448k cached

  PID USER      PR  NI  VIRT     RES  SHR    S  %CPU %MEM    TIME+  COMMAND
 3833 root      21   0   2457m  2.1g 9920   S   91.8    53.2      84027:12 java
    1 root         15   0  10364    640  548     S   0.0      0.0        0:07.51 init
    2 root         RT  -5     0           0    0       S   0.0      0.0        0:01.10 migration/0
    3 root         34  19     0           0    0       S   0.0      0.0        0:00.46 ksoftirqd/0
    4 root         RT  -5     0           0     0       S   0.0      0.0       0:00.00 watftirqd/0


Let’s see the output of the command,

top - 23:24:28 up 100 days, 23:30,  0 users,  load average: 3.64, 4.27, 3.94

23:24:28: Current System Time ( can be obtained by ‘date’ command )
100 days: Up time, The System was started before 100 days ( can get the same using ‘uptime’ command)
1 Users: One user
Load average 3.64, 4.27, 3.94:  The Load Average displayed for past 1min, 5min and 15 minute intervals.

Tasks:  97 total,   1 running, 96 sleeping,   0 stopped,   0 zombie

Total Number of tasks: 97
Running: This can be a taught as tasks that are ready to run.
Sleeping: by default sleeping process don’t use any cpu.

Cpu(s): 45.9%us, 0.0%sy, 0.0%ni, 53.9%id, 0.2%wa, 0.0%hi, 0.0%si, 0.0%st

This section Says about the CPU Details. This gives information on what cpu has spending time.

·         us -> User CPU time: The time the CPU has spent running users’ processes that are not niced.
·         sy -> System CPU time: The time the CPU has spent running the kernel and its processes.
·         ni -> Nice CPU time: The time the CPU has spent running users’ process that have been niced.
·         wa -> iowait: Amount of time the CPU has been waiting for I/O to complete.
·         hi -> Hardware IRQ: The amount of time the CPU has been servicing hardware interrupts.
·         si -> Software Interrupts.: The amount of time the CPU has been servicing software interrupts.
·         id is idle, in other words CPU idle status
·         st is Time stolen from a virtual machine

Mem:   4194304k total, 4116216k used,    78088k free,   666688k buffers
Swap:  4194296k total,       52k used, 4194244k free,   719448k cached

These give us the Details of the Memory and Swap.
PID USER      PR   NI   VIRT      RES   SHR    S   %CPU %MEM    IME+       COMMAND
 3833 root      21   0   2457m   2.1g   9920   S    91.8    53.2     84027:12  java

These details give you the Process information. The columns indicate
1.    PID - Process ID - The unique ID of the process (commonly used with the kill command)
2.    USER - Username which the process is running under
3.    PR - Priority for the process (ranges from -20 for very important processes to 19 for unimportant processes)
4.    NI - Nice value modifies the priority of the process (a negative value will increase the priority of the process and a positive value will decrease the priority of the process)
5.    VIRT - Total amount of virtual memory used by the process
6.    RES - Resident size (kb) - Non-swapped physical memory which the process has used
7.    SHR - Shared memory size (kb) - Amount of shared memory which the process has used (shared memory is memory which could be allocated to other processes)
8.    S - Process status - Possible values:
o    R - Running
o    D - Sleeping (may not be interrupted)
o    S - Sleeping (may be interrupted)
o    T - Traced or stopped
o    Z - Zombie or "hung" process
9.    %CPU - Percentage of CPU time the process was using at the time top last updated
10.  %MEM - Percentage of memory (RAM) the process was using at the time top last updated
11.  TIME+ - Cumulative CPU time which the process and children of the process have used
12.  COMMAND - Name of the process or the path to the command used to start the process (press c to toggle between the name of the process and the path to the command used to start the process)
To sort top output by any column
In Order to sort the top command output by any column, execute the top command and press ‘o’.It then provides you with a list of options to sort .
Some of them are
* A: PID          = Process Id
* E: USER       = User Name
* H: PR           = Priority
* I: NI            = Nice value
* O: VIRT       = Virtual Image (kb)
* Q: RES        = Resident size (kb)
Display Selected User Process In Top
In Order to get the Details of a process that belong to a selected users, we can use
Top –u root
Display Only Specific Process
If we need to find the information about a specific process, we can use
top -p 29547(PID)
Refresh Linux Top Command Output
By default, the output of the top command will be refreshed for every 3.0 seconds. If we need to change this feature we can press  ‘d’ while the top command is running. It allows us to enter the time frame for the refresh rate.
High Light Running process
We can high light the information a top command shows by pressing ‘z’ while the top command is executing.
Display Absolute Path of the Command
If we need to check the complete absolute path of the command that was executing in top command, we can press ‘c’.
Decrease the Process Display Count in Top
If We need to decrease the display of the process count in the output of top command, we can press ‘n’ while the top command is executing. It gives us an option to enter a value for the display of the process.
Write to a File
If we need to write the Output of the top command to a file for future reference ,we can use ‘w’ while the top command is executing.The contents are saved to ~/.toproc file.

top - 02:57:54 up 102 days,  3:08,  0 users,  load average: 0.16, 0.16, 0.16
Tasks: 106 total,   1 running, 105 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.3%us,  0.5%sy,  0.0%ni, 97.4%id,  1.7%wa,  0.0%hi,  0.2%si,  0.0%st
Mem:   4194304k total,  3595660k used,   598644k free,   706288k buffers
Swap:  4194296k total,       52k used,  4194244k free,   600820k cached
Wrote configuration to '/privdir/djbs002/.toprc'
Once the file is written it shows the confirmation.
There will be a mode available in top command, which allows performing more tasks. Tasks like
  • k, to kill a task (process/thread)
  • r, to re-nice a task
  • d or s, to change the delay or sleep interval
Consider if you want to kill a process, when the top command was executing you can press ‘k’ which will ask you for the process ID to be killed. Once you enter PID and enter , the process id will be killed.