Finding the number of context switches

Viewed 229

In order to measure the number of context switches for a multi-thread application, I followed two methods: 1) with perf sched and 2) with the information in /proc/pid/status. The difference is quite large, though. The steps I did are:

1- Using perf command, the number of switches is 7848.

$ sudo perf stat -e sched:sched_switch,task-clock,context-switches,cpu-migrations,page-faults,cycles,instructions ./mm_double_omp 4

Using 4 threads
PID = 395944

Performance counter stats for './mm_double_omp 4':

         7,601      sched:sched_switch        #    0.044 K/sec
    173,377.19 msec task-clock                #    3.973 CPUs utilized
         7,601      context-switches          #    0.044 K/sec
             2      cpu-migrations            #    0.000 K/sec
        24,780      page-faults               #    0.143 K/sec
 164,393,781,352      cycles                    #    0.948 GHz
 69,723,515,498      instructions              #    0.42  insn per cycle

  43.636463582 seconds time elapsed

 173.244505000 seconds user
   0.123880000 seconds sys

Please note that sched:sched_switch and context-switches are the same. If I only use sched:sched_switch the number is still in the order of 7000.

2- I modified the code to copy /proc/pid/status file two times: At the beginning and finish of the program.

int main() {
   char cmdbuf[256];
   int pid_num = getpid();
   printf("PID = %d\n", pid_num);
   snprintf(cmdbuf, sizeof(cmdbuf), "sudo cp /proc/%d/status %s", pid_num, "start.txt" );
   system(cmdbuf);
   // DO
   snprintf(cmdbuf, sizeof(cmdbuf), "sudo cp /proc/%d/status %s", pid_num, "finish.txt" );
   system(cmdbuf);
   return 0;
}

After the execution I see:

$ tail -n2  start.txt
voluntary_ctxt_switches:        2
nonvoluntary_ctxt_switches:     0
$ tail -n2  finish.txt
voluntary_ctxt_switches:        5
nonvoluntary_ctxt_switches:     573

So, there are less than 600 context switches which is far less than the perf result. Questions are:

  1. Does perf code affect the measurement? If yes, then it has a large overhead.

  2. Is the meaning of context switch is the same in both methods?

  3. Which one is more reliable then?

0 Answers
Related