Pages

Saturday, December 12, 2015

top command one liners


Understanding top command output sections

Sample top command output can be considered as below for basic understanding

# top
top - 02:29:06 up 74 days, 21:56,  1 user,  load average: 13.96, 17.06, 18.83
Tasks: 202 total,   1 running, 201 sleeping,   0 stopped,   0 zombie
Cpu(s): 18.4%us,  5.1%sy,  0.0%ni, 75.9%id,  0.2%wa,  0.0%hi,  0.0%si,  0.4%st
Mem:  12198272k total, 10801160k used,  1397112k free,   735780k buffers
Swap:        0k total,        0k used,        0k free,  8527900k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5548 root      20   0 15164 1176  804 R  1.9  0.0   0:00.01 top
    1 root      20   0 19356 1496 1188 S  0.0  0.0   0:05.09 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.43 kthreadd


Section 01:- Uptime and Load Averages

top - 02:29:06 up 74 days, 21:56,  1 user,  load average: 13.96, 17.06, 18.83

The fields display:
- current time
- the time your system is been up
- number of users logged in
- load average of 5, 10 and 15 minutes respectively.

Section 02:- Tasks

Tasks: 202 total,   1 running, 201 sleeping,   0 stopped,   0 zombie

- total number of processes
- number of processes running
- number of sleeping processes
- number of stopped
- number of processes in zombie state

Section 03:- CPU States

Cpu(s): 18.4%us,  5.1%sy,  0.0%ni, 75.9%id,  0.2%wa,  0.0%hi,  0.0%si,  0.4%st

- us, user: CPU time in running (un-niced) user processes
- sy, system: CPU time in running kernel processes
- ni, niced: CPU time in running niced user processes
- wa, IO wait: CPU time waiting for IO completion
- hi: CPU time serving hardware interrupts
- si: CPU time serving software interrupts
- st: CPU time stolen for this vm by the hipervisor.

Section 04:- Memory Usage

Mem:  12198272k total, 10801160k used,  1397112k free,   735780k buffers
Swap:        0k total,        0k used,        0k free,  8527900k cached

- 1st line is for physical memory and the second for virtual memory (swap space)
- Both memory are displayed as: total available memory, used memory, free memory, and memory used for buffers


Section 05:- Fields/Columns

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5548 root      20   0 15164 1176  804 R  1.9  0.0   0:00.01 top

- PID - The Process ID, to uniquely identify a processes.
- USER - The effective user name of the owner of the processes.
- PR - The scheduling priority of the process. Some values in this field are 'rt'. It means that the process is running under real-time.
- NI - The nice value of the process. Lower values mean higher priority.
- VIRT - The amount of virtual memory used by the process.
- RES - The resident memory size. Resident memory is the amount of non-swapped physical memory a task is using.
- SHR - SHR is the shared memory used by the process.
- S - This is the process status. It can have one of the following values:
  - D - uninterruptible sleep
  - R - running
  - S - sleeping
  - T - traced or stopped
  - Z - zombie
- %CPU - It is the percentage of CPU time the task has used since last update.
- %MEM - Percentage of available physical memory used by the process.
- TIME+ - The total CPU time the task has used since it started, with precision upto hundredth of a second.
- COMMAND - The command which was used to start the process.

To capture normal top processes in a file
top -n 1 -b > top_output.log

To capture top processes for a user in a file
top -n 1 -b -u username > top_output.log

To capture top command output for a PID in a file
top -n 1 -b -p 32073 > top_output.log

To capture top command output for a PID against 5 occurrences with a 3 sec interval in a file
top -n 5 -b -p 32073 > top_output.log

To capture top %CPU consuming processes in a file
top -n 1 -b | sort -nrk 10 |head > top_output.log

To capture top SHR (i.e. shared memory) consuming processes in a file
top -n 1 -b | sort -nrk 7 |head > top_output.log

To capture top RES (i.e. resident memory/actually used memory) consuming processes in a file
top -n 1 -b | sort -nrk 6 |head > top_output.log

To capture top VIRT (i.e. virtual memory/actually allocated memory) consuming processes in a file
top -n 1 -b | sort -nrk 5 |head > top_output.log

To capture top %MEM consuming processes in a file
top -n 1 -b | sort -nrk 11 |head > top_output.log

Interactive Options of top command to get output sorted by a particular parameter
- top
- Press [ Shift + o ]
- Press any character that you need to get output sorted by below are the options,
  A: PID        = Process Id
  b: PPID       = Parent Process Pid
  c: RUSER      = Real user name
  d: UID        = User Id
  e: USER       = User Name
  f: GROUP      = Group Name
  g: TTY        = Controlling Tty
  h: PR         = Priority
  i: NI         = Nice value
  j: P          = Last used cpu (SMP)
  k: %CPU       = CPU usage
  l: TIME       = CPU Time
  m: TIME+      = CPU Time, hundredths
  n: %MEM       = Memory usage (RES)
  o: VIRT       = Virtual Image (kb)
  p: SWAP       = Swapped size (kb)
  q: RES        = Resident size (kb)
  r: CODE       = Code size (kb)
  s: DATA       = Data+Stack size (kb)
  t: SHR        = Shared Mem size (kb)
  u: nFLT       = Page Fault count
  v: nDRT       = Dirty Pages count
  w: S          = Process Status
  x: COMMAND    = Command name/line
  y: WCHAN      = Sleeping in Function
  z: Flags      = Task Flags <sched.h>
- Done

Interactive Options of top command to get output with full command details/absolute path of process
- top
- Press c
- Done

Interactive Options of top command to get output highlighted for running processes
- top
- Press z
- Done

Interactive Options of top command to get output sorted by %CPU
- top
- Press [ Shift + p ]
- Done

Interactive Options of top command to get output sorted by %Memory
- top
- Press [ Shift + m ]
- Done

Back To Top

No comments:

Post a Comment