Skip to content
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

rt: add unstable option to disable the LIFO slot #4936

Merged
merged 8 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tokio/src/runtime/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ pub struct Builder {
/// How many ticks before yielding to the driver for timer and I/O events?
pub(super) event_interval: u32,

/// 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,
carllerche marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(tokio_unstable)]
pub(super) unhandled_panic: UnhandledPanic,
}
Expand Down Expand Up @@ -252,6 +257,8 @@ impl Builder {

#[cfg(tokio_unstable)]
unhandled_panic: UnhandledPanic::Ignore,

disable_lifo_slot: false,
}
}

Expand Down Expand Up @@ -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.
carllerche marked this conversation as resolved.
Show resolved Hide resolved
///
/// This configuration option will disable this heuristic resulting in
/// all scheduled tasks being pushed into the worker-local queue, which
/// is stealable.
Comment on lines +803 to +805
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth stating somewhere that this option does nothing with the single-threaded scheduler?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, it should though 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, then maybe we should change this paragraph, which only mentioned the multi-threaded scheduler?

/// The multi-threaded scheduler includes a heuristic for optimizing
/// message-passing patterns. This heuristic results in the **last**
/// scheduled task being polled first.
///
and/or add an example disabling the lifo slot in a single threaded scheduler?

///
/// Eventually, the LIFO slot will become stealable and this option will
/// probably go away.
carllerche marked this conversation as resolved.
Show resolved Hide resolved
///
carllerche marked this conversation as resolved.
Show resolved Hide resolved
carllerche marked this conversation as resolved.
Show resolved Hide resolved
/// ```
/// 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<Runtime> {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions tokio/src/runtime/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ pub(crate) struct Config {
/// Callback for a worker unparking itself
pub(crate) after_unpark: Option<Callback>,

/// 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.
#[cfg(not(tokio_wasm))]
pub(crate) disable_lifo_slot: bool,

#[cfg(tokio_unstable)]
/// How to respond to unhandled task panics.
pub(crate) unhandled_panic: crate::runtime::UnhandledPanic,
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/thread_pool/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tokio/tests/rt_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
}
}