时钟周期计数变化 Cortex A53 AArch64

问题描述 投票:0回答:1

我尝试使用以下函数计算 ARM Cortex-A53 上函数的 cpu 时钟周期:

#include <sys/time.h>
    readticks(unsigned int *result, int enabled)
    {
      struct timeval t;
      unsigned int cc;
      unsigned int val;
      if (!enabled) {
               // program the performance-counter control-register:
             asm volatile("msr pmcr_el0, %0" : : "r" (17));
             //enable all counters
             asm volatile("msr PMCNTENSET_EL0, %0" : : "r" (0x8000000f));
            //clear the overflow 
            asm volatile("msr PMOVSCLR_EL0, %0" : : "r" (0x8000000f));
             enabled = 1;
      }
      //read the coutner value
      asm volatile("mrs %0, PMCCNTR_EL0" : "=r" (cc));
      gettimeofday(&t,(struct timezone *) 0);
      result[0] = cc;
      result[1] = t.tv_usec;
      result[2] = t.tv_sec;
    }

这是我的用户空间应用程序:

#include <stio.h>
#include <inttypes.h>
#include <time.h>

int main(){
unsigned int init[3] = {0};
unsigned int start[3] = {0};
unsigned int end[3] = {0};
unsigned int overhead = 0;

readticks(init, 0);
readticks(start, 1);
readticks(end, 1);

overhead = end[0] - start[0];
readticks(init, 0);
readticks(start, 1);
foo(); //This is my function 
readticks(end, 1);

end[0] = end[0] - start[0] - overhead;
printf("clock cycles= %d\n", end[0]);
return 0;

}

当我多次运行代码时,我得到了不同的时钟周期,并且变化相对较高(几乎 5000)。我的代码应该运行大约 4000 个时钟周期,但我有 4500 - 9500 个时钟周期。有什么方法可以让我获得更准确的时钟周期计数吗?

c arm clock cortex-a
1个回答
0
投票

使用以下宏

#define mfcp(rn)    ({u32 rval = 0U; \
             __asm__ __volatile__(\
               "mrc " rn "\n"\
               : "=r" (rval)\
             );\
             rval;\
             })

并使用计数器寄存器调用 mfcp:

uint64_t t1,t2;
t1 = mfcp(CNTPCT_EL0);
// your code
t2 = mfcp(CNTPCT_EL0);
© www.soinside.com 2019 - 2024. All rights reserved.