2

I would like to make the Ubuntu Linux 16.10 System Monitor % CPU utilization measurement basically agree with the ps "pcpu", % CPU utilization for an individual Firefox browser process running on a Lenovo Thinkstation desktop with quad cores.

The Ubuntu Linux 16.10 System Monitor Preferences are currently set at:

Update interval in seconds: 3.00
   Enable smooth refresh: Yes
   Alert before ending or killing processes Yes
   Divide CPU usage by CPU count : Yes

The ps output for the Firefox process is:

$ ps -eo pid,rss,c,pcpu,cputime,cmd | grep firefox
2848 726024  3 3.5 00:50:23 /usr/lib/firefox/firefox -new-window

After reading Linux : See CPU usage by a process for the last second, I learned how to view cpu usage for the last second. The top output for the Firefox process is:

$ top -b -d 1 | grep -in "firefox"
8: 2848 ratio    20   0 1980240 641516 115240 S  18.8 15.9  73:10.86 firefox

For the Firefox browser app, I get 3.5% pcpu from ps -eo pid,rss,c,pcpu,cputime,cmd while for the same Firefox browser app, the GUI Application Ubuntu System Monitor shows 5% CPU utilization. In addition, I get 15.9% CPU usage from top -b -d 1 | grep -in "firefox". When that is divided by 4 for the 4 cores used by our CPU, I get 4.0% for top's output.

How can I get the ps or top outputs and System Monitor values to agree? Should I enable the Divide CPU usage by CPU count checkbox?

  • What happens if you enable the checkbox? Do the values agree? – terdon May 13 '16 at 15:05
  • @terdon, Thank you for the nice edit. When I enable the Divide CPU usage by CPU count checkbox, Sometimes and sporadically the values agree . Other times, they do not agree. Does it have anything to do with the Update interval in seconds = 3.00 seconds which was formerly 800 seconds an hour ago? – Frank May 13 '16 at 15:09
  • Very likely, yes. The CPU use % is a very dynamic value, it changes very often. I don't use the system monitor thing, but I'm guessing the difference is because ps reports CPU% as a percentage of a single core and the GUI tool reports it as a percentage of ll available cores. – terdon May 13 '16 at 15:14
  • @terdon, Our manager asked me to find a scientific method for making the values agree. Is this possible? If so, could you please provide a few guidelines for either System Monitor or the ps command? Thank you. – Frank May 13 '16 at 15:19
  • @terdon, If you have time and the willingness, may I ask you to post an answer for the Stack Exchange community? Thank you. – Frank May 13 '16 at 15:24
6

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting.

So, when a CPU usage percentage of 50% is reported for a process, that doesn't mean that only 50% of the CPU is being used. It means that over a specific time period, the CPU spent 50% of its time working on the process. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.

  • We just realized that gnome-system-monitor and top -b -d n | grep -in "firefox" , where n = update interval, yield equivalent cpu usage percentages when comparing apples with apples and we normalize both figures for the number of cores. – Frank May 13 '16 at 16:49
  • @Frank OK, but make sure you also use -n >1. See here and my answer here. You need at least 2 iterations of top to get the right value: top -bn 2 -d 0.01. – terdon May 13 '16 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.