Skip to content

Commit

Permalink
Add (feature flagged) tracing spans to spawns
Browse files Browse the repository at this point in the history
The instrumentation conforms to the standardized naming requirements for
`tokio-console` (see tokio-rs/console#41), so they will show up in the
console.

The `tracing` dependency is an opt-in feature flag.

Signed-off-by: Eliza Weisman <[email protected]>
  • Loading branch information
hawkw committed Feb 14, 2022
1 parent f45eee8 commit 1e8da3e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ exclude = ["/ci/*", "/scripts/*", "/.github/*", "/bors.toml"]
members = ["rayon-demo", "rayon-core"]
exclude = ["ci"]

[features]
tracing = ["rayon-core/tracing"]

[dependencies]
rayon-core = { version = "1.9.1", path = "rayon-core" }
crossbeam-deque = "0.8.1"
Expand Down
1 change: 1 addition & 0 deletions rayon-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ num_cpus = "1.2"
crossbeam-channel = "0.5.0"
crossbeam-deque = "0.8.1"
crossbeam-utils = "0.8.0"
tracing = { version = "0.1.26", optional = true }

[dev-dependencies]
rand = "0.8"
Expand Down
35 changes: 33 additions & 2 deletions rayon-core/src/scope/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,14 +534,30 @@ impl<'scope> Scope<'scope> {
/// task spawning.
///
/// [`scope` function]: fn.scope.html
#[cfg_attr(feature = "tracing", track_caller)]
pub fn spawn<BODY>(&self, body: BODY)
where
BODY: FnOnce(&Scope<'scope>) + Send + 'scope,
{
// Generate a tracing span for the function.
#[cfg(feature = "tracing")]
let span = {
let location = std::panic::Location::caller();
tracing::trace_span!(
"runtime.spawn",
kind = %"scoped",
"fn" = %std::any::type_name::<BODY>(),
spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()),
)
};
self.base.increment();
unsafe {
let job_ref = Box::new(HeapJob::new(move || {
self.base.execute_job(move || body(self))
self.base.execute_job(move || {
#[cfg(feature = "tracing")]
let _s = span.entered();
body(self)
})
}))
.as_job_ref();

Expand Down Expand Up @@ -579,10 +595,25 @@ impl<'scope> ScopeFifo<'scope> {
where
BODY: FnOnce(&ScopeFifo<'scope>) + Send + 'scope,
{
// Generate a tracing span for the function.
#[cfg(feature = "tracing")]
let span = {
let location = std::panic::Location::caller();
tracing::trace_span!(
"runtime.spawn",
kind = %"scoped-fifo",
"fn" = %std::any::type_name::<BODY>(),
spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()),
)
};
self.base.increment();
unsafe {
let job_ref = Box::new(HeapJob::new(move || {
self.base.execute_job(move || body(self))
self.base.execute_job(move || {
#[cfg(feature = "tracing")]
let _s = span.entered();
body(self)
})
}))
.as_job_ref();

Expand Down
35 changes: 35 additions & 0 deletions rayon-core/src/spawn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use std::sync::Arc;
/// GLOBAL_COUNTER.fetch_add(1, Ordering::SeqCst);
/// });
/// ```
#[cfg_attr(feature = "tracing", track_caller)]
pub fn spawn<F>(func: F)
where
F: FnOnce() + Send + 'static,
Expand All @@ -68,10 +69,27 @@ where
/// Spawns an asynchronous job in `registry.`
///
/// Unsafe because `registry` must not yet have terminated.
#[cfg_attr(feature = "tracing", track_caller)]
pub(super) unsafe fn spawn_in<F>(func: F, registry: &Arc<Registry>)
where
F: FnOnce() + Send + 'static,
{
// Generate a tracing span for the function.
#[cfg(feature = "tracing")]
let func = {
let location = std::panic::Location::caller();
let span = tracing::trace_span!(
"runtime.spawn",
kind = %"spawn",
"fn" = %std::any::type_name::<F>(),
spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()),
);
move || {
let _s = span.entered();
(func)()
}
};

// We assert that this does not hold any references (we know
// this because of the `'static` bound in the inferface);
// moreover, we assert that the code below is not supposed to
Expand Down Expand Up @@ -132,6 +150,7 @@ where
/// details.
///
/// [ph]: struct.ThreadPoolBuilder.html#method.panic_handler
#[cfg_attr(feature = "tracing", track_caller)]
pub fn spawn_fifo<F>(func: F)
where
F: FnOnce() + Send + 'static,
Expand All @@ -143,10 +162,26 @@ where
/// Spawns an asynchronous FIFO job in `registry.`
///
/// Unsafe because `registry` must not yet have terminated.
#[cfg_attr(feature = "tracing", track_caller)]
pub(super) unsafe fn spawn_fifo_in<F>(func: F, registry: &Arc<Registry>)
where
F: FnOnce() + Send + 'static,
{
// Generate a tracing span for the function.
#[cfg(feature = "tracing")]
let func = {
let location = std::panic::Location::caller();
let span = tracing::trace_span!(
"runtime.spawn",
kind = %"fifo",
"fn" = %std::any::type_name::<F>(),
spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()),
);
move || {
let _s = span.entered();
(func)()
}
};
// We assert that this does not hold any references (we know
// this because of the `'static` bound in the inferface);
// moreover, we assert that the code below is not supposed to
Expand Down

0 comments on commit 1e8da3e

Please sign in to comment.