Tuesday, February 03, 2009

handy TOP reference

Top is a program that monitors the CPU and memory utilization of individual processes on your Unix or Linux machine. The output of top normally defaults to a list of processes sorted from high CPU to low CPU utilization. It is helpful to know some of the commands, so that you can sort through and find specific processes based on memory, status or any number of statistics top has available to it.

The command sets listed below were tested in Fedora 10, but vary across distributions. Use the ? or type "h" to get the list of commands specific to your distribution.

default output
To run top interactively, just type "top" at a command line and hit enter. It will refresh once every three seconds by default until you quit (press "Q") to exit.
top - 14:23:03 up 14 days, 4:16, 4 users, load average: 0.25, 0.13, 0.09
Tasks: 127 total, 1 running, 125 sleeping, 0 stopped, 1 zombie
Cpu(s): 15.8%us, 9.0%sy, 0.0%ni, 74.5%id, 0.0%wa, 0.6%hi, 0.1%si, 0.0%st
Mem: 551296k total, 498888k used, 52408k free, 43980k buffers
Swap: 2031608k total, 88k used, 2031520k free, 134708k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

2406 tag 15 0 74868 45m 8860 S 43 8.4 3:58.37 Xorg
2541 tag 15 0 68596 17m 8636 S 6 3.3 0:18.07 gnome-terminal
18561 tag 15 0 734m 143m 12m S 1 26.6 0:09.36 cinelerra
2527 tag 15 0 17804 8992 6532 S 0 1.6 0:13.27 metacity
2405 tag 15 0 16636 1652 1080 S 0 0.3 0:06.01 gdm-binary
2657 tag 15 0 48260 10m 7788 S 0 1.9 0:22.62 mixer_applet2

19284 tag 15 0 2164 1036 796 R 0 0.2 0:00.06 top

You see the output defaults to a list of processes the length of your terminal window. To shorten this list to a more manageable set of processes, type # or n to limit the number of tasks displayed.

To sort the list by a column other than %CPU, type <> to sort the output by the column that is to the left or right of %CPU.

To choose a specific column to sort by, press "F" or "O" and select the letter that corresponds to the column you'd like to sort.

To reverse the sort, press "R"

To toggle columns of data on and off, press "f".

Interactive Mode
Here is a list of commands that you can type while in interactive mode:
f - toggle individual fields on and off
F or O - choose a field to sort on
?/h - help
H - toggle the display of program threads
I - toggle IRIX mode
K - kill a process
M - sort on %MEM
N - sort on process ID (PID)
P - sort on %CPU
R - reverse the sort
T - sort on TIME process has been running
U - select processes by user
#/n - display n number of processes
enter - refresh display


Process States
A process can be in a number of states, shown by the Status column:
D - uninterruptable sleep
R - running
S - sleeping
T - traced or stopped
Z - zombie


Command line options
-d delay in seconds
-p display process ID (PID)
-H toggle display of threads
-i toggle idle processes
-n number of iterations before top ends
-u monitor by user
-v version


Notes on fields related to memory
VIRT = total amount of virtual memory used by the task. Includes all code/data/shared libraries and swapped pages
VIRT = SWAP + RES
SWAP = the swapped out portion of a task's total virtual memory
RES = Non-swapped physical memory a task has used
RES = CODE + DATA
CODE = amount of physical memory devoted to executable code. "text resident set"
DATA = amount of physical memory devoted to other than executable code. "data resident set"
SHR = amount of shared memory. This reflects the memory that could be potentially shared with other processes.


On my Fedora 10 machine, I noticed that RES does NOT equal the sum of the text plus data resident sets. Maybe I should file a bug?

In any case, that's a synopsis of top.

Enjoy,
TAG

No comments:

Feel free to drop me a line or ask me a question.