17

Is there a command or any other way to get the current or average CPU utilization (for a multi-processor environment) in Linux?

I am using embedded Linux in a small system. Basically, I need to determine the CPU utilization, so that if it is high, I can instead divert a new process to another controller in the system, rather than executing on the main processor, which could be busy doing a more important process.

This question is not about merely prioritizing processes, the other controller can sufficiently handle the new process, just that when the main processor is not busy, I would prefer it to do the execution.

  • You might want to use task cpusets to force the Linux scheduler to do that for you; you can limit which core(s) a task can run on. I am assuming you are using SMP here. – MarkR Sep 22 '10 at 21:57
  • Why have you repeated huseyinalb's answer and then chosen yourself? This looks like some attempt to game the reputation system. – Andrew Sep 25 '10 at 11:12
  • what reputation do I gain for answering my own question? –  Sep 25 '10 at 14:11
  • Well I assumed that you get the reputation for an accepted answer, but even if you don't you are denying it to huseyinalb, which doesn't seem very fair. – Andrew Sep 26 '10 at 17:45

5 Answers5

28

The answer to the question after much searching and tinkering:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long double a[4], b[4], loadavg;
    FILE *fp;
    char dump[50];

    for(;;)
    {
        fp = fopen("/proc/stat","r");
        fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3]);
        fclose(fp);
        sleep(1);

        fp = fopen("/proc/stat","r");
        fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3]);
        fclose(fp);

        loadavg = ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
        printf("The current CPU utilization is : %Lf\n",loadavg);
    }

    return(0);
}

I am getting the same values as those reported by the System Monitor.

David Guyon
  • 2,759
  • 1
  • 28
  • 40
  • No, j0h, this is a procfs file. Those aren't a normal filesystem. PS: Sorry to necro, but I had to answer. – ArsenArsen Mar 27 '17 at 19:21
  • Why long double and not long int? – ocirocir Jul 28 '17 at 10:05
  • 3
    There is a recent explanation on [Gathering CPU utilization from /proc/stat](https://www.idnt.net/en-GB/kb/941772) that includes an example in bash. – Jaime Oct 22 '17 at 15:49
  • why not just seek file to beginning instead reopen? – Marek R Dec 20 '18 at 16:40
  • N.B. Don't forget to `#include ` for using `sleep()`. – red0ct Aug 11 '21 at 18:19
  • You need to sum up *all* fields (except for `guest` and `guest_nice` since they are already included in user and nice, respectively, see https://unix.stackexchange.com/a/303224/20626). Currently this code only sums up `user`, `nice`, `system` and `idle`. The remaining fields `irq`, `softirq` and `steal` are missing. They must be handled individually since they are *not* already contained in system! – scai Dec 17 '21 at 08:59
11
cat /proc/stat

you will see something like this

cpu  178877 11039 58012 5027374 22025 2616 1298 0 0
cpu0 122532 8808 34213 2438147 10881 1050 448 0 0
cpu1 56344 2230 23799 2589227 11143 1565 850 0 0

Simply take the sums of first three numbers and divide them with sums of first four integer

The first 4 numbers are user, nice, system, and idle times

note: This gives overall average. If you want to take spontaneous average, you should take two samples and subtract them from each other before the divide.

oguzalb
  • 428
  • 4
  • 15
  • Is this in terms of % or some factor? I mean, is the maximum value possible here 100 or 1? –  Sep 22 '10 at 15:45
10

You need to sample the values in /proc/stat at two times, and calculate the average utilisation over that time. (Instantaneous utilisation doesn't make a whole lot of sense - it'll always be 100% on a single core machine, since your utilsation-measuring code is running whenever it looks).

caf
  • 233,326
  • 40
  • 323
  • 462
3

The /proc filesystem has all kinds of interesting information. Look at man proc for more information.

doron
  • 27,972
  • 12
  • 65
  • 103
1

Just use top if it is available. You can use it in a non-interactive mode:

top -n 1

If you want something specific then just grep that output. The exact details will depend on how your top command formats its output, but for example:

top -n 1 | grep 'Load'
Andrew
  • 2,943
  • 18
  • 23
  • Incorrect, that would give you average load times which is not the same as CPU Utilization. (the former being extracted from /proc/loadavg while the latter from /proc/stat) [see man proc] – tsotso May 14 '14 at 08:47
  • No, that is correct. The *example* that I gave grabs the load average, but the *method* can also pull out the CPU utilisation. On OSX and linux the formatting is slightly different, but on both systems it is the line below the load making it easy to filter out. – Andrew Jul 24 '14 at 09:41