ARTICLE

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

C. Fast or slow update?

Before we answer this question, let's take a short look on how top really works. Strace is your friend here:

$ strace -o /tmp/trace.txt top -b -n 1

Use you favourite text editor and load /tmp/trace.txt. What do you think? A lot of jobs for single invocation, that is what I think and maybe you'll agree. One of the jobs top must do in every iteration is opening many files and parsing their contents, as shown by the number:

$ grep open\( /tmp/hasil.txt | wc -l

Just for illustration, in my Linux system, it yields 304. Closer look reveals that top iterates inside /proc directory to gather processes information. /proc itself is pseudo filesystem, meaning it doesn't exist on real disk but is created on the fly by the Linux kernel and live on RAM. Within directory such as /proc/2097 (2097 is a PID), Linux kernel exports information about related process and this is where top gathers processes information along with resource usage.

Also try these:

$ time top -b -n 1

This will give you illustration how fast top works on single round. In my system, this yields around 0.5-0.6 seconds. Look at "real" field, not the "user" or "system" field because "real" reflects the total time top needs to work.

So, realizing this fact, it will be wise to use moderate update interval. Browsing RAM based filesystem takes time too, so be wise. As rule of thumb, 1 to 3 seconds interval is enough for most users. Use -d in command line option or press "s" inside interactive mode to set it. You can use fractional number as interval, e.g: 2.5, 4.1 and so on

When we should faster than 1 seconds?

  • You need more samples during a time. For this need, better use batch mode and redirect standart output to a file so you can analyze it better.
  • You don't mind with extra CPU load carried by top. Yes, it is small but it is still a load. If your Linux system is relatively idle, feel free to use short interval, but if not, better preserve your CPU time for more important task.
  • One way to reduce top's work is by monitoring certain PIDs only. This way, top won't need to traverse all the /proc sub-directory. How about user name filtering? It won't do any good. User name filtering brings extra work for top, thus combining it with very short interval will increase CPU load.

    Of course, whenever you need to force the update, just press Space and top will refresh the statistic right away.



    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!

    Where is info on load average?
    Written by Ali Ross on 2008-09-16 08:54:22

    I don't think this guide will help people completely new to top because you don't even touch on what the load average figures are all about, which is which is very important line of info in the tool.
    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!

    top process states
    Written by pavan on 2008-05-15 09:05:04
    can anybody tell what is "SWN" state of a process
    Discuss! Reply!

    Monitor process via command name?
    Written by viettrung on 2008-07-04 10:21:40
    Hi,

    Thank you for a useful article.

    I just want to know whether it is possible to monitor a process via its (command) name rather than its process identifier? If so, how?

    Regards,

    .viettrung.
    Discuss! Reply!

    what about threads?
    Written by Greg on 2008-08-27 18:25:54
    Great article.

    However I want help debugging a complex application I'm developing, which is a process that starts ~20 threads. I'm trying to figure out which threads are becoming CPU and memory intensive. The '-H' flag is great, and solves the problem of finding the CPU usage for each thread. However the listing shows the same memory usage for each thread - that of the whole process. Is there any way with top to list the memory for each thread? It not, is there another utility that's appropriate?
    Discuss! Reply!

    give process name instead of PID
    Written by Greg on 2008-08-27 18:34:56
    Quote:

    Hi,

    Thank you for a useful article.

    I just want to know whether it is possible to monitor a process via its (command) name rather than its process identifier? If so, how?

    Regards,

    .viettrung.






    I use a simple script to do this:

    #!/bin/bash

    top -p `ps cu | grep $1 | awk '{print $2}'`


    Notes: $1 is the command line argument to this script. $2 is the 2nd column from the output of 'ps' - which is the process ID. Add whatever top options you want to this script. They could theoretically be passed on by the script as another command-line argument but I didn't go that far.

    If these 2 lines are in an executable script called 'mytop', and you want to look at a process called "myprog" just run 'mytop myprog'

    Hope that helps. awk, sed, and grep rule. Kickin' it old school.
    Discuss! Reply!

    .
    Written by lenik on 2008-09-02 22:52:04

    > top -p `ps cu | grep $1 | awk '{print $2}'`

    I'd suggest to use top -p `pidof $1`, is is much easier to read than ps|grep|awk.

    Discuss! Reply!