When doing ps -r -e -o pcpu=,comm=, the % CPU displayed per process, over how long time is this value measured? Is it one second? The manual page does not seem to mention this.

And a follow-up question: Is it possible to have ps (or another standard UNIX utility) report the CPU percentage per process over a longer period of time, like 5 seconds? (Obviously, one could just gather up 5 values and calculate the average, but I'm not sure if that would even be correct ...)

PS! If it matters, I'm on macOS (which runs a BSD variant).

  • I don't think ps measures anything, it just reports the information that the kernel gives it. – Kusalananda Aug 6 '18 at 14:41
  • @Kusalananda: So this happens on quite a low level, then. I'd be interested in hearing a bit more from someone who knows the UNIX kernel and how this is calculated. At least if I'm going to calculate averages over time, I need to know if the straight-forward method will give a correct representation. – forthrin Aug 6 '18 at 14:54
  • Note that ps is not actually a standard Unix utility, so you probably want to say what Unix system you are running on if you want to know about how ps queries for the information that it shows. – Kusalananda Aug 6 '18 at 14:56

For PS the percent CPU is based on the lifetime of the process. Basically (user time + system time) / (now - process start time).

So 5% means the process has been bothering the CPU 5% of the time it has been alive.

Top does it differently, it's based on the refresh time of top. So most of the time these numbers will not match.

  • Are you sure about this...? I tried this now by starting an intense ffmpeg process which takes 100%. If I put it to the background after 10 seconds, ps drops to 0 almost immediately, whereas if it calculated an average over its lifespan like you suggest, displayed CPU% would have dropped gradually. I did: while true; do ps -e -o pcpu,comm | grep ffmpeg; sleep 1; done – forthrin Aug 7 '18 at 6:17

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.