When I used valgrind to do the same task it gave me the answer of 97487 which is nearly equal to the previous one. But when I used perf the answer is 421,256. What would be the reason for this discrepancy among various tools?
My guess is that perf
gives you both user and kernel mode instructions (this is the default). Please try
perf stat -e instructions:u your_executable
which is supposed to count only instructions executed in user mode. More details in the perf tutorial.
To find more details I have compiled the C program into a x86 assembly and it consists about 20-30 lines of assembly instructions, But when I used objdump to disassemble the binary it was result in a 200-300 lines of assembly instructions. I was not able to figure out the reason for this difference too.
In the first case you only get the assembly instructions exclusively for your code. In the second case you get all the instructions contained in the executable. Please compile
int main() { }
and run objdump -d name_of_the_executable
. As you will see, many things happen before the main()
is executed; and after main()
has finished, a clean-up is executed.
Linux x86 Program Start Up or - How the heck do we get to main()? seems like a nice tutorial.