Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I found this answer on StackOverflow for calculating total CPU usage: http://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);
}
share|improve this question

closed as off-topic by Caleb, cuonglm, slm, Thomas Nyman, Ramesh Jun 14 at 13:20

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
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. –  Cerales Dec 27 '12 at 0:02
4  
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 at 9:56

2 Answers 2

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.

share|improve this answer
    
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. –  Cerales 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.

share|improve this answer

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