Erik Rigtorp

Example on using the Linux perf profiler

In this note I provide some useful examples on how to use perf. There are lots additional good usage examples at Brendan Gregg’s website.

Identifying sources of allocations

In this example I’ll show you how to identify sources of allocations in userspace programs, in other words calls to the malloc function. The same idea can of course be adapted to find calls to any other function.

First add a trace point for malloc:

perf probe -x /lib64/libc.so.6 malloc

Run your program and record allocation events with stack trace:

perf record -e probe_libc:malloc --call-graph dwarf -- sleep 1

View the report:

perf report

In this example with sleep you should get something like this:

# Samples: 30  of event 'probe_libc:malloc'
# Event count (approx.): 30
#
# Children      Self  Trace output  
# ........  ........  ..............
#
   100.00%   100.00%  (7f1a810c4620)
            |
            ---0x55d6679efbb4
               __libc_start_main_alias_2 (inlined)
               __libc_start_call_main
               |          
               |--93.33%--0x55d6679ef6b8
               |          __GI_setlocale (inlined)
               |          |          
               |          |--50.00%--_nl_find_locale
               |          |          _nl_load_locale_from_archive
               |          |          |          
               |          |          |--40.00%--_nl_intern_locale_data
               |          |          |          __GI___libc_malloc (inlined)
               |          |          |          
               |          |          |--3.33%--_nl_normalize_codeset
               |          |          |          __GI___libc_malloc (inlined)
               |          |          |          
               |          |          |--3.33%--__GI___strdup (inlined)
               |          |          |          __GI___libc_malloc (inlined)
               |          |          |          
               |          |           --3.33%--__GI___libc_malloc (inlined)
               |          |          
               |          |--40.00%--__GI___strdup (inlined)
               |          |          __GI___libc_malloc (inlined)
               |          |          
               |           --3.33%--new_composite_name
               |                     __GI___libc_malloc (inlined)
               |          
               |--3.33%--0x55d6679ef6cf
               |          __textdomain
               |          __GI___strdup (inlined)
               |          __GI___libc_malloc (inlined)
               |          
                --3.33%--0x55d6679ef6c7
                          __bindtextdomain
                          set_binding_values.part.0
                          __GI___libc_malloc (inlined)