title: Understanding Linux perf report output
body: <p>Though I can intuitively get most of the results, I'm having hard time fully understanding the output of the <code>perf report</code> command especially for what concerns the call graph, so I wrote a stupid test to solve this issue of mine once for all.</p>
  
  <h1>The stupid test</h1>
  
  <p>I compiled what follows with:</p>
  
  <pre><code>gcc -Wall -pedantic -lm perf-test.c -o perf-test
  </code></pre>
  
  <p>No aggressive optimizations to avoid inlining and such.</p>
  
  <pre><code>#include &lt;math.h&gt;
  
  #define N 10000000UL
  
  #define USELESSNESS(n)                          \
      do {                                        \
          unsigned long i;                        \
          double x = 42;                          \
          for (i = 0; i &lt; (n); i++) x = sin(x);   \
      } while (0)
  
  void baz()
  {
      USELESSNESS(N);
  }
  
  void bar()
  {
      USELESSNESS(2 * N);
      baz();
  }
  
  void foo()
  {
      USELESSNESS(3 * N);
      bar();
      baz();
  }
  
  int main()
  {
      foo();
      return 0;
  }
  </code></pre>
  
  <h1>Flat profiling</h1>
  
  <pre><code>perf record ./perf-test
  perf report
  </code></pre>
  
  <p>With these I get:</p>
  
  <pre><code>  94,44%  perf-test  libm-2.19.so       [.] __sin_sse2
     2,09%  perf-test  perf-test          [.] sin@plt
     1,24%  perf-test  perf-test          [.] foo
     0,85%  perf-test  perf-test          [.] baz
     0,83%  perf-test  perf-test          [.] bar
  </code></pre>
  
  <p>Which sounds reasonable since the heavy work is actually performed by <code>__sin_sse2</code> and <code>sin@plt</code> is probably just a wrapper, while the overhead of my functions take into account just the loop, overall: <code>3*N</code> iterations for <code>foo</code>, <code>2*N</code> for the other two.</p>
  
  <h1>Hierarchical profiling</h1>
  
  <pre><code>perf record -g ./perf-test
  perf report -G
  perf report
  </code></pre>
  
  <p>Now the overhead columns that I get are two: <code>Children</code> (the output is sorted by this one by default) and <code>Self</code> (the same overhead of the flat profile).</p>
  
  <p>Here is where I start feeling I miss something: regardless of the fact that I use <code>-G</code> or not I'm unable to explain the hierarchy in terms of "x calls y" or "y is called by x", for example:</p>
  
  <ul>
  <li><p>without <code>-G</code> ("y is called by x"):</p>
  
  <pre><code>-   94,34%    94,06%  perf-test  libm-2.19.so       [.] __sin_sse2
     - __sin_sse2
        + 43,67% foo
        + 41,45% main
        + 14,88% bar
  -   37,73%     0,00%  perf-test  perf-test          [.] main
       main
       __libc_start_main
  -   23,41%     1,35%  perf-test  perf-test          [.] foo
       foo
       main
       __libc_start_main
  -    6,43%     0,83%  perf-test  perf-test          [.] bar
       bar
       foo
       main
       __libc_start_main
  -    0,98%     0,98%  perf-test  perf-test          [.] baz
     - baz
        + 54,71% foo
        + 45,29% bar
  </code></pre>
  
  <ol>
  <li>Why <code>__sin_sse2</code> is called by <code>main</code> (indirectly?), <code>foo</code> and <code>bar</code> but not by <code>baz</code>?</li>
  <li>Why functions sometimes have a percent and a hierarchy attached (e.g., the last instance of <code>baz</code>) and sometimes not (e.g., the last instance of <code>bar</code>)?</li>
  </ol></li>
  <li><p>with <code>-G</code> ("x calls y"):</p>
  
  <pre><code>-   94,34%    94,06%  perf-test  libm-2.19.so       [.] __sin_sse2
     + __sin_sse2
     + __libc_start_main
     + main
  -   37,73%     0,00%  perf-test  perf-test          [.] main
     - main
        + 62,05% foo
        + 35,73% __sin_sse2
          2,23% sin@plt
  -   23,41%     1,35%  perf-test  perf-test          [.] foo
     - foo
        + 64,40% __sin_sse2
        + 29,18% bar
        + 3,98% sin@plt
          2,44% baz
       __libc_start_main
       main
       foo
  </code></pre>
  
  <ol start="3">
  <li>How should I interpret the first three entries under <code>__sin_sse2</code>?</li>
  <li><code>main</code> calls <code>foo</code> and that's ok, but why if it calls <code>__sin_sse2</code> and <code>sin@plt</code> (indirectly?) it does not also call <code>bar</code> and <code>baz</code>?</li>
  <li>Why do <code>__libc_start_main</code> and <code>main</code> appear under <code>foo</code>? And why <code>foo</code> appears twice?</li>
  </ol></li>
  </ul>
  
  <p>Suspect is that there are two levels of this hierarchy, in which the second actually represents the "x calls y"/"y is called by x" semantics, but I'm tired to guess so I'm asking here. And the documentation doesn't seem to help.</p>
  
  <hr>
  
  <p>Sorry for the long post but I hope that all this context may help or act as a reference for someone else too.</p>