Skip to content

Commit f45d9d3

Browse files
committed
Merge branch 'pr/cpu-sched-profiling' of github.com:dial9-rs/dial9-tokio-telemetry into pr/metrics-service-updates
2 parents d2b44e8 + 66e2dd7 commit f45d9d3

2 files changed

Lines changed: 21 additions & 43 deletions

File tree

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,12 @@ impl Default for CpuProfilingConfig {
4444
/// Uses `perf_event_open` with `SwContextSwitches` in per-thread mode,
4545
/// so each worker thread gets its own perf fd via `on_thread_start`.
4646
#[derive(Debug, Clone)]
47+
#[derive(Default)]
4748
pub struct SchedEventConfig {
4849
/// Whether to include kernel stack frames.
4950
pub include_kernel: bool,
5051
}
5152

52-
impl Default for SchedEventConfig {
53-
fn default() -> Self {
54-
Self {
55-
include_kernel: false,
56-
}
57-
}
58-
}
5953

6054
/// Manages the perf sampler and converts samples to telemetry events.
6155
pub(crate) struct CpuProfiler {
@@ -109,11 +103,10 @@ impl CpuProfiler {
109103
.unwrap_or(UNKNOWN_WORKER);
110104
// Eagerly cache thread name for non-worker tids while the thread
111105
// is still alive and /proc/self/task/<tid>/comm is readable.
112-
if worker_id == UNKNOWN_WORKER && !self.tid_to_name.contains_key(&sample.tid) {
113-
if let Some(name) = read_thread_name(sample.tid) {
106+
if worker_id == UNKNOWN_WORKER && !self.tid_to_name.contains_key(&sample.tid)
107+
&& let Some(name) = read_thread_name(sample.tid) {
114108
self.tid_to_name.insert(sample.tid, name);
115109
}
116-
}
117110
events.push(TelemetryEvent::CpuSample {
118111
timestamp_nanos,
119112
worker_id,

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

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,12 @@ impl TelemetryRecorder {
467467
cpu_events = profiler.drain();
468468
// Pull eagerly-cached thread names into our intern table
469469
for event in &cpu_events {
470-
if let TelemetryEvent::CpuSample { tid, worker_id, .. } = event {
471-
if *worker_id == UNKNOWN_WORKER
470+
if let TelemetryEvent::CpuSample { tid, worker_id, .. } = event
471+
&& *worker_id == UNKNOWN_WORKER
472472
&& !self.thread_name_intern.contains_key(tid)
473-
{
474-
if let Some(name) = profiler.thread_name(*tid) {
473+
&& let Some(name) = profiler.thread_name(*tid) {
475474
self.thread_name_intern.insert(*tid, name.to_string());
476475
}
477-
}
478-
}
479476
}
480477
}
481478
{
@@ -486,8 +483,8 @@ impl TelemetryRecorder {
486483
}
487484
for event in &cpu_events {
488485
// Emit ThreadNameDef for non-worker tids before their first sample in this file
489-
if let TelemetryEvent::CpuSample { worker_id, tid, .. } = event {
490-
if *worker_id == UNKNOWN_WORKER
486+
if let TelemetryEvent::CpuSample { worker_id, tid, .. } = event
487+
&& *worker_id == UNKNOWN_WORKER
491488
&& !self.thread_name_emitted_this_file.contains(tid)
492489
{
493490
if self.writer.take_rotated() {
@@ -504,9 +501,8 @@ impl TelemetryRecorder {
504501
}
505502
self.thread_name_emitted_this_file.insert(*tid);
506503
}
507-
}
508-
if self.inline_callframe_symbols {
509-
if let TelemetryEvent::CpuSample { callchain, .. } = event {
504+
if self.inline_callframe_symbols
505+
&& let TelemetryEvent::CpuSample { callchain, .. } = event {
510506
// Check for rotation before writing defs
511507
if self.writer.take_rotated() {
512508
self.flush_state.on_rotate();
@@ -516,15 +512,15 @@ impl TelemetryRecorder {
516512
for &addr in callchain {
517513
if !self.callframe_emitted_this_file.contains(&addr) {
518514
// Symbolicate if not yet interned
519-
if !self.callframe_intern.contains_key(&addr) {
515+
self.callframe_intern.entry(addr).or_insert_with(|| {
520516
let sym = perf_self_profile::resolve_symbol(addr);
521517
let symbol = sym.name.unwrap_or_else(|| format!("{:#x}", addr));
522518
let location = sym.code_info.map(|info| match info.line {
523519
Some(line) => format!("{}:{}", info.file, line),
524520
None => info.file,
525521
});
526-
self.callframe_intern.insert(addr, (symbol, location));
527-
}
522+
(symbol, location)
523+
});
528524
let (symbol, location) = self.callframe_intern[&addr].clone();
529525
let def = TelemetryEvent::CallframeDef {
530526
address: addr,
@@ -536,7 +532,6 @@ impl TelemetryRecorder {
536532
}
537533
}
538534
}
539-
}
540535
let _ = self.writer.write_event(event);
541536
}
542537
}
@@ -641,18 +636,16 @@ impl TelemetryRecorder {
641636
let s_stop = shared.clone();
642637
builder
643638
.on_thread_start(move || {
644-
if let Ok(mut prof) = s_start.sched_profiler.lock() {
645-
if let Some(ref mut p) = *prof {
639+
if let Ok(mut prof) = s_start.sched_profiler.lock()
640+
&& let Some(ref mut p) = *prof {
646641
let _ = p.track_current_thread();
647642
}
648-
}
649643
})
650644
.on_thread_stop(move || {
651-
if let Ok(mut prof) = s_stop.sched_profiler.lock() {
652-
if let Some(ref mut p) = *prof {
645+
if let Ok(mut prof) = s_stop.sched_profiler.lock()
646+
&& let Some(ref mut p) = *prof {
653647
p.stop_tracking_current_thread();
654648
}
655-
}
656649
});
657650
}
658651

@@ -862,25 +855,17 @@ impl TracedRuntimeBuilder {
862855

863856
// Start CPU profiler if configured
864857
#[cfg(feature = "cpu-profiling")]
865-
let sampler = if let Some(config) = self.cpu_profiling_config {
866-
Some(crate::telemetry::cpu_profile::CpuProfiler::start(
858+
let sampler = self.cpu_profiling_config.map(|config| crate::telemetry::cpu_profile::CpuProfiler::start(
867859
config,
868860
start_mono_ns,
869-
))
870-
} else {
871-
None
872-
};
861+
));
873862

874863
// Start sched event profiler if configured
875864
#[cfg(feature = "cpu-profiling")]
876-
let sched = if let Some(config) = self.sched_event_config {
877-
Some(crate::telemetry::cpu_profile::SchedProfiler::new(
865+
let sched = self.sched_event_config.map(|config| crate::telemetry::cpu_profile::SchedProfiler::new(
878866
config,
879867
start_mono_ns,
880-
))
881-
} else {
882-
None
883-
};
868+
));
884869

885870
let recorder = TelemetryRecorder::install(
886871
&mut builder,

0 commit comments

Comments
 (0)