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

4

5

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?

Gutshall answered 26/6, 2015 at 13:1 Comment(5)
Usually, you can either disassemble (objdump with GNU binutils tools) or use a program like size. Answers depend on compiler, compiler version, command line arguments, output format, etc.Carnet
you will find that same code same target same compiler will have different results based on optimization and debug and other compiler options, and then as you change versions of compilers (same code same target) even more variation, and then when you change targets it varies again. Basically you should end up with hundreds of different numbers if instructions needed for the same source code across the two targets.Nimitz
Do you mean a) size of program in instructions, or b) number of instructions executed when the program runs? For example, a little loop could have a small (a) and a large (b).Sheppard
@MikeDunlavey Actually I meant b), the number of instructions :DGutshall
Then you want the answer from @VAndreiSheppard
S
6

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.

Stogy answered 27/6, 2015 at 17:32 Comment(0)
L
4

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 :)

Lukewarm answered 26/6, 2015 at 20:12 Comment(0)
P
2
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.

Precinct answered 9/7, 2015 at 22:53 Comment(1)
This objdump based command just print the count of disassembly lines which gives an approximation of instructions in the binary, not executed instructions(it not include into account loops, branches, ...)Caucasia
R
0

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

Rob answered 21/9, 2018 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.