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 a new metric for budget exhaustion yields #5517

Merged
merged 4 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tokio/src/runtime/coop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,35 @@ cfg_coop! {
cell.set(budget);
Poll::Ready(restore)
} else {
inc_budget_forced_yield_count();

cx.waker().wake_by_ref();
Poll::Pending
}
}).unwrap_or(Poll::Ready(RestoreOnPending(Cell::new(Budget::unconstrained()))))
}

cfg_rt! {
cfg_metrics! {
#[inline(always)]
fn inc_budget_forced_yield_count() {
if let Ok(handle) = context::try_current() {
handle.scheduler_metrics().inc_budget_forced_yield_count();
}
}
}

cfg_not_metrics! {
#[inline(always)]
fn inc_budget_forced_yield_count() {}
}
}

cfg_not_rt! {
#[inline(always)]
fn inc_budget_forced_yield_count() {}
}

impl Budget {
/// Decrements the budget. Returns `true` if successful. Decrementing fails
/// when there is not enough remaining budget.
Expand Down
15 changes: 15 additions & 0 deletions tokio/src/runtime/metrics/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ impl RuntimeMetrics {
.load(Relaxed)
}

/// 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)
}

/// Returns the total number of times the given worker thread has parked.
///
/// The worker park count starts at zero when the runtime is created and
Expand Down
7 changes: 7 additions & 0 deletions tokio/src/runtime/metrics/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ use crate::loom::sync::atomic::{AtomicU64, Ordering::Relaxed};
pub(crate) struct SchedulerMetrics {
/// Number of tasks that are scheduled from outside the runtime.
pub(super) remote_schedule_count: AtomicU64,
pub(super) budget_forced_yield_count: AtomicU64,
}

impl SchedulerMetrics {
pub(crate) fn new() -> SchedulerMetrics {
SchedulerMetrics {
remote_schedule_count: AtomicU64::new(0),
budget_forced_yield_count: AtomicU64::new(0),
}
}

/// Increment the number of tasks scheduled externally
pub(crate) fn inc_remote_schedule_count(&self) {
self.remote_schedule_count.fetch_add(1, Relaxed);
}

/// Increment the number of tasks forced to yield due to budget exhaustion
pub(crate) fn inc_budget_forced_yield_count(&self) {
self.budget_forced_yield_count.fetch_add(1, Relaxed);
}
}
31 changes: 31 additions & 0 deletions tokio/tests/rt_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", tokio_unstable, not(tokio_wasi)))]

use std::future::Future;
use std::sync::{Arc, Mutex};
use std::task::Poll;
use tokio::macros::support::poll_fn;

use tokio::runtime::Runtime;
use tokio::task::consume_budget;
use tokio::time::{self, Duration};

#[test]
Expand Down Expand Up @@ -433,6 +437,33 @@ fn worker_local_queue_depth() {
});
}

#[test]
fn budget_exhaustion_yield() {
let rt = current_thread();
let metrics = rt.metrics();

assert_eq!(0, metrics.budget_forced_yield_count());

let mut did_yield = false;

// block on a task which consumes budget until it yields
rt.block_on(poll_fn(|cx| loop {
if did_yield {
return Poll::Ready(());
}

let fut = consume_budget();
tokio::pin!(fut);

if fut.poll(cx).is_pending() {
did_yield = true;
return Poll::Pending;
}
}));

assert_eq!(1, rt.metrics().budget_forced_yield_count());
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn io_driver_fd_count() {
Expand Down