Implement event tracing core
SY
4mo ago+746+746+012345678901234567890123456789-5-5-0123456789
Implement a compile-time type-checked kernel event tracing subsystem
backed by an X-macro ring buffer. Event writes are always-inlined with
zero overhead when CONFIG_DEBUG_TRACE=0.
- X-macro single-source-of-truth — trace_events.h included 5 times to
generate enums, structs, inline writers, print functions, and dispatch
table. Adding an event is one line. - Per-event always_inline writers with typed C prototypes —
compile-time type checking on every trace call, zero function-call
overhead at runtime - Per-event write cost: ~17–26 instructions on RV32I at -O2
TASK_SWITCH : 17 instructions.
TASK_CREATE : 26 instructions. - Fixed-stride ring buffer — each event occupies exactly
DEBUG_EVENT_STRUCT_SIZE (configurable, default 16 bytes), with a
union projection so only populated fields are stored - CSR-level interrupt gating — TR_SAFE_ENTER/LEAVE via csrrci/csrsi
(1 instruction per direction, vs 3 for hal_interrupt_set) - SAFE events re-enable interrupts around the write (used inside
CRITICAL_ENTER sections like TASK_CREATE), RAW events skip
gating (used where interrupts are already disabled, like
dispatch/TASK_SWITCH) - Event slot size configurable via DEBUG_EVENT_STRUCT_SIZE in
config.h, new fields added via __field in trace_events.h —
STATIC_ASSERT catches sizing errors at compile time - All tracepoints guarded by #if CONFIG_DEBUG_TRACE — when disabled,
the entire tracing subsystem compiles to nothing - All reschedule paths route through _yield() — weak-alias contract
preserved
Supersedes #79.
Summary by cubic
Add a lightweight, type-checked kernel event tracing system with a fixed-size ring buffer and instrumented task lifecycle/switch events. Tracing is off by default, compiles out when CONFIG_DEBUG_TRACE=0, snapshots the buffer to shorten interrupt-off time in debug_dump_events, and now skips re-entrant dumps to avoid snapshot corruption.
-
New Features
- X-macro tracing core with typed, always-inlined writers; single-source event registry in
include/sys/trace_events.h. - Fixed-stride ring buffer with SAFE/RAW interrupt protocols and per-event printers via a dispatch table.
- Config flags: CONFIG_DEBUG_TRACE (default 0), DEBUG_EVENT_BUFFER_SIZE (256), DEBUG_EVENT_STRUCT_SIZE (16).
- Public API: DEBUG_TRACE_EVENT(...), plus debug_dump_events, debug_clear_events, debug_trace_count, debug_trace_overwrites, debug_trace_get.
- Instrumented task.c with TASK_CREATE, TASK_DESTROY, TASK_SWITCH, TASK_DELAY, TASK_SUSPEND, TASK_RESUME, TASK_YIELD. Internal reschedules use _yield() to avoid double-tracing.
- X-macro tracing core with typed, always-inlined writers; single-source event registry in
-
Migration
- Enable by setting CONFIG_DEBUG_TRACE=1 in config.h; tune DEBUG_EVENT_BUFFER_SIZE and DEBUG_EVENT_STRUCT_SIZE if needed.
- Define new events in include/sys/trace_events.h and emit with DEBUG_TRACE_EVENT(EVENT_NAME, ...).
- Use SAFE for normal paths; use RAW when interrupts are already disabled (e.g., ISR/critical sections).
- No behavior change to public APIs; mo_task_delay/mo_task_suspend now call _yield() internally to keep one event per action.