0

So I am working in C and I am considering opening the proc/cpuinfo file and just extracting the data. (Honestly not even sure how to do that.) But I am not sure how to calculate it. Is it just CPU MHz divided by total CPU speed?

enter image description here

So in the above case, something like 900.160MHZ -> 0.9 GHz.

Total Cpu Usage = 0.9GHz/ 3.20GHz *100?

Also does anyone know a better way to do this?

1 Answers1

0

You are looking at the wrong place for that. You are looking for cpu usage, and /proc/cpuinfo is not about that.

What you are looking for is this: https://www.idnt.net/en-GB/kb/941772

Additionally to files, you can get that from other linux commands suchas iostat, sar, etc.

There are also libraries to do that, such as Sigar.

sigar_t *sigar_cpu;
sigar_cpu_t old;
sigar_cpu_t current;

sigar_open(&sigar_cpu); sigar_cpu_get(sigar_cpu, &old);

sigar_cpu_perc_t perc;

while(1) { sigar_cpu_get(sigar_cpu, &current); sigar_cpu_perc_calculate(&old, &current, &perc);

std::cout << "CPU " << perc.combined * 100 << "%\n";
old = current;
Sleep(100);

}

sigar_close(sigar_cpu); return 0;

And so many other ways! I am dropping some here!

Cheers!