# Calculating total CPU system usage in C [closed]

I found this answer on StackOverflow for calculating total CPU usage: https://stackoverflow.com/questions/1420426/calculating-cpu-usage-of-a-process-in-linux/4497769#4497769

But how do I calculate that as a proportion of the total available CPU cycles? I'm trying to do it without having to spawn another process - just with syscalls and files.

I thought that number would just be the `total` property of the information provided by `glibtop_cpu` (as it seems to be `user + nice + sys + idle`, but it actually changes over time.

I thought that might just be because the cycles will naturally vary from second to second, but in that case why does it report different numbers to `top`? If I spawn a new tab in Firefox and load up a page, `top`, shows Firefox using about 35% of one core for a couple of seconds - but the following code just shows a consistent 7.24%:

``````#include <stdio.h>
#include <time.h>
#include <glibtop/cpu.h>
#include <unistd.h>

float // Get CPU usge as a decimal percentage.
get_cpu(glibtop_cpu *cpustruct) {
glibtop_get_cpu(cpustruct);
return 100 - (float)cpustruct->idle / (float)cpustruct->total * 100;
}

int
main(void) {
glibtop_cpu cpustruct;
unsigned int sleepfor = 5000;
float usage;
for(;;usleep(sleepfor)) {
usage = get_cpu(&cpustruct);
printf("%.2f\n", usage);
}
exit(0);
}
``````
• I'm not following the question. The SO article you reference is computing the cpu usage of a single process as a fraction of total available jiffies. What are you trying to compute that's different? – ckhan Dec 26 '12 at 6:38
• I'm trying to compute the total usage of all processes against the total jiffies, which is what glibtop shows. What I don't understand is a) why the total jiffies changes from second to second and b) why computing that ratio shows a constant usage even when the `top` command shows some processes using more than 50% of a CPU core. – Cera Dec 27 '12 at 0:02
• This question appears to be off-topic because it is about c-programming rather than UNIX system administration. This would be on topic over on Stack Overflow. – Caleb Jun 14 '14 at 9:56

## Update 1

You can consider incorporate the `time` command into your program. The source is here. The main c file(`time.c`) is only 20kb.

## Original

May not be what you looking for, but a "lazy" way is to use the `time` command like follow

``````time firefox
``````

When firefox is closed, you get the following

``````~\$ time firefox
NOTE: child process received `Goodbye', closing down

real    3m16.070s
user    0m17.329s
sys     0m17.045s
``````

The "NOTE:" line is `firefox` output.

The `real` time is the time the program has been running.

The `user` and `sys` are cpu time used.

• I'm trying to get the total usage from all processes; preferably without forking another process, as I want to run this once a second or so. – Cera Dec 27 '12 at 0:03
• Updated with 1 more suggestion. – John Siu Dec 27 '12 at 3:34
• @Cerales - Doesn't top and sysstat already do what you want? – jordanm Jan 26 '13 at 4:27

Because `glibtop_get_cpu()` returns the total number of jiffies used in each category since the system started. What else could it do? It doesn't know how long it's been since you called it last.

But you can, and should. You have to measure how those numbers have changed from one call to the next. You cannot calculate current (as in, from the past second or half second or whatever) usage with just one call.

You could just base this on the amount of time in your sleep, although it will be more accurate, particularly under load, if you have a separate timer.