Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perf stat gives different number of instruction for every run

I ran perf analysis on the following empty program,

#include <stdio.h>
int main() {
}

After compiling and running perf stat ./a.out I got the following output saying (along with other data like number of cycles, task-clock etc):

418,869 instructions # 0.87 insns per cycle

The number of instructions changes during every 'perf' analysis on the same elf.

My actual need is to find the number of instructions in a particular function I wrote. So I will be subtracting the above number from the number of instructions in the new program.(I could count the number of line in program.s created using -S tag in gcc but I'm confused after looking at perf behaviour)

Why is the number of instructions not consistent, to be exact not same?

Update I followed the example given in man page to use perf_event_open() in C

like image 648
Vignesh Avatar asked Oct 11 '14 07:10

Vignesh


1 Answers

To measure the number of instructions executed by your function, I suggest to start and stop events counting with perf_event_open() at function's entry and exit rather than running twice your program with and without the function.

Regarding the non determinism of the number of instructions executed by your empty program, you are may be counting events in both user and kernel lands. I think that the user land count should stay the same between two runs, but for kernel part, many thing are happening behind the scene to execute this program, so I guess the non determinism comes from within what is happening in kernel code. To count only user space instructions you can use:

perf stat -e instructions:u a.out

Can you give more details about the differences ?

like image 52
Manuel Selva Avatar answered Oct 23 '22 00:10

Manuel Selva