January 10, 2005

CLI Magic: Getting on top of things

Author: Joe Barr

You have to take charge of things early in the year, or you never will. But you can't really get on top of all things your computer is doing while you're lounging in your Strata-GUI. So hop down out of there for awhile and I'll show you how you can get on top of things at the CLI. Better hurry, though, or your little sister is going to learn it before you do. Top -- the system utility of which I speak -- has lots of secrets to tell. Let's start with the basics. Just enter top at a command line and see what it has to say. Here's what it has to say in the first 12 lines of output on my desktop box as I type this column:
top - 22:28:20 up 1 day,  3:11,  2 users,  load average: 0.21, 0.11, 0.09
Tasks: 103 total,   1 running, 102 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.9% us,  1.0% sy,  0.0% ni, 93.8% id,  0.0% wa,  0.3% hi,  0.0% si
Mem:   1034760k total,  1014380k used,    20380k free,    50104k buffers
Swap:   554232k total,     4200k used,   550032k free,   442440k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
18583 warthawg 15 0 23660 13m 18m S 3.3 1.3 0:01.24 gnome-terminal
5094 root 15 0 113m 45m 80m S 2.0 4.5 38:42.16 X
6 root 5 -10 0 0 0 S 0.3 0.0 1:57.01 kacpid
6028 warthawg 16 0 31428 17m 19m S 0.3 1.8 0:12.91 nautilus
18592 warthawg 17 0 1968 956 1764 R 0.3 0.1 0:00.03 top

As you can see, the first five lines provide a detailed status of the system, including time since the last boot, number of tasks, number of zombies, CPU and memory utilization. Those five lines make up the summary section. The real fun comes in the command line (not shown above) and the task section, which includes the column titles and the data displayed beneath them. If you're a tweaker, you're going to love top. You can control almost everything about it.

Here are a few of the command line options available to you with top. Let me get this plug in early. The man pages for top are exceptionally well written. Refer to them early and often as you experiment with the utility. Now, back to the command line options:

  • -b Turns batch mode on. In batch mode, no keyboard input is accepted.
  • -c Flip-flops command line/program name column. Whatever was shown last time is not shown this time.
  • -d ss.tt Sets the delay time for updating the screen in seconds and tenths of seconds
  • -n number Sets the number of times top should update the screen before shutting itself off.
  • -u somebody Shows only the tasks run by selected user. A capital -U does the same thing, only for real. No tasks run by somebody setting the effective user name to that selected are shown.
  • -p PIDx Show only the PIDs (up to 20 of them) specified

The command line options are fine, but top doesn't really get to be fun until you get familiar with the dynamic command set, entered at the keyboard while it's running. Did you know there are 26 different columns of information about a task that top can display? Let's start by listing what's being displayed at a given point in time. Enter an "f" (or an "o") in the terminal window where top is running. A new screen appears, showing -- and explaining -- which columns are currently part of the top display. It looks something like this:

  
 Current Fields:  AEHIOQTWKNMbcdfgjplrsuvyzX  for window 1:Def
Toggle fields via field letter, type any other key to return

* A: PID = Process Id 0x00000100 PF_SUPERPRIV
* E: USER = User Name 0x00000200 PF_DUMPCORE
* H: PR = Priority 0x00000400 PF_SIGNALED
* I: NI = Nice value 0x00000800 PF_MEMALLOC
* O: VIRT = Virtual Image (kb) 0x00002000 PF_FREE_PAGES (2.5)
* Q: RES = Resident size (kb) 0x00008000 debug flag (2.5)
* T: SHR = Shared Mem size (kb) 0x00024000 special threads (2.5)
* W: S = Process Status 0x001D0000 special states (2.5)
* K: %CPU = CPU usage 0x00100000 PF_USEDFPU (thru 2.4)
* N: %MEM = Memory usage (RES)
* M: TIME+ = CPU Time, hundredths
b: PPID = Parent Process Pid
c: RUSER = Real user name
d: UID = User Id
f: GROUP = Group Name
g: TTY = Controlling Tty
j: #C = Last used cpu (SMP)
p: SWAP = Swapped size (kb)
l: TIME = CPU Time
r: CODE = Code size (kb)
s: DATA = Data+Stack size (kb)
u: nFLT = Page Fault count
v: nDRT = Dirty Pages count
y: WCHAN = Sleeping in Function
z: Flags = Task Flags
* X: COMMAND = Command name/line

Flags field:
0x00000001 PF_ALIGNWARN
0x00000002 PF_STARTING
0x00000004 PF_EXITING
0x00000040 PF_FORKNOEXEC

As you read the alpha codes following "Current Fields" in the display above, you'll notice several things. There are 26 possible columns, one for each letter of the alphabet. Some of them are capitalized, some are not. As you read down the list of explanations of the codes, some of them are preceded by an asterisk, some are not. The caps and the asterisks match up one for one. If a field letter is capitalized, it means that that will be displayed in the Task Section, screen width permitting. Also note that the field codes are not in strict alphabetic order. They appear on this screen in the order they appear in the Task Section.

If you pressed "f" to display this screen, you can toggle field selections simply by pressing the letter associated with it. The letter in question will cycle between upper and lower case as you do. If you pressed "o" instead, you can control the order in which a field is displayed. Pressing a capital letter moves that field to the left, meaning it is displayed before those fields to its right. Pressing the lower case of that same letter moves it to the right.

As usual, we've just scratched the surface here. If you're interested in what else top can do -- and it can do a lot -- I refer you again to its man pages. In closing, I'll show you one more thing from the "Stupid Tricks" section. As it noted there, this will show you all the stuff Linux is doing for you beneath the covers, as it happens.

As root, open a terminal in full-screen mode. Then enter this at the command line:


nice -n -10 top -d.09

There you go. Now sit back and watch the top screen dance.

Click Here!