简体   繁体   中英

How to verify cycle count on ARM?

I am measuring cycle counts on Raspberry Pi 3b+ (ARM Cortex A53).

static inline uint32_t read_counter(void)
{
    uint32_t cc = 0;
    __asm__ volatile ("mrc p15, 0, %0, c9, c13, 0":"=r" (cc));  
    return cc;
}

This is what I do ( tutorial ):

uint32_t t1 = read_counter();       

volatile uint64_t n = 100000000;
while(n > 0) n--;

t1 = ccnt_read();
printf("%u\n", t1-t0);

When I execute the code (using taskset to keep the process on one CPU: taskset 0x1 my_counter ) I get at the output: 1201120230 cycles ( 1.201.120.230 ). It varies, but it is always around 1.201.000.000 .

$ time taskset 0x1./cycles

How would I verify that this is OK? How do I find out my CPU frequency? Is there a better way than using the 100000000 while loop?

Edit: sudo cat /sys/devices/system/cpu/cpu[0-3]/cpufreq/cpuinfo_cur_freq gives

1400000
1400000
1400000
1400000

This means expected value for the 1 second loop would be 1.400.000.000 cycles?

ps I still don't understand why the above while loop is supposed to be 1 second long?

After running:

$ time taskset 0x1./cycles

the output will give cycles and seconds:

Count (about) 1 second: 1201120230 cycles:

real    0m0.855s
user    0m0.854s
sys     0m0.000s

To check if the values "make sense", divide your cycles with the clock frequency

1201120230 / 1400000000 = 0.857

and compare with the time measured.

In this case it seems close, so the cycle count is OK.

Tip: since the clock frequency often varies as the CPU goes into power-save modes, it's a good idea to first set the clock to constant frequency. More info on how to do that here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM