|
| 1 | +/* |
| 2 | + * sampling_profiler.h — CPU-time sampling profiler. |
| 3 | + * |
| 4 | + * At rate `N` Hz, an ITIMER_PROF timer delivers SIGPROF to an arbitrary |
| 5 | + * running thread. The handler walks the frame-pointer chain via the |
| 6 | + * ucontext registers and appends a sample (timestamp, thread id, depth, |
| 7 | + * optional bytecode-VM pc, variable-length PC array) to a per-process |
| 8 | + * bump-allocated ring. |
| 9 | + * |
| 10 | + * Separate from src/core/profiler.cc's RangePush/RangePop instrumentation. |
| 11 | + * That profiler measures user-annotated regions; this one periodically |
| 12 | + * snapshots whatever code is running. |
| 13 | + * |
| 14 | + * See Phase 4 / Phase 5 for post-mortem symbolication and flame-graph |
| 15 | + * output — this header covers the recording side only. |
| 16 | + * Phase 4 is Symbolication |
| 17 | + * Phase 5 is collapsed-stacks aggregation - see sampling_profiler.cc |
| 18 | + */ |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | +#include <cstddef> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace core { |
| 27 | + |
| 28 | +// Per-sample header (variable-length record). A SampleHeader is followed |
| 29 | +// immediately in the ring buffer by `depth` × uint64_t native PCs. |
| 30 | +struct SampleHeader { |
| 31 | + uint64_t timestamp_ns; // CLOCK_MONOTONIC at signal delivery |
| 32 | + uint64_t vm_pc; // bytecode VM's _pc at sample time, or 0 |
| 33 | + uint32_t thread_id; // Linux tid / macOS port id (truncated) |
| 34 | + uint32_t depth; // number of trailing PCs (0 if walk failed) |
| 35 | +}; |
| 36 | + |
| 37 | +// Aggregated symbolicated sample: one entry per unique (thread_id, frames) |
| 38 | +// group. `frames` is outermost-first (index 0 is the root, last is the |
| 39 | +// leaf). `sample_count` is the number of raw samples that collapsed into |
| 40 | +// this entry. |
| 41 | +struct SymbolicatedSample { |
| 42 | + uint32_t thread_id; |
| 43 | + size_t sample_count; |
| 44 | + std::vector<std::string> frames; |
| 45 | + core::T_sp encode(); |
| 46 | +}; |
| 47 | + |
| 48 | +// Start the profiler. |
| 49 | +// rate_hz : sampling rate in Hz (e.g. 97). Clamped to [1, 10000]. |
| 50 | +// max_depth : per-sample stack-depth cap. Clamped to [1, 8192]. |
| 51 | +// buffer_bytes : ring buffer size (0 = default 256 MiB). |
| 52 | +// Returns true on success. Fails if the profiler is already running or the |
| 53 | +// OS timer/signal setup fails. |
| 54 | +bool sampling_profiler_start(unsigned rate_hz, |
| 55 | + unsigned max_depth, |
| 56 | + size_t buffer_bytes); |
| 57 | + |
| 58 | +// Stop sampling. The buffer is preserved; call |
| 59 | +// sampling_profiler_save / sampling_profiler_reset to drain / clear. |
| 60 | +void sampling_profiler_stop(); |
| 61 | + |
| 62 | +// True while a profile session is active. |
| 63 | +bool sampling_profiler_running(); |
| 64 | + |
| 65 | +// Discard all captured samples and reset the bump pointer. |
| 66 | +void sampling_profiler_reset(); |
| 67 | + |
| 68 | +// Drop the ring buffer contents to `path` as collapsed-stacks format |
| 69 | +// (one stack per line, semicolon-separated, trailing ' <count>'), ready |
| 70 | +// to feed Brendan Gregg's flamegraph.pl. Symbolicates on the fly using |
| 71 | +// the arena side table, ObjectFile lookup, bytecode-module scan, and |
| 72 | +// dladdr. Returns true on success, false on I/O error. |
| 73 | +bool sampling_profiler_save(const char* path); |
| 74 | + |
| 75 | +// Return one entry per recorded sample. Each inner vector holds the |
| 76 | +// symbolicated frame names for that sample, outermost-first (index 0 |
| 77 | +// is the root, last index is the leaf). Prints a warning and returns |
| 78 | +// an empty vector if the profiler is still running. |
| 79 | +std::vector<SymbolicatedSample> sampling_profiler_symbolicated_samples(); |
| 80 | + |
| 81 | +// Populate the calling thread's stack bounds for later frame-walking. |
| 82 | +// Must be called from a non-signal context. |
| 83 | +void sampling_profiler_register_current_thread(); |
| 84 | + |
| 85 | +// Register an executable memory range with the profiler's return-address |
| 86 | +// validator. Call this when new executable pages are allocated (JIT, arena) |
| 87 | +// so the frame-pointer walker recognizes return addresses in them. |
| 88 | +// Lock-free, safe to call from any thread while the profiler is running. |
| 89 | +void sampling_profiler_add_executable_range(uintptr_t lo, uintptr_t hi); |
| 90 | + |
| 91 | +// Diagnostics. |
| 92 | +size_t sampling_profiler_samples_recorded(); |
| 93 | +size_t sampling_profiler_samples_dropped(); |
| 94 | +size_t sampling_profiler_bytes_used(); |
| 95 | + |
| 96 | +} // namespace core |
0 commit comments