title: Measuring execution time using rdtsc within kernel
body: <p>I am writing a code to measure the time consumption of a sequence of codes in kernel by loading the codes as module into the kernel. I uses common rdtsc routine to calculate the time. Interesting thing is similar routine running in user mode results in normal values, whereas the results is always 0 when running in kernel mode, no matter how many lines of codes I have added into the time_count function. The calculation I use here is a common matrix product function, and the running cycles should increase rapidly through the increasing of matrix dimension. Can anyone point out the mistakes in my code why I could not measure the cycle number in kernel?</p>
  
  <pre><code>#include &lt;linux/init.h&gt;
  #include &lt;linux/module.h&gt;
  
  int matrix_product(){
    int array1[500][500], array2[500][500], array3[500][500];
    int i, j, k, sum;
  
    for(i = 0; i &lt; 50000; i++){
      for(j = 0; j &lt; 50000; j++){
        array1[i][j] = 5*i + j;
        array2[i][j] = 5*i + j;
      }
    }
  
    for(i = 0; i &lt; 50000; i++){
      for(j = 0; j &lt; 50000; j++){
        for(k = 0; k &lt; 50000; k++)
      sum += array1[i][k]*array2[k][j];
        array3[i][j] = sum;
        sum = 0;
      }
    }
    return 0;
  }
  
  static __inline__ unsigned long long rdtsc(void)
  {
   unsigned long hi, lo;
   __asm__ __volatile__ ("xorl %%eax,%%eax\ncpuid" ::: "%rax", "%rbx", "%rcx", "%rdx");
   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
   return ((unsigned long long)lo) | (((unsigned long long)hi)&lt;&lt;32) ;
  }
  
  static int my_init(void)
  {
    unsigned long str, end, curr, best, tsc, best_curr;
    long i, t;
  
  #define time_count(codes) for(i=0; i&lt;120000; i++){str=rdtsc(); codes; end=rdtsc(); curr=end-str; if(curr&lt;best)best=curr;}
  
   best = ~0;
   time_count();
   tsc = best;
  
   best = ~0;
   time_count(matrix_product());
   best_curr = best;
   printk("&lt;0&gt;matrix product: %lu ticks\n", best_curr-tsc);
  
   return 0;
  }
  
  static void my_exit(void){
    return;
  }
  
  module_init(my_init);
  module_exit(my_exit);`
  </code></pre>
  
  <p>Any help is appreciated! Thanks.</p>