Skip to content

Commit

Permalink
Remove loom gating & cfg_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed May 20, 2024
1 parent e74e836 commit 89a6dbc
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 97 deletions.
25 changes: 5 additions & 20 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,11 @@ macro_rules! cfg_macros {
}
}

// When running tests in loom, mocks are used instead of metrics.
macro_rules! cfg_metrics {
($($item:item)*) => {
$(
#[cfg(not(loom))]
$item
)*
}
}

// See https://github.com/tokio-rs/tokio/issues/6546, https://github.com/tokio-rs/tokio/issues/4073
macro_rules! cfg_unstable_metrics {
($($item:item)*) => {
$(
#[cfg(all(tokio_unstable, not(loom)))]
#[cfg(all(tokio_unstable))]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
$item
)*
Expand All @@ -239,26 +229,21 @@ macro_rules! cfg_unstable_metrics {
macro_rules! cfg_not_unstable_metrics {
($($item:item)*) => {
$(
#[cfg(any(not(tokio_unstable), loom))]
#[cfg(any(not(tokio_unstable)))]
$item
)*
}
}

// Swap in mock metrics when loom is active
macro_rules! cfg_not_metrics {
($($item:item)*) => {
$(
#[cfg(loom)]
$item
)*
}
($($item:item)*) => {};
}

macro_rules! cfg_not_rt_and_metrics_and_net {
($($item:item)*) => {
$( #[cfg(not(all(feature = "net", feature = "rt", all(tokio_unstable, not(loom)))))]$item )*
}
$( #[cfg(not(all(feature = "net", feature = "rt", tokio_unstable)))]$item )*
};
}

macro_rules! cfg_net_or_process {
Expand Down
36 changes: 17 additions & 19 deletions tokio/src/runtime/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,26 +395,24 @@ impl Handle {
}
}

cfg_metrics! {
use crate::runtime::RuntimeMetrics;
use crate::runtime::RuntimeMetrics;

impl Handle {
/// Returns a view that lets you get information about how the runtime
/// is performing.
///
/// # Examples
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let metrics = Handle::current().metrics();
/// println!("The runtime has {} workers", metrics.num_workers());
/// }
/// ```
pub fn metrics(&self) -> RuntimeMetrics {
RuntimeMetrics::new(self.clone())
}
impl Handle {
/// Returns a view that lets you get information about how the runtime
/// is performing.
///
/// # Examples
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let metrics = Handle::current().metrics();
/// println!("The runtime has {} workers", metrics.num_workers());
/// }
/// ```
pub fn metrics(&self) -> RuntimeMetrics {
RuntimeMetrics::new(self.clone())
}
}

Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/metrics/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::loom::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};

use std::cmp;
use std::ops::Range;
Expand Down
40 changes: 17 additions & 23 deletions tokio/src/runtime/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,34 @@
//! [unstable]: crate#unstable-features
#![allow(clippy::module_inception)]

cfg_metrics! {
mod runtime;
pub use runtime::RuntimeMetrics;
mod runtime;
pub use runtime::RuntimeMetrics;

cfg_unstable_metrics! {
mod batch;
pub(crate) use batch::MetricsBatch;
cfg_unstable_metrics! {

mod histogram;
pub(crate) use histogram::{HistogramBatch, HistogramBuilder, Histogram};
mod batch;
pub(crate) use batch::MetricsBatch;

mod scheduler;
pub(crate) use scheduler::SchedulerMetrics;
mod scheduler;
pub(crate) use scheduler::SchedulerMetrics;

mod worker;
pub(crate) use worker::WorkerMetrics;
mod histogram;
pub(crate) use histogram::{Histogram, HistogramBatch, HistogramBuilder};

cfg_net_and_unstable! {
mod io;
pub(crate) use io::IoDriverMetrics;
}
mod worker;
pub(crate) use worker::WorkerMetrics;

#[allow(unreachable_pub)] // rust-lang/rust#57411
pub use histogram::HistogramScale;
cfg_net_and_unstable! {
mod io;
pub(crate) use io::IoDriverMetrics;
}

cfg_not_unstable_metrics! {
mod mock;

pub(crate) use mock::{SchedulerMetrics, WorkerMetrics, MetricsBatch, HistogramBuilder};
}
#[allow(unreachable_pub)] // rust-lang/rust#57411
pub use histogram::HistogramScale;
}

cfg_not_metrics! {
cfg_not_unstable_metrics! {
mod mock;

pub(crate) use mock::{SchedulerMetrics, WorkerMetrics, MetricsBatch, HistogramBuilder};
Expand Down
12 changes: 5 additions & 7 deletions tokio/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,13 @@ cfg_rt! {
pub(crate) use thread_id::ThreadId;

pub(crate) mod metrics;
cfg_metrics! {
pub use metrics::RuntimeMetrics;
pub use metrics::RuntimeMetrics;

cfg_unstable_metrics! {
pub use metrics::HistogramScale;
cfg_unstable_metrics! {
pub use metrics::HistogramScale;

cfg_net! {
pub(crate) use metrics::IoDriverMetrics;
}
cfg_net! {
pub(crate) use metrics::IoDriverMetrics;
}
}

Expand Down
10 changes: 4 additions & 6 deletions tokio/src/runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,10 @@ impl Runtime {
self.shutdown_timeout(Duration::from_nanos(0));
}

cfg_metrics! {
/// Returns a view that lets you get information about how the runtime
/// is performing.
pub fn metrics(&self) -> crate::runtime::RuntimeMetrics {
self.handle.metrics()
}
/// Returns a view that lets you get information about how the runtime
/// is performing.
pub fn metrics(&self) -> crate::runtime::RuntimeMetrics {
self.handle.metrics()
}
}

Expand Down
18 changes: 8 additions & 10 deletions tokio/src/runtime/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,14 @@ 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(),
}
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(),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions tokio/src/runtime/scheduler/multi_thread_alt/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use crate::util::RngSeedGenerator;

use std::fmt;

cfg_metrics! {
mod metrics;
}
mod metrics;

/// Handle to the multi thread scheduler
pub(crate) struct Handle {
Expand Down
8 changes: 3 additions & 5 deletions tokio/src/runtime/scheduler/multi_thread_alt/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,9 @@ impl<T> Steal<T> {
}
}

cfg_metrics! {
impl<T> Steal<T> {
pub(crate) fn len(&self) -> usize {
self.0.len() as _
}
impl<T> Steal<T> {
pub(crate) fn len(&self) -> usize {
self.0.len() as _
}
}

Expand Down
4 changes: 1 addition & 3 deletions tokio/src/runtime/scheduler/multi_thread_alt/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ use std::cmp;
use std::task::Waker;
use std::time::Duration;

cfg_metrics! {
mod metrics;
}
mod metrics;

mod taskdump_mock;

Expand Down

0 comments on commit 89a6dbc

Please sign in to comment.