|
| 1 | +.. zephyr:code-sample:: instrumentation |
| 2 | + :name: Instrumentation |
| 3 | + |
| 4 | + Demonstrate the instrumentation subsystem tracing and profiling features. |
| 5 | + |
| 6 | +Overview |
| 7 | +******** |
| 8 | + |
| 9 | +This sample shows the instrumentation subsystem tracing and profiling |
| 10 | +features. It basically consists of two threads in a ping-pong mode, taking |
| 11 | +turns to execute loops that spend some CPU cycles. |
| 12 | + |
| 13 | +Requirements |
| 14 | +************ |
| 15 | + |
| 16 | +A Linux host and a UART console is required to run this sample. |
| 17 | + |
| 18 | +Building and Running |
| 19 | +******************** |
| 20 | + |
| 21 | +Build and flash the sample as follows, changing ``mps2/an385`` for your |
| 22 | +board. |
| 23 | + |
| 24 | +.. zephyr-app-commands:: |
| 25 | + :zephyr-app: samples/subsys/instrumentation |
| 26 | + :host-os: unix |
| 27 | + :board: mps2/an385 |
| 28 | + :goals: build flash |
| 29 | + :compact: |
| 30 | + |
| 31 | +Alternatively you can run this using QEMU: |
| 32 | + |
| 33 | +.. zephyr-app-commands:: |
| 34 | + :zephyr-app: samples/subsys/instrumentation |
| 35 | + :host-os: unix |
| 36 | + :board: mps2/an385 |
| 37 | + :goals: run |
| 38 | + :gen-args: '-DQEMU_SOCKET=y' |
| 39 | + :compact: |
| 40 | + |
| 41 | +After the sample is flashed to the target (or QEMU is running), it must be possible to |
| 42 | +collect and visualize traces and profiling info using the instrumentation CLI |
| 43 | +tool, :zephyr_file:`scripts/zaru.py`. |
| 44 | + |
| 45 | +.. note:: |
| 46 | + Please note, that this subsystem uses the ``retained_mem`` driver, hence it's necessary |
| 47 | + to add the proper devicetree overlay for the target board. See |
| 48 | + :zephyr_file:`./samples/subsys/instrumentation/boards/mps2_an385.overlay` for an example. |
| 49 | + |
| 50 | +Connect the board's UART port to the host device and |
| 51 | +run the :zephyr_file:`scripts/zaru.py` script on the host. |
| 52 | + |
| 53 | +Source the :zephyr_file:`zephyr-env.sh` file to set the ``ZEPHYR_BASE`` variable and get |
| 54 | +:zephyr_file:`scripts/zaru.py` in your PATH: |
| 55 | + |
| 56 | +.. code-block:: console |
| 57 | +
|
| 58 | + . zephyr-env.sh |
| 59 | +
|
| 60 | +Check instrumentation status: |
| 61 | + |
| 62 | +.. code-block:: console |
| 63 | +
|
| 64 | + zaru.py status |
| 65 | +
|
| 66 | +Set the tracing/profiling trigger; in this sample the function |
| 67 | +``get_sem_and_exec_function`` is the one interesting to allow the observation |
| 68 | +of context switches: |
| 69 | + |
| 70 | +.. code-block:: console |
| 71 | +
|
| 72 | + zaru.py trace -v -c get_sem_and_exec_function |
| 73 | +
|
| 74 | +Reboot target so tracing/profiling at the location is effective: |
| 75 | + |
| 76 | +.. code-block:: console |
| 77 | +
|
| 78 | + zaru.py reboot |
| 79 | +
|
| 80 | +Wait ~2 seconds so the sample finishes 2 rounds of ping-pong between ``main`` |
| 81 | +and ``thread_A``, and get the traces: |
| 82 | + |
| 83 | +.. code-block:: console |
| 84 | +
|
| 85 | + zaru.py trace -v |
| 86 | +
|
| 87 | +Get the profile: |
| 88 | + |
| 89 | +.. code-block:: console |
| 90 | +
|
| 91 | + zaru.py profile -v -n 10 |
| 92 | +
|
| 93 | +Or alternatively, export the traces to Perfetto (it's necessary |
| 94 | +to reboot because ``zaru.py trace`` dumped the buffer and it's now empty): |
| 95 | + |
| 96 | +.. code-block:: console |
| 97 | +
|
| 98 | + zaru.py reboot |
| 99 | + zaru.py trace -v --perfetto --output perfetto_zephyr.json |
| 100 | +
|
| 101 | +Then, go to http://perfetto.dev, Trace Viewer, and load ``perfetto_zephyr.json``. |
0 commit comments