1use crate::runtime::metrics::Histogram;
2use crate::runtime::Config;
3use crate::util::metric_atomics::{MetricAtomicU64, MetricAtomicUsize};
4use std::sync::atomic::Ordering::Relaxed;
5use std::sync::Mutex;
6use std::thread::ThreadId;
78/// Retrieve runtime worker metrics.
9///
10/// **Note**: This is an [unstable API][unstable]. The public API of this type
11/// may break in 1.x releases. See [the documentation on unstable
12/// features][unstable] for details.
13///
14/// [unstable]: crate#unstable-features
15#[derive(Debug, Default)]
16#[repr(align(128))]
17pub(crate) struct WorkerMetrics {
18/// Number of times the worker parked.
19pub(crate) park_count: MetricAtomicU64,
2021/// Number of times the worker parked and unparked.
22pub(crate) park_unpark_count: MetricAtomicU64,
2324/// Number of times the worker woke then parked again without doing work.
25pub(crate) noop_count: MetricAtomicU64,
2627/// Number of tasks the worker stole.
28pub(crate) steal_count: MetricAtomicU64,
2930/// Number of times the worker stole
31pub(crate) steal_operations: MetricAtomicU64,
3233/// Number of tasks the worker polled.
34pub(crate) poll_count: MetricAtomicU64,
3536/// EWMA task poll time, in nanoseconds.
37pub(crate) mean_poll_time: MetricAtomicU64,
3839/// Amount of time the worker spent doing work vs. parking.
40pub(crate) busy_duration_total: MetricAtomicU64,
4142/// Number of tasks scheduled for execution on the worker's local queue.
43pub(crate) local_schedule_count: MetricAtomicU64,
4445/// Number of tasks moved from the local queue to the global queue to free space.
46pub(crate) overflow_count: MetricAtomicU64,
4748/// Number of tasks currently in the local queue. Used only by the
49 /// current-thread scheduler.
50pub(crate) queue_depth: MetricAtomicUsize,
5152/// If `Some`, tracks the number of polls by duration range.
53pub(super) poll_count_histogram: Option<Histogram>,
5455/// Thread id of worker thread.
56thread_id: Mutex<Option<ThreadId>>,
57}
5859impl WorkerMetrics {
60pub(crate) fn from_config(config: &Config) -> WorkerMetrics {
61let mut worker_metrics = WorkerMetrics::new();
62 worker_metrics.poll_count_histogram = config
63 .metrics_poll_count_histogram
64 .as_ref()
65 .map(|histogram_builder| histogram_builder.build());
66 worker_metrics
67 }
6869pub(crate) fn new() -> WorkerMetrics {
70 WorkerMetrics::default()
71 }
7273pub(crate) fn queue_depth(&self) -> usize {
74self.queue_depth.load(Relaxed)
75 }
7677pub(crate) fn set_queue_depth(&self, len: usize) {
78self.queue_depth.store(len, Relaxed);
79 }
8081pub(crate) fn thread_id(&self) -> Option<ThreadId> {
82*self.thread_id.lock().unwrap()
83 }
8485pub(crate) fn set_thread_id(&self, thread_id: ThreadId) {
86*self.thread_id.lock().unwrap() = Some(thread_id);
87 }
88}