Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dashmap and replace with static array #16

Merged
merged 10 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ tracing-core = {version = "0.1", default-features = false}
tracing-subscriber = {version="0.3", default-features = false, features=["std", "fmt", "registry"]}
chrono = {version="0.4", default-features = false, features=["std"]}
once_cell = ">=1.18"
dashmap = "6"
paste = "1"
thiserror = "1"
thiserror = {version="2", default-features = false}

[target.'cfg(not(target_os = "linux"))'.dependencies]
tracelogging = ">= 1.2.0"
Expand Down
9 changes: 9 additions & 0 deletions src/_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ pub struct EventMetadata {
pub identity: tracing::callsite::Identifier,
pub event_tag: u32,
}

// A EventMetadata with the identity replaced by its hash, because Identity doesn't implement comparisons
// but we need a stable ordering.
#[derive(Clone)]
pub(crate) struct ParsedEventMetadata {
pub(crate) identity_hash: u64,
pub(crate) kw: u64,
pub(crate) event_tag: u32
}
4 changes: 2 additions & 2 deletions src/layer/filter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tracing::Subscriber;
use tracing_subscriber::{layer::Filter, registry::LookupSpan};

use crate::{native::{EventWriter, ProviderTypes}, statics::EVENT_METADATA};
use crate::{native::{EventWriter, ProviderTypes}, statics::get_event_metadata};

use super::*;

Expand All @@ -15,7 +15,7 @@ where
&self,
metadata: &'static tracing::Metadata<'static>,
) -> tracing::subscriber::Interest {
let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let keyword = if let Some(meta) = etw_meta {
meta.kw
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/layer/layer_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
&self,
metadata: &'static tracing::Metadata<'static>,
) -> tracing::subscriber::Interest {
let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let keyword = if let Some(meta) = etw_meta {
meta.kw
} else {
Expand Down Expand Up @@ -84,7 +84,7 @@ where
.event_span(event)
.map_or(0, |evt| evt.parent().map_or(0, |p| p.id().into_u64()));

let etw_meta = EVENT_METADATA.get(&event.metadata().callsite());
let etw_meta = get_event_metadata(&event.metadata().callsite());
let (name, keyword, tag) = if let Some(meta) = etw_meta {
(event.metadata().name(), meta.kw, meta.event_tag)
} else {
Expand Down Expand Up @@ -203,7 +203,7 @@ where
return;
};

let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let (keyword, tag) = if let Some(meta) = etw_meta {
(meta.kw, meta.event_tag)
} else {
Expand Down Expand Up @@ -244,7 +244,7 @@ where
return;
};

let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let (keyword, tag) = if let Some(meta) = etw_meta {
(meta.kw, meta.event_tag)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::Subscriber;
use tracing_core::callsite;
use tracing_subscriber::registry::LookupSpan;

use crate::{native::{EventWriter, ProviderTypes}, statics::EVENT_METADATA};
use crate::{native::{EventWriter, ProviderTypes}, statics::get_event_metadata};

pub(crate) struct _EtwLayer<S, Mode: ProviderTypes>
where
Expand Down Expand Up @@ -59,7 +59,7 @@ where
Mode::Provider: EventWriter<Mode> + 'static,
{
fn is_enabled(&self, callsite: &callsite::Identifier, level: &tracing_core::Level) -> bool {
let etw_meta = EVENT_METADATA.get(callsite);
let etw_meta = get_event_metadata(callsite);
let keyword = if let Some(meta) = etw_meta {
meta.kw
} else {
Expand Down
49 changes: 42 additions & 7 deletions src/statics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Module for static variables that are used by the crate.

use std::{cmp, hash::{BuildHasher, RandomState}};

pub(crate) static GLOBAL_ACTIVITY_SEED: once_cell::sync::Lazy<[u8; 16]> =
once_cell::sync::Lazy::new(|| {
let now = std::time::SystemTime::now()
Expand All @@ -14,8 +16,10 @@ once_cell::sync::Lazy::new(|| {
data
});

static BH: once_cell::sync::Lazy<RandomState> = once_cell::sync::Lazy::new(|| {RandomState::new()});

pub(crate) static EVENT_METADATA: once_cell::sync::Lazy<
dashmap::DashMap<tracing::callsite::Identifier, &'static crate::_details::EventMetadata>,
Box<[crate::_details::ParsedEventMetadata]>,
> = once_cell::sync::Lazy::new(|| {
unsafe {
let start =
Expand All @@ -30,7 +34,7 @@ pub(crate) static EVENT_METADATA: once_cell::sync::Lazy<
&mut *core::ptr::slice_from_raw_parts_mut(start, stop.offset_from(start) as usize);

if events_slice.is_empty() {
return dashmap::DashMap::new();
return Box::new_uninit_slice(0).assume_init();
}

// Sort spurious nulls to the end
Expand Down Expand Up @@ -61,13 +65,44 @@ pub(crate) static EVENT_METADATA: once_cell::sync::Lazy<
next_pos += 1;
}

let map = dashmap::DashMap::with_capacity(events_slice.len());
let mut map: Box<[core::mem::MaybeUninit<crate::_details::ParsedEventMetadata>]> = Box::new_uninit_slice(good_pos + 1);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy says new_uninit_slice was only stabilized last month (1.82), so maybe I should hold off on using it. Though github's CI is apparently running 1.82 already and so are the internal pipelines, so it's probably fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My (strong) recommendation is to create a vec, reserve space, push items into it, and call into_boxed_slice when you're done.

next_pos = 0;
while next_pos < good_pos {
let event = &*events_slice[next_pos];
map.insert(event.identity.clone(), event);
while next_pos <= good_pos {
let next = &*events_slice[next_pos];
map[next_pos].as_mut_ptr().write(crate::_details::ParsedEventMetadata { identity_hash: BH.hash_one(&next.identity), kw: next.kw, event_tag: next.event_tag });
next_pos += 1;
}
map
let mut sorted = map.assume_init();
sorted.sort_unstable_by(|a, b| b.cmp(a));
sorted
}
});

impl core::cmp::PartialEq for crate::_details::ParsedEventMetadata {
fn eq(&self, other: &Self) -> bool {
self.identity_hash == other.identity_hash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.hash == other.hash && self.identity == other.identity

}
}

impl core::cmp::Eq for crate::_details::ParsedEventMetadata {}

impl core::cmp::PartialOrd for crate::_details::ParsedEventMetadata {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.identity_hash.partial_cmp(&other.identity_hash)
}
}

impl core::cmp::Ord for crate::_details::ParsedEventMetadata {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.identity_hash.cmp(&other.identity_hash)
}
}

pub(crate) fn get_event_metadata(id: &tracing::callsite::Identifier) -> Option<&'static crate::_details::ParsedEventMetadata> {
let hash = BH.hash_one(id);
let etw_meta = EVENT_METADATA.binary_search_by_key(&hash, |m| { m.identity_hash });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use partition_point to get the index of the lower bound, then iterate until you find one of:

  • The end of the slice. (Not found.)
  • An item where hash doesn't match. (Not found.)
  • An item where this.identity == other.identity. (Found.)

match etw_meta {
Ok(idx) => Some(&EVENT_METADATA[idx]),
_ => None
}
}