Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: add MetricAtomicU64 and use in metrics #6574

Merged
merged 4 commits into from
May 23, 2024

Conversation

rcoh
Copy link
Contributor

@rcoh rcoh commented May 20, 2024

Motivation

As we move to stabilize metrics, we need a graceful way to support U64 on platforms without 64-bit atomics.

Note: this is a breaking change on platforms without 32-bit atomics. Previously, they would have used a lock fallback provided by loom. Now, they simply do not have read access to these metrics.

Solution

This commit adds MetricAtomicU64 which is empty on platforms that do not support 64-bit atomics. Write operations are no-ops and read operations are not defined. This also allows us to remove the loom gating on cfg_metrics. This will allow #6556 to avoid the need for a separate cfg_metrics gate.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-metrics Module: tokio/runtime/metrics labels May 21, 2024
@Darksonn
Copy link
Contributor

Is this intended to be merged before or after #6556?

@rcoh
Copy link
Contributor Author

rcoh commented May 21, 2024 via email

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall lgtm.

spellcheck.dic Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also update the counter on the first line.

Comment on lines 230 to 246
macro_rules! cfg_64bit_metrics {
($($item:item)*) => {
$(
#[cfg(all(target_has_atomic = "64"))]
#[cfg_attr(docsrs, doc(cfg(target_has_atomic = "64")))]
$item
)*
}

}

macro_rules! cfg_no_64bit_metrics {
($($item:item)*) => {
$(
// For now, metrics is only disabled in loom tests.
// When stabilized, it might have a dedicated feature flag.
#[cfg(not(target_has_atomic = "64"))]
$item
)*
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some empty lines here (line 238 and 250).

Comment on lines 241 to 245
macro_rules! cfg_no_64bit_metrics {
($($item:item)*) => {
$(
// For now, metrics is only disabled in loom tests.
// When stabilized, it might have a dedicated feature flag.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems out of date.

Comment on lines 117 to 119
cfg_64bit_metrics! {

/// Returns the number of times that tasks have been forced to yield back to the scheduler
/// after exhausting their task budgets.
///
/// This count starts at zero when the runtime is created and increases by one each time a task yields due to exhausting its budget.
///
/// The counter is monotonically increasing. It is never decremented or
/// reset to zero.
pub fn budget_forced_yield_count(&self) -> u64 {
self.handle
.inner
.scheduler_metrics()
.budget_forced_yield_count
.load(Relaxed)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty lines.

@@ -1,4 +1,4 @@
use crate::loom::sync::atomic::{AtomicU64, Ordering::Relaxed};
use crate::{loom::sync::atomic::Ordering::Relaxed, util::metric_atomics::MetricAtomicU64};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines 43 to 44
// on platforms without 64-bit atomics, fetch-add returns unit
pub(crate) fn fetch_add(&self, _value: u64, _ordering: Ordering) { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I would probably just return unit from both. (And perhaps rename to add.)

// 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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this comment is also out of date?

macro_rules! cfg_64bit_metrics {
($($item:item)*) => {
$(
#[cfg(all(target_has_atomic = "64"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could simplify this condition:

Suggested change
#[cfg(all(target_has_atomic = "64"))]
#[cfg(target_has_atomic = "64")]

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

rcoh added 4 commits May 23, 2024 10:23
As we move to stabilize metrics, we need a graceful way to support U64 on platforms
without 64-bit atomics. This commit adds MetricAtomicU64 which is empty on platforms
that do not support 64-bit atomics. Write operations are no-ops and read operations are not
defined.
@Darksonn Darksonn merged commit cba86cf into tokio-rs:master May 23, 2024
77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-metrics Module: tokio/runtime/metrics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants