From c717313899e3894273c0a7d64a9c56a04f3cdcc9 Mon Sep 17 00:00:00 2001 From: "wudi.daniel" Date: Sun, 21 Nov 2021 13:46:32 +0800 Subject: [PATCH 01/10] feat(rt): read default worker thread from env --- tokio/src/lib.rs | 15 +++++++++------ tokio/src/runtime/builder.rs | 37 +++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 9821c1a62f5..2a1f927e414 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -175,12 +175,15 @@ //! swapping the currently running task on each thread. However, this kind of //! swapping can only happen at `.await` points, so code that spends a long time //! without reaching an `.await` will prevent other tasks from running. To -//! combat this, Tokio provides two kinds of threads: Core threads and blocking -//! threads. The core threads are where all asynchronous code runs, and Tokio -//! will by default spawn one for each CPU core. The blocking threads are -//! spawned on demand, can be used to run blocking code that would otherwise -//! block other tasks from running and are kept alive when not used for a certain -//! amount of time which can be configured with [`thread_keep_alive`]. +//! combat this, Tokio provides two kinds of threads: Core threads and blocking threads. +//! +//! The core threads are where all asynchronous code runs, and Tokio will by default +//! spawn one for each CPU core. You can use the environment variable "TOKIO_WORKER_THREAD" +//! to override the default value. +//! +//! The blocking threads are spawned on demand, can be used to run blocking code +//! that would otherwise block other tasks from running and are kept alive when +//! not used for a certain amount of time which can be configured with [`thread_keep_alive`]. //! Since it is not possible for Tokio to swap out blocking tasks, like it //! can do with asynchronous code, the upper limit on the number of blocking //! threads is very large. These limits can be configured on the [`Builder`]. diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 91c365fd516..225c8bacc6c 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -5,6 +5,10 @@ use std::fmt; use std::io; use std::time::Duration; +/// This key is used to specify the default worker thread for multi-thread runtime. +#[cfg(feature = "rt-multi-thread")] +const ENV_WORKER_THREAD: &str = "TOKIO_WORKER_THREAD"; + /// Builds Tokio Runtime with custom configuration values. /// /// Methods can be chained in order to set the configuration values. The @@ -82,6 +86,7 @@ pub struct Builder { pub(crate) type ThreadNameFn = std::sync::Arc String + Send + Sync + 'static>; +#[derive(Clone, Copy)] pub(crate) enum Kind { CurrentThread, #[cfg(feature = "rt-multi-thread")] @@ -127,8 +132,9 @@ impl Builder { // The clock starts not-paused start_paused: false, + // Read from environment variable first in multi-threaded mode. // Default to lazy auto-detection (one thread per CPU core) - worker_threads: None, + worker_threads: Self::default_worker_thread(kind), max_blocking_threads: 512, @@ -178,6 +184,8 @@ impl Builder { /// This can be any number above 0 though it is advised to keep this value /// on the smaller side. /// + /// This will override the value read from environment variable "TOKIO_WORKER_THREAD". + /// /// # Default /// /// The default value is the number of cores available to the system. @@ -584,6 +592,33 @@ impl Builder { blocking_pool, }) } + + #[cfg(not(feature = "rt-multi-thread"))] + fn default_worker_thread(_: Kind) -> Option { + None + } + + #[cfg(feature = "rt-multi-thread")] + fn default_worker_thread(kind: Kind) -> Option { + match kind { + // Always return None if using current thread + Kind::CurrentThread => return None, + Kind::MultiThread => {} + }; + match std::env::var(ENV_WORKER_THREAD) { + Ok(s) => { + let n: usize = s.parse().unwrap_or_else(|e| { + panic!( + "{} must be usize, error: {}, value: {}", + ENV_WORKER_THREAD, e, s + ) + }); + assert!(n > 0, "{} cannot be set to 0", ENV_WORKER_THREAD); + Some(n) + } + Err(_) => None, + } + } } cfg_io_driver! { From acf5c5e7e2c2a220ed6f054bccb9fc30e9555808 Mon Sep 17 00:00:00 2001 From: Pure White Date: Tue, 23 Nov 2021 21:31:32 +0800 Subject: [PATCH 02/10] change env name to `TOKIO_WORKER_THREADS` --- tokio/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 2a1f927e414..a731f9be059 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -178,7 +178,7 @@ //! combat this, Tokio provides two kinds of threads: Core threads and blocking threads. //! //! The core threads are where all asynchronous code runs, and Tokio will by default -//! spawn one for each CPU core. You can use the environment variable "TOKIO_WORKER_THREAD" +//! spawn one for each CPU core. You can use the environment variable "TOKIO_WORKER_THREADS" //! to override the default value. //! //! The blocking threads are spawned on demand, can be used to run blocking code From afaff5730339b7982845a881bd8c1ce3b7848065 Mon Sep 17 00:00:00 2001 From: Pure White Date: Tue, 23 Nov 2021 21:31:51 +0800 Subject: [PATCH 03/10] change env name to `TOKIO_WORKER_THREADS` --- 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 225c8bacc6c..2e7716e1725 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -7,7 +7,7 @@ use std::time::Duration; /// This key is used to specify the default worker thread for multi-thread runtime. #[cfg(feature = "rt-multi-thread")] -const ENV_WORKER_THREAD: &str = "TOKIO_WORKER_THREAD"; +const ENV_WORKER_THREAD: &str = "TOKIO_WORKER_THREADS"; /// Builds Tokio Runtime with custom configuration values. /// From 21270cda5d98ed2be126990305f75d77bdb5172d Mon Sep 17 00:00:00 2001 From: Pure White Date: Tue, 23 Nov 2021 21:32:25 +0800 Subject: [PATCH 04/10] change env name to `TOKIO_WORKER_THREADS` --- 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 2e7716e1725..10d4c2b62b1 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -184,7 +184,7 @@ impl Builder { /// This can be any number above 0 though it is advised to keep this value /// on the smaller side. /// - /// This will override the value read from environment variable "TOKIO_WORKER_THREAD". + /// This will override the value read from environment variable "TOKIO_WORKER_THREADS". /// /// # Default /// From 74c90ee8e7cc6c4b53b6c081dc5f59a28aa1b8d1 Mon Sep 17 00:00:00 2001 From: Pure White Date: Tue, 23 Nov 2021 21:38:00 +0800 Subject: [PATCH 05/10] change env name to `TOKIO_WORKER_THREADS` --- tokio/src/runtime/builder.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 10d4c2b62b1..b2a0374ed74 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -5,9 +5,9 @@ use std::fmt; use std::io; use std::time::Duration; -/// This key is used to specify the default worker thread for multi-thread runtime. +/// This key is used to specify the default worker threads for multi-thread runtime. #[cfg(feature = "rt-multi-thread")] -const ENV_WORKER_THREAD: &str = "TOKIO_WORKER_THREADS"; +const ENV_WORKER_THREADS: &str = "TOKIO_WORKER_THREADS"; /// Builds Tokio Runtime with custom configuration values. /// @@ -134,7 +134,7 @@ impl Builder { // Read from environment variable first in multi-threaded mode. // Default to lazy auto-detection (one thread per CPU core) - worker_threads: Self::default_worker_thread(kind), + worker_threads: Self::default_worker_threads(kind), max_blocking_threads: 512, @@ -594,26 +594,26 @@ impl Builder { } #[cfg(not(feature = "rt-multi-thread"))] - fn default_worker_thread(_: Kind) -> Option { + fn default_worker_threads(_: Kind) -> Option { None } #[cfg(feature = "rt-multi-thread")] - fn default_worker_thread(kind: Kind) -> Option { + fn default_worker_threads(kind: Kind) -> Option { match kind { // Always return None if using current thread Kind::CurrentThread => return None, Kind::MultiThread => {} }; - match std::env::var(ENV_WORKER_THREAD) { + match std::env::var(ENV_WORKER_THREADS) { Ok(s) => { let n: usize = s.parse().unwrap_or_else(|e| { panic!( "{} must be usize, error: {}, value: {}", - ENV_WORKER_THREAD, e, s + ENV_WORKER_THREADS, e, s ) }); - assert!(n > 0, "{} cannot be set to 0", ENV_WORKER_THREAD); + assert!(n > 0, "{} cannot be set to 0", ENV_WORKER_THREADS); Some(n) } Err(_) => None, From 74c83e8d035f6421f9ec3101155d465805e18fc8 Mon Sep 17 00:00:00 2001 From: Pure White Date: Fri, 28 Oct 2022 02:32:21 +0800 Subject: [PATCH 06/10] fix compile fail on wasm32-wasi --- tokio/src/runtime/builder.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index d94a7a77c24..fd3465134c2 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -7,7 +7,7 @@ use std::io; use std::time::Duration; /// This key is used to specify the default worker threads for multi-thread runtime. -#[cfg(feature = "rt-multi-thread")] +#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))] const ENV_WORKER_THREADS: &str = "TOKIO_WORKER_THREADS"; /// Builds Tokio Runtime with custom configuration values. @@ -925,12 +925,12 @@ impl Builder { }) } - #[cfg(not(feature = "rt-multi-thread"))] + #[cfg(any(not(feature = "rt-multi-thread"), tokio_wasi))] fn default_worker_threads(_: Kind) -> Option { None } - #[cfg(feature = "rt-multi-thread")] + #[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))] fn default_worker_threads(kind: Kind) -> Option { match kind { // Always return None if using current thread From 06ca08e00ff393b87da61eeb8da7ee8da9bc563d Mon Sep 17 00:00:00 2001 From: Pure White Date: Wed, 21 Dec 2022 11:34:09 +0800 Subject: [PATCH 07/10] Update tokio/src/lib.rs Co-authored-by: Alice Ryhl --- tokio/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 366cc222ba7..05767d017bc 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -177,7 +177,7 @@ //! combat this, Tokio provides two kinds of threads: Core threads and blocking threads. //! //! The core threads are where all asynchronous code runs, and Tokio will by default -//! spawn one for each CPU core. You can use the environment variable "TOKIO_WORKER_THREADS" +//! spawn one for each CPU core. You can use the environment variable `TOKIO_WORKER_THREADS` //! to override the default value. //! //! The blocking threads are spawned on demand, can be used to run blocking code From 015d3a72b9067f04ec9fb4f17b4220afa4295a69 Mon Sep 17 00:00:00 2001 From: Pure White Date: Wed, 21 Dec 2022 12:19:26 +0800 Subject: [PATCH 08/10] move detection of threads into num_cpus --- tokio/src/loom/std/mod.rs | 16 +++++++++++++++- tokio/src/runtime/builder.rs | 33 +-------------------------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/tokio/src/loom/std/mod.rs b/tokio/src/loom/std/mod.rs index f0fcd46d23d..15d599b90e4 100644 --- a/tokio/src/loom/std/mod.rs +++ b/tokio/src/loom/std/mod.rs @@ -81,7 +81,21 @@ pub(crate) mod sync { pub(crate) mod sys { #[cfg(feature = "rt-multi-thread")] pub(crate) fn num_cpus() -> usize { - usize::max(1, num_cpus::get()) + const ENV_WORKER_THREADS: &str = "TOKIO_WORKER_THREADS"; + + match std::env::var(ENV_WORKER_THREADS) { + Ok(s) => { + let n = s.parse().unwrap_or_else(|e| { + panic!( + "{} must be usize, error: {}, value: {}", + ENV_WORKER_THREADS, e, s + ) + }); + assert!(n > 0, "{} cannot be set to 0", ENV_WORKER_THREADS); + n + } + Err(_) => usize::max(1, num_cpus::get()), + } } #[cfg(not(feature = "rt-multi-thread"))] diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index beb1db5b100..dc55e89da4c 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -6,10 +6,6 @@ use std::fmt; use std::io; use std::time::Duration; -/// This key is used to specify the default worker threads for multi-thread runtime. -#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))] -const ENV_WORKER_THREADS: &str = "TOKIO_WORKER_THREADS"; - /// Builds Tokio Runtime with custom configuration values. /// /// Methods can be chained in order to set the configuration values. The @@ -244,7 +240,7 @@ impl Builder { // Read from environment variable first in multi-threaded mode. // Default to lazy auto-detection (one thread per CPU core) - worker_threads: Self::default_worker_threads(kind), + worker_threads: None, max_blocking_threads: 512, @@ -926,33 +922,6 @@ impl Builder { blocking_pool, )) } - - #[cfg(any(not(feature = "rt-multi-thread"), tokio_wasi))] - fn default_worker_threads(_: Kind) -> Option { - None - } - - #[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))] - fn default_worker_threads(kind: Kind) -> Option { - match kind { - // Always return None if using current thread - Kind::CurrentThread => return None, - Kind::MultiThread => {} - }; - match std::env::var(ENV_WORKER_THREADS) { - Ok(s) => { - let n: usize = s.parse().unwrap_or_else(|e| { - panic!( - "{} must be usize, error: {}, value: {}", - ENV_WORKER_THREADS, e, s - ) - }); - assert!(n > 0, "{} cannot be set to 0", ENV_WORKER_THREADS); - Some(n) - } - Err(_) => None, - } - } } cfg_io_driver! { From 47103428f10239f994884a71cb5688791a8c203f Mon Sep 17 00:00:00 2001 From: Pure White Date: Wed, 21 Dec 2022 12:24:00 +0800 Subject: [PATCH 09/10] change " to ' --- 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 dc55e89da4c..ea0df2e3b4c 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -306,7 +306,7 @@ impl Builder { /// This can be any number above 0 though it is advised to keep this value /// on the smaller side. /// - /// This will override the value read from environment variable "TOKIO_WORKER_THREADS". + /// This will override the value read from environment variable `TOKIO_WORKER_THREADS`. /// /// # Default /// From 01fee3defa2a790396094ed8c2128368e39e299c Mon Sep 17 00:00:00 2001 From: Pure White Date: Wed, 21 Dec 2022 18:56:49 +0800 Subject: [PATCH 10/10] fix invliad unicode --- tokio/src/loom/std/mod.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tokio/src/loom/std/mod.rs b/tokio/src/loom/std/mod.rs index 15d599b90e4..6bd1ad93dcf 100644 --- a/tokio/src/loom/std/mod.rs +++ b/tokio/src/loom/std/mod.rs @@ -87,14 +87,20 @@ pub(crate) mod sys { Ok(s) => { let n = s.parse().unwrap_or_else(|e| { panic!( - "{} must be usize, error: {}, value: {}", + "\"{}\" must be usize, error: {}, value: {}", ENV_WORKER_THREADS, e, s ) }); - assert!(n > 0, "{} cannot be set to 0", ENV_WORKER_THREADS); + assert!(n > 0, "\"{}\" cannot be set to 0", ENV_WORKER_THREADS); n } - Err(_) => usize::max(1, num_cpus::get()), + Err(std::env::VarError::NotPresent) => usize::max(1, num_cpus::get()), + Err(std::env::VarError::NotUnicode(e)) => { + panic!( + "\"{}\" must be valid unicode, error: {:?}", + ENV_WORKER_THREADS, e + ) + } } }