Skip to content

Commit 986587a

Browse files
authored
Merge branch 'main' into release-0.3
2 parents 307e8aa + 95f2937 commit 986587a

24 files changed

Lines changed: 729 additions & 143 deletions

Cargo.lock

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dial9-tokio-telemetry/examples/blocking_pool_tracking.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ fn main() {
2929
let writer = RotatingWriter::single_file("blocking_pool_trace.bin").unwrap();
3030
let (runtime, _guard) = TracedRuntime::builder()
3131
.with_task_tracking(true)
32-
.with_cpu_profiling(CpuProfilingConfig {
33-
frequency_hz: 999,
34-
..Default::default()
35-
})
32+
.with_cpu_profiling(CpuProfilingConfig::default().frequency_hz(999))
3633
.build_and_start(builder, writer)
3734
.unwrap();
3835

dial9-tokio-telemetry/src/telemetry/cpu_profile.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ pub(crate) fn read_thread_name(tid: u32) -> Option<String> {
2121
/// Configuration for CPU profiling integration.
2222
#[derive(Debug, Clone)]
2323
pub struct CpuProfilingConfig {
24-
/// Sampling frequency in Hz. Default: 99 (low overhead).
25-
pub frequency_hz: u64,
26-
/// Which perf event source to use.
27-
pub event_source: EventSource,
28-
/// Whether to include kernel stack frames.
29-
pub include_kernel: bool,
24+
frequency_hz: u64,
25+
event_source: EventSource,
26+
include_kernel: bool,
3027
}
3128

3229
impl Default for CpuProfilingConfig {
@@ -39,6 +36,26 @@ impl Default for CpuProfilingConfig {
3936
}
4037
}
4138

39+
impl CpuProfilingConfig {
40+
/// Sampling frequency in Hz. Default: 99 (low overhead).
41+
pub fn frequency_hz(mut self, hz: u64) -> Self {
42+
self.frequency_hz = hz;
43+
self
44+
}
45+
46+
/// Which perf event source to use.
47+
pub fn event_source(mut self, source: EventSource) -> Self {
48+
self.event_source = source;
49+
self
50+
}
51+
52+
/// Whether to include kernel stack frames.
53+
pub fn include_kernel(mut self, yes: bool) -> Self {
54+
self.include_kernel = yes;
55+
self
56+
}
57+
}
58+
4259
/// Configuration for per-worker sched event capture (context switches).
4360
///
4461
/// Uses `perf_event_open` with `SwContextSwitches` in per-thread mode,
@@ -82,11 +99,12 @@ pub(crate) struct CpuProfiler {
8299

83100
impl CpuProfiler {
84101
pub(crate) fn start(config: CpuProfilingConfig) -> io::Result<Self> {
85-
let sampler = PerfSampler::start(SamplerConfig {
86-
event_source: config.event_source,
87-
sampling: SamplingMode::FrequencyHz(config.frequency_hz),
88-
include_kernel: config.include_kernel,
89-
})?;
102+
let sampler = PerfSampler::start(
103+
SamplerConfig::default()
104+
.event_source(config.event_source)
105+
.sampling(SamplingMode::FrequencyHz(config.frequency_hz))
106+
.include_kernel(config.include_kernel),
107+
)?;
90108
Ok(Self {
91109
sampler,
92110
pid: std::process::id(),
@@ -130,11 +148,12 @@ pub(crate) struct SchedProfiler {
130148

131149
impl SchedProfiler {
132150
pub(crate) fn new(config: SchedEventConfig) -> io::Result<Self> {
133-
let sampler = PerfSampler::new_per_thread(SamplerConfig {
134-
event_source: EventSource::SwContextSwitches,
135-
sampling: SamplingMode::Period(config.sampling_interval.unwrap_or(1)),
136-
include_kernel: config.include_kernel,
137-
})?;
151+
let sampler = PerfSampler::new_per_thread(
152+
SamplerConfig::default()
153+
.event_source(EventSource::SwContextSwitches)
154+
.sampling(SamplingMode::Period(config.sampling_interval.unwrap_or(1)))
155+
.include_kernel(config.include_kernel),
156+
)?;
138157
Ok(Self { sampler })
139158
}
140159

dial9-tokio-telemetry/src/telemetry/recorder/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ fn flush_once(
8282
let cpu_events_time = Instant::now();
8383
#[cfg(feature = "cpu-profiling")]
8484
{
85-
event_writer.flush_cpu(shared);
85+
if shared.enabled.load(Ordering::Relaxed) {
86+
event_writer.flush_cpu(shared);
87+
}
8688
}
8789
let cpu_flush_duration = cpu_events_time.elapsed();
8890

@@ -1095,6 +1097,13 @@ fn run_flush_loop(
10951097
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
10961098
}
10971099

1100+
// When disabled, skip all recording work (queue sampling, metadata
1101+
// merging, drain coordination, flush). The loop still wakes every
1102+
// 5ms to check for control commands and the exit signal.
1103+
if !exit && !shared.enabled.load(Ordering::Relaxed) {
1104+
continue;
1105+
}
1106+
10981107
let now = Instant::now();
10991108
if now.duration_since(last_sample) >= sample_interval {
11001109
last_sample = now;

dial9-tokio-telemetry/tests/background_symbolization.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ fn background_symbolization_produces_symbol_table_entries() {
4141
builder.worker_threads(2).enable_all();
4242

4343
let (runtime, guard) = TracedRuntime::builder()
44-
.with_cpu_profiling(CpuProfilingConfig {
45-
frequency_hz: 999,
46-
..Default::default()
47-
})
44+
.with_cpu_profiling(CpuProfilingConfig::default().frequency_hz(999))
4845
.with_trace_path(&trace_path)
4946
.with_worker_poll_interval(std::time::Duration::from_millis(50))
5047
.build_and_start(builder, writer)

dial9-tokio-telemetry/tests/cpu_sample_clock_alignment.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ fn cpu_sample_timestamps_align_with_wall_clock() {
3333
builder.worker_threads(num_workers).enable_all();
3434

3535
let (runtime, guard) = TracedRuntime::builder()
36-
.with_cpu_profiling(CpuProfilingConfig {
37-
frequency_hz: 999,
38-
..Default::default()
39-
})
36+
.with_cpu_profiling(CpuProfilingConfig::default().frequency_hz(999))
4037
.build_and_start(builder, writer)
4138
.unwrap();
4239

@@ -274,10 +271,7 @@ fn thread_name_attribution_for_external_and_blocking_threads() {
274271
.enable_all();
275272

276273
let (runtime, guard) = TracedRuntime::builder()
277-
.with_cpu_profiling(CpuProfilingConfig {
278-
frequency_hz: 999,
279-
..Default::default()
280-
})
274+
.with_cpu_profiling(CpuProfilingConfig::default().frequency_hz(999))
281275
.build_and_start(builder, writer)
282276
.unwrap();
283277

0 commit comments

Comments
 (0)