3
votes

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.

2
The hardware doesn't directly support automatically starting to count when execution reaches a certain address, or stuff like that, so it's not totally surprising that there isn't a way to do that; perf would have to set breakpoints or something! - Peter Cordes

2 Answers

2
votes

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.

2
votes

Since the man page specifies that trace filtering is only supported for Hardware PMU trace events, you will need to specify one of these events with -e to be able to filter your record properly.

The system I use supports Intel PT:

perf list
  intel_pt//                                         [Kernel PMU event]

You can see in this blog post a few examples of using perf with Intel PT. In my case the following command did the trick for me:

perf record --call-graph lbr -g -e "intel_pt//" --filter "filter main @ $HOME/test" -- ./test
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.047 MB perf.data ]

Note that depending on your kernel runtime configuration you may need to run this command as root.

The reason you can't filter using the cycles event is that it consists on a hardware counter in a particular area of the processor, known as Performance Monitoring Unit (PMU). This counter increments for every cycle, so you can guess how many cycles passed between two points in time by reading and subtracting the values.

Intel PT, on the other hand, is a hardware facility specifically designed to collect control flow information during the execution. This is the kind of information that allows you to know if your program is running in a particular part of your code (such as a function).