mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
metrics: stabilize RuntimeMetrics::worker_count (#6556)
This commit is contained in:
@@ -215,7 +215,7 @@ macro_rules! cfg_macros {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! cfg_metrics {
|
macro_rules! cfg_unstable_metrics {
|
||||||
($($item:item)*) => {
|
($($item:item)*) => {
|
||||||
$(
|
$(
|
||||||
#[cfg(tokio_unstable)]
|
#[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)*) => {
|
($($item:item)*) => {
|
||||||
$(
|
$(
|
||||||
#[cfg(not(tokio_unstable))]
|
#[cfg(not(tokio_unstable))]
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ impl SpawnerMetrics {
|
|||||||
self.num_idle_threads.load(Ordering::Relaxed)
|
self.num_idle_threads.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
fn queue_depth(&self) -> usize {
|
fn queue_depth(&self) -> usize {
|
||||||
self.queue_depth.load(Ordering::Relaxed)
|
self.queue_depth.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
@@ -474,7 +474,7 @@ impl Spawner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
impl Spawner {
|
impl Spawner {
|
||||||
pub(crate) fn num_threads(&self) -> usize {
|
pub(crate) fn num_threads(&self) -> usize {
|
||||||
self.inner.metrics.num_threads()
|
self.inner.metrics.num_threads()
|
||||||
|
|||||||
@@ -957,7 +957,7 @@ impl Builder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
/// Enables tracking the distribution of task poll times.
|
/// Enables tracking the distribution of task poll times.
|
||||||
///
|
///
|
||||||
/// Task poll times are not instrumented by default as doing so requires
|
/// Task poll times are not instrumented by default as doing so requires
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ cfg_coop! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg_rt! {
|
cfg_rt! {
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn inc_budget_forced_yield_count() {
|
fn inc_budget_forced_yield_count() {
|
||||||
let _ = context::with_current(|handle| {
|
let _ = context::with_current(|handle| {
|
||||||
@@ -206,7 +206,7 @@ cfg_coop! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_not_metrics! {
|
cfg_not_unstable_metrics! {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn inc_budget_forced_yield_count() {}
|
fn inc_budget_forced_yield_count() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#[cfg(tokio_unstable)]
|
#[cfg(tokio_unstable)]
|
||||||
use crate::runtime;
|
use crate::runtime;
|
||||||
use crate::runtime::{context, scheduler, RuntimeFlavor};
|
use crate::runtime::{context, scheduler, RuntimeFlavor, RuntimeMetrics};
|
||||||
|
|
||||||
/// Handle to the runtime.
|
/// Handle to the runtime.
|
||||||
///
|
///
|
||||||
@@ -393,17 +393,11 @@ impl Handle {
|
|||||||
owned_id.into()
|
owned_id.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
cfg_metrics! {
|
/// Returns a view that lets you get information about how the runtime
|
||||||
use crate::runtime::RuntimeMetrics;
|
/// is performing.
|
||||||
|
pub fn metrics(&self) -> RuntimeMetrics {
|
||||||
impl Handle {
|
RuntimeMetrics::new(self.clone())
|
||||||
/// Returns a view that lets you get information about how the runtime
|
|
||||||
/// is performing.
|
|
||||||
pub fn metrics(&self) -> RuntimeMetrics {
|
|
||||||
RuntimeMetrics::new(self.clone())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ cfg_not_rt_and_metrics_and_net! {
|
|||||||
|
|
||||||
cfg_net! {
|
cfg_net! {
|
||||||
cfg_rt! {
|
cfg_rt! {
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
pub(crate) use crate::runtime::IoDriverMetrics;
|
pub(crate) use crate::runtime::IoDriverMetrics;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::loom::sync::atomic::Ordering::Relaxed;
|
|
||||||
use crate::util::metric_atomics::MetricAtomicU64;
|
use crate::util::metric_atomics::MetricAtomicU64;
|
||||||
|
use std::sync::atomic::Ordering::Relaxed;
|
||||||
|
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#![cfg_attr(not(feature = "net"), allow(dead_code))]
|
#![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)]
|
#[derive(Default)]
|
||||||
pub(crate) struct IoDriverMetrics {
|
pub(crate) struct IoDriverMetrics {
|
||||||
|
|||||||
@@ -8,7 +8,10 @@
|
|||||||
//! [unstable]: crate#unstable-features
|
//! [unstable]: crate#unstable-features
|
||||||
#![allow(clippy::module_inception)]
|
#![allow(clippy::module_inception)]
|
||||||
|
|
||||||
cfg_metrics! {
|
mod runtime;
|
||||||
|
pub use runtime::RuntimeMetrics;
|
||||||
|
|
||||||
|
cfg_unstable_metrics! {
|
||||||
mod batch;
|
mod batch;
|
||||||
pub(crate) use batch::MetricsBatch;
|
pub(crate) use batch::MetricsBatch;
|
||||||
|
|
||||||
@@ -17,9 +20,6 @@ cfg_metrics! {
|
|||||||
#[allow(unreachable_pub)] // rust-lang/rust#57411
|
#[allow(unreachable_pub)] // rust-lang/rust#57411
|
||||||
pub use histogram::HistogramScale;
|
pub use histogram::HistogramScale;
|
||||||
|
|
||||||
mod runtime;
|
|
||||||
#[allow(unreachable_pub)] // rust-lang/rust#57411
|
|
||||||
pub use runtime::RuntimeMetrics;
|
|
||||||
|
|
||||||
mod scheduler;
|
mod scheduler;
|
||||||
pub(crate) use scheduler::SchedulerMetrics;
|
pub(crate) use scheduler::SchedulerMetrics;
|
||||||
@@ -33,7 +33,7 @@ cfg_metrics! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_not_metrics! {
|
cfg_not_unstable_metrics! {
|
||||||
mod mock;
|
mod mock;
|
||||||
|
|
||||||
pub(crate) use mock::{SchedulerMetrics, WorkerMetrics, MetricsBatch, HistogramBuilder};
|
pub(crate) use mock::{SchedulerMetrics, WorkerMetrics, MetricsBatch, HistogramBuilder};
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -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::metrics::Histogram;
|
||||||
use crate::runtime::Config;
|
use crate::runtime::Config;
|
||||||
use crate::util::metric_atomics::MetricAtomicU64;
|
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.
|
/// Retrieve runtime worker metrics.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -388,21 +388,18 @@ cfg_rt! {
|
|||||||
mod thread_id;
|
mod thread_id;
|
||||||
pub(crate) use thread_id::ThreadId;
|
pub(crate) use thread_id::ThreadId;
|
||||||
|
|
||||||
cfg_metrics! {
|
pub(crate) mod metrics;
|
||||||
mod metrics;
|
pub use metrics::RuntimeMetrics;
|
||||||
pub use metrics::{RuntimeMetrics, HistogramScale};
|
|
||||||
|
|
||||||
pub(crate) use metrics::{MetricsBatch, SchedulerMetrics, WorkerMetrics, HistogramBuilder};
|
cfg_unstable_metrics! {
|
||||||
|
pub use metrics::HistogramScale;
|
||||||
|
|
||||||
cfg_net! {
|
cfg_net! {
|
||||||
pub(crate) use metrics::IoDriverMetrics;
|
pub(crate) use metrics::IoDriverMetrics;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_not_metrics! {
|
pub(crate) use metrics::{MetricsBatch, SchedulerMetrics, WorkerMetrics, HistogramBuilder};
|
||||||
pub(crate) mod metrics;
|
|
||||||
pub(crate) use metrics::{SchedulerMetrics, WorkerMetrics, MetricsBatch, HistogramBuilder};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// After thread starts / before thread stops
|
/// After thread starts / before thread stops
|
||||||
type Callback = std::sync::Arc<dyn Fn() + Send + Sync>;
|
type Callback = std::sync::Arc<dyn Fn() + Send + Sync>;
|
||||||
|
|||||||
@@ -455,6 +455,12 @@ impl Runtime {
|
|||||||
pub fn shutdown_background(self) {
|
pub fn shutdown_background(self) {
|
||||||
self.shutdown_timeout(Duration::from_nanos(0));
|
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
|
#[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::UnwindSafe for Runtime {}
|
||||||
|
|
||||||
impl std::panic::RefUnwindSafe 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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -502,7 +502,7 @@ impl Handle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
impl Handle {
|
impl Handle {
|
||||||
pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
|
pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
|
||||||
&self.shared.scheduler_metrics
|
&self.shared.scheduler_metrics
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ cfg_rt_multi_thread! {
|
|||||||
mod rt_multi_thread;
|
mod rt_multi_thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
mod metrics;
|
mod metrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -163,20 +163,22 @@ cfg_rt! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
impl Handle {
|
||||||
|
pub(crate) fn num_workers(&self) -> usize {
|
||||||
|
match self {
|
||||||
|
Handle::CurrentThread(_) => 1,
|
||||||
|
#[cfg(feature = "rt-multi-thread")]
|
||||||
|
Handle::MultiThread(handle) => handle.num_workers(),
|
||||||
|
#[cfg(all(tokio_unstable, feature = "rt-multi-thread"))]
|
||||||
|
Handle::MultiThreadAlt(handle) => handle.num_workers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_unstable_metrics! {
|
||||||
use crate::runtime::{SchedulerMetrics, WorkerMetrics};
|
use crate::runtime::{SchedulerMetrics, WorkerMetrics};
|
||||||
|
|
||||||
impl Handle {
|
impl Handle {
|
||||||
pub(crate) fn num_workers(&self) -> usize {
|
|
||||||
match self {
|
|
||||||
Handle::CurrentThread(_) => 1,
|
|
||||||
#[cfg(feature = "rt-multi-thread")]
|
|
||||||
Handle::MultiThread(handle) => handle.num_workers(),
|
|
||||||
#[cfg(all(tokio_unstable, feature = "rt-multi-thread"))]
|
|
||||||
Handle::MultiThreadAlt(handle) => handle.num_workers(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn num_blocking_threads(&self) -> usize {
|
pub(crate) fn num_blocking_threads(&self) -> usize {
|
||||||
match_flavor!(self, Handle(handle) => handle.num_blocking_threads())
|
match_flavor!(self, Handle(handle) => handle.num_blocking_threads())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,7 @@ use crate::util::RngSeedGenerator;
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
cfg_metrics! {
|
mod metrics;
|
||||||
mod metrics;
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg_taskdump! {
|
cfg_taskdump! {
|
||||||
mod taskdump;
|
mod taskdump;
|
||||||
|
|||||||
@@ -1,44 +1,48 @@
|
|||||||
use super::Handle;
|
use super::Handle;
|
||||||
|
|
||||||
use crate::runtime::{SchedulerMetrics, WorkerMetrics};
|
cfg_unstable_metrics! {
|
||||||
|
use crate::runtime::{SchedulerMetrics, WorkerMetrics};
|
||||||
|
}
|
||||||
|
|
||||||
impl Handle {
|
impl Handle {
|
||||||
pub(crate) fn num_workers(&self) -> usize {
|
pub(crate) fn num_workers(&self) -> usize {
|
||||||
self.shared.worker_metrics.len()
|
self.shared.worker_metrics.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn num_blocking_threads(&self) -> usize {
|
cfg_unstable_metrics! {
|
||||||
// workers are currently spawned using spawn_blocking
|
pub(crate) fn num_blocking_threads(&self) -> usize {
|
||||||
self.blocking_spawner
|
// workers are currently spawned using spawn_blocking
|
||||||
.num_threads()
|
self.blocking_spawner
|
||||||
.saturating_sub(self.num_workers())
|
.num_threads()
|
||||||
}
|
.saturating_sub(self.num_workers())
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn num_idle_blocking_threads(&self) -> usize {
|
pub(crate) fn num_idle_blocking_threads(&self) -> usize {
|
||||||
self.blocking_spawner.num_idle_threads()
|
self.blocking_spawner.num_idle_threads()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn active_tasks_count(&self) -> usize {
|
pub(crate) fn active_tasks_count(&self) -> usize {
|
||||||
self.shared.owned.active_tasks_count()
|
self.shared.owned.active_tasks_count()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
|
pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
|
||||||
&self.shared.scheduler_metrics
|
&self.shared.scheduler_metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn worker_metrics(&self, worker: usize) -> &WorkerMetrics {
|
pub(crate) fn worker_metrics(&self, worker: usize) -> &WorkerMetrics {
|
||||||
&self.shared.worker_metrics[worker]
|
&self.shared.worker_metrics[worker]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn injection_queue_depth(&self) -> usize {
|
pub(crate) fn injection_queue_depth(&self) -> usize {
|
||||||
self.shared.injection_queue_depth()
|
self.shared.injection_queue_depth()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn worker_local_queue_depth(&self, worker: usize) -> usize {
|
pub(crate) fn worker_local_queue_depth(&self, worker: usize) -> usize {
|
||||||
self.shared.worker_local_queue_depth(worker)
|
self.shared.worker_local_queue_depth(worker)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn blocking_queue_depth(&self) -> usize {
|
pub(crate) fn blocking_queue_depth(&self) -> usize {
|
||||||
self.blocking_spawner.queue_depth()
|
self.blocking_spawner.queue_depth()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -546,7 +546,7 @@ impl<T> Steal<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
impl<T> Steal<T> {
|
impl<T> Steal<T> {
|
||||||
pub(crate) fn len(&self) -> usize {
|
pub(crate) fn len(&self) -> usize {
|
||||||
self.0.len() as _
|
self.0.len() as _
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ use std::cell::RefCell;
|
|||||||
use std::task::Waker;
|
use std::task::Waker;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
mod metrics;
|
mod metrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use crate::util::RngSeedGenerator;
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
mod metrics;
|
mod metrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -538,7 +538,7 @@ impl<T> Steal<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
impl<T> Steal<T> {
|
impl<T> Steal<T> {
|
||||||
pub(crate) fn len(&self) -> usize {
|
pub(crate) fn len(&self) -> usize {
|
||||||
self.0.len() as _
|
self.0.len() as _
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ use std::cmp;
|
|||||||
use std::task::Waker;
|
use std::task::Waker;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
mod metrics;
|
mod metrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ fn fits_256_one_at_a_time() {
|
|||||||
local.push_back_or_overflow(task, &inject, &mut stats);
|
local.push_back_or_overflow(task, &inject, &mut stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
assert_metrics!(stats, overflow_count == 0);
|
assert_metrics!(stats, overflow_count == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ fn overflow() {
|
|||||||
local.push_back_or_overflow(task, &inject, &mut stats);
|
local.push_back_or_overflow(task, &inject, &mut stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
assert_metrics!(stats, overflow_count == 1);
|
assert_metrics!(stats, overflow_count == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ fn steal_batch() {
|
|||||||
|
|
||||||
assert!(steal1.steal_into(&mut local2, &mut stats).is_some());
|
assert!(steal1.steal_into(&mut local2, &mut stats).is_some());
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
assert_metrics!(stats, steal_count == 2);
|
assert_metrics!(stats, steal_count == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +184,7 @@ fn stress1() {
|
|||||||
thread::yield_now();
|
thread::yield_now();
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_unstable_metrics! {
|
||||||
assert_metrics!(stats, steal_count == n as _);
|
assert_metrics!(stats, steal_count == n as _);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user