Skip to content

Commit

Permalink
Add rubicon 3.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 18, 2024
1 parent 6d00d7d commit 2b993f2
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 152 deletions.
1 change: 1 addition & 0 deletions tracing-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ maintenance = { status = "actively-developed" }

[dependencies]
once_cell = { version = "1.13.0", optional = true }
rubicon = "3.0.1"

[target.'cfg(tracing_unstable)'.dependencies]
valuable = { version = "0.1.0", optional = true, default-features = false }
Expand Down
20 changes: 12 additions & 8 deletions tracing-core/src/callsite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,16 @@ pub fn register(callsite: &'static dyn Callsite) {
CALLSITES.push_dyn(callsite);
}

static CALLSITES: Callsites = Callsites {
list_head: AtomicPtr::new(ptr::null_mut()),
has_locked_callsites: AtomicBool::new(false),
};
rubicon::process_local! {
static CALLSITES: Callsites = Callsites {
list_head: AtomicPtr::new(ptr::null_mut()),
has_locked_callsites: AtomicBool::new(false),
};

static DISPATCHERS: Dispatchers = Dispatchers::new();
static DISPATCHERS: Dispatchers = Dispatchers::new();

static LOCKED_CALLSITES: Lazy<Mutex<Vec<&'static dyn Callsite>>> = Lazy::new(Default::default);
static LOCKED_CALLSITES: Lazy<Mutex<Vec<&'static dyn Callsite>>> = Lazy::new(Default::default);
}

struct Callsites {
list_head: AtomicPtr<DefaultCallsite>,
Expand Down Expand Up @@ -526,8 +528,10 @@ mod dispatchers {
has_just_one: AtomicBool,
}

static LOCKED_DISPATCHERS: Lazy<RwLock<Vec<dispatcher::Registrar>>> =
Lazy::new(Default::default);
rubicon::process_local! {
static LOCKED_DISPATCHERS: Lazy<RwLock<Vec<dispatcher::Registrar>>> =
Lazy::new(Default::default);
}

pub(super) enum Rebuilder<'a> {
JustOne,
Expand Down
56 changes: 33 additions & 23 deletions tracing-core/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,30 +186,34 @@ enum Kind<T> {
}

#[cfg(feature = "std")]
thread_local! {
rubicon::thread_local! {
static CURRENT_STATE: State = State {
default: RefCell::new(None),
can_enter: Cell::new(true),
};
}

static EXISTS: AtomicBool = AtomicBool::new(false);
static GLOBAL_INIT: AtomicUsize = AtomicUsize::new(UNINITIALIZED);
rubicon::process_local! {
static EXISTS: AtomicBool = AtomicBool::new(false);
static GLOBAL_INIT: AtomicUsize = AtomicUsize::new(UNINITIALIZED);

#[cfg(feature = "std")]
static SCOPED_COUNT: AtomicUsize = AtomicUsize::new(0);
#[cfg(feature = "std")]
static SCOPED_COUNT: AtomicUsize = AtomicUsize::new(0);
}

const UNINITIALIZED: usize = 0;
const INITIALIZING: usize = 1;
const INITIALIZED: usize = 2;

static mut GLOBAL_DISPATCH: Dispatch = Dispatch {
subscriber: Kind::Global(&NO_SUBSCRIBER),
};
static NONE: Dispatch = Dispatch {
subscriber: Kind::Global(&NO_SUBSCRIBER),
};
static NO_SUBSCRIBER: NoSubscriber = NoSubscriber::new();
rubicon::process_local! {
static mut GLOBAL_DISPATCH: Dispatch = Dispatch {
subscriber: Kind::Global(&NO_SUBSCRIBER),
};
static NONE: Dispatch = Dispatch {
subscriber: Kind::Global(&NO_SUBSCRIBER),
};
static NO_SUBSCRIBER: NoSubscriber = NoSubscriber::new();
}

/// The dispatch state of a thread.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -929,15 +933,17 @@ mod test {
}

struct TestCallsite;
static TEST_CALLSITE: TestCallsite = TestCallsite;
static TEST_META: Metadata<'static> = metadata! {
name: "test",
target: module_path!(),
level: Level::DEBUG,
fields: &[],
callsite: &TEST_CALLSITE,
kind: Kind::EVENT
};
rubicon::process_local! {
static TEST_CALLSITE: TestCallsite = TestCallsite;
static TEST_META: Metadata<'static> = metadata! {
name: "test",
target: module_path!(),
level: Level::DEBUG,
fields: &[],
callsite: &TEST_CALLSITE,
kind: Kind::EVENT
};
}

impl Callsite for TestCallsite {
fn set_interest(&self, _: Interest) {}
Expand Down Expand Up @@ -966,7 +972,9 @@ mod test {
fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}

fn event(&self, _: &Event<'_>) {
static EVENTS: AtomicUsize = AtomicUsize::new(0);
rubicon::process_local! {
static EVENTS: AtomicUsize = AtomicUsize::new(0);
}
assert_eq!(
EVENTS.fetch_add(1, Ordering::Relaxed),
0,
Expand Down Expand Up @@ -1007,7 +1015,9 @@ mod test {
}

fn new_span(&self, _: &span::Attributes<'_>) -> span::Id {
static NEW_SPANS: AtomicUsize = AtomicUsize::new(0);
rubicon::process_local! {
static NEW_SPANS: AtomicUsize = AtomicUsize::new(0);
}
assert_eq!(
NEW_SPANS.fetch_add(1, Ordering::Relaxed),
0,
Expand Down
55 changes: 32 additions & 23 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,9 @@ impl fmt::Debug for dyn Value {
// We are only going to be recording the field value, so we don't
// actually care about the field name here.
struct NullCallsite;
static NULL_CALLSITE: NullCallsite = NullCallsite;
rubicon::process_local! {
static NULL_CALLSITE: NullCallsite = NullCallsite;
}
impl crate::callsite::Callsite for NullCallsite {
fn set_interest(&self, _: crate::subscriber::Interest) {
unreachable!("you somehow managed to register the null callsite?")
Expand All @@ -651,10 +653,12 @@ impl fmt::Debug for dyn Value {
}
}

static FIELD: Field = Field {
i: 0,
fields: FieldSet::new(&[], crate::identify_callsite!(&NULL_CALLSITE)),
};
rubicon::process_local! {
static FIELD: Field = Field {
i: 0,
fields: FieldSet::new(&[], crate::identify_callsite!(&NULL_CALLSITE)),
};
}

let mut res = Ok(());
self.record(&FIELD, &mut |_: &Field, val: &dyn fmt::Debug| {
Expand Down Expand Up @@ -1091,15 +1095,18 @@ mod test {

// Make sure TEST_CALLSITE_* have non-zero size, so they can't be located at the same address.
struct TestCallsite1(u8);
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1(0);
static TEST_META_1: Metadata<'static> = metadata! {
name: "field_test1",
target: module_path!(),
level: Level::INFO,
fields: &["foo", "bar", "baz"],
callsite: &TEST_CALLSITE_1,
kind: Kind::SPAN,
};

rubicon::process_local! {
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1(0);
static TEST_META_1: Metadata<'static> = metadata! {
name: "field_test1",
target: module_path!(),
level: Level::INFO,
fields: &["foo", "bar", "baz"],
callsite: &TEST_CALLSITE_1,
kind: Kind::SPAN,
};
}

impl crate::callsite::Callsite for TestCallsite1 {
fn set_interest(&self, _: crate::subscriber::Interest) {
Expand All @@ -1112,15 +1119,17 @@ mod test {
}

struct TestCallsite2(u8);
static TEST_CALLSITE_2: TestCallsite2 = TestCallsite2(0);
static TEST_META_2: Metadata<'static> = metadata! {
name: "field_test2",
target: module_path!(),
level: Level::INFO,
fields: &["foo", "bar", "baz"],
callsite: &TEST_CALLSITE_2,
kind: Kind::SPAN,
};
rubicon::process_local! {
static TEST_CALLSITE_2: TestCallsite2 = TestCallsite2(0);
static TEST_META_2: Metadata<'static> = metadata! {
name: "field_test2",
target: module_path!(),
level: Level::INFO,
fields: &["foo", "bar", "baz"],
callsite: &TEST_CALLSITE_2,
kind: Kind::SPAN,
};
}

impl crate::callsite::Callsite for TestCallsite2 {
fn set_interest(&self, _: crate::subscriber::Interest) {
Expand Down
4 changes: 3 additions & 1 deletion tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ pub struct LevelFilter(Option<Level>);
#[derive(Clone, Debug)]
pub struct ParseLevelFilterError(());

static MAX_LEVEL: AtomicUsize = AtomicUsize::new(LevelFilter::OFF_USIZE);
rubicon::process_local! {
static MAX_LEVEL: AtomicUsize = AtomicUsize::new(LevelFilter::OFF_USIZE);
}

// ===== impl Metadata =====

Expand Down
30 changes: 30 additions & 0 deletions tracing-core/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,36 @@ impl Subscriber for NoSubscriber {
fn exit(&self, _span: &span::Id) {}
}

impl Subscriber for rubicon::TrustedExtern<NoSubscriber> {
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
self.0.enabled(metadata)
}

fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
self.0.new_span(span)
}

fn record(&self, span: &span::Id, values: &span::Record<'_>) {
self.0.record(span, values)
}

fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
self.0.record_follows_from(span, follows)
}

fn event(&self, event: &Event<'_>) {
self.0.event(event)
}

fn enter(&self, span: &span::Id) {
self.0.enter(span)
}

fn exit(&self, span: &span::Id) {
self.0.exit(span)
}
}

impl NoSubscriber {
/// Returns a new `NoSubscriber`.
#[must_use]
Expand Down
1 change: 1 addition & 0 deletions tracing-flame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ smallvec = ["tracing-subscriber/smallvec"]
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3.0", default-features = false, features = ["registry", "fmt"] }
tracing = { path = "../tracing", version = "0.1.35", default-features = false, features = ["std"] }
once_cell = "1.13.0"
rubicon = "3.0.1"


[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions tracing-flame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ use tracing_subscriber::Layer;

mod error;

static START: Lazy<Instant> = Lazy::new(Instant::now);
rubicon::process_local! {
static START: Lazy<Instant> = Lazy::new(Instant::now);
}

thread_local! {
rubicon::thread_local! {
static LAST_EVENT: Cell<Instant> = Cell::new(*START);

static THREAD_NAME: String = {
Expand Down
1 change: 1 addition & 0 deletions tracing-log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ log = { version = "0.4.17" }
once_cell = "1.13.0"
lru = { version = "0.7.7", optional = true }
ahash = { version = "0.7.7", optional = true }
rubicon = "3.0.1"

[dev-dependencies]
tracing = { path = "../tracing", version = "0.1.35"}
Expand Down
46 changes: 26 additions & 20 deletions tracing-log/src/interest_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ impl State {
// like and whether subscribers will actually be interested in it, since nothing will actually
// be logged from it.

static INTEREST_CACHE_EPOCH: AtomicUsize = AtomicUsize::new(0);
rubicon::process_local! {
static INTEREST_CACHE_EPOCH: AtomicUsize = AtomicUsize::new(0);
}

fn interest_cache_epoch() -> usize {
INTEREST_CACHE_EPOCH.load(Ordering::Relaxed)
Expand All @@ -129,24 +131,26 @@ impl tracing_core::Callsite for SentinelCallsite {
}
}

static SENTINEL_CALLSITE: SentinelCallsite = SentinelCallsite;
static SENTINEL_METADATA: tracing_core::Metadata<'static> = tracing_core::Metadata::new(
"log interest cache",
"log",
tracing_core::Level::ERROR,
None,
None,
None,
tracing_core::field::FieldSet::new(&[], tracing_core::identify_callsite!(&SENTINEL_CALLSITE)),
tracing_core::metadata::Kind::EVENT,
);

static CONFIG: Lazy<Mutex<InterestCacheConfig>> = Lazy::new(|| {
tracing_core::callsite::register(&SENTINEL_CALLSITE);
Mutex::new(InterestCacheConfig::disabled())
});

thread_local! {
rubicon::process_local! {
static SENTINEL_CALLSITE: SentinelCallsite = SentinelCallsite;
static SENTINEL_METADATA: tracing_core::Metadata<'static> = tracing_core::Metadata::new(
"log interest cache",
"log",
tracing_core::Level::ERROR,
None,
None,
None,
tracing_core::field::FieldSet::new(&[], tracing_core::identify_callsite!(&SENTINEL_CALLSITE)),
tracing_core::metadata::Kind::EVENT,
);

static CONFIG: Lazy<Mutex<InterestCacheConfig>> = Lazy::new(|| {
tracing_core::callsite::register(&SENTINEL_CALLSITE);
Mutex::new(InterestCacheConfig::disabled())
});
}

rubicon::thread_local! {
static STATE: RefCell<State> = {
let config = CONFIG.lock().unwrap();
RefCell::new(State::new(interest_cache_epoch(), &config))
Expand Down Expand Up @@ -235,7 +239,9 @@ mod tests {

fn lock_for_test() -> impl Drop {
// We need to make sure only one test runs at a time.
static LOCK: Lazy<Mutex<()>> = Lazy::new(Mutex::new);
rubicon::process_local! {
static LOCK: Lazy<Mutex<()>> = Lazy::new(Mutex::new);
}

match LOCK.lock() {
Ok(guard) => guard,
Expand Down
Loading

0 comments on commit 2b993f2

Please sign in to comment.