[英]Clock Cycles Count Variation Cortex A53 AArch64
我尝试使用以下函数为我在 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 个时钟周期。 有什么办法可以让我获得更准确的时钟周期数吗?
使用以下宏
#define mfcp(rn) ({u32 rval = 0U; \
__asm__ __volatile__(\
"mrc " rn "\n"\
: "=r" (rval)\
);\
rval;\
})
#endif
使用计数器寄存器调用 mfcp
uint64_t t1,t2;
t1 = mfcp(CNTPCT_EL0);
// your code
t2 = mfcp(CNTPCT_EL0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.