Skip to content

Commit b99ba44

Browse files
committed
fix: make perf-self-profile compile on macOS
Cfg-gates linux-specific code to allow compilation on non-linux platofrms. Also fixes missing required-features for blocking_pool_tracking example and silences dead code warnings that fired without the cpu-profiling feature enabled.
1 parent 6dff7be commit b99ba44

22 files changed

Lines changed: 1121 additions & 964 deletions

dial9-tokio-telemetry/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ required-features = ["task-dump"]
9292
name = "debug_timing"
9393
required-features = ["task-dump"]
9494

95+
[[example]]
96+
name = "blocking_pool_tracking"
97+
required-features = ["cpu-profiling"]
98+
9599
[[example]]
96100
name = "blocking_sleep"
97101
required-features = ["cpu-profiling"]

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl CpuSampleSource {
3535
pub struct ThreadName(Arc<str>);
3636

3737
impl ThreadName {
38+
#[cfg(feature = "cpu-profiling")]
3839
pub fn new(name: String) -> Self {
3940
Self(name.into())
4041
}
@@ -259,6 +260,7 @@ pub enum RawEvent {
259260
target_worker: u8,
260261
},
261262
/// A CPU stack trace sample from perf_event, attributed to a worker thread.
263+
#[cfg_attr(not(feature = "cpu-profiling"), allow(dead_code))]
262264
CpuSample(Box<CpuSampleData>),
263265
}
264266

@@ -275,12 +277,12 @@ pub struct CpuSampleData {
275277
}
276278

277279
/// Get the OS thread ID (tid) of the calling thread via `gettid()`.
278-
#[cfg(target_os = "linux")]
280+
#[cfg(all(target_os = "linux", feature = "cpu-profiling"))]
279281
pub fn current_tid() -> u32 {
280282
unsafe { libc::syscall(libc::SYS_gettid) as u32 }
281283
}
282284

283-
#[cfg(not(target_os = "linux"))]
285+
#[cfg(all(not(target_os = "linux"), feature = "cpu-profiling"))]
284286
pub fn current_tid() -> u32 {
285287
// No gettid on non-Linux; use a thread-local counter as a unique ID.
286288
use std::sync::atomic::{AtomicU32, Ordering};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl EventWriter {
3737
}
3838

3939
/// Encode a RawEvent into a batch and write it through the writer.
40-
#[cfg(test)]
40+
#[cfg(all(test, feature = "cpu-profiling"))]
4141
pub(crate) fn write_raw_event(
4242
&mut self,
4343
raw: crate::telemetry::events::RawEvent,

dial9-tokio-telemetry/tests/cpu_sample_clock_alignment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! by finding the `PollStart` event whose time interval (PollStart →
1313
//! corresponding PollEnd) contains the burn window.
1414
15+
#[cfg(feature = "cpu-profiling")]
1516
mod common;
1617

1718
#[cfg(feature = "cpu-profiling")]

dial9-tokio-telemetry/tests/sched_clock_alignment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC, timestamps would drift
88
//! and events would fall outside the expected windows.
99
10+
#[cfg(feature = "cpu-profiling")]
1011
mod common;
1112

1213
#[cfg(feature = "cpu-profiling")]

dial9-tokio-telemetry/tests/sched_events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Integration test: sched event capture via per-thread perf profiling.
22
3+
#[cfg(feature = "cpu-profiling")]
34
mod common;
45

56
#[cfg(feature = "cpu-profiling")]

perf-self-profile/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ targets = ["x86_64-unknown-linux-gnu"]
1212
all-features = true
1313

1414
[dependencies]
15+
dial9-trace-format = { workspace = true }
16+
tracing = "0.1.44"
17+
18+
[target.'cfg(target_os = "linux")'.dependencies]
1519
libc = "0.2"
1620
blazesym = "0.2"
17-
dial9-trace-format = { workspace = true }
1821
perf-event-data = "0.1.8"
1922
perf-event-open-sys2 = "5.0.6"
20-
tracing = "0.1.44"

perf-self-profile/examples/test_sleep.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ fn do_sleep() {
88
}
99

1010
fn main() {
11-
unsafe { libc::prctl(libc::PR_SET_DUMPABLE, 1) };
11+
#[cfg(target_os = "linux")]
12+
unsafe {
13+
libc::prctl(libc::PR_SET_DUMPABLE, 1);
14+
}
15+
1216
let sampler = Arc::new(Mutex::new(
1317
PerfSampler::new_per_thread(SamplerConfig {
1418
frequency_hz: 1,

perf-self-profile/src/lib.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,20 @@
3838
//! ```
3939
4040
pub mod offline_symbolize;
41-
mod ring_buffer;
4241
mod sampler;
4342
mod symbolize;
43+
mod sys;
4444
pub mod tracepoint;
4545

46-
/// Upper bound of userspace virtual addresses. Addresses at or above this limit
47-
/// are kernel addresses.
48-
///
49-
/// - x86_64: canonical address hole starts at bit 47
50-
/// - aarch64: TTBR0 (user) vs TTBR1 (kernel) selected by bit 63
51-
#[cfg(target_arch = "x86_64")]
52-
pub(crate) const USER_ADDR_LIMIT: u64 = 0x0000_8000_0000_0000;
53-
#[cfg(target_arch = "aarch64")]
54-
pub(crate) const USER_ADDR_LIMIT: u64 = 0x8000_0000_0000_0000;
55-
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
56-
compile_error!("perf-self-profile: USER_ADDR_LIMIT not defined for this architecture");
57-
5846
pub use offline_symbolize::SymbolTableEntry;
59-
pub use sampler::{EventSource, PerfSampler, Sample, SamplerConfig};
47+
pub use sampler::{EventSource, Sample, SamplerConfig};
6048
pub use symbolize::{CodeInfo, MapsEntry, SymbolInfo};
6149
pub use symbolize::{parse_proc_maps, read_proc_maps};
62-
pub use symbolize::{resolve_symbol, resolve_symbol_with_maps, resolve_symbols_with_maps};
50+
51+
// Platform-dispatched re-exports
52+
pub use sys::PerfSampler;
53+
pub use sys::resolve_symbol;
54+
55+
// blazesym-dependent APIs
56+
#[cfg(target_os = "linux")]
57+
pub use sys::{resolve_symbol_with_maps, resolve_symbols_with_maps};

0 commit comments

Comments
 (0)