-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcpu_profile.rs
More file actions
181 lines (162 loc) · 5.75 KB
/
cpu_profile.rs
File metadata and controls
181 lines (162 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! CPU profiling integration: merges perf stack traces into the telemetry stream.
//!
//! When enabled, a process-wide `PerfSampler` captures CPU stack traces at a
//! configurable frequency. The flush thread drains raw samples; the caller
//! (EventWriter) maps OS thread IDs to worker IDs via SharedState.thread_roles.
use crate::telemetry::events::{CpuSampleSource, ThreadName};
use dial9_perf_self_profile::{EventSource, PerfSampler, SamplerConfig, SamplingMode};
use std::collections::HashMap;
use std::io;
/// Read the thread name from `/proc/self/task/<tid>/comm`.
/// Returns `None` if the file can't be read.
pub(crate) fn read_thread_name(tid: u32) -> Option<String> {
std::fs::read_to_string(format!("/proc/self/task/{tid}/comm"))
.ok()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
}
/// Configuration for CPU profiling integration.
#[derive(Debug, Clone)]
pub struct CpuProfilingConfig {
frequency_hz: u64,
event_source: EventSource,
include_kernel: bool,
}
impl Default for CpuProfilingConfig {
fn default() -> Self {
Self {
frequency_hz: 99,
event_source: EventSource::SwCpuClock,
include_kernel: false,
}
}
}
impl CpuProfilingConfig {
/// Sampling frequency in Hz. Default: 99 (low overhead).
pub fn frequency_hz(mut self, hz: u64) -> Self {
self.frequency_hz = hz;
self
}
/// Which perf event source to use.
pub fn event_source(mut self, source: EventSource) -> Self {
self.event_source = source;
self
}
/// Whether to include kernel stack frames.
pub fn include_kernel(mut self, yes: bool) -> Self {
self.include_kernel = yes;
self
}
}
/// Configuration for per-worker sched event capture (context switches).
///
/// Uses `perf_event_open` with `SwContextSwitches` in per-thread mode,
/// so each worker thread gets its own perf fd on first poll/park.
#[derive(Debug, Clone, Default)]
pub struct SchedEventConfig {
sampling_interval: Option<u64>,
include_kernel: bool,
}
impl SchedEventConfig {
/// Record every Nth context switch. Default records every event.
pub fn sampling_interval(mut self, n: u64) -> Self {
self.sampling_interval = Some(n);
self
}
/// Include kernel stack frames in callchains.
pub fn include_kernel(mut self, yes: bool) -> Self {
self.include_kernel = yes;
self
}
}
/// A raw CPU sample before worker-id resolution.
pub(crate) struct RawCpuSample {
pub tid: u32,
pub timestamp_nanos: u64,
pub callchain: Vec<u64>,
pub source: CpuSampleSource,
pub cpu: Option<u32>,
}
/// Manages the process-wide perf sampler. Yields raw samples without worker IDs.
pub(crate) struct CpuProfiler {
sampler: PerfSampler,
pid: u32,
/// OS tid → thread name, eagerly cached at drain time so short-lived threads
/// are captured before they exit and `/proc/self/task/<tid>/comm` disappears.
tid_to_name: HashMap<u32, ThreadName>,
}
impl CpuProfiler {
pub(crate) fn start(config: CpuProfilingConfig) -> io::Result<Self> {
let sampler = PerfSampler::start(
SamplerConfig::default()
.event_source(config.event_source)
.sampling(SamplingMode::FrequencyHz(config.frequency_hz))
.include_kernel(config.include_kernel),
)?;
Ok(Self {
sampler,
pid: std::process::id(),
tid_to_name: HashMap::new(),
})
}
/// Drain all pending perf samples as raw (tid, callchain) tuples.
///
/// Filters out child-process samples (perf `inherit` leaks them).
/// Eagerly caches thread names for non-worker tids.
pub(crate) fn drain(&mut self, mut f: impl FnMut(RawCpuSample, Option<&ThreadName>)) {
let pid = self.pid;
self.sampler.for_each_sample(|sample| {
if sample.pid != pid {
return;
}
if !self.tid_to_name.contains_key(&sample.tid)
&& let Some(name) = read_thread_name(sample.tid)
{
self.tid_to_name.insert(sample.tid, ThreadName::new(name));
}
let thread_name = self.tid_to_name.get(&sample.tid);
f(
RawCpuSample {
tid: sample.tid,
timestamp_nanos: sample.time,
callchain: sample.callchain.clone(),
source: CpuSampleSource::CpuProfile,
cpu: sample.cpu,
},
thread_name,
);
});
}
}
/// Per-thread sched event profiler. Yields raw samples without worker IDs.
pub(crate) struct SchedProfiler {
sampler: PerfSampler,
}
impl SchedProfiler {
pub(crate) fn new(config: SchedEventConfig) -> io::Result<Self> {
let sampler = PerfSampler::new_per_thread(
SamplerConfig::default()
.event_source(EventSource::SwContextSwitches)
.sampling(SamplingMode::Period(config.sampling_interval.unwrap_or(1)))
.include_kernel(config.include_kernel),
)?;
Ok(Self { sampler })
}
pub(crate) fn track_current_thread(&mut self) -> io::Result<()> {
self.sampler.track_current_thread()
}
pub(crate) fn stop_tracking_current_thread(&mut self) {
self.sampler.stop_tracking_current_thread()
}
pub(crate) fn drain(&mut self, mut f: impl FnMut(RawCpuSample)) {
self.sampler.for_each_sample(|sample| {
f(RawCpuSample {
tid: sample.tid,
timestamp_nanos: sample.time,
callchain: sample.callchain.clone(),
source: CpuSampleSource::SchedEvent,
cpu: sample.cpu,
});
});
}
}