2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 11:20:30 -04:00
|
|
|
use crate::serde::Serialize;
|
|
|
|
use crate::OpId;
|
2022-03-08 03:28:20 -05:00
|
|
|
use std::cell::UnsafeCell;
|
2021-10-10 11:20:30 -04:00
|
|
|
|
|
|
|
// TODO(@AaronO): split into AggregateMetrics & PerOpMetrics
|
|
|
|
#[derive(Clone, Default, Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct OpMetrics {
|
|
|
|
pub ops_dispatched: u64,
|
|
|
|
pub ops_dispatched_sync: u64,
|
|
|
|
pub ops_dispatched_async: u64,
|
2021-11-25 13:49:09 -05:00
|
|
|
// TODO(bartlomieju): this field is never updated
|
2021-10-10 11:20:30 -04:00
|
|
|
pub ops_dispatched_async_unref: u64,
|
|
|
|
pub ops_completed: u64,
|
|
|
|
pub ops_completed_sync: u64,
|
|
|
|
pub ops_completed_async: u64,
|
2021-11-25 13:49:09 -05:00
|
|
|
// TODO(bartlomieju): this field is never updated
|
2021-10-10 11:20:30 -04:00
|
|
|
pub ops_completed_async_unref: u64,
|
|
|
|
pub bytes_sent_control: u64,
|
|
|
|
pub bytes_sent_data: u64,
|
|
|
|
pub bytes_received: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@AaronO): track errors
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct OpsTracker {
|
2022-03-08 03:28:20 -05:00
|
|
|
pub ops: UnsafeCell<Vec<OpMetrics>>,
|
2021-10-10 11:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpsTracker {
|
|
|
|
pub fn per_op(&self) -> Vec<OpMetrics> {
|
2022-03-08 03:28:20 -05:00
|
|
|
self.ops_mut().clone()
|
2021-10-10 11:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn aggregate(&self) -> OpMetrics {
|
|
|
|
let mut sum = OpMetrics::default();
|
|
|
|
|
2022-03-08 03:28:20 -05:00
|
|
|
for metrics in self.ops_mut().iter() {
|
2021-10-10 11:20:30 -04:00
|
|
|
sum.ops_dispatched += metrics.ops_dispatched;
|
|
|
|
sum.ops_dispatched_sync += metrics.ops_dispatched_sync;
|
|
|
|
sum.ops_dispatched_async += metrics.ops_dispatched_async;
|
|
|
|
sum.ops_dispatched_async_unref += metrics.ops_dispatched_async_unref;
|
|
|
|
sum.ops_completed += metrics.ops_completed;
|
|
|
|
sum.ops_completed_sync += metrics.ops_completed_sync;
|
|
|
|
sum.ops_completed_async += metrics.ops_completed_async;
|
|
|
|
sum.ops_completed_async_unref += metrics.ops_completed_async_unref;
|
|
|
|
sum.bytes_sent_control += metrics.bytes_sent_control;
|
|
|
|
sum.bytes_sent_data += metrics.bytes_sent_data;
|
|
|
|
sum.bytes_received += metrics.bytes_received;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
2022-03-08 03:28:20 -05:00
|
|
|
#[allow(clippy::mut_from_ref)]
|
|
|
|
#[inline]
|
|
|
|
fn ops_mut(&self) -> &mut Vec<OpMetrics> {
|
2022-06-25 18:13:24 -04:00
|
|
|
// SAFETY: `OpsTracker` is created after registering ops so it is guaranteed
|
|
|
|
// that that `ops` will be initialized.
|
2022-03-08 03:28:20 -05:00
|
|
|
unsafe { &mut *self.ops.get() }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::mut_from_ref)]
|
|
|
|
#[inline]
|
|
|
|
fn metrics_mut(&self, id: OpId) -> &mut OpMetrics {
|
2022-06-25 18:13:24 -04:00
|
|
|
// SAFETY: `OpsTracker` is created after registering ops, and ops
|
|
|
|
// cannot be unregistered during runtime, so it is guaranteed that `id`
|
|
|
|
// is not causing out-of-bound access.
|
2022-03-14 13:44:15 -04:00
|
|
|
unsafe { self.ops_mut().get_unchecked_mut(id) }
|
2021-10-10 11:20:30 -04:00
|
|
|
}
|
|
|
|
|
2022-03-08 03:28:20 -05:00
|
|
|
#[inline]
|
2021-10-24 13:30:55 -04:00
|
|
|
pub fn track_sync(&self, id: OpId) {
|
2022-03-08 03:28:20 -05:00
|
|
|
let metrics = self.metrics_mut(id);
|
2021-10-10 11:20:30 -04:00
|
|
|
metrics.ops_dispatched += 1;
|
|
|
|
metrics.ops_completed += 1;
|
|
|
|
metrics.ops_dispatched_sync += 1;
|
|
|
|
metrics.ops_completed_sync += 1;
|
|
|
|
}
|
|
|
|
|
2022-03-08 03:28:20 -05:00
|
|
|
#[inline]
|
2021-10-24 13:30:55 -04:00
|
|
|
pub fn track_async(&self, id: OpId) {
|
2022-03-08 03:28:20 -05:00
|
|
|
let metrics = self.metrics_mut(id);
|
2021-10-10 11:20:30 -04:00
|
|
|
metrics.ops_dispatched += 1;
|
|
|
|
metrics.ops_dispatched_async += 1;
|
|
|
|
}
|
|
|
|
|
2022-03-08 03:28:20 -05:00
|
|
|
#[inline]
|
2021-10-24 13:30:55 -04:00
|
|
|
pub fn track_async_completed(&self, id: OpId) {
|
2022-03-08 03:28:20 -05:00
|
|
|
let metrics = self.metrics_mut(id);
|
2021-10-10 11:20:30 -04:00
|
|
|
metrics.ops_completed += 1;
|
|
|
|
metrics.ops_completed_async += 1;
|
|
|
|
}
|
|
|
|
}
|