1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 23:48:12 +00:00

fixup! build(core): emulator valgrind support

This commit is contained in:
Martin Milata 2024-10-24 13:01:33 +02:00
parent 116aafc437
commit 59140d9ca6

View File

@ -1,28 +1,37 @@
# Profiling emulator with Valgrind # Profiling emulator with Valgrind
Sometimes it can be helpful to know which parts of your code take most of the CPU time. Sometimes, it can be helpful to know which parts of your code take most of the CPU time.
[Callgrind](https://valgrind.org/docs/manual/cl-manual.html) tool from the [Valgrind](https://valgrind.org/) [Callgrind](https://valgrind.org/docs/manual/cl-manual.html) tool from the [Valgrind](https://valgrind.org/)
instrumentation framework can generate profiling data for a run of Trezor emulator. These can then be visualized instrumentation framework can generate profiling data for a run of Trezor emulator. These can then be visualized
with [KCachegrind](https://kcachegrind.github.io/). with [KCachegrind](https://kcachegrind.github.io/).
Bear in mind that profiling the emulator is of very limited usefulness due to: Bear in mind that profiling the emulator is of very limited usefulness due to:
* different CPU architecture, * different CPU architecture,
* different drivers, * different/mocked drivers,
* & other differences from actual hardware. * & other differences from actual hardware.
Still it might be a way to get *some* insight without a [hardware debugger](../systemview/index.md) Still, it might be a way to get *some* insight without a [hardware debugger](../systemview/index.md)
and a development board. 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 ## Build
``` ```
make build_unix_frozen TREZOR_EMULATOR_DEBUGGABLE=1 ADDRESS_SANITIZER=0 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` With `PYOPT=0`, most of the execution time is spent formatting and writing logs, so it is recommended to use
(and lose DebugLink) or get rid of logging manually. `PYOPT=1` (and lose DebugLink) or get rid of logging manually.
## Run ## 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: Record profiling data on some device tests:
``` ```
./emu.py -a --debugger --valgrind -c 'sleep 10; pytest ../../tests/device_tests/ -v --other-pytest-args...' ./emu.py -a --debugger --valgrind -c 'sleep 10; pytest ../../tests/device_tests/ -v --other-pytest-args...'