Ashwin kumar Ashwin kumar - 1 month ago 10
C Question

What is the best way to measure total context switches in a C/C++ application?

This is simple way I am trying to get the resource usage, specially total context switches that happen in the execution of the program.

#include<stdio.h>
#include <sys/resource.h>

int appgetrusage(struct rusage *);
int appgetdiffrusage(struct rusage *, struct rusage *);

int main() {
struct rusage begin, end;

appgetrusage(&begin);

/*
* core of the program goes here
* where lot of system threads are spawned and joined
*
*/

appgetrusage(&end);
appgetdiffrusage(&begin, &end);

return 0;
}


int appgetrusage(struct rusage *usage){
int who = RUSAGE_SELF;
struct timeval start, end;
getrusage(RUSAGE_SELF, usage);
return 1;
}


int appgetdiffrusage(struct rusage *oldr, struct rusage *newr){

printf("\n");
printf("user time (ms): %llu\n",1000 * ((newr->ru_utime).tv_sec - (oldr->ru_utime).tv_sec) +
((newr->ru_utime).tv_usec - (oldr->ru_utime).tv_usec) / 1000);

printf("system time (ms): %ld\n", 1000 * ((newr->ru_stime).tv_sec - (oldr->ru_stime).tv_sec) +
((newr->ru_stime).tv_usec - (oldr->ru_stime).tv_usec) / 1000);

printf("voluntary context switches : %ld\n", newr->ru_nvcsw - oldr->ru_nvcsw);
printf("involuntary context switches : %ld\n", newr->ru_nivcsw - oldr->ru_nivcsw);

return 1;
}



  1. Is this the right approach?

  2. Can anybody suggest an alternative? or Corrections? :-)


Answer

Can anybody suggest an alternative?

Use perf (https://perf.wiki.kernel.org/index.php/Tutorial#Counting_with_perf_stat):

perf stat your-program

Like this:

>perf stat ./my_test 2
Thread 139828421826304:
Thread 139828411336448:
^[[A./my_test: Terminated

 Performance counter stats for './my_test 2':

      74333.536760 task-clock                #    1.999 CPUs utilized
               627 context-switches          #    0.008 K/sec
                26 cpu-migrations            #    0.000 K/sec
               282 page-faults               #    0.004 K/sec
      182727508914 cycles                    #    2.458 GHz                     [50.00%]
   <not supported> stalled-cycles-frontend
   <not supported> stalled-cycles-backend
      121168605770 instructions              #    0.66  insns per cycle         [75.00%]
       30262379463 branches                  #  407.116 M/sec                   [74.99%]
           1635031 branch-misses             #    0.01% of all branches         [75.01%]

      37.181359478 seconds time elapsed
Comments