0
$\begingroup$

I was trying to count the cpu_cycles from an ARM processor 4 core Cortex A78 to be exact) using the PMU registers. Now, at the beginning, I read the cpu cycles counter register, then for test just run one asm("nop") and then read the cpu cycles register back. I consistently get the result 7. At this point, if I add up to 15 more nop instructions, I still get 7 cycles. After this point, every 4 instructions gives me 1 more cycle.

Tried with the following instead of nop:

  asm volatile ("ADD %w[result], %w[in1], %w[in2]"
            : [result]"=r"(i)
            : [in1] "r" (i), [in2] "r" (j));

And get the same exact results.

I would like to understand why this is the case. Is it due to pipeline that I see this effect? Thank you in advance.

$\endgroup$

1 Answer 1

1
$\begingroup$

Two effects:

  1. instructions measuring cycles have their own effects and they can't be used for such a small code snippet. they are better used for code running for thousands of cycles
  2. Modern CPUs are OoOE and they execute multiple instructions per cycle and in arbitrary order. before trying to measure anything you have to learn how it works at all
$\endgroup$
2
  • $\begingroup$ As the OP might not be familiar with that abbreviation, "OoOE" means (or at least I interpreted it as such, and if made the statement make sense to me) "Out of Order Execution" $\endgroup$ Nov 10, 2023 at 11:36
  • $\begingroup$ yeah, and it's the first link in google $\endgroup$
    – Bulat
    Nov 11, 2023 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.