Skip to content

Commit

Permalink
metrics: fix blocking_threads count (#6551)
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate authored May 25, 2024
1 parent 0a85a96 commit 2890d0c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions tokio/tests/rt_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2890d0c

Please sign in to comment.