From 9c7d5013314463dc3278dcf6df9d087551dbd7f7 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 23 Aug 2022 16:06:33 -0700 Subject: [PATCH 1/8] rt: add unstable option to disable the LIFO slot The multi-threaded scheduler includes a per-worker LIFO slot used to store the last scheduled task. This can improve certain usage patterns, especially message passing between tasks. However, this LIFO slot is not currently stealable. Eventually, the LIFO slot **will** become stealable, however as a stop-gap, this unstable option lets users disable the LIFO task. --- tokio/src/runtime/builder.rs | 40 +++++++++++++++++++++++++ tokio/src/runtime/config.rs | 9 ++++++ tokio/src/runtime/thread_pool/worker.rs | 2 +- tokio/tests/rt_threaded.rs | 24 +++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 29ae2f8d104..d8533789148 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -87,6 +87,11 @@ pub struct Builder { #[cfg(tokio_unstable)] pub(super) unhandled_panic: UnhandledPanic, + + /// When true, the multi-threade scheduler LIFO slot should not be used. + /// + /// This option should only be exposed as unstable. + pub(super) disable_lifo_slot: bool, } cfg_unstable! { @@ -252,6 +257,8 @@ impl Builder { #[cfg(tokio_unstable)] unhandled_panic: UnhandledPanic::Ignore, + + disable_lifo_slot: false, } } @@ -781,6 +788,37 @@ impl Builder { self.unhandled_panic = behavior; self } + + /// Disables the LIFO task scheduler heuristic. + /// + /// The multi-threaded scheduler includes a heuristic for optimizing + /// message-passing patterns. This heuristic results in the **last** + /// scheduled task being polled first. + /// + /// To implement this heuristic, each worker thread has a slot which + /// holds the task that should be polled next. However, this slot cannot + /// be stolen by other worker threads, which can result in lower total + /// throughput when tasks tend to have longer poll times. + /// + /// This configuration option will disable this heuristic resulting in + /// all scheduled tasks being pushed into the worker-local queue, which + /// is stealable. + /// + /// Eventually, the LIFO slot will become stealable and this option will + /// probably go away. + /// + /// ``` + /// use tokio::runtime; + /// + /// let rt = runtime::Builder::new_multi_threaded() + /// .disable_lifo_slot + /// .build() + /// .unwrap(); + /// ``` + pub fn disable_lifo_slot(&mut self) -> &mut Self { + self.disable_lifo_slot = true; + self + } } fn build_basic_runtime(&mut self) -> io::Result { @@ -814,6 +852,7 @@ impl Builder { event_interval: self.event_interval, #[cfg(tokio_unstable)] unhandled_panic: self.unhandled_panic.clone(), + disable_lifo_slot: self.disable_lifo_slot, }, ); let spawner = Spawner::Basic(scheduler.spawner().clone()); @@ -932,6 +971,7 @@ cfg_rt_multi_thread! { event_interval: self.event_interval, #[cfg(tokio_unstable)] unhandled_panic: self.unhandled_panic.clone(), + disable_lifo_slot: self.disable_lifo_slot, }, ); let spawner = Spawner::ThreadPool(scheduler.spawner().clone()); diff --git a/tokio/src/runtime/config.rs b/tokio/src/runtime/config.rs index b5ff6eadd8d..78ab1601542 100644 --- a/tokio/src/runtime/config.rs +++ b/tokio/src/runtime/config.rs @@ -16,4 +16,13 @@ pub(crate) struct Config { #[cfg(tokio_unstable)] /// How to respond to unhandled task panics. pub(crate) unhandled_panic: crate::runtime::UnhandledPanic, + + /// The multi-threaded scheduler includes a per-worker LIFO slot used to + /// store the last scheduled task. This can improve certain usage patterns, + /// especially message passing between tasks. However, this LIFO slot is not + /// currently stealable. + /// + /// Eventually, the LIFO slot **will** become stealable, however as a + /// stop-gap, this unstable option lets users disable the LIFO task. + pub(crate) disable_lifo_slot: bool, } diff --git a/tokio/src/runtime/thread_pool/worker.rs b/tokio/src/runtime/thread_pool/worker.rs index 2e4a810d8e0..b4c8893fccb 100644 --- a/tokio/src/runtime/thread_pool/worker.rs +++ b/tokio/src/runtime/thread_pool/worker.rs @@ -758,7 +758,7 @@ impl Shared { // task must always be pushed to the back of the queue, enabling other // tasks to be executed. If **not** a yield, then there is more // flexibility and the task may go to the front of the queue. - let should_notify = if is_yield { + let should_notify = if is_yield || self.config.disable_lifo_slot { core.run_queue .push_back(task, &self.inject, &mut core.metrics); true diff --git a/tokio/tests/rt_threaded.rs b/tokio/tests/rt_threaded.rs index dd137fa9b18..f2fce0800dd 100644 --- a/tokio/tests/rt_threaded.rs +++ b/tokio/tests/rt_threaded.rs @@ -542,3 +542,27 @@ async fn test_block_in_place4() { fn rt() -> runtime::Runtime { runtime::Runtime::new().unwrap() } + +#[cfg(tokio_unstable)] +mod unstable { + use super::*; + + #[test] + fn test_disable_lifo_slot() { + let rt = runtime::Builder::new_multi_thread() + .disable_lifo_slot() + .worker_threads(2) + .build() + .unwrap(); + + rt.block_on(async { + tokio::spawn(async { + // Spawn another task and block the thread until completion. If the LIFO slot + // is used then the test doesn't complete. + futures::executor::block_on(tokio::spawn(async {})).unwrap(); + }) + .await + .unwrap(); + }) + } +} From de51a1d69f720af6867d2ad200b4c1570c73e0ed Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 08:17:20 -0700 Subject: [PATCH 2/8] move field --- tokio/src/runtime/builder.rs | 6 +++--- tokio/src/runtime/config.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index d8533789148..755af6ddda2 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -85,13 +85,13 @@ pub struct Builder { /// How many ticks before yielding to the driver for timer and I/O events? pub(super) event_interval: u32, - #[cfg(tokio_unstable)] - pub(super) unhandled_panic: UnhandledPanic, - /// When true, the multi-threade scheduler LIFO slot should not be used. /// /// This option should only be exposed as unstable. pub(super) disable_lifo_slot: bool, + + #[cfg(tokio_unstable)] + pub(super) unhandled_panic: UnhandledPanic, } cfg_unstable! { diff --git a/tokio/src/runtime/config.rs b/tokio/src/runtime/config.rs index 78ab1601542..f41e45c242c 100644 --- a/tokio/src/runtime/config.rs +++ b/tokio/src/runtime/config.rs @@ -13,10 +13,6 @@ pub(crate) struct Config { /// Callback for a worker unparking itself pub(crate) after_unpark: Option, - #[cfg(tokio_unstable)] - /// How to respond to unhandled task panics. - pub(crate) unhandled_panic: crate::runtime::UnhandledPanic, - /// The multi-threaded scheduler includes a per-worker LIFO slot used to /// store the last scheduled task. This can improve certain usage patterns, /// especially message passing between tasks. However, this LIFO slot is not @@ -25,4 +21,8 @@ pub(crate) struct Config { /// Eventually, the LIFO slot **will** become stealable, however as a /// stop-gap, this unstable option lets users disable the LIFO task. pub(crate) disable_lifo_slot: bool, + + #[cfg(tokio_unstable)] + /// How to respond to unhandled task panics. + pub(crate) unhandled_panic: crate::runtime::UnhandledPanic, } From 97f6693cc1c759900c03d6e5205278bb57ea2da7 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 08:27:01 -0700 Subject: [PATCH 3/8] try to fix CI --- tokio/src/runtime/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tokio/src/runtime/config.rs b/tokio/src/runtime/config.rs index f41e45c242c..38f725b8653 100644 --- a/tokio/src/runtime/config.rs +++ b/tokio/src/runtime/config.rs @@ -20,6 +20,7 @@ pub(crate) struct Config { /// /// Eventually, the LIFO slot **will** become stealable, however as a /// stop-gap, this unstable option lets users disable the LIFO task. + #[cfg(not(tokio_wasm))] pub(crate) disable_lifo_slot: bool, #[cfg(tokio_unstable)] From 8f63463817a2b94ad5f1c06c7e79f8e9610d543c Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 09:40:35 -0700 Subject: [PATCH 4/8] tweak docs and try to fix CI --- tokio/src/runtime/builder.rs | 14 ++++++++++++-- tokio/src/runtime/config.rs | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 755af6ddda2..5718421b0b2 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -804,8 +804,18 @@ impl Builder { /// all scheduled tasks being pushed into the worker-local queue, which /// is stealable. /// - /// Eventually, the LIFO slot will become stealable and this option will - /// probably go away. + /// Consider trying this option when the task "scheduled" time is high + /// but the runtime is underutilized. Use tokio-rs/tokio-metrics to + /// collect this data. + /// + /// # Unstable + /// + /// This configuration option is considered a workaround for the LIFO + /// slot not being stealable. When the slot becomes stealable, we will + /// revisit whther or not this option is necessary. See + /// tokio-rs/tokio#4941. + /// + /// # Examples /// /// ``` /// use tokio::runtime; diff --git a/tokio/src/runtime/config.rs b/tokio/src/runtime/config.rs index 38f725b8653..52487356512 100644 --- a/tokio/src/runtime/config.rs +++ b/tokio/src/runtime/config.rs @@ -1,3 +1,4 @@ +#![cfg_attr(not(feature = "full"), allow(dead_code))] use crate::runtime::Callback; pub(crate) struct Config { From f805c839e26fc50bd9e708141948741ca9d0780b Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 09:48:05 -0700 Subject: [PATCH 5/8] try fixing CI for wasm... again --- tokio/src/runtime/config.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tokio/src/runtime/config.rs b/tokio/src/runtime/config.rs index 52487356512..e78661f10bc 100644 --- a/tokio/src/runtime/config.rs +++ b/tokio/src/runtime/config.rs @@ -21,7 +21,6 @@ pub(crate) struct Config { /// /// Eventually, the LIFO slot **will** become stealable, however as a /// stop-gap, this unstable option lets users disable the LIFO task. - #[cfg(not(tokio_wasm))] pub(crate) disable_lifo_slot: bool, #[cfg(tokio_unstable)] From 2fe6b5d8143042169cd8675115c87f08fb198664 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 09:59:44 -0700 Subject: [PATCH 6/8] and again --- tokio/src/runtime/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/config.rs b/tokio/src/runtime/config.rs index e78661f10bc..59c19988e5e 100644 --- a/tokio/src/runtime/config.rs +++ b/tokio/src/runtime/config.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(feature = "full"), allow(dead_code))] +#![cfg_attr(any(not(feature = "full"), tokio_wasm), allow(dead_code))] use crate::runtime::Callback; pub(crate) struct Config { From 1ba424b079fc52648267ccb8f1a0f5b4be3ccb54 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 10:27:50 -0700 Subject: [PATCH 7/8] fix doc test --- tokio/src/runtime/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 5718421b0b2..96137049342 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -820,7 +820,7 @@ impl Builder { /// ``` /// use tokio::runtime; /// - /// let rt = runtime::Builder::new_multi_threaded() + /// let rt = runtime::Builder::new_multi_thread() /// .disable_lifo_slot /// .build() /// .unwrap(); From 87b58043e525b0356bb3f535d46b231db61aa841 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 10:41:37 -0700 Subject: [PATCH 8/8] and again --- tokio/src/runtime/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 96137049342..a4851a681c9 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -821,7 +821,7 @@ impl Builder { /// use tokio::runtime; /// /// let rt = runtime::Builder::new_multi_thread() - /// .disable_lifo_slot + /// .disable_lifo_slot() /// .build() /// .unwrap(); /// ```