Read performance counters on Linux periodically

Is there a way to periodically read performance counters on Linux?

Something like perf statsampling every X cycles is what I'm looking for.

Basically, I would like to be able to read the instruction counter (the number of instructions executed) every X the number of processor cycles for some program.

+5
source share
3 answers

It seems that the perf tool on Linux works by recording the event when the counters reach a certain value, rather than sampling at regular intervals.

perf record -e cycles,instructions -c 10000 10000 10000 . pid. perf.data .

- . perf script, :

ls 16040 2152149.005813: cycles:          c113a068  ([kernel.kallsyms])
ls 16040 2152149.005820: cycles:          c1576af0  ([kernel.kallsyms])
ls 16040 2152149.005827: cycles:          c10ed6aa  ([kernel.kallsyms])
ls 16040 2152149.005831: instructions:          c1104b30  ([kernel.kallsyms])
ls 16040 2152149.005835: cycles:          c11777c1  ([kernel.kallsyms])
ls 16040 2152149.005842: cycles:          c10702a8  ([kernel.kallsyms])
...

script, "" "" . , -c 10000 .

, perf stat perf record ls /. Stat 2 634 205 , 1 725 255 , script 410 189 . -c, , -, .

-F perf record, . , .

: perf stat, , pids, ctrl-c. , N , .

+5

: (Linux 3.9), perf stat -I msecs .

https://patchwork.kernel.org/patch/2004891/

$ perf stat -I 1000 -e cycles noploop 10
noploop for 10 seconds
1.000086918         2385155642 cycles                    #    0.000 GHz
2.000267937         2392279774 cycles                    #    0.000 GHz
3.000385400         2390971450 cycles                    #    0.000 GHz
4.000504408         2390996752 cycles                    #    0.000 GHz
5.000626878         2390853097 cycles                    #    0.000 GHz

http://man7.org/linux/man-pages/man1/perf-stat.1.html

-I msecs, --interval-print msecs

N (: 10 )

+4

perf stat, .

, , .

The changes I made are mainly done by the run_perf_stat function in the while (! Done) loop

Just move the lines below (! Done) {sleep (1);} inside the loop and change the dream to a nanolayer with the period of time that you want to try in

This should do the punching of the output to STDOUT (or STDERR)

If you want to keep these values, I suggest you create a 2-dimensional array of type struct stats, update it with each sample and periodically write it to a file

0
source

All Articles