title: Measure executing time on ARM Cortex-A8 using hardware counter
body: <p>I'm using a Exynos 3110 processor (1 GHz Single-core ARM Cortex-A8, e.g. used in the Nexus S) and try to measure execution times of particular functions. I have an Android 4.0.3 running on the Nexus S. I tried the method from </p>
  
  <p>[1] <a href="https://stackoverflow.com/questions/3247373/how-to-measure-program-execution-time-in-arm-cortex-a8-processor?answertab=oldest#tab-top">How to measure program execution time in ARM Cortex-A8 processor?</a></p>
  
  <p>I loaded the kernel module to allow reading the register values in user mode. I am using the following program to test the counter:</p>
  
  <pre><code>static inline unsigned int get_cyclecount (void)
  {
      unsigned int value;
      // Read CCNT Register
      asm volatile ("MRC p15, 0, %0, c9, c13, 0\t\n": "=r"(value));
      return value;
  }
  
  
  static inline void init_perfcounters (int do_reset, int enable_divider)
  {
      // in general enable all counters (including cycle counter)
      int value = 1;
  
      // peform reset:  
      if (do_reset)
      {
          value |= 2;     // reset all counters to zero.
          value |= 4;     // reset cycle counter to zero.
      } 
  
      if (enable_divider)
          value |= 8;     // enable "by 64" divider for CCNT.
  
      value |= 16;
  
      // program the performance-counter control-register:
      asm volatile ("MCR p15, 0, %0, c9, c12, 0\t\n" :: "r"(value));  
  
      // enable all counters:  
      asm volatile ("MCR p15, 0, %0, c9, c12, 1\t\n" :: "r"(0x8000000f));  
  
      // clear overflows:
      asm volatile ("MCR p15, 0, %0, c9, c12, 3\t\n" :: "r"(0x8000000f));
  }
  
  
  int main(int argc, char **argv)
  {
      int i = 0;
      unsigned int start = 0;
      unsigned int end = 0;
  
      printf("Hello Counter\n");
  
      init_perfcounters(1,0);
  
      for(i=0;i&lt;10;i++)
      {
          start = get_cyclecount();
          sleep(1); // sleep one second
          end = get_cyclecount();
  
          printf("%u %u %u\n", start, end, end - start);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>According to [1] the counter is incremented with each clock cycle. I switched the scaling_governor to userspace and set the CPU frequency to 1GHz to make sure that the clock frequency is not change by Android.</p>
  
  <p>If I run the program the sleeps of 1 second are executed, but the counter values are in the range of ~200e6, instead of the expected 1e9. Is there anything processor specific I am missing here? Is the clock rate of the counters different to the clock rate of the processor ?</p>