|
| 1 | +.. _profiling-perf: |
| 2 | + |
| 3 | + |
| 4 | +Perf |
| 5 | +#### |
| 6 | + |
| 7 | + |
| 8 | +Perf is a profiler tool, based on stack tracing. It can be used to light weight profiling |
| 9 | +almost without any code overhead. |
| 10 | + |
| 11 | +Work principe |
| 12 | +************* |
| 13 | + |
| 14 | +``perf record`` shell-command starts timer with perf tracer function. |
| 15 | +Timers are driven by inerrupts, so perf tracer function is calling during interruption. |
| 16 | +Zephyr core save return address and frame pointer in interrupt stack or ``callee_saved`` |
| 17 | +struture before caling interrupt hundler. So perf trace function makes stack traces by |
| 18 | +using return address and frame pointer. |
| 19 | + |
| 20 | +Then stackcollapse.py script convert return addresses in stack trace to function names |
| 21 | +by elf file and print them in required for FlameGraph format. |
| 22 | + |
| 23 | +Configuration |
| 24 | +************* |
| 25 | + |
| 26 | +Configure this module using the following options. |
| 27 | + |
| 28 | +* :kconfig:option:`CONFIG_PROFILING_PERF`: enable the module. This opion add |
| 29 | + perf command in shell. |
| 30 | + |
| 31 | +* :kconfig:option:`CONFIG_PROFILING_PERF_BUFFER_SIZE`: sets size of perf buffer, |
| 32 | + where samples are saved before printing. |
| 33 | + |
| 34 | +Usage |
| 35 | +***** |
| 36 | + |
| 37 | +More specific example can be found in :zephyr:code-sample:`profiling-perf`. |
| 38 | + |
| 39 | +#. Build with perf enabled and run. |
| 40 | + |
| 41 | +#. Begin perf sampling by shell-command |
| 42 | + |
| 43 | + .. code-block:: text |
| 44 | +
|
| 45 | + perf record <period> <frequency> |
| 46 | +
|
| 47 | + This command will start timer with perf sampler for *period* in seconds and |
| 48 | + with *frequency* in hertz. |
| 49 | + |
| 50 | + Wait for the completion message ``perf done!`` or ``perf buf override!`` |
| 51 | + (if perf buffer size is smaller than required). |
| 52 | + |
| 53 | +#. Print made by perf samles in terminal by shell-command |
| 54 | + |
| 55 | + .. code-block:: text |
| 56 | +
|
| 57 | + perf printbuf |
| 58 | +
|
| 59 | + Output exaple: |
| 60 | + |
| 61 | + .. code-block:: text |
| 62 | +
|
| 63 | + Perf buf length 1985 |
| 64 | + 0000000000000004 |
| 65 | + 0000000080004e58 |
| 66 | + 00000000800071ca |
| 67 | + 0000000080000964 |
| 68 | + aaaaaaaaaaaaaaaa |
| 69 | + 0000000000000002 |
| 70 | + .... |
| 71 | + 00000000800163e0 |
| 72 | +
|
| 73 | + Copy gotten output in some file, for example *perf_buf*. |
| 74 | + |
| 75 | +#. Translate perf samples by stackcollapse script to format, required by FlameGraph |
| 76 | + |
| 77 | + .. code-block:: bash |
| 78 | +
|
| 79 | + python zephyr/scripts/perf/stackcollapse.py perf_buf <build_dir>/zephyr/zephyr.elf > perf_buf.folded |
| 80 | +
|
| 81 | + And turn into .svg FlameGraph (for example *graph.svg*) |
| 82 | + |
| 83 | + .. code-block:: bash |
| 84 | +
|
| 85 | + ./FlameGraph/flamegraph.pl perf_buf.folded > graph.svg |
| 86 | +
|
| 87 | + All this step can be done by one command |
| 88 | + |
| 89 | + .. code-block:: bash |
| 90 | +
|
| 91 | + python zephyr/scripts/perf/stackcollapse.py perf_buf <build_dir>/zephyr/zephyr.elf | ./FlameGraph/flamegraph.pl > graph.svg |
0 commit comments