From 2890d0c3db4f595330d8d223bfbfeb81e205b048 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sat, 25 May 2024 22:49:17 +0100 Subject: [PATCH] metrics: fix blocking_threads count (#6551) --- tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs | 5 ++++- .../src/runtime/scheduler/multi_thread_alt/handle/metrics.rs | 5 ++++- tokio/tests/rt_metrics.rs | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs b/tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs index 838694fc89e..3d614b478c5 100644 --- a/tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs +++ b/tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs @@ -8,7 +8,10 @@ impl Handle { } pub(crate) fn num_blocking_threads(&self) -> usize { - self.blocking_spawner.num_threads() + // workers are currently spawned using spawn_blocking + self.blocking_spawner + .num_threads() + .saturating_sub(self.num_workers()) } pub(crate) fn num_idle_blocking_threads(&self) -> usize { diff --git a/tokio/src/runtime/scheduler/multi_thread_alt/handle/metrics.rs b/tokio/src/runtime/scheduler/multi_thread_alt/handle/metrics.rs index 838694fc89e..3d614b478c5 100644 --- a/tokio/src/runtime/scheduler/multi_thread_alt/handle/metrics.rs +++ b/tokio/src/runtime/scheduler/multi_thread_alt/handle/metrics.rs @@ -8,7 +8,10 @@ impl Handle { } pub(crate) fn num_blocking_threads(&self) -> usize { - self.blocking_spawner.num_threads() + // workers are currently spawned using spawn_blocking + self.blocking_spawner + .num_threads() + .saturating_sub(self.num_workers()) } pub(crate) fn num_idle_blocking_threads(&self) -> usize { diff --git a/tokio/tests/rt_metrics.rs b/tokio/tests/rt_metrics.rs index 6a710a46ce6..2446deb6b41 100644 --- a/tokio/tests/rt_metrics.rs +++ b/tokio/tests/rt_metrics.rs @@ -31,6 +31,11 @@ fn num_blocking_threads() { assert_eq!(0, rt.metrics().num_blocking_threads()); let _ = rt.block_on(rt.spawn_blocking(move || {})); assert_eq!(1, rt.metrics().num_blocking_threads()); + + let rt = threaded(); + assert_eq!(0, rt.metrics().num_blocking_threads()); + let _ = rt.block_on(rt.spawn_blocking(move || {})); + assert_eq!(1, rt.metrics().num_blocking_threads()); } #[test]