|
| 1 | +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +use deno_core::op2; |
| 4 | +use deno_core::GarbageCollected; |
| 5 | + |
| 6 | +use std::cell::Cell; |
| 7 | + |
| 8 | +#[derive(Debug, thiserror::Error)] |
| 9 | +pub enum PerfHooksError { |
| 10 | + #[error(transparent)] |
| 11 | + TokioEld(#[from] tokio_eld::Error), |
| 12 | +} |
| 13 | + |
| 14 | +pub struct EldHistogram { |
| 15 | + eld: tokio_eld::EldHistogram<u64>, |
| 16 | + started: Cell<bool>, |
| 17 | +} |
| 18 | + |
| 19 | +impl GarbageCollected for EldHistogram {} |
| 20 | + |
| 21 | +#[op2] |
| 22 | +impl EldHistogram { |
| 23 | + // Creates an interval EldHistogram object that samples and reports the event |
| 24 | + // loop delay over time. |
| 25 | + // |
| 26 | + // The delays will be reported in nanoseconds. |
| 27 | + #[constructor] |
| 28 | + #[cppgc] |
| 29 | + pub fn new(#[smi] resolution: u32) -> Result<EldHistogram, PerfHooksError> { |
| 30 | + Ok(EldHistogram { |
| 31 | + eld: tokio_eld::EldHistogram::new(resolution as usize)?, |
| 32 | + started: Cell::new(false), |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + // Disables the update interval timer. |
| 37 | + // |
| 38 | + // Returns true if the timer was stopped, false if it was already stopped. |
| 39 | + #[fast] |
| 40 | + fn enable(&self) -> bool { |
| 41 | + if self.started.get() { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + self.eld.start(); |
| 46 | + self.started.set(true); |
| 47 | + |
| 48 | + true |
| 49 | + } |
| 50 | + |
| 51 | + // Enables the update interval timer. |
| 52 | + // |
| 53 | + // Returns true if the timer was started, false if it was already started. |
| 54 | + #[fast] |
| 55 | + fn disable(&self) -> bool { |
| 56 | + if !self.started.get() { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + self.eld.stop(); |
| 61 | + self.started.set(false); |
| 62 | + |
| 63 | + true |
| 64 | + } |
| 65 | + |
| 66 | + // Returns the value at the given percentile. |
| 67 | + // |
| 68 | + // `percentile` ∈ (0, 100] |
| 69 | + #[fast] |
| 70 | + #[number] |
| 71 | + fn percentile(&self, percentile: f64) -> u64 { |
| 72 | + self.eld.value_at_percentile(percentile) |
| 73 | + } |
| 74 | + |
| 75 | + // Returns the value at the given percentile as a bigint. |
| 76 | + #[fast] |
| 77 | + #[bigint] |
| 78 | + fn percentile_big_int(&self, percentile: f64) -> u64 { |
| 79 | + self.eld.value_at_percentile(percentile) |
| 80 | + } |
| 81 | + |
| 82 | + // The number of samples recorded by the histogram. |
| 83 | + #[getter] |
| 84 | + #[number] |
| 85 | + fn count(&self) -> u64 { |
| 86 | + self.eld.len() |
| 87 | + } |
| 88 | + |
| 89 | + // The number of samples recorded by the histogram as a bigint. |
| 90 | + #[getter] |
| 91 | + #[bigint] |
| 92 | + fn count_big_int(&self) -> u64 { |
| 93 | + self.eld.len() |
| 94 | + } |
| 95 | + |
| 96 | + // The maximum recorded event loop delay. |
| 97 | + #[getter] |
| 98 | + #[number] |
| 99 | + fn max(&self) -> u64 { |
| 100 | + self.eld.max() |
| 101 | + } |
| 102 | + |
| 103 | + // The maximum recorded event loop delay as a bigint. |
| 104 | + #[getter] |
| 105 | + #[bigint] |
| 106 | + fn max_big_int(&self) -> u64 { |
| 107 | + self.eld.max() |
| 108 | + } |
| 109 | + |
| 110 | + // The mean of the recorded event loop delays. |
| 111 | + #[getter] |
| 112 | + fn mean(&self) -> f64 { |
| 113 | + self.eld.mean() |
| 114 | + } |
| 115 | + |
| 116 | + // The minimum recorded event loop delay. |
| 117 | + #[getter] |
| 118 | + #[number] |
| 119 | + fn min(&self) -> u64 { |
| 120 | + self.eld.min() |
| 121 | + } |
| 122 | + |
| 123 | + // The minimum recorded event loop delay as a bigint. |
| 124 | + #[getter] |
| 125 | + #[bigint] |
| 126 | + fn min_big_int(&self) -> u64 { |
| 127 | + self.eld.min() |
| 128 | + } |
| 129 | + |
| 130 | + // The standard deviation of the recorded event loop delays. |
| 131 | + #[getter] |
| 132 | + fn stddev(&self) -> f64 { |
| 133 | + self.eld.stdev() |
| 134 | + } |
| 135 | +} |
0 commit comments