Build a mini NVTX-style tracer
CPU · your Macscript120 minrequiredGoal · A small Python tracing library that records nested ranges with correlation ids and exports valid Chrome trace-event JSON.
- 1Design the event model, begin and end events forming ranges, instant markers, counters, and a flow id that links events across producer and consumer threads.
- 2Implement a Tracer with a context manager and decorator that record monotonic timestamps and thread ids into an in-memory event list.
- 3Add a correlation id that propagates through a queue between two worker threads, so a unit of work can be followed end to end.
- 4Export the event list to Chrome trace-event JSON (ph B, E, X, i, C fields) and open it in Perfetto or chrome://tracing to confirm ranges nest correctly.
- 5Measure per-event overhead with time.perf_counter_ns around a million begin and end pairs, and report nanoseconds per event.
- 6Add a sampling mode that records only every Nth event and an aggregate mode that keeps counters instead of events; re-measure overhead for all three modes.
- Deliverable
- A tracer.py module, an exported trace file that renders correctly in Perfetto, and a short overhead table for the three modes.
- Check
- The trace opens without parse errors and shows nested ranges; correlation ids connect events across threads; full-tracing overhead is under 2 microseconds per event and aggregate mode is at least 10x cheaper.