tokio/runtime/metrics/
worker.rs1use crate::runtime::Config;
2use crate::util::metric_atomics::{MetricAtomicU64, MetricAtomicUsize};
3use std::sync::atomic::Ordering::Relaxed;
4use std::sync::Mutex;
5use std::thread::ThreadId;
6
7cfg_unstable_metrics! {
8    use crate::runtime::metrics::Histogram;
9}
10
11#[derive(Debug, Default)]
19#[repr(align(128))]
20pub(crate) struct WorkerMetrics {
21    pub(crate) busy_duration_total: MetricAtomicU64,
23
24    pub(crate) queue_depth: MetricAtomicUsize,
27
28    thread_id: Mutex<Option<ThreadId>>,
30
31    pub(crate) park_count: MetricAtomicU64,
33
34    pub(crate) park_unpark_count: MetricAtomicU64,
36
37    #[cfg(tokio_unstable)]
38    pub(crate) noop_count: MetricAtomicU64,
40
41    #[cfg(tokio_unstable)]
42    pub(crate) steal_count: MetricAtomicU64,
44
45    #[cfg(tokio_unstable)]
46    pub(crate) steal_operations: MetricAtomicU64,
48
49    #[cfg(tokio_unstable)]
50    pub(crate) poll_count: MetricAtomicU64,
52
53    #[cfg(tokio_unstable)]
54    pub(crate) mean_poll_time: MetricAtomicU64,
56
57    #[cfg(tokio_unstable)]
58    pub(crate) local_schedule_count: MetricAtomicU64,
60
61    #[cfg(tokio_unstable)]
62    pub(crate) overflow_count: MetricAtomicU64,
64
65    #[cfg(tokio_unstable)]
66    pub(super) poll_count_histogram: Option<Histogram>,
68}
69
70impl WorkerMetrics {
71    pub(crate) fn new() -> WorkerMetrics {
72        WorkerMetrics::default()
73    }
74
75    pub(crate) fn set_queue_depth(&self, len: usize) {
76        self.queue_depth.store(len, Relaxed);
77    }
78
79    pub(crate) fn set_thread_id(&self, thread_id: ThreadId) {
80        *self.thread_id.lock().unwrap() = Some(thread_id);
81    }
82
83    cfg_metrics_variant! {
84        stable: {
85            pub(crate) fn from_config(_: &Config) -> WorkerMetrics {
86                WorkerMetrics::new()
87            }
88        },
89        unstable: {
90            pub(crate) fn from_config(config: &Config) -> WorkerMetrics {
91                let mut worker_metrics = WorkerMetrics::new();
92                worker_metrics.poll_count_histogram = config
93                    .metrics_poll_count_histogram
94                    .as_ref()
95                    .map(|histogram_builder| histogram_builder.build());
96                worker_metrics
97            }
98        }
99    }
100
101    cfg_unstable_metrics! {
102        pub(crate) fn queue_depth(&self) -> usize {
103            self.queue_depth.load(Relaxed)
104        }
105
106        pub(crate) fn thread_id(&self) -> Option<ThreadId> {
107            *self.thread_id.lock().unwrap()
108        }
109    }
110}