2

I'm familiar with how the top is supposed to work. The individual processes are relative to 1 vcpu, and the summary is relative to all vcpus.

I have a process that does a lot of sleeping, and I'm trying to understand its real impact on the server.

In top, I have:

%Cpu(s):  1.4 us,  1.8 sy,  0.0 ni, 96.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                                                 
  38772 root      20   0   62140  15480  12108 R   7.6   1.5   0:15.15 php     

I only have 1 CPU listed in /proc/cpuinfo (and in /sys/devices/system/cpu/)

If this one process is using 7.6% CPU, how can the server be 96.8% idle? This is not a one-off misreporting, the CPU usage of the process in top is consistently 2-3x the usage for the entire server as reported in top.

I'm assuming that top is pulling from /proc/stat and /proc/pid/stat

So with that in mind...

While the process is running on the server, I checked both of those virtual files:

First Check

/proc/stat

cpu  1970483 7927 2795587 10758959 2446 0 7301 631701 0 0

/proc/38772/stat

38772 (php) S 36827 38772 36827 34816 38772 1077936128 1251 0 0 0 2809 1819

Second Check

/proc/stat

cpu  1970572 7927 2795638 10763502 2446 0 7301 632255 0 0

/proc/38772/stat

38772 (php) S 36827 38772 36827 34816 38772 1077936128 1251 0 0 0 3098 1986

Doing the math...

So, for the process, I think the calculation is just this:

3098 - 2809 = 289
1986 - 1819 = 167

And for the server, it should be this:

1970572 - 1970483 = 89
1970572 - 1970483 = 51

... so it seems to me like top and /proc agree, that somehow, this process is using more CPU than the entire server. How is this possible? Which calculation should I assume is more accurate?

I'm running the latest Ubuntu 20.04 release (5.4.0-88-generic)

3
  • I guess it is related to both cpu-usage vs load vs other measurement method maybe ?
    – francois P
    Nov 25, 2021 at 19:26
  • I would increase the interval of top from the default 3 seconds to, let's say, 30 seconds - top -d 30, and see if it changes the result. I guess that on one core machine it's more tricky to measure it accurately, because all the processes - including the top - compete on the same resources, and between the time top measures the total CPU usage and the time it measures the process usage, the php process might already take some CPU cycles. Increasing the interval - even to a minute - might give a bit more accurate results.
    – aviro
    Nov 28, 2021 at 7:33
  • I would also press on 1 when top is running to see the per-CPU-stats, just to confirm the previous results are consistent with what you see on the actual CPU (even though on a single core machine it should be identical to the total, but who knows - it might give you a hint).
    – aviro
    Nov 28, 2021 at 7:38

1 Answer 1

0

If this one process is using 7.6% CPU, how can the server be 96.8% idle?

If you only have a single CPU then it can only run one thread of execution at a time. So at the time when the output is being written to the screen the only thing using CPU is the thing writing to your screen. And its using al the CPU at that time. The values you see here are averaged over time - the per-process CPU usage over a longer period than the header figures for utilization.

3
  • "The values you see here are averaged over time - the per-process CPU usage over a longer period than the header figures for utilization." Do you have a source for that? That sounds incorrect. "ps" definitely does show the usage over the life of the process, but I don't believe that is true for top for any usage shown. I believe the only "average" is in between the intervals of showing top results, for both server and process stats. Nov 25, 2021 at 19:51
  • Not an explicit source - try Google. The figure is obtained from /proc/$PID/stat - check the sums for yourself - web.archive.org/web/20130302063336/http://www.lindevdoc.org/…
    – symcbean
    Nov 25, 2021 at 19:57
  • @GoldenNewby is correct. The "average" is in between the intervals of showing top results, for both server and process stats - except for the first iterartion top shows - which are the total averages. The subsequent iterations show the averages between each sample interval.
    – aviro
    Nov 28, 2021 at 7:47

You must log in to answer this question.

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