diff options
Diffstat (limited to 'core/ops_metrics.rs')
-rw-r--r-- | core/ops_metrics.rs | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/core/ops_metrics.rs b/core/ops_metrics.rs index 9dd5e2edf..5c40a47ca 100644 --- a/core/ops_metrics.rs +++ b/core/ops_metrics.rs @@ -1,6 +1,8 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use crate::serde::Serialize; use crate::OpId; +use std::cell::RefCell; +use std::cell::RefMut; // TODO(@AaronO): split into AggregateMetrics & PerOpMetrics #[derive(Clone, Default, Debug, Serialize)] @@ -22,18 +24,18 @@ pub struct OpMetrics { // TODO(@AaronO): track errors #[derive(Default, Debug)] pub struct OpsTracker { - pub ops: Vec<OpMetrics>, + pub ops: RefCell<Vec<OpMetrics>>, } impl OpsTracker { pub fn per_op(&self) -> Vec<OpMetrics> { - self.ops.clone() + self.ops.borrow().clone() } pub fn aggregate(&self) -> OpMetrics { let mut sum = OpMetrics::default(); - for metrics in self.ops.iter() { + for metrics in self.ops.borrow().iter() { sum.ops_dispatched += metrics.ops_dispatched; sum.ops_dispatched_sync += metrics.ops_dispatched_sync; sum.ops_dispatched_async += metrics.ops_dispatched_async; @@ -50,46 +52,47 @@ impl OpsTracker { sum } - fn ensure_capacity(&mut self, op_id: OpId) { - if op_id >= self.ops.len() { - let delta_len = 1 + op_id - self.ops.len(); - self.ops.extend(vec![OpMetrics::default(); delta_len]) + fn ensure_capacity(&self, op_id: OpId) { + let ops = &mut self.ops.borrow_mut(); + if op_id >= ops.len() { + let delta_len = 1 + op_id - ops.len(); + ops.extend(vec![OpMetrics::default(); delta_len]) } } - fn metrics_mut(&mut self, id: OpId) -> &mut OpMetrics { + fn metrics_mut(&self, id: OpId) -> RefMut<OpMetrics> { self.ensure_capacity(id); - self.ops.get_mut(id).unwrap() + RefMut::map(self.ops.borrow_mut(), |ops| ops.get_mut(id).unwrap()) } - pub fn track_sync(&mut self, id: OpId) { - let metrics = self.metrics_mut(id); + pub fn track_sync(&self, id: OpId) { + let metrics = &mut self.metrics_mut(id); metrics.ops_dispatched += 1; metrics.ops_completed += 1; metrics.ops_dispatched_sync += 1; metrics.ops_completed_sync += 1; } - pub fn track_async(&mut self, id: OpId) { - let metrics = self.metrics_mut(id); + pub fn track_async(&self, id: OpId) { + let metrics = &mut self.metrics_mut(id); metrics.ops_dispatched += 1; metrics.ops_dispatched_async += 1; } - pub fn track_async_completed(&mut self, id: OpId) { - let metrics = self.metrics_mut(id); + pub fn track_async_completed(&self, id: OpId) { + let metrics = &mut self.metrics_mut(id); metrics.ops_completed += 1; metrics.ops_completed_async += 1; } - pub fn track_unref(&mut self, id: OpId) { - let metrics = self.metrics_mut(id); + pub fn track_unref(&self, id: OpId) { + let metrics = &mut self.metrics_mut(id); metrics.ops_dispatched += 1; metrics.ops_dispatched_async_unref += 1; } - pub fn track_unref_completed(&mut self, id: OpId) { - let metrics = self.metrics_mut(id); + pub fn track_unref_completed(&self, id: OpId) { + let metrics = &mut self.metrics_mut(id); metrics.ops_completed += 1; metrics.ops_completed_async_unref += 1; } |