Is there an equivalent instruction to rdtsc in ARM?

独自空忆成欢 提交于 2019-12-09 09:28:38

问题


For my project I must use inline assembly instructions such as rdtsc to calculate the execution time of some C/C++ instructions.

The following code seems to work on Intel but not on ARM processors:

{unsigned a, d;asm volatile("rdtsc" : "=a" (a), "=d" (d)); t0 = ((unsigned long)a) | (((unsigned long)d) << 32);}
//The C++ statement to measure its execution time
{unsigned a, d;asm volatile("rdtsc" : "=a" (a), "=d" (d)); t1 = ((unsigned long)a) | (((unsigned long)d) << 32);}
time = t1-t0;

My question is:

How to write an inline assembly code similar to the above (to calculate the execution elapsed time of an instruction) to work on ARM processors?


回答1:


You should read the PMCCNTR register of a co-processor p15 (not an actual co-processor, just an entry point for CPU functions) to obtain a cycle count. Note that it is available to an unprivileged app only if:

  1. Unprivileged PMCCNTR reads are alowed:

    Bit 0 of PMUSERENR register must be set to 1 (official docs)

  2. PMCCNTR is actually counting cycles:

    Bit 31 of PMCNTENSET register must be set to 1 (official docs)

This is a real-world example of how it`s done.



来源:https://stackoverflow.com/questions/40454157/is-there-an-equivalent-instruction-to-rdtsc-in-arm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!