metrics: stabilize RuntimeMetrics::worker_count (#6556)

This commit is contained in:
Russell Cohen 2024-05-28 15:55:20 -04:00 committed by GitHub
parent 9e00b266e0
commit 86658bd87d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 877 additions and 880 deletions

View File

@ -215,7 +215,7 @@ macro_rules! cfg_macros {
}
}
macro_rules! cfg_metrics {
macro_rules! cfg_unstable_metrics {
($($item:item)*) => {
$(
#[cfg(tokio_unstable)]
@ -245,7 +245,7 @@ macro_rules! cfg_no_64bit_metrics {
}
}
macro_rules! cfg_not_metrics {
macro_rules! cfg_not_unstable_metrics {
($($item:item)*) => {
$(
#[cfg(not(tokio_unstable))]

View File

@ -40,7 +40,7 @@ impl SpawnerMetrics {
self.num_idle_threads.load(Ordering::Relaxed)
}
cfg_metrics! {
cfg_unstable_metrics! {
fn queue_depth(&self) -> usize {
self.queue_depth.load(Ordering::Relaxed)
}
@ -474,7 +474,7 @@ impl Spawner {
}
}
cfg_metrics! {
cfg_unstable_metrics! {
impl Spawner {
pub(crate) fn num_threads(&self) -> usize {
self.inner.metrics.num_threads()

View File

@ -957,7 +957,7 @@ impl Builder {
}
}
cfg_metrics! {
cfg_unstable_metrics! {
/// Enables tracking the distribution of task poll times.
///
/// Task poll times are not instrumented by default as doing so requires

View File

@ -197,7 +197,7 @@ cfg_coop! {
}
cfg_rt! {
cfg_metrics! {
cfg_unstable_metrics! {
#[inline(always)]
fn inc_budget_forced_yield_count() {
let _ = context::with_current(|handle| {
@ -206,7 +206,7 @@ cfg_coop! {
}
}
cfg_not_metrics! {
cfg_not_unstable_metrics! {
#[inline(always)]
fn inc_budget_forced_yield_count() {}
}

View File

@ -1,6 +1,6 @@
#[cfg(tokio_unstable)]
use crate::runtime;
use crate::runtime::{context, scheduler, RuntimeFlavor};
use crate::runtime::{context, scheduler, RuntimeFlavor, RuntimeMetrics};
/// Handle to the runtime.
///
@ -393,19 +393,13 @@ impl Handle {
owned_id.into()
}
}
}
cfg_metrics! {
use crate::runtime::RuntimeMetrics;
impl Handle {
/// Returns a view that lets you get information about how the runtime
/// is performing.
pub fn metrics(&self) -> RuntimeMetrics {
RuntimeMetrics::new(self.clone())
}
}
}
cfg_taskdump! {
impl Handle {

View File

@ -17,7 +17,7 @@ cfg_not_rt_and_metrics_and_net! {
cfg_net! {
cfg_rt! {
cfg_metrics! {
cfg_unstable_metrics! {
pub(crate) use crate::runtime::IoDriverMetrics;
}
}

View File

@ -1,5 +1,5 @@
use crate::loom::sync::atomic::Ordering::Relaxed;
use crate::util::metric_atomics::MetricAtomicU64;
use std::sync::atomic::Ordering::Relaxed;
use std::cmp;
use std::ops::Range;

View File

@ -1,6 +1,7 @@
#![cfg_attr(not(feature = "net"), allow(dead_code))]
use crate::{loom::sync::atomic::Ordering::Relaxed, util::metric_atomics::MetricAtomicU64};
use crate::util::metric_atomics::MetricAtomicU64;
use std::sync::atomic::Ordering::Relaxed;
#[derive(Default)]
pub(crate) struct IoDriverMetrics {

View File

@ -8,7 +8,10 @@
//! [unstable]: crate#unstable-features
#![allow(clippy::module_inception)]
cfg_metrics! {
mod runtime;
pub use runtime::RuntimeMetrics;
cfg_unstable_metrics! {
mod batch;
pub(crate) use batch::MetricsBatch;
@ -17,9 +20,6 @@ cfg_metrics! {
#[allow(unreachable_pub)] // rust-lang/rust#57411
pub use histogram::HistogramScale;
mod runtime;
#[allow(unreachable_pub)] // rust-lang/rust#57411
pub use runtime::RuntimeMetrics;
mod scheduler;
pub(crate) use scheduler::SchedulerMetrics;
@ -33,7 +33,7 @@ cfg_metrics! {
}
}
cfg_not_metrics! {
cfg_not_unstable_metrics! {
mod mock;
pub(crate) use mock::{SchedulerMetrics, WorkerMetrics, MetricsBatch, HistogramBuilder};

View File

@ -1,10 +1,12 @@
use crate::runtime::Handle;
cfg_unstable_metrics! {
use std::ops::Range;
cfg_64bit_metrics! {
use std::sync::atomic::Ordering::Relaxed;
}
use std::time::Duration;
}
/// Handle to the runtime's metrics.
///
@ -45,6 +47,8 @@ impl RuntimeMetrics {
self.handle.inner.num_workers()
}
cfg_unstable_metrics! {
/// Returns the number of additional threads spawned by the runtime.
///
/// The number of workers is set by configuring `max_blocking_threads` on
@ -839,10 +843,8 @@ impl RuntimeMetrics {
pub fn blocking_queue_depth(&self) -> usize {
self.handle.inner.blocking_queue_depth()
}
}
cfg_net! {
impl RuntimeMetrics {
cfg_64bit_metrics! {
/// Returns the number of file descriptors that have been registered with the
/// runtime's I/O driver.
@ -930,3 +932,4 @@ cfg_net! {
}
}
}
}

View File

@ -1,8 +1,10 @@
use crate::loom::sync::atomic::AtomicUsize;
use crate::loom::sync::atomic::Ordering::Relaxed;
use crate::runtime::metrics::Histogram;
use crate::runtime::Config;
use crate::util::metric_atomics::MetricAtomicU64;
// This is NOT the Loom atomic. To avoid an unnecessary state explosion in loom,
// all metrics use regular atomics.
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::Relaxed;
/// Retrieve runtime worker metrics.
///

View File

@ -388,21 +388,18 @@ cfg_rt! {
mod thread_id;
pub(crate) use thread_id::ThreadId;
cfg_metrics! {
mod metrics;
pub use metrics::{RuntimeMetrics, HistogramScale};
pub(crate) mod metrics;
pub use metrics::RuntimeMetrics;
pub(crate) use metrics::{MetricsBatch, SchedulerMetrics, WorkerMetrics, HistogramBuilder};
cfg_unstable_metrics! {
pub use metrics::HistogramScale;
cfg_net! {
pub(crate) use metrics::IoDriverMetrics;
}
}
cfg_not_metrics! {
pub(crate) mod metrics;
pub(crate) use metrics::{SchedulerMetrics, WorkerMetrics, MetricsBatch, HistogramBuilder};
}
pub(crate) use metrics::{MetricsBatch, SchedulerMetrics, WorkerMetrics, HistogramBuilder};
/// After thread starts / before thread stops
type Callback = std::sync::Arc<dyn Fn() + Send + Sync>;

View File

@ -455,6 +455,12 @@ impl Runtime {
pub fn shutdown_background(self) {
self.shutdown_timeout(Duration::from_nanos(0));
}
/// Returns a view that lets you get information about how the runtime
/// is performing.
pub fn metrics(&self) -> crate::runtime::RuntimeMetrics {
self.handle.metrics()
}
}
#[allow(clippy::single_match)] // there are comments in the error branch, so we don't want if-let
@ -486,13 +492,3 @@ impl Drop for Runtime {
impl std::panic::UnwindSafe for Runtime {}
impl std::panic::RefUnwindSafe for Runtime {}
cfg_metrics! {
impl Runtime {
/// Returns a view that lets you get information about how the runtime
/// is performing.
pub fn metrics(&self) -> crate::runtime::RuntimeMetrics {
self.handle.metrics()
}
}
}

View File

@ -502,7 +502,7 @@ impl Handle {
}
}
cfg_metrics! {
cfg_unstable_metrics! {
impl Handle {
pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
&self.shared.scheduler_metrics

View File

@ -16,7 +16,7 @@ cfg_rt_multi_thread! {
mod rt_multi_thread;
}
cfg_metrics! {
cfg_unstable_metrics! {
mod metrics;
}

View File

@ -163,9 +163,6 @@ cfg_rt! {
}
}
cfg_metrics! {
use crate::runtime::{SchedulerMetrics, WorkerMetrics};
impl Handle {
pub(crate) fn num_workers(&self) -> usize {
match self {
@ -176,7 +173,12 @@ cfg_rt! {
Handle::MultiThreadAlt(handle) => handle.num_workers(),
}
}
}
cfg_unstable_metrics! {
use crate::runtime::{SchedulerMetrics, WorkerMetrics};
impl Handle {
pub(crate) fn num_blocking_threads(&self) -> usize {
match_flavor!(self, Handle(handle) => handle.num_blocking_threads())
}

View File

@ -9,9 +9,7 @@ use crate::util::RngSeedGenerator;
use std::fmt;
cfg_metrics! {
mod metrics;
}
cfg_taskdump! {
mod taskdump;

View File

@ -1,12 +1,15 @@
use super::Handle;
cfg_unstable_metrics! {
use crate::runtime::{SchedulerMetrics, WorkerMetrics};
}
impl Handle {
pub(crate) fn num_workers(&self) -> usize {
self.shared.worker_metrics.len()
}
cfg_unstable_metrics! {
pub(crate) fn num_blocking_threads(&self) -> usize {
// workers are currently spawned using spawn_blocking
self.blocking_spawner
@ -42,3 +45,4 @@ impl Handle {
self.blocking_spawner.queue_depth()
}
}
}

View File

@ -546,7 +546,7 @@ impl<T> Steal<T> {
}
}
cfg_metrics! {
cfg_unstable_metrics! {
impl<T> Steal<T> {
pub(crate) fn len(&self) -> usize {
self.0.len() as _

View File

@ -74,7 +74,7 @@ use std::cell::RefCell;
use std::task::Waker;
use std::time::Duration;
cfg_metrics! {
cfg_unstable_metrics! {
mod metrics;
}

View File

@ -9,7 +9,7 @@ use crate::util::RngSeedGenerator;
use std::fmt;
cfg_metrics! {
cfg_unstable_metrics! {
mod metrics;
}

View File

@ -538,7 +538,7 @@ impl<T> Steal<T> {
}
}
cfg_metrics! {
cfg_unstable_metrics! {
impl<T> Steal<T> {
pub(crate) fn len(&self) -> usize {
self.0.len() as _

View File

@ -74,7 +74,7 @@ use std::cmp;
use std::task::Waker;
use std::time::Duration;
cfg_metrics! {
cfg_unstable_metrics! {
mod metrics;
}

View File

@ -40,7 +40,7 @@ fn fits_256_one_at_a_time() {
local.push_back_or_overflow(task, &inject, &mut stats);
}
cfg_metrics! {
cfg_unstable_metrics! {
assert_metrics!(stats, overflow_count == 0);
}
@ -98,7 +98,7 @@ fn overflow() {
local.push_back_or_overflow(task, &inject, &mut stats);
}
cfg_metrics! {
cfg_unstable_metrics! {
assert_metrics!(stats, overflow_count == 1);
}
@ -128,7 +128,7 @@ fn steal_batch() {
assert!(steal1.steal_into(&mut local2, &mut stats).is_some());
cfg_metrics! {
cfg_unstable_metrics! {
assert_metrics!(stats, steal_count == 2);
}
@ -184,7 +184,7 @@ fn stress1() {
thread::yield_now();
}
cfg_metrics! {
cfg_unstable_metrics! {
assert_metrics!(stats, steal_count == n as _);
}