1

I started using Linux OS a while ago so, I am a beginner. I wanted to collect total CPU for a given process into a file with a given interval. For that reason I am using sar utility.

cpx@cpx-VirtualBox:~$ sar -u 1 4

Linux 4.4.0-142-generic (cpx-VirtualBox)        07/15/2019      _x86_64_        (1 CPU)

10:28:25 PM     CPU     %user       %nice        %system    %iowait      %steal     %idle
10:28:26 PM     all      7.22        0.00        2.06        0.00        0.00       90.72
10:28:27 PM     all     10.10        0.00        3.03        0.00        0.00       86.87
10:28:28 PM     all      7.14        0.00        2.04        0.00        0.00       90.82
10:28:29 PM     all      7.00        0.00        2.00        0.00        0.00       91.00

In Windows, task manager shows overall CPU usage with user and kernel usage in the graph. So, I am unaware of other parameters and in Linux, it seems there's no easy way to see the total CPU.

So, What is the Total CPU here? Should I just do: 100 - idle% value to get the total CPU Usage? Or, is it just %user + %system i.e. user time + kernel time?

2
  • why from sar? seems to much work ... why not use /proc/stat ? or from top? can you install software? "mpstat" from package "sysstat" is what was made for this question ;)
    – Rinzwind
    Jul 16, 2019 at 6:31
  • I found that proc/stat doesn't give real time CPU. Mpstat seems like sar. I am not using top because I want to log it every second so it seemed like sar has interval option to collect it.
    – cpx
    Jul 16, 2019 at 6:36

1 Answer 1

1

You need %user + %nice + %system for total CPU.

If %user gets close to 100% it might be because of you running "sar"; keep the intervals relative long.

  • %iowait is time that the processor/processors are waiting but it is only for a single CPU So useless for multi cores.
  • %steal is the percentage of time a virtual CPU waits for a real CPU
5
  • Why not do 100 - %idle? Thanks. Are %iowait and %steal not relevant?
    – cpx
    Jul 16, 2019 at 6:57
  • iowait is time that the processor/processors are waiting but it is only for a single CPU So useless for multi cores. %steal is the percentage of time a virtual CPU waits for a real CPU %idle: the percentage of time the processor is idle and is not waiting for I/O If you don't want that latter you can use %idle
    – Rinzwind
    Jul 16, 2019 at 7:12
  • I see. So, if I add %user, %nice, %system, and %idle. Will that give me a value of 100%? Or, do I need to add all the values in the row to get it?
    – cpx
    Jul 16, 2019 at 7:34
  • Yes. If you count all the lines you will get 400 so you need to divide by 4 CPUs.
    – Rinzwind
    Jul 16, 2019 at 7:58
  • I see. In my example I have only one CPU :)
    – cpx
    Jul 16, 2019 at 8:01

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.