Skip to content

Commit

Permalink
metrics: Add steal operations counting
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwe committed Dec 9, 2022
1 parent bf31759 commit 4e8777e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tokio/src/runtime/metrics/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) struct MetricsBatch {

/// Number of times stolen.
steal_count: u64,
steal_operations: u64,

/// Number of tasks that were polled by the worker.
poll_count: u64,
Expand All @@ -39,6 +40,7 @@ impl MetricsBatch {
park_count: 0,
noop_count: 0,
steal_count: 0,
steal_operations: 0,
poll_count: 0,
poll_count_on_last_park: 0,
local_schedule_count: 0,
Expand All @@ -52,6 +54,7 @@ impl MetricsBatch {
worker.park_count.store(self.park_count, Relaxed);
worker.noop_count.store(self.noop_count, Relaxed);
worker.steal_count.store(self.steal_count, Relaxed);
worker.steal_operations.store(self.steal_operations, Relaxed);
worker.poll_count.store(self.poll_count, Relaxed);

worker
Expand Down Expand Up @@ -98,6 +101,10 @@ cfg_rt_multi_thread! {
self.steal_count += by as u64;
}

pub(crate) fn incr_steal_operations(&mut self) {
self.steal_operations += 1;
}

pub(crate) fn incr_overflow_count(&mut self) {
self.overflow_count += 1;
}
Expand Down
1 change: 1 addition & 0 deletions tokio/src/runtime/metrics/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl MetricsBatch {
cfg_rt_multi_thread! {
impl MetricsBatch {
pub(crate) fn incr_steal_count(&mut self, _by: u16) {}
pub(crate) fn incr_steal_operations(&mut self) {}
pub(crate) fn incr_overflow_count(&mut self) {}
}
}
8 changes: 8 additions & 0 deletions tokio/src/runtime/metrics/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ impl RuntimeMetrics {
.load(Relaxed)
}

pub fn worker_steal_operations(&self, worker: usize) -> u64 {
self.handle
.inner
.worker_metrics(worker)
.steal_operations
.load(Relaxed)
}

/// Returns the number of tasks the given worker thread has polled.
///
/// The worker poll count starts at zero when the runtime is created and
Expand Down
6 changes: 5 additions & 1 deletion tokio/src/runtime/metrics/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ pub(crate) struct WorkerMetrics {
/// Number of times the worker woke then parked again without doing work.
pub(crate) noop_count: AtomicU64,

/// Number of times the worker attempted to steal.
/// Number of tasks the worker stole.
pub(crate) steal_count: AtomicU64,

/// Number of times the worker stole
pub(crate) steal_operations: AtomicU64,

/// Number of tasks the worker polled.
pub(crate) poll_count: AtomicU64,

Expand All @@ -43,6 +46,7 @@ impl WorkerMetrics {
park_count: AtomicU64::new(0),
noop_count: AtomicU64::new(0),
steal_count: AtomicU64::new(0),
steal_operations: AtomicU64::new(0),
poll_count: AtomicU64::new(0),
overflow_count: AtomicU64::new(0),
busy_duration_total: AtomicU64::new(0),
Expand Down
1 change: 1 addition & 0 deletions tokio/src/runtime/scheduler/multi_thread/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ impl<T> Steal<T> {
}

dst_metrics.incr_steal_count(n as u16);
dst_metrics.incr_steal_operations();

// We are returning a task here
n -= 1;
Expand Down

0 comments on commit 4e8777e

Please sign in to comment.