Skip to content

Commit

Permalink
native-layer: Fix build on MacOS (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dflemstr authored Nov 18, 2024
1 parent da800f7 commit ed65b98
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions crates/layer/src/native_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use tracing_perfetto_sdk_schema::{
use tracing_perfetto_sdk_sys::ffi;
use tracing_subscriber::{fmt, layer, registry};

use crate::{debug_annotations, error, flavor, ids, init};
#[cfg(feature = "sdk")]
use crate::ffi_utils;
use crate::{debug_annotations, error, flavor, ids, init};

/// A layer to be used with `tracing-subscriber` that natively writes the
/// Perfetto trace packets in Rust code, but also polls the Perfetto SDK for
Expand Down Expand Up @@ -1003,8 +1003,8 @@ where
flush_timeout: time::Duration,
poll_timeout: time::Duration,
) -> error::Result<()> {

#[cfg(feature = "sdk")] {
#[cfg(feature = "sdk")]
{
use std::io::Write as _;

let data = ffi_utils::with_session_lock(&*self.ffi_session, |session| {
Expand All @@ -1019,7 +1019,8 @@ where
}

fn stop(&self) -> error::Result<()> {
#[cfg(feature = "sdk")] {
#[cfg(feature = "sdk")]
{
// Can't use ffi_utils::with_session_lock here because we want to take the
// session object
let mut session = self
Expand All @@ -1044,36 +1045,60 @@ where
}
}

#[cfg(not(feature="sdk"))]
#[cfg(not(feature = "sdk"))]
static HAS_BOOTTIME: sync::LazyLock<bool> = sync::LazyLock::new(|| {
nix::time::clock_gettime(nix::time::ClockId::CLOCK_BOOTTIME).is_ok()
#[cfg(any(linux_android, target_os = "emscripten", target_os = "fuchsia"))]
{
nix::time::clock_gettime(nix::time::ClockId::CLOCK_BOOTTIME).is_ok()
}
#[cfg(not(any(linux_android, target_os = "emscripten", target_os = "fuchsia")))]
{
false
}
});

#[cfg(feature="sdk")]
#[cfg(feature = "sdk")]
fn trace_time_ns() -> u64 {
ffi::trace_time_ns()
}

#[cfg(not(feature="sdk"))]
#[cfg(not(feature = "sdk"))]
fn trace_time_ns() -> u64 {
use nix::time;
if *HAS_BOOTTIME {
std::time::Duration::from(time::clock_gettime(time::ClockId::CLOCK_BOOTTIME).unwrap()).as_nanos() as u64
} else {
std::time::Duration::from(time::clock_gettime(time::ClockId::CLOCK_MONOTONIC).unwrap()).as_nanos() as u64
#[cfg(any(linux_android, target_os = "emscripten", target_os = "fuchsia"))]
{
if *HAS_BOOTTIME {
std::time::Duration::from(time::clock_gettime(time::ClockId::CLOCK_BOOTTIME).unwrap())
.as_nanos() as u64
} else {
std::time::Duration::from(time::clock_gettime(time::ClockId::CLOCK_MONOTONIC).unwrap())
.as_nanos() as u64
}
}
#[cfg(not(any(linux_android, target_os = "emscripten", target_os = "fuchsia")))]
{
std::time::Duration::from(time::clock_gettime(time::ClockId::CLOCK_MONOTONIC).unwrap())
.as_nanos() as u64
}
}

#[cfg(feature="sdk")]
#[cfg(feature = "sdk")]
fn trace_clock_id() -> u32 {
ffi::trace_clock_id()
}

#[cfg(not(feature="sdk"))]
#[cfg(not(feature = "sdk"))]
fn trace_clock_id() -> u32 {
if *HAS_BOOTTIME {
schema::BuiltinClock::Boottime as u32
} else {
#[cfg(any(linux_android, target_os = "emscripten", target_os = "fuchsia"))]
{
if *HAS_BOOTTIME {
schema::BuiltinClock::Boottime as u32
} else {
schema::BuiltinClock::Monotonic as u32
}
}
#[cfg(not(any(linux_android, target_os = "emscripten", target_os = "fuchsia")))]
{
schema::BuiltinClock::Monotonic as u32
}
}
Expand Down

0 comments on commit ed65b98

Please sign in to comment.