MetallicPriest
MetallicPriest

Reputation: 30735

How can you profile all cores for a multicore processor?

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?

Upvotes: 0

Views: 445

Answers (2)

user0815
user0815

Reputation: 1406

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.

Upvotes: 1

juhist
juhist

Reputation: 4314

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.

Upvotes: 1

Related Questions