Skip to content

Commit

Permalink
made original_size.bytes field optional
Browse files Browse the repository at this point in the history
No point setting it if it's the same as the size.bytes field.
  • Loading branch information
hds committed Oct 2, 2024
1 parent d926d82 commit d358a3b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
27 changes: 3 additions & 24 deletions tokio/src/runtime/blocking/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::runtime::builder::ThreadNameFn;
use crate::runtime::task::{self, JoinHandle};
use crate::runtime::{Builder, Callback, Handle, BOX_FUTURE_THRESHOLD};
use crate::util::metric_atomics::MetricAtomicUsize;
use crate::util::trace::SpawnMeta;
use crate::util::trace::{blocking_task, SpawnMeta};

use std::collections::{HashMap, VecDeque};
use std::fmt;
Expand Down Expand Up @@ -375,30 +375,9 @@ impl Spawner {
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let fut = BlockingTask::new(func);
let id = task::Id::next();
#[cfg(all(tokio_unstable, feature = "tracing"))]
let fut = {
use tracing::Instrument;
let location = std::panic::Location::caller();
let span = tracing::trace_span!(
target: "tokio::task::blocking",
"runtime.spawn",
kind = %"blocking",
task.name = %spawn_meta.name.unwrap_or_default(),
task.id = id.as_u64(),
"fn" = %std::any::type_name::<F>(),
original_size.bytes = spawn_meta.original_size,
size.bytes = std::mem::size_of::<F>(),
loc.file = location.file(),
loc.line = location.line(),
loc.col = location.column(),
);
fut.instrument(span)
};

#[cfg(not(all(tokio_unstable, feature = "tracing")))]
let _ = spawn_meta;
let fut =
blocking_task::<F, BlockingTask<F>>(BlockingTask::new(func), spawn_meta, id.as_u64());

let (task, handle) = task::unowned(fut, BlockingSchedule::new(rt), id);

Expand Down
44 changes: 43 additions & 1 deletion tokio/src/util/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cfg_rt! {
use pin_project_lite::pin_project;
use std::mem;
use std::future::Future;
use tracing::instrument::Instrument;
pub(crate) use tracing::instrument::Instrumented;

#[inline]
Expand All @@ -53,14 +54,19 @@ cfg_rt! {
#[track_caller]
fn get_span(kind: &'static str, spawn_meta: SpawnMeta<'_>, id: u64, task_size: usize) -> tracing::Span {
let location = std::panic::Location::caller();
let original_size = if spawn_meta.original_size != task_size {
Some(spawn_meta.original_size)
} else {
None
};
tracing::trace_span!(
target: "tokio::task",
parent: None,
"runtime.spawn",
%kind,
task.name = %spawn_meta.name.unwrap_or_default(),
task.id = id,
original_size.bytes = spawn_meta.original_size,
original_size.bytes = original_size,
size.bytes = task_size,
loc.file = location.file(),
loc.line = location.line(),
Expand All @@ -72,6 +78,35 @@ cfg_rt! {
task.instrument(span)
}

#[inline]
#[track_caller]
pub(crate) fn blocking_task<Fn, Fut>(task: Fut, spawn_meta: SpawnMeta<'_>, id: u64) -> Instrumented<Fut> {
let location = std::panic::Location::caller();

let fn_size = mem::size_of::<Fn>();
let original_size = if spawn_meta.original_size != fn_size {
Some(spawn_meta.original_size)
} else {
None
};

let span = tracing::trace_span!(
target: "tokio::task::blocking",
"runtime.spawn",
kind = %"blocking",
task.name = %spawn_meta.name.unwrap_or_default(),
task.id = id,
"fn" = %std::any::type_name::<Fn>(),
original_size.bytes = original_size,
size.bytes = fn_size,
loc.file = location.file(),
loc.line = location.line(),
loc.col = location.column(),
);
task.instrument(span)

}

pub(crate) fn async_op<P,F>(inner: P, resource_span: tracing::Span, source: &str, poll_op_name: &'static str, inherits_child_attrs: bool) -> InstrumentedAsyncOp<F>
where P: FnOnce() -> F {
resource_span.in_scope(|| {
Expand Down Expand Up @@ -131,6 +166,13 @@ cfg_rt! {
// nop
task
}

#[inline]
pub(crate) fn blocking_task<Fn, Fut>(task: Fut, _spawn_meta: SpawnMeta<'_>, _id: u64) -> Fut {
let _ = PhantomData::<&Fn>;
// nop
task
}
}
}

Expand Down

0 comments on commit d358a3b

Please sign in to comment.