Skip to content

Commit

Permalink
metrics: add MetricAtomicU64 and use in metrics (#6574)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh authored May 23, 2024
1 parent 16ef7b1 commit cba86cf
Show file tree
Hide file tree
Showing 14 changed files with 722 additions and 633 deletions.
3 changes: 2 additions & 1 deletion spellcheck.dic
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
284
285
&
+
<
Expand Down Expand Up @@ -34,6 +34,7 @@ amongst
api
APIs
async
atomics
awaitable
backend
backpressure
Expand Down
28 changes: 23 additions & 5 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,45 @@ macro_rules! cfg_macros {
macro_rules! cfg_metrics {
($($item:item)*) => {
$(
// For now, metrics is only disabled in loom tests.
// When stabilized, it might have a dedicated feature flag.
#[cfg(all(tokio_unstable, not(loom)))]
#[cfg(tokio_unstable)]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
$item
)*
}
}

/// Some metrics require 64-bit atomics.
macro_rules! cfg_64bit_metrics {
($($item:item)*) => {
$(
#[cfg(target_has_atomic = "64")]
#[cfg_attr(docsrs, doc(cfg(target_has_atomic = "64")))]
$item
)*
}
}

macro_rules! cfg_no_64bit_metrics {
($($item:item)*) => {
$(
#[cfg(not(target_has_atomic = "64"))]
$item
)*
}
}

macro_rules! cfg_not_metrics {
($($item:item)*) => {
$(
#[cfg(not(all(tokio_unstable, not(loom))))]
#[cfg(not(tokio_unstable))]
$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 )*
}
}

Expand Down
15 changes: 9 additions & 6 deletions tokio/src/runtime/metrics/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::loom::sync::atomic::{AtomicU64, Ordering::Relaxed};
use crate::loom::sync::atomic::Ordering::Relaxed;
use crate::util::metric_atomics::MetricAtomicU64;

use std::cmp;
use std::ops::Range;

#[derive(Debug)]
pub(crate) struct Histogram {
/// The histogram buckets
buckets: Box<[AtomicU64]>,
buckets: Box<[MetricAtomicU64]>,

/// Bucket scale, linear or log
scale: HistogramScale,
Expand Down Expand Up @@ -53,8 +54,10 @@ impl Histogram {
self.buckets.len()
}

pub(crate) fn get(&self, bucket: usize) -> u64 {
self.buckets[bucket].load(Relaxed)
cfg_64bit_metrics! {
pub(crate) fn get(&self, bucket: usize) -> u64 {
self.buckets[bucket].load(Relaxed)
}
}

pub(crate) fn bucket_range(&self, bucket: usize) -> Range<u64> {
Expand Down Expand Up @@ -150,7 +153,7 @@ impl HistogramBuilder {

Histogram {
buckets: (0..self.num_buckets)
.map(|_| AtomicU64::new(0))
.map(|_| MetricAtomicU64::new(0))
.collect::<Vec<_>>()
.into_boxed_slice(),
resolution,
Expand All @@ -165,7 +168,7 @@ impl Default for HistogramBuilder {
}
}

#[cfg(test)]
#[cfg(all(test, target_has_atomic = "64"))]
mod test {
use super::*;

Expand Down
14 changes: 7 additions & 7 deletions tokio/src/runtime/metrics/io.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#![cfg_attr(not(feature = "net"), allow(dead_code))]

use crate::loom::sync::atomic::{AtomicU64, Ordering::Relaxed};
use crate::{loom::sync::atomic::Ordering::Relaxed, util::metric_atomics::MetricAtomicU64};

#[derive(Default)]
pub(crate) struct IoDriverMetrics {
pub(super) fd_registered_count: AtomicU64,
pub(super) fd_deregistered_count: AtomicU64,
pub(super) ready_count: AtomicU64,
pub(super) fd_registered_count: MetricAtomicU64,
pub(super) fd_deregistered_count: MetricAtomicU64,
pub(super) ready_count: MetricAtomicU64,
}

impl IoDriverMetrics {
pub(crate) fn incr_fd_count(&self) {
self.fd_registered_count.fetch_add(1, Relaxed);
self.fd_registered_count.add(1, Relaxed);
}

pub(crate) fn dec_fd_count(&self) {
self.fd_deregistered_count.fetch_add(1, Relaxed);
self.fd_deregistered_count.add(1, Relaxed);
}

pub(crate) fn incr_ready_count_by(&self, amt: u64) {
self.ready_count.fetch_add(amt, Relaxed);
self.ready_count.add(amt, Relaxed);
}
}
Loading

0 comments on commit cba86cf

Please sign in to comment.