I want to profile percentage occupied by all the cores in the system and save the results in a file after intervals of around 100ms. For example, if I have 8 cores, I want to know how busy each core is. Its fine for me, if the profiler gives an aggregate, like 620%, or just gives me percentage for each separate processor, like 89% for core1, 82% for core 2 etc. Which profiler is capable of giving such statistics on Linux and how?

- 204,365
- 48
- 270
- 300

- 29,191
- 52
- 200
- 356
-
'top' can do it. http://unix.stackexchange.com/questions/146085/top-command-on-multi-core-processor, – kaylum Mar 23 '15 at 10:07
-
But I want to do it progammatically and save the results in a file after an interval of say 100 ms. – MetallicPriest Mar 23 '15 at 10:11
-
Nitpick: it isn't _profiler_ what you want, as profiler measures the execution time of the functions of a program. It's a system monitoring tool. – juhist Mar 23 '15 at 10:15
-
Ok, you didn't actually mention programmatic nor saving results to file. You may want to edit your question to clarify that. Probably should define what you mean by 'programmatic' as well (e.g. shell, C library, etc). And 'top' may still work for you. It can run in batch mode (-b) with a set interval (-d) and a set number of iterations (-n). So if running a shell script you can redirect the batch mode top output to a file. – kaylum Mar 23 '15 at 10:19
2 Answers
You can read the file /proc/stat and take the lines "cpu0", "cpu1", ... for further analysis. See http://www.linuxhowtos.org/System/procstat.htm for the explanation of the various columns.
You do need to take the readings two times to get the utilization between a time interval. The values are total amounts since system startup, not instantaneous utilizations.

- 4,210
- 16
- 33
This is one example of reading the values from /proc/stat
. It will retrieve the CPU statistics at the beginning and at the end of the program and measures the consumed utilization.
#include <stdio.h>
typedef struct {
unsigned long long user;
unsigned long long nice;
unsigned long long sys;
unsigned long long idle;
} cpu_stats;
int read_cpu_stats( cpu_stats *stats ) {
unsigned int cpu;
cpu_stats stat;
char line[1024];
FILE *f = popen( "cat /proc/stat | grep cpu[0-9]", "r" );
if( f == NULL )
return 1;
do {
if( fgets( line, sizeof(line), f ) == NULL )
break;
sscanf( line + 3, "%u %llu %llu %llu %llu\n", &cpu, &stat.user, &stat.nice, &stat.sys, &stat.idle );
stats[cpu] = stat;
} while( !feof(f) && !ferror(f) );
pclose(f);
return 0;
}
float get_util( cpu_stats stat1, cpu_stats stat2 ) {
unsigned long long util1 = stat1.user + stat1.nice + stat1.sys;
unsigned long long util2 = stat2.user + stat2.nice + stat2.sys;
return (float)(util2 - util1) / ( (util2 + stat2.idle) - (util1 + stat1.idle) );
}
/* assuming at most 4 cpus in the system */
#define N_CPUS 4
int main() {
unsigned int i;
cpu_stats stats1[N_CPUS];
cpu_stats stats2[N_CPUS];
read_cpu_stats( stats1 );
/* do something */
usleep( 5000 * 1000 );
read_cpu_stats( stats2 );
for( i = 0; i < N_CPUS; i++ )
printf( "cpu %u: %f\n", i, get_util( stats1[i], stats2[i] ) );
return 0;
}
This will basically collect the number of idle and busy cycles spent between both calls to read_cpu_stats
and calculates the ratio between busy cycles and the total number of cycles per CPU.
You can simply wrap this into a loop if you want to measure the utilization continuously.

- 1,376
- 7
- 8
-
What happens if there is an error reading the pipe? That's right! Infinite loop! (`feof` returns zero on an error) http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – William Pursell Mar 23 '15 at 10:42
-
You want `while(fgets(...) != NULL` (or possibly `do...while(!feof(f) && !ferror(f))` but the former is idiomatic. – William Pursell Mar 23 '15 at 10:44
-
@WilliamPursell Thanks for pointing this out. It was meant as a an example on how to use the CPU statistics and not as a reference to best practice `c`. Thus there are some other places in the code which are not handled as they should. – user0815 Mar 23 '15 at 10:56
-
Failing to edit the failures is an egregious error. People will see this example and use it to perpetuate bad practice. The fact that these errors are not central to the problem you are trying to address in no way excuses them. Please fix! – William Pursell Mar 23 '15 at 11:06