|
| 1 | +//! Integration test: sched profiler should only track worker threads, not blocking pool threads. |
| 2 | +//! |
| 3 | +//! Before the fix, every thread that started (including blocking pool threads) opened a |
| 4 | +//! perf_event_open fd + 2 MB mmap ring buffer for sched event sampling. With Tokio's default |
| 5 | +//! blocking pool limit of 512 threads, this could exhaust file descriptors. |
| 6 | +//! |
| 7 | +//! Additionally, `stop_tracking_current_thread` never cleaned up fds because `open_perf_event` |
| 8 | +//! stored `tid: 0` (the "current thread" sentinel) while the cleanup searched for `gettid()`. |
| 9 | +
|
| 10 | +#![cfg(all(feature = "cpu-profiling", target_os = "linux"))] |
| 11 | + |
| 12 | +mod common; |
| 13 | + |
| 14 | +use dial9_tokio_telemetry::telemetry::TracedRuntime; |
| 15 | +use dial9_tokio_telemetry::telemetry::cpu_profile::SchedEventConfig; |
| 16 | +use std::sync::Mutex; |
| 17 | +use std::time::Duration; |
| 18 | + |
| 19 | +/// Serialize tests that inspect process-wide perf_event fd counts. |
| 20 | +/// `cargo test` runs tests in the same binary in parallel (threads), |
| 21 | +/// so concurrent tests would see each other's fds. |
| 22 | +static PERF_FD_TEST_LOCK: Mutex<()> = Mutex::new(()); |
| 23 | + |
| 24 | +/// Count open perf_event fds specifically. |
| 25 | +fn count_perf_fds() -> usize { |
| 26 | + std::fs::read_dir("/proc/self/fd") |
| 27 | + .expect("failed to read /proc/self/fd") |
| 28 | + .filter_map(|e| e.ok()) |
| 29 | + .filter(|e| { |
| 30 | + std::fs::read_link(e.path()) |
| 31 | + .map(|p| p.to_string_lossy().contains("perf_event")) |
| 32 | + .unwrap_or(false) |
| 33 | + }) |
| 34 | + .count() |
| 35 | +} |
| 36 | + |
| 37 | +/// Spawning many blocking threads should NOT cause fd count to grow proportionally. |
| 38 | +/// |
| 39 | +/// With the bug, each `spawn_blocking` call opens a perf fd that is never reclaimed. |
| 40 | +/// After the fix, only worker threads (a fixed, small number) get perf fds. |
| 41 | +#[test] |
| 42 | +fn sched_profiler_fds_bounded_with_many_blocking_threads() { |
| 43 | + let _lock = PERF_FD_TEST_LOCK.lock().unwrap(); |
| 44 | + let (writer, _events) = common::CapturingWriter::new(); |
| 45 | + |
| 46 | + let num_workers = 2; |
| 47 | + let num_blocking_tasks = 50; |
| 48 | + |
| 49 | + let mut builder = tokio::runtime::Builder::new_multi_thread(); |
| 50 | + builder.worker_threads(num_workers).enable_all(); |
| 51 | + |
| 52 | + let (runtime, guard) = TracedRuntime::builder() |
| 53 | + .with_sched_events(SchedEventConfig::default()) |
| 54 | + .build_and_start(builder, writer) |
| 55 | + .unwrap(); |
| 56 | + |
| 57 | + // Let workers start and resolve their identity. |
| 58 | + runtime.block_on(async { |
| 59 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 60 | + }); |
| 61 | + |
| 62 | + let perf_fds_before = count_perf_fds(); |
| 63 | + |
| 64 | + // Spawn many blocking tasks. Each one creates a new blocking pool thread. |
| 65 | + // Use std::thread::sleep to ensure they actually block and force new threads. |
| 66 | + runtime.block_on(async { |
| 67 | + let mut handles = Vec::new(); |
| 68 | + for _ in 0..num_blocking_tasks { |
| 69 | + handles.push(tokio::task::spawn_blocking(|| { |
| 70 | + std::thread::sleep(Duration::from_millis(50)); |
| 71 | + })); |
| 72 | + } |
| 73 | + for h in handles { |
| 74 | + h.await.unwrap(); |
| 75 | + } |
| 76 | + // Wait for threads to exit and on_thread_stop to fire. |
| 77 | + tokio::time::sleep(Duration::from_millis(500)).await; |
| 78 | + }); |
| 79 | + |
| 80 | + let perf_fds_after = count_perf_fds(); |
| 81 | + |
| 82 | + drop(runtime); |
| 83 | + drop(guard); |
| 84 | + |
| 85 | + // Only worker threads should have perf fds. Before the fix, we'd see |
| 86 | + // ~50 new perf fds (one per blocking thread). After the fix, the count |
| 87 | + // should stay at exactly num_workers. |
| 88 | + assert_eq!( |
| 89 | + perf_fds_before, perf_fds_after, |
| 90 | + "perf fd count changed from {perf_fds_before} to {perf_fds_after} after \ |
| 91 | + spawning {num_blocking_tasks} blocking tasks. \ |
| 92 | + Sched profiler is likely opening fds for blocking pool threads." |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +/// Verify that sched profiler fds are properly cleaned up when the runtime shuts down. |
| 97 | +/// |
| 98 | +/// This catches the tid=0 bug where `stop_tracking_current_thread` can never find |
| 99 | +/// the event to remove because `open_perf_event` stored tid=0 instead of the real tid. |
| 100 | +#[test] |
| 101 | +fn sched_profiler_fds_cleaned_up_on_shutdown() { |
| 102 | + let _lock = PERF_FD_TEST_LOCK.lock().unwrap(); |
| 103 | + assert_eq!(count_perf_fds(), 0, "no perf fds should exist before test"); |
| 104 | + |
| 105 | + { |
| 106 | + let (writer, _events) = common::CapturingWriter::new(); |
| 107 | + |
| 108 | + let num_workers = 4; |
| 109 | + let mut builder = tokio::runtime::Builder::new_multi_thread(); |
| 110 | + builder.worker_threads(num_workers).enable_all(); |
| 111 | + |
| 112 | + let (runtime, guard) = TracedRuntime::builder() |
| 113 | + .with_sched_events(SchedEventConfig::default()) |
| 114 | + .build_and_start(builder, writer) |
| 115 | + .unwrap(); |
| 116 | + |
| 117 | + // Do some work so workers resolve their identity. |
| 118 | + runtime.block_on(async { |
| 119 | + for _ in 0..10 { |
| 120 | + tokio::spawn(async { tokio::task::yield_now().await }) |
| 121 | + .await |
| 122 | + .unwrap(); |
| 123 | + } |
| 124 | + tokio::time::sleep(Duration::from_millis(200)).await; |
| 125 | + }); |
| 126 | + |
| 127 | + // Workers should have perf fds while running. |
| 128 | + let perf_fds_during = count_perf_fds(); |
| 129 | + assert!( |
| 130 | + perf_fds_during > 0, |
| 131 | + "expected perf fds while runtime is running, got 0" |
| 132 | + ); |
| 133 | + |
| 134 | + drop(runtime); |
| 135 | + drop(guard); |
| 136 | + } |
| 137 | + |
| 138 | + let perf_fds_after = count_perf_fds(); |
| 139 | + assert_eq!( |
| 140 | + perf_fds_after, 0, |
| 141 | + "leaked {perf_fds_after} perf_event fds after runtime shutdown. \ |
| 142 | + stop_tracking_current_thread likely failed to find events due to tid=0 bug." |
| 143 | + ); |
| 144 | +} |
0 commit comments