Skip to content

Commit

Permalink
feat(core): more enhancements to the stats API (#492)
Browse files Browse the repository at this point in the history
Add an op_exclusions filter and keep op names as `&'static str`.
  • Loading branch information
mmastrac authored Jan 26, 2024
1 parent 20e8cf1 commit a453a99
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
8 changes: 6 additions & 2 deletions core/runtime/op_driver/futures_unordered_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::OpInflightStats;
use crate::OpId;
use crate::PromiseId;
use anyhow::Error;
use bit_set::BitSet;
use deno_unsync::spawn;
use deno_unsync::JoinHandle;
use deno_unsync::UnsyncWaker;
Expand Down Expand Up @@ -229,11 +230,14 @@ impl<C: OpMappingContext> OpDriver<C> for FuturesUnorderedDriver<C> {
self.len.get()
}

fn stats(&self) -> OpInflightStats {
fn stats(&self, op_exclusions: &BitSet) -> OpInflightStats {
let q = self.queue.queue.queue.borrow();
let mut v: Vec<PendingOpInfo> = Vec::with_capacity(self.len.get());
for f in q.iter() {
v.push(f.context())
let context = f.context();
if !op_exclusions.contains(context.1 as _) {
v.push(context);
}
}
OpInflightStats {
ops: v.into_boxed_slice(),
Expand Down
3 changes: 2 additions & 1 deletion core/runtime/op_driver/joinset_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::OpInflightStats;
use crate::OpId;
use crate::PromiseId;
use anyhow::Error;
use bit_set::BitSet;
use deno_unsync::JoinSet;
use futures::task::noop_waker_ref;
use futures::FutureExt;
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<C: OpMappingContext> OpDriver<C> for JoinSetDriver<C> {
self.pending_ops.borrow().len()
}

fn stats(&self) -> OpInflightStats {
fn stats(&self, _op_exclusions: &BitSet) -> OpInflightStats {
OpInflightStats::default()
}
}
5 changes: 4 additions & 1 deletion core/runtime/op_driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::OpId;
use crate::PromiseId;
use anyhow::Error;
use bit_set::BitSet;
use std::future::Future;
use std::task::Context;
use std::task::Poll;
Expand Down Expand Up @@ -134,8 +135,10 @@ pub(crate) trait OpDriver<C: OpMappingContext = V8OpMappingContext>:
/// may not be a cheap operation and calling it large number of times (for example, in an
/// event loop) may cause slowdowns.
///
/// If `op_exclusions` is passed to this function, any ops in the set are excluded from the stats.
///
/// A [`PromiseId`] will appear in this list until its results have been picked up in `poll_ready`.
fn stats(&self) -> OpInflightStats;
fn stats(&self, op_exclusions: &BitSet) -> OpInflightStats;
}

#[cfg(test)]
Expand Down
43 changes: 26 additions & 17 deletions core/runtime/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use super::op_driver::OpDriver;
use super::op_driver::OpInflightStats;
use super::ContextState;
use crate::OpId;
use crate::OpState;
use crate::PromiseId;
use crate::ResourceId;
Expand All @@ -17,11 +18,12 @@ pub struct RuntimeActivityStatsFactory {
}

/// Selects the statistics that you are interested in capturing.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
#[derive(Clone, Default, PartialEq, Eq)]
pub struct RuntimeActivityStatsFilter {
include_timers: bool,
include_ops: bool,
include_resources: bool,
op_filter: BitSet,
}

impl RuntimeActivityStatsFilter {
Expand All @@ -30,6 +32,7 @@ impl RuntimeActivityStatsFilter {
include_ops: true,
include_resources: true,
include_timers: true,
op_filter: BitSet::default(),
}
}

Expand All @@ -48,15 +51,28 @@ impl RuntimeActivityStatsFilter {
self
}

pub fn omit_op(mut self, op: OpId) -> Self {
self.op_filter.insert(op as _);
self
}

pub fn is_empty(&self) -> bool {
*self == Self::default()
// This ensures we don't miss a newly-added field in the empty comparison
let Self {
include_ops,
include_resources,
include_timers,
op_filter: _,
} = self;
!(*include_ops) && !(*include_resources) && !(*include_timers)
}
}

impl RuntimeActivityStatsFactory {
/// Capture the current runtime activity.
pub fn capture(
self,
filter: RuntimeActivityStatsFilter,
filter: &RuntimeActivityStatsFilter,
) -> RuntimeActivityStats {
let resources = if filter.include_resources {
let res = &self.op_state.borrow().resource_table;
Expand Down Expand Up @@ -91,7 +107,7 @@ impl RuntimeActivityStatsFactory {
};

let ops = if filter.include_ops {
self.context_state.pending_ops.stats()
self.context_state.pending_ops.stats(&filter.op_filter)
} else {
OpInflightStats::default()
};
Expand Down Expand Up @@ -131,7 +147,7 @@ pub struct RuntimeActivityStats {
#[derive(Debug, Serialize)]
pub enum RuntimeActivity {
/// An async op, including the promise ID and op name.
AsyncOp(PromiseId, String),
AsyncOp(PromiseId, &'static str),
/// A resource, including the resource ID and name.
Resource(ResourceId, String),
/// A timer, including the timer ID.
Expand Down Expand Up @@ -173,10 +189,7 @@ impl RuntimeActivityStats {
);
let ops = self.context_state.op_ctxs.borrow();
for op in self.ops.ops.iter() {
v.push(RuntimeActivity::AsyncOp(
op.0,
ops[op.1 as usize].decl.name.to_owned(),
));
v.push(RuntimeActivity::AsyncOp(op.0, ops[op.1 as usize].decl.name));
}
for resource in self.resources.resources.iter() {
v.push(RuntimeActivity::Resource(resource.0, resource.1.clone()))
Expand Down Expand Up @@ -205,19 +218,15 @@ impl RuntimeActivityStats {
// continuing op
} else {
// before, but not after
disappeared.push(RuntimeActivity::AsyncOp(
op.0,
ops[op.1 as usize].decl.name.to_owned(),
));
disappeared
.push(RuntimeActivity::AsyncOp(op.0, ops[op.1 as usize].decl.name));
}
}
for op in after.ops.ops.iter() {
if a.contains(op.0 as usize) {
// after but not before
appeared.push(RuntimeActivity::AsyncOp(
op.0,
ops[op.1 as usize].decl.name.to_owned(),
));
appeared
.push(RuntimeActivity::AsyncOp(op.0, ops[op.1 as usize].decl.name));
}
}

Expand Down
2 changes: 1 addition & 1 deletion testing/checkin/runner/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn op_stats_capture(#[string] name: String, state: Rc<RefCell<OpState>>) {
.borrow()
.borrow::<RuntimeActivityStatsFactory>()
.clone();
let data = stats.capture(RuntimeActivityStatsFilter::all());
let data = stats.capture(&RuntimeActivityStatsFilter::all());
let mut state = state.borrow_mut();
let test_data = state.borrow_mut::<TestData>();
test_data.insert(name, data);
Expand Down

0 comments on commit a453a99

Please sign in to comment.