How can I get the number of instructions executed by a program?

9.8k views Asked by At

I have written and cross compiled a small c++ program, and I could run it in an ARM or a PC. Since ARM and a PC have different instruction set architectures, I wanna to compare them. Is that possible for me to get the number of executed instructions in this c++ program for both ISAs?

4

There are 4 answers

0
VAndrei On BEST ANSWER

What you need is a profiler. perf would be one easy to use. It will give you the number of instructions that executed, which is the best metric if you want to compare ISA efficiency.

Check the tutorial here.

You need to use: perf stat ./your binary

Look for instructions metric. This approach uses a register in your CPU's performance monitoring unit - PMU - that counts the number of instructions.

0
KeyC0de On

Linux: valgrind --tool=callgrind ./program 1 > /dev/null

0
Tayyar R On

Are you trying to get the number of static instructions or dynamic instructions? So, for instance, if you have the following loop (pseudocode):

for (i 0 to N):
 a[i] = b[i] + c[i]

Static instruction count will be just under 10 instructions, give or take based on your ISA, but the dynamic count would depend on N, on the branch prediction implementation and so on.

So for static count I would recommend using objdump, as per recommendations in the comments. You can find the entry and exit labels of your subroutine and count the number of instructions in between.

For dynamic instruction count, I would recommend one of two things:

  • You can simulate running that code using an instruction set simulator (there are open source ISA simulators for both ARM and x86 out there - Gem5 for instance implements both of them, there are others out there that support one or the other.
  • Your second option is to run this natively on the target system and setup performance counters in the CPU to report dynamic instruction count. You would reset before executing your code, and read it afterwards (there might be some noise here associated with calling your subroutine and exiting, but you should be able to isolate that out)

Hope this helps :)

1
srking On
objdump -dw mybinary | wc -l

On Linux and friends, this gives a good approximation of the number of instructions in an executable, library or object file. This is a static count, which is of course completely different than runtime behavior.