fix(node): add reset method to event loop delay histogram (#28788)

Fixes https://github.com/denoland/deno/issues/28767

Don't love the `RefCell`, but don't really see a (safe) way around it.
This commit is contained in:
Nathan Whitaker 2025-04-07 22:05:53 -07:00 committed by GitHub
parent 9bc3b07819
commit 6d33141d8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
// Copyright 2018-2025 the Deno authors. MIT license. // Copyright 2018-2025 the Deno authors. MIT license.
use std::cell::Cell; use std::cell::Cell;
use std::cell::RefCell;
use deno_core::op2; use deno_core::op2;
use deno_core::GarbageCollected; use deno_core::GarbageCollected;
@ -13,7 +14,7 @@ pub enum PerfHooksError {
} }
pub struct EldHistogram { pub struct EldHistogram {
eld: tokio_eld::EldHistogram<u64>, eld: RefCell<tokio_eld::EldHistogram<u64>>,
started: Cell<bool>, started: Cell<bool>,
} }
@ -29,7 +30,7 @@ impl EldHistogram {
#[cppgc] #[cppgc]
pub fn new(#[smi] resolution: u32) -> Result<EldHistogram, PerfHooksError> { pub fn new(#[smi] resolution: u32) -> Result<EldHistogram, PerfHooksError> {
Ok(EldHistogram { Ok(EldHistogram {
eld: tokio_eld::EldHistogram::new(resolution as usize)?, eld: RefCell::new(tokio_eld::EldHistogram::new(resolution as usize)?),
started: Cell::new(false), started: Cell::new(false),
}) })
} }
@ -43,7 +44,7 @@ impl EldHistogram {
return false; return false;
} }
self.eld.start(); self.eld.borrow().start();
self.started.set(true); self.started.set(true);
true true
@ -58,79 +59,84 @@ impl EldHistogram {
return false; return false;
} }
self.eld.stop(); self.eld.borrow().stop();
self.started.set(false); self.started.set(false);
true true
} }
#[fast]
fn reset(&self) {
self.eld.borrow_mut().reset();
}
// Returns the value at the given percentile. // Returns the value at the given percentile.
// //
// `percentile` ∈ (0, 100] // `percentile` ∈ (0, 100]
#[fast] #[fast]
#[number] #[number]
fn percentile(&self, percentile: f64) -> u64 { fn percentile(&self, percentile: f64) -> u64 {
self.eld.value_at_percentile(percentile) self.eld.borrow().value_at_percentile(percentile)
} }
// Returns the value at the given percentile as a bigint. // Returns the value at the given percentile as a bigint.
#[fast] #[fast]
#[bigint] #[bigint]
fn percentile_big_int(&self, percentile: f64) -> u64 { fn percentile_big_int(&self, percentile: f64) -> u64 {
self.eld.value_at_percentile(percentile) self.eld.borrow().value_at_percentile(percentile)
} }
// The number of samples recorded by the histogram. // The number of samples recorded by the histogram.
#[getter] #[getter]
#[number] #[number]
fn count(&self) -> u64 { fn count(&self) -> u64 {
self.eld.len() self.eld.borrow().len()
} }
// The number of samples recorded by the histogram as a bigint. // The number of samples recorded by the histogram as a bigint.
#[getter] #[getter]
#[bigint] #[bigint]
fn count_big_int(&self) -> u64 { fn count_big_int(&self) -> u64 {
self.eld.len() self.eld.borrow().len()
} }
// The maximum recorded event loop delay. // The maximum recorded event loop delay.
#[getter] #[getter]
#[number] #[number]
fn max(&self) -> u64 { fn max(&self) -> u64 {
self.eld.max() self.eld.borrow().max()
} }
// The maximum recorded event loop delay as a bigint. // The maximum recorded event loop delay as a bigint.
#[getter] #[getter]
#[bigint] #[bigint]
fn max_big_int(&self) -> u64 { fn max_big_int(&self) -> u64 {
self.eld.max() self.eld.borrow().max()
} }
// The mean of the recorded event loop delays. // The mean of the recorded event loop delays.
#[getter] #[getter]
fn mean(&self) -> f64 { fn mean(&self) -> f64 {
self.eld.mean() self.eld.borrow().mean()
} }
// The minimum recorded event loop delay. // The minimum recorded event loop delay.
#[getter] #[getter]
#[number] #[number]
fn min(&self) -> u64 { fn min(&self) -> u64 {
self.eld.min() self.eld.borrow().min()
} }
// The minimum recorded event loop delay as a bigint. // The minimum recorded event loop delay as a bigint.
#[getter] #[getter]
#[bigint] #[bigint]
fn min_big_int(&self) -> u64 { fn min_big_int(&self) -> u64 {
self.eld.min() self.eld.borrow().min()
} }
// The standard deviation of the recorded event loop delays. // The standard deviation of the recorded event loop delays.
#[getter] #[getter]
fn stddev(&self) -> f64 { fn stddev(&self) -> f64 {
self.eld.stdev() self.eld.borrow().stdev()
} }
} }