1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-15 02:48:11 +00:00
trezor-firmware/docs/core/emulator/valgrind.md
Martin Milata 9dee211c27 build(core): emulator valgrind support
[no changelog]
2024-11-27 09:50:15 +01:00

1.7 KiB

Profiling emulator with Valgrind

Sometimes, it can be helpful to know which parts of your code take most of the CPU time. Callgrind tool from the Valgrind instrumentation framework can generate profiling data for a run of Trezor emulator. These can then be visualized with KCachegrind.

Bear in mind that profiling the emulator is of very limited usefulness due to:

  • different CPU architecture,
  • different/mocked drivers,
  • & other differences from actual hardware. Still, it might be a way to get some insight without a hardware debugger and a development board.

Valgrind also currently doesn't understand MicroPython call stack so it won't help you when your code is spending a lot of time in pure python functions that don't call out to C. It might be possible to instrument trezor-core so that Valgrind is aware of MicroPython stack frames.

Build

make build_unix_frozen TREZOR_EMULATOR_DEBUGGABLE=1 ADDRESS_SANITIZER=0

With PYOPT=0, most of the execution time is spent formatting and writing logs, so it is recommended to use PYOPT=1 (and lose DebugLink) or get rid of logging manually.

Run

If you're using Nix, you can use Valgrind and KCachegrind packages from our shell.nix:

nix-shell --args devTools true --run "poetry shell"

Record profiling data on some device tests:

./emu.py -a --debugger --valgrind -c 'sleep 10; pytest ../../tests/device_tests/ -v --other-pytest-args...'

Open profiling data in KCachegrind (file suffix is different for each emulator process):

kcachegrind src/callgrind.out.$PID