|
| 1 | +# QEMU Trace Profiling |
| 2 | + |
| 3 | +This directory contains a QEMU TCG plugin used to profile the Linmo tracing hot path without modifying kernel behavior. |
| 4 | + |
| 5 | +## Scope |
| 6 | + |
| 7 | +The current plugin targets `debug_trace_event()` and reports: |
| 8 | + |
| 9 | +- `insn_count` |
| 10 | +- `store_count` |
| 11 | + |
| 12 | +These numbers are intended for QEMU-based relative analysis only. They are not hardware-cycle measurements. |
| 13 | + |
| 14 | +## Files |
| 15 | + |
| 16 | +- `debug_trace_profiler.c` |
| 17 | + - QEMU TCG plugin that counts guest instructions and stores within a target PC range |
| 18 | +- `Makefile` |
| 19 | + - builds `debug_trace_profiler.so` |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +- QEMU system emulator with plugin support |
| 24 | +- `qemu-plugin.h` |
| 25 | +- `cc` |
| 26 | +- `pkg-config` |
| 27 | +- `glib-2.0` development headers visible to `pkg-config` |
| 28 | + |
| 29 | +In the current environment, the plugin header is provided through: |
| 30 | + |
| 31 | +```bash |
| 32 | +QEMU_PLUGIN_INC=/path/to/qemu/include |
| 33 | +``` |
| 34 | + |
| 35 | +## Build |
| 36 | + |
| 37 | +From the Linmo repo root, export the environment first: |
| 38 | + |
| 39 | +```bash |
| 40 | +export CROSS_COMPILE=<your-riscv-tool-prefix> |
| 41 | +export QEMU_PLUGIN_INC=/path/to/qemu/include |
| 42 | +export QEMU_BIN=$(command -v qemu-system-riscv32) |
| 43 | +export TRACE_PLUGIN_SO="$(pwd)/tools/qemu/debug_trace_profiler.so" |
| 44 | +``` |
| 45 | + |
| 46 | +Then build the plugin: |
| 47 | + |
| 48 | +```bash |
| 49 | +make -C tools/qemu clean |
| 50 | +make -C tools/qemu QEMU_PLUGIN_INC="$QEMU_PLUGIN_INC" |
| 51 | +``` |
| 52 | + |
| 53 | +This produces: |
| 54 | + |
| 55 | +```text |
| 56 | +tools/qemu/debug_trace_profiler.so |
| 57 | +``` |
| 58 | + |
| 59 | +## Reproduction |
| 60 | + |
| 61 | +### 1. Build the target workload |
| 62 | + |
| 63 | +For the bounded scheduler-tracing workload: |
| 64 | + |
| 65 | +```bash |
| 66 | +make trace_sched CROSS_COMPILE="$CROSS_COMPILE" |
| 67 | +``` |
| 68 | + |
| 69 | +If your toolchain binaries are not already on `PATH`, export that before running the commands above. |
| 70 | + |
| 71 | +### 2. Resolve the `debug_trace_event()` address range |
| 72 | + |
| 73 | +```bash |
| 74 | +"${CROSS_COMPILE}nm" -S --defined-only build/image.elf | \ |
| 75 | + rg ' debug_trace_event$' |
| 76 | +``` |
| 77 | + |
| 78 | +Example output: |
| 79 | + |
| 80 | +```text |
| 81 | +80004a54 000000f0 T debug_trace_event |
| 82 | +``` |
| 83 | + |
| 84 | +Interpret that as: |
| 85 | + |
| 86 | +- `start = 0x80004a54` |
| 87 | +- `size = 0x000000f0` |
| 88 | +- `end = 0x80004b44` |
| 89 | + |
| 90 | +### 3. Run QEMU with `icount` and the plugin |
| 91 | + |
| 92 | +```bash |
| 93 | +"$QEMU_BIN" \ |
| 94 | + -machine virt -nographic -bios none \ |
| 95 | + -kernel build/image.elf \ |
| 96 | + -icount shift=0,align=off,sleep=off \ |
| 97 | + -plugin "$TRACE_PLUGIN_SO",start=0x80004a54,end=0x80004b44 |
| 98 | +``` |
| 99 | + |
| 100 | +Expected bounded workload output includes: |
| 101 | + |
| 102 | +```text |
| 103 | +Overall: PASS |
| 104 | +Trace totals: count=256 overwrites=1090 total_events=1346 |
| 105 | +debug_trace_profiler: start=0x80004a54 end=0x80004b44 insn_count=62795 store_count=17511 |
| 106 | +``` |
| 107 | + |
| 108 | +## Interpreting the result |
| 109 | + |
| 110 | +The plugin itself reports only: |
| 111 | + |
| 112 | +- PC range |
| 113 | +- total instruction count within that range |
| 114 | +- total store count within that range |
| 115 | + |
| 116 | +To compute per-event values, use the workload diagnostics: |
| 117 | + |
| 118 | +```text |
| 119 | +total_events = debug_trace_count() + debug_trace_overwrites() |
| 120 | +``` |
| 121 | + |
| 122 | +With the example above: |
| 123 | + |
| 124 | +- `total_events = 1346` |
| 125 | +- `instructions/event = 62795 / 1346 = 46.653` |
| 126 | +- `stores/event = 17511 / 1346 = 13.010` |
| 127 | + |
| 128 | +## Notes |
| 129 | + |
| 130 | +- `QEMU_BIN` should resolve to a QEMU binary with plugin support. |
| 131 | +- `TRACE_PLUGIN_SO` should remain an absolute path. |
| 132 | +- The plugin is intended for deterministic QEMU-based profiling with `-icount`. |
| 133 | +- This tooling characterizes the tracing hot path; it should not be used as proof of hardware-cycle timing. |
0 commit comments