@@ -12,7 +12,7 @@ pub(crate) struct MetricsBatch {
12
12
busy_duration_total : u64 ,
13
13
14
14
/// Instant at which work last resumed (continued after park).
15
- processing_scheduled_tasks_started_at : Instant ,
15
+ processing_scheduled_tasks_started_at : Option < Instant > ,
16
16
17
17
/// Number of times the worker parked.
18
18
park_count : u64 ,
@@ -67,25 +67,34 @@ cfg_unstable_metrics! {
67
67
68
68
impl MetricsBatch {
69
69
pub ( crate ) fn new ( worker_metrics : & WorkerMetrics ) -> MetricsBatch {
70
- let now = Instant :: now ( ) ;
71
- Self :: new_unstable ( worker_metrics, now )
70
+ let maybe_now = now ( ) ;
71
+ Self :: new_unstable ( worker_metrics, maybe_now )
72
72
}
73
73
74
74
cfg_metrics_variant ! {
75
75
stable: {
76
76
#[ inline( always) ]
77
- fn new_unstable( _worker_metrics: & WorkerMetrics , now : Instant ) -> MetricsBatch {
77
+ fn new_unstable( _worker_metrics: & WorkerMetrics , maybe_now : Option < Instant > ) -> MetricsBatch {
78
78
MetricsBatch {
79
79
busy_duration_total: 0 ,
80
- processing_scheduled_tasks_started_at: now ,
80
+ processing_scheduled_tasks_started_at: maybe_now ,
81
81
park_count: 0 ,
82
82
park_unpark_count: 0 ,
83
83
}
84
84
}
85
85
} ,
86
86
unstable: {
87
87
#[ inline( always) ]
88
- fn new_unstable( worker_metrics: & WorkerMetrics , now: Instant ) -> MetricsBatch {
88
+ fn new_unstable( worker_metrics: & WorkerMetrics , maybe_now: Option <Instant >) -> MetricsBatch {
89
+ let poll_timer = maybe_now. and_then( |now| {
90
+ worker_metrics
91
+ . poll_count_histogram
92
+ . as_ref( )
93
+ . map( |worker_poll_counts| PollTimer {
94
+ poll_counts: HistogramBatch :: from_histogram( worker_poll_counts) ,
95
+ poll_started_at: now,
96
+ } )
97
+ } ) ;
89
98
MetricsBatch {
90
99
park_count: 0 ,
91
100
park_unpark_count: 0 ,
@@ -97,13 +106,8 @@ impl MetricsBatch {
97
106
local_schedule_count: 0 ,
98
107
overflow_count: 0 ,
99
108
busy_duration_total: 0 ,
100
- processing_scheduled_tasks_started_at: now,
101
- poll_timer: worker_metrics. poll_count_histogram. as_ref( ) . map(
102
- |worker_poll_counts| PollTimer {
103
- poll_counts: HistogramBatch :: from_histogram( worker_poll_counts) ,
104
- poll_started_at: now,
105
- } ,
106
- ) ,
109
+ processing_scheduled_tasks_started_at: maybe_now,
110
+ poll_timer,
107
111
}
108
112
}
109
113
}
@@ -186,13 +190,17 @@ impl MetricsBatch {
186
190
187
191
/// Start processing a batch of tasks
188
192
pub ( crate ) fn start_processing_scheduled_tasks ( & mut self ) {
189
- self . processing_scheduled_tasks_started_at = Instant :: now ( ) ;
193
+ self . processing_scheduled_tasks_started_at = now ( ) ;
190
194
}
191
195
192
196
/// Stop processing a batch of tasks
193
197
pub ( crate ) fn end_processing_scheduled_tasks ( & mut self ) {
194
- let busy_duration = self . processing_scheduled_tasks_started_at . elapsed ( ) ;
195
- self . busy_duration_total += duration_as_u64 ( busy_duration) ;
198
+ if let Some ( processing_scheduled_tasks_started_at) =
199
+ self . processing_scheduled_tasks_started_at
200
+ {
201
+ let busy_duration = processing_scheduled_tasks_started_at. elapsed ( ) ;
202
+ self . busy_duration_total += duration_as_u64 ( busy_duration) ;
203
+ }
196
204
}
197
205
198
206
cfg_metrics_variant ! {
@@ -279,3 +287,17 @@ cfg_rt_multi_thread! {
279
287
pub ( crate ) fn duration_as_u64 ( dur : Duration ) -> u64 {
280
288
u64:: try_from ( dur. as_nanos ( ) ) . unwrap_or ( u64:: MAX )
281
289
}
290
+
291
+ /// Gate unsupported time metrics for `wasm32-unknown-unknown`
292
+ /// <https://github.com/tokio-rs/tokio/issues/7319>
293
+ fn now ( ) -> Option < Instant > {
294
+ if cfg ! ( all(
295
+ target_arch = "wasm32" ,
296
+ target_os = "unknown" ,
297
+ target_vendor = "unknown"
298
+ ) ) {
299
+ None
300
+ } else {
301
+ Some ( Instant :: now ( ) )
302
+ }
303
+ }
0 commit comments