-
Notifications
You must be signed in to change notification settings - Fork 729
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
Add an interest cache for logs emitted through the log
crate
#1636
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
51cff4c
log: add an interest cache for logs emitted through the `log` crate
koute 729914d
Detect when the core interest cache changes through a dummy callsite
koute f98ff14
Fix rustfmt and clippy
koute ce76fb3
Align to review comments
koute f5d9663
Improve the interest cache's collision resistance
koute 0f3107b
Further alignments to review comments
koute File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use log::trace; | ||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; | ||
use std::sync::Arc; | ||
use std::time::{Duration, Instant}; | ||
use tracing_subscriber::{EnvFilter, FmtSubscriber}; | ||
|
||
// This creates a bunch of threads and makes sure they start executing | ||
// a given callback almost exactly at the same time. | ||
fn run_on_many_threads<F, R>(thread_count: usize, callback: F) -> Vec<R> | ||
where | ||
F: Fn() -> R + 'static + Send + Clone, | ||
R: Send + 'static, | ||
{ | ||
let started_count = Arc::new(AtomicUsize::new(0)); | ||
let barrier = Arc::new(AtomicBool::new(false)); | ||
let threads: Vec<_> = (0..thread_count) | ||
.map(|_| { | ||
let started_count = started_count.clone(); | ||
let barrier = barrier.clone(); | ||
let callback = callback.clone(); | ||
|
||
std::thread::spawn(move || { | ||
started_count.fetch_add(1, Ordering::SeqCst); | ||
while !barrier.load(Ordering::SeqCst) { | ||
std::thread::yield_now(); | ||
} | ||
|
||
callback() | ||
}) | ||
}) | ||
.collect(); | ||
|
||
while started_count.load(Ordering::SeqCst) != thread_count { | ||
std::thread::yield_now(); | ||
} | ||
barrier.store(true, Ordering::SeqCst); | ||
|
||
threads | ||
.into_iter() | ||
.map(|handle| handle.join()) | ||
.collect::<Result<Vec<R>, _>>() | ||
.unwrap() | ||
} | ||
|
||
fn bench_logger(c: &mut Criterion) { | ||
let env_filter = EnvFilter::default() | ||
.add_directive("info".parse().unwrap()) | ||
.add_directive("ws=off".parse().unwrap()) | ||
.add_directive("yamux=off".parse().unwrap()) | ||
.add_directive("regalloc=off".parse().unwrap()) | ||
.add_directive("cranelift_codegen=off".parse().unwrap()) | ||
.add_directive("cranelift_wasm=warn".parse().unwrap()) | ||
.add_directive("hyper=warn".parse().unwrap()) | ||
.add_directive("dummy=trace".parse().unwrap()); | ||
|
||
let builder = tracing_log::LogTracer::builder().with_max_level(log::LevelFilter::Trace); | ||
|
||
#[cfg(feature = "interest-cache")] | ||
let builder = builder.with_interest_cache(Some(tracing_log::InterestCacheConfig::default())); | ||
|
||
builder.init().unwrap(); | ||
|
||
let builder = FmtSubscriber::builder() | ||
.with_env_filter(env_filter) | ||
.with_filter_reloading(); | ||
|
||
let subscriber = builder.finish(); | ||
tracing::subscriber::set_global_default(subscriber).unwrap(); | ||
|
||
const THREAD_COUNT: usize = 8; | ||
|
||
c.bench_function("log_from_multiple_threads", |b| { | ||
b.iter_custom(|count| { | ||
let durations = run_on_many_threads(THREAD_COUNT, move || { | ||
let start = Instant::now(); | ||
for _ in 0..count { | ||
trace!("A dummy log!"); | ||
} | ||
start.elapsed() | ||
}); | ||
|
||
let total_time: Duration = durations.into_iter().sum(); | ||
Duration::from_nanos((total_time.as_nanos() / THREAD_COUNT as u128) as u64) | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, bench_logger); | ||
criterion_main!(benches); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of adding this in
tracing-core
, since it's a newdoc(hidden)
API that's used only bytracing-log
.I think there's a way to avoid having to change
tracing-core
, and detect the number of times the interest cache has been reloaded purely intracing-log
. We can do this by adding a new dummy type implementing thetracing_core::Callsite
trait, and registering it with the subscriber the first time alog
event is recorded. Every time the interest cache is rebuilt, the dummy callsite's [Callsite::set_interest
] method will be called again. In that method, we can increment the epoch counter.This should allow us to determine the interest cache's current epoch solely in the
tracing-log
crate, without addingdoc(hidden)
code intracing-core
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Please let me know if there's anything else you'd like me to change.