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 #[cfg(feature = "schedule-latency")]
70 pub(super) schedule_latency_histogram: Option<Histogram>,
72}
73
74impl WorkerMetrics {
75 pub(crate) fn new() -> WorkerMetrics {
76 WorkerMetrics::default()
77 }
78
79 pub(crate) fn set_queue_depth(&self, len: usize) {
80 self.queue_depth.store(len, Relaxed);
81 }
82
83 pub(crate) fn set_thread_id(&self, thread_id: ThreadId) {
84 *self.thread_id.lock().unwrap() = Some(thread_id);
85 }
86
87 cfg_metrics_variant! {
88 stable: {
89 pub(crate) fn from_config(_: &Config) -> WorkerMetrics {
90 WorkerMetrics::new()
91 }
92 },
93 unstable: {
94 pub(crate) fn from_config(config: &Config) -> WorkerMetrics {
95 let mut worker_metrics = WorkerMetrics::new();
96 worker_metrics.poll_count_histogram = config
97 .metrics_poll_count_histogram
98 .as_ref()
99 .map(|histogram_builder| histogram_builder.build());
100 #[cfg(feature = "schedule-latency")]
101 {
102 worker_metrics.schedule_latency_histogram = config
103 .metrics_schedule_latency_histogram
104 .as_ref()
105 .map(|histogram_builder| histogram_builder.build());
106 }
107
108 worker_metrics
109 }
110 }
111 }
112
113 cfg_unstable_metrics! {
114 pub(crate) fn queue_depth(&self) -> usize {
115 self.queue_depth.load(Relaxed)
116 }
117
118 pub(crate) fn thread_id(&self) -> Option<ThreadId> {
119 *self.thread_id.lock().unwrap()
120 }
121 }
122}