0

I got confused while reading this question on stack overflow. The asker says:

"'top' will simply not do, as there is a delay between cpu dumps, it requires 2 dumps and several seconds, which hangs my program".

What the asker is talking about? I've always assumed that top does exactly the same thing that the accepted answer recommends. And his claim that top "hangs his program" doesn't make any sense to me, but no one called him out on it in the comments.

Typically I need to get overall CPU utilization for a large group of processes running on a single machine. Usually I just run top with a fairly long sampling rate (usually 10 seconds) while running my processes under a worst case load and determine an average load estimate. Is getting a measurement this way totally wrong?

Note: While getting my measurements I also ensure that no other unnecessary services or user space processes are running.

3
  • Your assumption is correct. Without more information from the author of the comment its hard to understand what they were actually talking about.
    – Ramhound
    Sep 26, 2013 at 13:20
  • From the OPs post, he indicated that top won't respond in less than a second, because it calculates process time, which requires multiple state readings be compared. Because its being called on the custom programs main thread, it introduced freezes in its output, even if they are only a second or two. Top is made for interactive display, not programmatic querying, for which procstat is much more appropriate. Sep 26, 2013 at 16:08
  • @FrankThomas Ah I see. Somehow I missed that he was querying programmatically. Makes sense now.
    – mshildt
    Sep 26, 2013 at 19:26

1 Answer 1

0

The poster of that comment is probably referring to this:

Description of problem: When I use a particular option to the 'top' command, specifically, -b -n1, I do not get the proper output.

When running top in regular mode, it keeps updating and the values on the Cpu(s) line changes with each update. However, when running top with the -bn1 switches (batch mode, 1 iteration), the Cpu(s) line does not change from run to run over seconds, minutes or even hours.

The 1st iteration of top -b returns the percentages since boot, we therefore need at least two iterations (-n 2) to get the current percentage. So, as long as you run top once before using it to gather your data you will be fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .