Search code examples
profilingperf

How to start and stop perf sampling


I am sampling the performance of my program with perf.

This works for me:

$ perf record -g  ./bench
...
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.023 MB perf.data (93 samples) ]
$ ls -al perf.data
-rw------- 1 bram bram 26848 Oct 25 10:22 perf.data

But now I want to start and stop the collection at specific points in my program. Yet if I do:

$ perf record -g -e cycles --filter="start render_image" ./bench
--filter option should follow a -e tracepoint option

I'm stumped, because the filter flag does follow the -e option. What's going on here?

I am also puzzled by the small size of perf.data, is it really only 93 samples collected? It ran for a few seconds.

Lastly, I am assuming that '-e cycles' is the default event?

UPDATE: As explained by Arnabjyoti Kalita, there is a special category of events call Tracepoints. They only show up in 'perf list' for me, if I run perf as root.

Too bad perf seems to be able to start/stop collection only if you trace that type of event, and not the default cpu cycles.


Solution

  • I would suggest start reading the perf record man page for filter options. After I went through the perf record --filter options with the help of the man-page I find this :-

    --filter

    Event filter. This option should follow a event selector (-e) which selects either tracepoint event(s) or a hardware trace PMU (e.g. Intel PT or CoreSight)

    Basically only if the event is either a tracepoint event or a hardware trace event (like IntelPT) will the filter option work. So let us check if the event denoted by -e cycles is a tracepoint event.

    If I do a perf list in my system, I get the below details-

    ~/linux-4.11.3/tools/perf/perf list
    List of pre-defined events (to be used in -e):
    cpu-cycles OR cycles                               [Hardware event]
    

    Clearly cycles is not a tracepoint event. It is a basic hardware event. An example of a tracepoint event could be :

    kmem:kmalloc                             [Tracepoint event]
    

    The perf.data can collect packets based on many parameters. The samples that have been collected have been done so at a particular frequency/period.

    There is perf record -F using which you can set a particular frequency to collect packets. I would suggest reading through all of the options available with perf record. So the less number of samples that have been collected could be attributed to this frequency - maybe you can try increasing the frequency and see if there is some improvement. There is also an option to set the time period of the event as well. Note that there is usually a limit to which you can increase this frequency - beyond which even the CPU will start having problems managing itself(due to interrupts and all...).

    Yes you are right when you say that -e cycles is the default event i.e. if you do not specify any event and do a perf record, perf will by default only collect cycles events.