From b43d90d88543f531d6b3fa73c6651475d34e5728 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 11:43:39 -0700 Subject: [PATCH 01/10] tracing: just use `tracing` 0.1.21's instrument Signed-off-by: Eliza Weisman --- tokio/Cargo.toml | 9 +++------ tokio/src/util/trace.rs | 39 ++++----------------------------------- 2 files changed, 7 insertions(+), 41 deletions(-) diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 6c5de90ddc2..c8df85acc2b 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -70,10 +70,7 @@ process = [ ] # Includes basic task execution capabilities rt = ["slab"] -rt-multi-thread = [ - "num_cpus", - "rt", -] +rt-multi-thread = ["num_cpus", "rt"] signal = [ "lazy_static", "libc", @@ -100,9 +97,9 @@ lazy_static = { version = "1.0.2", optional = true } memchr = { version = "2.2", optional = true } mio = { version = "0.7.3", optional = true } num_cpus = { version = "1.8.0", optional = true } -parking_lot = { version = "0.11.0", optional = true } # Not in full +parking_lot = { version = "0.11.0", optional = true }# Not in full slab = { version = "0.4.1", optional = true } -tracing = { version = "0.1.16", default-features = false, features = ["std"], optional = true } # Not in full +tracing = { version = "0.1.21", default-features = false, features = ["std"], optional = true }# Not in full [target.'cfg(unix)'.dependencies] libc = { version = "0.2.42", optional = true } diff --git a/tokio/src/util/trace.rs b/tokio/src/util/trace.rs index 18956a36306..2c0591645fc 100644 --- a/tokio/src/util/trace.rs +++ b/tokio/src/util/trace.rs @@ -1,47 +1,16 @@ cfg_trace! { cfg_rt! { - use std::future::Future; - use std::pin::Pin; - use std::task::{Context, Poll}; - use pin_project_lite::pin_project; - - use tracing::Span; - - pin_project! { - /// A future that has been instrumented with a `tracing` span. - #[derive(Debug, Clone)] - pub(crate) struct Instrumented { - #[pin] - inner: T, - span: Span, - } - } - - impl Future for Instrumented { - type Output = T::Output; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.project(); - let _enter = this.span.enter(); - this.inner.poll(cx) - } - } - - impl Instrumented { - pub(crate) fn new(inner: T, span: Span) -> Self { - Self { inner, span } - } - } + pub(crate) use tracing::instrument::Instrumented; #[inline] pub(crate) fn task(task: F, kind: &'static str) -> Instrumented { - let span = tracing::trace_span!( + use tracing::instrument::Instrument; + task.instrument(tracing::trace_span!( target: "tokio::task", "task", %kind, future = %std::any::type_name::(), - ); - Instrumented::new(task, span) + )) } } } From e48d3e30bd73ad6af74016dda90c4f0ff860e2f2 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 11:44:01 -0700 Subject: [PATCH 02/10] tracing: remove future names from task spans Signed-off-by: Eliza Weisman --- tokio/src/util/trace.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tokio/src/util/trace.rs b/tokio/src/util/trace.rs index 2c0591645fc..925c4fc134b 100644 --- a/tokio/src/util/trace.rs +++ b/tokio/src/util/trace.rs @@ -9,7 +9,6 @@ cfg_trace! { target: "tokio::task", "task", %kind, - future = %std::any::type_name::(), )) } } From a0bc98a5c4f3356a906215046c20715383e4570a Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 12:16:35 -0700 Subject: [PATCH 03/10] tracing: add spawn location to task spans instead --- tokio/build.rs | 43 +++++++++++++++++++++++++++++++++++++++++ tokio/src/task/local.rs | 2 ++ tokio/src/task/spawn.rs | 1 + tokio/src/util/trace.rs | 16 +++++++++++++-- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tokio/build.rs diff --git a/tokio/build.rs b/tokio/build.rs new file mode 100644 index 00000000000..4299a35960d --- /dev/null +++ b/tokio/build.rs @@ -0,0 +1,43 @@ +use std::{env, ffi::OsString, process::Command}; + +fn main() { + match rustc_minor_version() { + // If rustc >= 1.47.0, we can enable `track_caller`. + Ok(minor) if minor >= 47 => println!("cargo:rustc-cfg=tokio_track_caller"), + Err(e) => println!("cargo:warning=could not parse rustc version: {}", e), + _ => {} + } +} + +fn rustc_minor_version() -> Result> { + let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); + let out = Command::new(rustc).arg("-V").output()?; + let version_str = std::str::from_utf8(&out.stdout)?; + let mut parts = version_str.split(' '); + if parts.next() != Some("rustc") { + Err(format!( + "weird rustc version: {:?} (missing 'rustc') ", + version_str + ))?; + } + if let Some(part) = parts.next() { + let mut parts = part.split('.'); + if parts.next() != Some("1") { + Err(format!( + "weird rustc version: {:?} (does not start with 1)", + version_str + ))?; + } + if let Some(middle) = parts.next() { + Ok(middle.parse()?) + } else { + Err(format!("weird rustc version: {:?} (no minor version)", version_str).into()) + } + } else { + Err(format!( + "weird rustc version: {:?} (missing version entirely?) ", + version_str, + ) + .into()) + } +} diff --git a/tokio/src/task/local.rs b/tokio/src/task/local.rs index 5896126c7e5..566b2f2d7f8 100644 --- a/tokio/src/task/local.rs +++ b/tokio/src/task/local.rs @@ -190,6 +190,7 @@ cfg_rt! { /// }).await; /// } /// ``` + #[cfg_attr(tokio_track_caller, track_caller)] pub fn spawn_local(future: F) -> JoinHandle where F: Future + 'static, @@ -273,6 +274,7 @@ impl LocalSet { /// } /// ``` /// [`spawn_local`]: fn@spawn_local + #[cfg_attr(tokio_track_caller, track_caller)] pub fn spawn_local(&self, future: F) -> JoinHandle where F: Future + 'static, diff --git a/tokio/src/task/spawn.rs b/tokio/src/task/spawn.rs index 77acb579f73..a060852dc23 100644 --- a/tokio/src/task/spawn.rs +++ b/tokio/src/task/spawn.rs @@ -122,6 +122,7 @@ cfg_rt! { /// ```text /// error[E0391]: cycle detected when processing `main` /// ``` + #[cfg_attr(tokio_track_caller, track_caller)] pub fn spawn(task: T) -> JoinHandle where T: Future + Send + 'static, diff --git a/tokio/src/util/trace.rs b/tokio/src/util/trace.rs index 925c4fc134b..96a9db91d1f 100644 --- a/tokio/src/util/trace.rs +++ b/tokio/src/util/trace.rs @@ -3,13 +3,25 @@ cfg_trace! { pub(crate) use tracing::instrument::Instrumented; #[inline] + #[cfg_attr(tokio_track_caller, track_caller)] pub(crate) fn task(task: F, kind: &'static str) -> Instrumented { use tracing::instrument::Instrument; - task.instrument(tracing::trace_span!( + #[cfg(tokio_track_caller)] + let location = std::panic::Location::caller(); + #[cfg(tokio_track_caller)] + let span = tracing::trace_span!( target: "tokio::task", "task", %kind, - )) + spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()), + ); + #[cfg(not(tokio_track_caller))] + let span = tracing::trace_span!( + target: "tokio::task", + "task", + %kind, + ); + task.instrument(span) } } } From 5e78045cb1457a727c423ce810b2eb794731703a Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 13:25:34 -0700 Subject: [PATCH 04/10] unformat Cargo.toml whoops, i didn't realize i had a vscode plugin installed that does that. Signed-off-by: Eliza Weisman --- tokio/Cargo.toml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index c8df85acc2b..df739f524cd 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -70,7 +70,10 @@ process = [ ] # Includes basic task execution capabilities rt = ["slab"] -rt-multi-thread = ["num_cpus", "rt"] +rt-multi-thread = [ + "num_cpus", + "rt" +] signal = [ "lazy_static", "libc", @@ -97,9 +100,9 @@ lazy_static = { version = "1.0.2", optional = true } memchr = { version = "2.2", optional = true } mio = { version = "0.7.3", optional = true } num_cpus = { version = "1.8.0", optional = true } -parking_lot = { version = "0.11.0", optional = true }# Not in full +parking_lot = { version = "0.11.0", optional = true } # Not in full slab = { version = "0.4.1", optional = true } -tracing = { version = "0.1.21", default-features = false, features = ["std"], optional = true }# Not in full +tracing = { version = "0.1.21", default-features = false, features = ["std"], optional = true } # Not in full [target.'cfg(unix)'.dependencies] libc = { version = "0.2.42", optional = true } @@ -128,4 +131,4 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -features = ["full"] +features = ["full"] \ No newline at end of file From 00e7f02d8eecad35022edaaa702752b9e3e6b456 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 13:47:01 -0700 Subject: [PATCH 05/10] also add locations to spawn_blocking --- tokio/src/runtime/handle.rs | 15 ++++++++++++++- tokio/src/task/blocking.rs | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tokio/src/runtime/handle.rs b/tokio/src/runtime/handle.rs index b1e8d8f187c..c9ffd5cd0e4 100644 --- a/tokio/src/runtime/handle.rs +++ b/tokio/src/runtime/handle.rs @@ -39,13 +39,26 @@ impl Handle { // context::enter(self.clone(), f) // } - /// Run the provided function on an executor dedicated to blocking operations. + /// Run the provided function on an executor dedicated to blocking + /// operations. + #[cfg_attr(tokio_track_caller, track_caller)] pub(crate) fn spawn_blocking(&self, func: F) -> JoinHandle where F: FnOnce() -> R + Send + 'static, { #[cfg(feature = "tracing")] let func = { + #[cfg(tokio_track_caller)] + let location = std::panic::Location::caller(); + #[cfg(tokio_track_caller)] + let span = tracing::trace_span!( + target: "tokio::task", + "task", + kind = %"blocking", + function = %std::any::type_name::(), + spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()), + ); + #[cfg(not(tokio_track_caller))] let span = tracing::trace_span!( target: "tokio::task", "task", diff --git a/tokio/src/task/blocking.rs b/tokio/src/task/blocking.rs index fc6632be3b7..36bc4579bfe 100644 --- a/tokio/src/task/blocking.rs +++ b/tokio/src/task/blocking.rs @@ -104,6 +104,7 @@ cfg_rt_multi_thread! { /// # Ok(()) /// # } /// ``` +#[cfg_attr(tokio_track_caller, track_caller)] pub fn spawn_blocking(f: F) -> JoinHandle where F: FnOnce() -> R + Send + 'static, From eb11d8e96540d33c53991bcac9af5266e413301b Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 13:49:20 -0700 Subject: [PATCH 06/10] also add locations to tasks spawned via a Runtime Signed-off-by: Eliza Weisman --- tokio/src/runtime/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs index 01788b9cc84..c76f5cf6d1c 100644 --- a/tokio/src/runtime/mod.rs +++ b/tokio/src/runtime/mod.rs @@ -357,11 +357,14 @@ cfg_rt! { /// }); /// # } /// ``` + #[cfg_attr(tokio_track_caller, track_caller)] pub fn spawn(&self, future: F) -> JoinHandle where F: Future + Send + 'static, F::Output: Send + 'static, { + #[cfg(feature = "tracing")] + let future = crate::util::trace::task(future, "task"); match &self.kind { #[cfg(feature = "rt-multi-thread")] Kind::ThreadPool(exec) => exec.spawn(future), @@ -385,6 +388,7 @@ cfg_rt! { /// println!("now running on a worker thread"); /// }); /// # } + #[cfg_attr(tokio_track_caller, track_caller)] pub fn spawn_blocking(&self, func: F) -> JoinHandle where F: FnOnce() -> R + Send + 'static, From e04688ddc5bc4126860cc4bed24332cf0a77b5d3 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 13:57:48 -0700 Subject: [PATCH 07/10] put back missing trailing comma --- tokio/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index df739f524cd..628bb3760e6 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -72,7 +72,7 @@ process = [ rt = ["slab"] rt-multi-thread = [ "num_cpus", - "rt" + "rt", ] signal = [ "lazy_static", From 0a913534fc6e43ef9053cecc4d23197ad224558d Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 13:58:24 -0700 Subject: [PATCH 08/10] *whatever*, clippy --- tokio/build.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tokio/build.rs b/tokio/build.rs index 4299a35960d..3b156de18bf 100644 --- a/tokio/build.rs +++ b/tokio/build.rs @@ -15,10 +15,7 @@ fn rustc_minor_version() -> Result> { let version_str = std::str::from_utf8(&out.stdout)?; let mut parts = version_str.split(' '); if parts.next() != Some("rustc") { - Err(format!( - "weird rustc version: {:?} (missing 'rustc') ", - version_str - ))?; + return Err(format!("weird rustc version: {:?} (missing 'rustc') ", version_str).into()); } if let Some(part) = parts.next() { let mut parts = part.split('.'); From f05a46977e0da7e2774f3d9cc7d0014195547fed Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 29 Oct 2020 16:35:24 -0700 Subject: [PATCH 09/10] more clippy placation --- tokio/build.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tokio/build.rs b/tokio/build.rs index 3b156de18bf..da4718017a3 100644 --- a/tokio/build.rs +++ b/tokio/build.rs @@ -20,10 +20,11 @@ fn rustc_minor_version() -> Result> { if let Some(part) = parts.next() { let mut parts = part.split('.'); if parts.next() != Some("1") { - Err(format!( + return Err(format!( "weird rustc version: {:?} (does not start with 1)", version_str - ))?; + ) + .into()); } if let Some(middle) = parts.next() { Ok(middle.parse()?) From 47017ecb863eb91108538c5a973c690548fefd17 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 30 Oct 2020 10:54:11 -0700 Subject: [PATCH 10/10] use autocfg Signed-off-by: Eliza Weisman --- tokio/Cargo.toml | 3 +++ tokio/build.rs | 51 +++++++++++++++--------------------------------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 628bb3760e6..c4825c20650 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -126,6 +126,9 @@ tempfile = "3.1.0" [target.'cfg(loom)'.dev-dependencies] loom = { version = "0.3.5", features = ["futures", "checkpoint"] } +[build-dependencies] +autocfg = "1" # Needed for conditionally enabling `track-caller` + [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] diff --git a/tokio/build.rs b/tokio/build.rs index da4718017a3..fe5c8300560 100644 --- a/tokio/build.rs +++ b/tokio/build.rs @@ -1,41 +1,22 @@ -use std::{env, ffi::OsString, process::Command}; +use autocfg::AutoCfg; fn main() { - match rustc_minor_version() { - // If rustc >= 1.47.0, we can enable `track_caller`. - Ok(minor) if minor >= 47 => println!("cargo:rustc-cfg=tokio_track_caller"), - Err(e) => println!("cargo:warning=could not parse rustc version: {}", e), - _ => {} - } -} - -fn rustc_minor_version() -> Result> { - let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); - let out = Command::new(rustc).arg("-V").output()?; - let version_str = std::str::from_utf8(&out.stdout)?; - let mut parts = version_str.split(' '); - if parts.next() != Some("rustc") { - return Err(format!("weird rustc version: {:?} (missing 'rustc') ", version_str).into()); - } - if let Some(part) = parts.next() { - let mut parts = part.split('.'); - if parts.next() != Some("1") { - return Err(format!( - "weird rustc version: {:?} (does not start with 1)", - version_str - ) - .into()); + match AutoCfg::new() { + Ok(ac) => { + // The #[track_caller] attribute was stabilized in rustc 1.46.0. + if ac.probe_rustc_version(1, 46) { + autocfg::emit("tokio_track_caller") + } } - if let Some(middle) = parts.next() { - Ok(middle.parse()?) - } else { - Err(format!("weird rustc version: {:?} (no minor version)", version_str).into()) + + Err(e) => { + // If we couldn't detect the compiler version and features, just + // print a warning. This isn't a fatal error: we can still build + // Tokio, we just can't enable cfgs automatically. + println!( + "cargo:warning=tokio: failed to detect compiler features: {}", + e + ); } - } else { - Err(format!( - "weird rustc version: {:?} (missing version entirely?) ", - version_str, - ) - .into()) } }