Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Top Individual Process Percentages

Help
Jim Deich
2007-11-20
2013-05-08
  • Jim Deich
    Jim Deich
    2007-11-20

    How are top individual process percentages computed for display?

    If you take the case of a single processor machine, there is some brief period(s) that
    any process will be at 100% while it's on the processor.  Getting from this
    brief period to a display percentages probably involves summing over a number of short
    intervals and doing the math. So .2 secs in 5 seconds is 4%.

    Possible / likely there is sampling too ..

    A help to me to know what the timebase is, If it can be varied, etc. Also
    Or other details if I'm entirely in the wrong direction here.

    I'm trying to reconcile reports from two different monitoring systems and top.

    Thanks in Advance.

    Jim

     
    • Sorry for the delay in responding.....apparently the monitoring I set up for these forums has stopped working.

      You are essentially correct.  At each time interval top collects the entire process table.  It then determines the total amount of cpu time that each process accumulated by adding the number of seconds and microseconds (or nanoseconds as appropriate).  Then it calculates a delta by subtracting the cpu time that was calculated at the previous interval.  This gives the amount of cpu time used for each process during the time interval.  These numbers are divided by the total amount of time elapsed during that interval (seconds and microseconds) to obtain a percentage.  The time interval is the time between updates, which is initially 5 seconds but can be set to any positive integer.

      With version 3.7 nearly every port of top upon startup will take two samples, one second apart, before producing its first display.  The two samples provide the delta necessary to accurately display %cpu on the first screen.

      There could be several reasons top's %cpu differs from other monitoring tools.  Top strives to provide an accurate picture of what is happening on the system right now (or within the last few seconds).  Other tools will simply extract the %cpu figure calculated and tracked by the kernel.  That number is designed for the scheduler's use and usually includes a decaying average.  As the kernel calculates %cpu an idle process might still show 50% utilization if it was consuming all cpu 30 seconds ago.  But top would show such a process as consuming 0%: top does not calculate a decaying average but instead displays only current activity.

      Another reason there might be a difference concerns systems with multiple cpus.  On Solaris top displays cpu usage as a percentage of the total amount of available cpu.  Thus a process with only one thread on a 4-cpu system can only use 25% of the available cpu power.  This interpretation of %cpu has not been applied consistently to other operating systems supported by top, but I'm working on that.

      For a more complete description on how top calculates %cpu, please read my blog entry here:
      http://lopsa.org/trackback/1495

      Finally, these comments only apply to Unixtop -- the original top.  I cannot speak for the version of top that was written solely for and distributed with linux, as I had no hand in it.