ARTICLE

Using Top More Efficiently
Contributed by Mulyadi Santosa in Misc on 2006-03-28 04:05:13

For desktop users, monitoring resource usage is an important task. By doing this, we can locate system bottleneck, planning what to do to optimize our system, identifying memory leak and so on. The problem is, which software one should use and how to use it according to our need.

Among many monitoring tools that available, most people use "top" (a part of procps package). Top provide almost everything we need to monitor our system's resource usage within single shot. In this article, all the information are based on procps 3.2.5 running on top of Linux kernel 2.6.x

Here, we assume that procps package is already installed and run well in your Linux system. No previous experience with top is needed here, but if you had given it a try briefly, that would be an advantage.

Here are some challenges:

A. Interactive or batch mode?

By default, top is invoked using interactive mode. In this mode, top runs indefinitely and accepts keypress to redefine how top works. But, sometimes you need to post-process the top's output and this is hardly achieved using this mode. The solution? Use batch mode.

$ top -b

You will get output like below:

top - 15:22:45 up  4:19,  5 users,  load average: 0.00, 0.03, 0.00
Tasks:  60 total,   1 running,  59 sleeping,   0 stopped,   0 zombie
Cpu(s):  3.8% us,  2.9% sy,  0.0% ni, 89.6% id,  3.3% wa,  0.4% hi,  0.0% si
Mem:    515896k total,   495572k used,    20324k free,    13936k buffers
Swap:   909676k total,        4k used,   909672k free,   377608k cached
PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
  1 root      16   0  1544  476  404 S  0.0  0.1   0:01.35 init
  2 root      34  19     0    0    0 S  0.0  0.0   0:00.02 ksoftirqd/0
  3 root      10  -5     0    0    0 S  0.0  0.0   0:00.11 events/0

Uh, wait, it runs repeatedly, just like interactive mode does. Don't worry, limit its repetition with -n. So, if you just want single shot, type:

$ top -b -n 1

The real advantage of this mode is you can easily combine in with at or cron. Together, top can snapshot resource usage at certain time unattendedly. For example, using at, we can schedule top to run 1 minute later.

$ cat ./test.at
TERM=linux top -b -n 1 >/tmp/top-report.txt
$ at -f ./test.at now+1minutes

Careful reader might ask "why do I need to set TERM environment before invoking top when creating new at job?". The answer is, top needs this variable set but unfortunately "at" isn't retained it from the time of invocation. Simply set it like above and top will work smoothly.

B. How to monitor certain processes only?

Sometimes, we are only interested on several processes only, maybe just 4 or 5 of the whole existing processes. For example, if you want monitor process identifier (PID) 4360 and 4358, you type:

$ top -p 4360,4358
OR
$ top -p 4360 -p 4358

Seems easy, just use -p and list all the PIDs you need, each separated with comma or simply use -p multiple times coupled with the target PID.

Another possibility is just monitoring process with certain user identifier (UID). For this need, you can use -u or -U option. Assuming user "johndoe" has UID 500, you can type:

$ top -u johndoe
OR
$ top -u 500
OR
$ top -U johndoe

The conclusion is, you can either use the plain user name or the numeric UID. "-u, -U? Those two are different?" Yes. Like almost any other GNU tools, options are case sensitive. -U means top will find matching effective, real, saved and filesystem UIDs, while -u just find matching effective user id. Just for reminder, every *nix process runs using effective UID and sometimes it isn't equal with real user ID. Most likely, one is interested in effective UID as filesystem permission and operating system capability are checked against it, not real UID.

While -p is just command-line option only, both -U and -u can be used inside interactive mode. Like you guess, press 'U' or 'u' to filter the processes based on their user name. Same rule is applied, 'u' for effective UID and 'U' for real/effective/saved/filesystem user name. You will be asked to enter the user name or the numeric UID.



Article Index
Using Top More Efficiently
Fast or slow update?
Fields we need
Multi view are better than one?
Conclusion
 
Discussion(s)
Network engineer
Written by kourosh on 2008-01-28 13:32:24
Hope you don't mind but not enough examples given
Discuss! Reply!

Network Engineer
Written by Mark on 2008-02-07 10:43:18
Quote:

Hope you don't mind but not enough examples given





Congratulations on the most polite criticism ever delivered on the web. Well, at least in my travels, I've never seen a critique of an article so gently worded. Anyway, Kudos to you for your politeness.

As it turns out, I beg to differ with your critique. IMHO, the article had sufficient examples: Just enough to whet your appetite and get you reading the man page for more.
Discuss! Reply!

RE:
Written by Ritwik Malvi on 2008-02-11 17:14:16
Quote:

Quote:

Hope you don't mind but not enough examples given





Congratulations on the most polite criticism ever delivered on the web. Well, at least in my travels, I've never seen a critique of an article so gently worded. Anyway, Kudos to you for your politeness.

As it turns out, I beg to differ with your critique. IMHO, the article had sufficient examples: Just enough to whet your appetite and get you reading the man page for more.





Is there any way to monitor just the top 10 processes in Linux(like using the -n switch in Unixware)?
Discuss! Reply!

RE:
Written by Ritwik Malvi on 2008-02-11 17:15:41
Quote:

Quote:

Quote:

Hope you don't mind but not enough examples given





Congratulations on the most polite criticism ever delivered on the web. Well, at least in my travels, I've never seen a critique of an article so gently worded. Anyway, Kudos to you for your politeness.

As it turns out, I beg to differ with your critique. IMHO, the article had sufficient examples: Just enough to whet your appetite and get you reading the man page for more.





Is there any way to monitor just the top 10 processes in Linux(like using the -n switch in Unixware)?





I meant the top 10 processes consuming CPU (the top 10 processes from the top -S command)
Discuss! Reply!

Thanks!
Written by Anonymous bin Ich on 2008-04-04 03:17:25
Good article.

It would be nice if you added once sentence descriptions to all the Fields of top.

(Ok, I know that higher NICE value means lower priority, but what about higher PR value?)
Discuss! Reply!

Stop the loop?
Written by Sonya* on 2008-04-21 05:12:51
I typed top -b and it was indefintely loop as you said, but how can I stop this loop? I had to kill the putty session to quit it. Thank you.
Discuss! Reply!

Processes and Loop
Written by Jorge Chollet on 2008-04-22 18:53:02
Quote:

I typed top -b and it was indefintely loop as you said, but how can I stop this loop? I had to kill the putty session to quit it. Thank you.





@Ritwik Malvi: You can enter top in interactive mode (just type 'top' without parameters) and type 'n', then enter the number of processes you want to see.

@Sonya: Just press Ctrl+C. I use Putty myself at work.
Discuss! Reply!

a
Written by mark on 2008-05-06 19:24:16
Quote:

I typed top -b and it was indefintely loop as you said, but how can I stop this loop? I had to kill the putty session to quit it. Thank you.






I have the same problem. How do you stop top??
Discuss! Reply!

a
Written by Mark on 2008-05-06 19:26:17

I just found your reply to use control c to stop top.

That works great! Thanks!!
Discuss! Reply!