2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-03-02 13:13:36 -05:00
|
|
|
#[derive(Default, Debug)]
|
2019-11-04 10:38:52 -05:00
|
|
|
pub struct Metrics {
|
2020-02-11 11:23:40 -05:00
|
|
|
pub ops_dispatched: u64,
|
2020-03-02 13:13:36 -05:00
|
|
|
pub ops_dispatched_sync: u64,
|
|
|
|
pub ops_dispatched_async: u64,
|
|
|
|
pub ops_dispatched_async_unref: u64,
|
2020-02-11 11:23:40 -05:00
|
|
|
pub ops_completed: u64,
|
2020-03-02 13:13:36 -05:00
|
|
|
pub ops_completed_sync: u64,
|
|
|
|
pub ops_completed_async: u64,
|
|
|
|
pub ops_completed_async_unref: u64,
|
2020-02-11 11:23:40 -05:00
|
|
|
pub bytes_sent_control: u64,
|
|
|
|
pub bytes_sent_data: u64,
|
|
|
|
pub bytes_received: u64,
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2020-03-02 13:13:36 -05:00
|
|
|
|
|
|
|
impl Metrics {
|
2020-09-05 20:34:02 -04:00
|
|
|
fn op_dispatched(
|
|
|
|
&mut self,
|
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
|
|
|
) {
|
2020-03-02 13:13:36 -05:00
|
|
|
self.ops_dispatched += 1;
|
2020-09-05 20:34:02 -04:00
|
|
|
self.bytes_sent_control += bytes_sent_control as u64;
|
|
|
|
self.bytes_sent_data += bytes_sent_data as u64;
|
2020-03-02 13:13:36 -05:00
|
|
|
}
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
fn op_completed(&mut self, bytes_received: usize) {
|
2020-03-02 13:13:36 -05:00
|
|
|
self.ops_completed += 1;
|
2020-09-05 20:34:02 -04:00
|
|
|
self.bytes_received += bytes_received as u64;
|
2020-03-02 13:13:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_sync(
|
|
|
|
&mut self,
|
2020-09-05 20:34:02 -04:00
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
|
|
|
bytes_received: usize,
|
2020-03-02 13:13:36 -05:00
|
|
|
) {
|
|
|
|
self.ops_dispatched_sync += 1;
|
|
|
|
self.op_dispatched(bytes_sent_control, bytes_sent_data);
|
|
|
|
self.ops_completed_sync += 1;
|
|
|
|
self.op_completed(bytes_received);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_dispatched_async(
|
|
|
|
&mut self,
|
2020-09-05 20:34:02 -04:00
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
2020-03-02 13:13:36 -05:00
|
|
|
) {
|
|
|
|
self.ops_dispatched_async += 1;
|
|
|
|
self.op_dispatched(bytes_sent_control, bytes_sent_data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_dispatched_async_unref(
|
|
|
|
&mut self,
|
2020-09-05 20:34:02 -04:00
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
2020-03-02 13:13:36 -05:00
|
|
|
) {
|
|
|
|
self.ops_dispatched_async_unref += 1;
|
|
|
|
self.op_dispatched(bytes_sent_control, bytes_sent_data)
|
|
|
|
}
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
pub fn op_completed_async(&mut self, bytes_received: usize) {
|
2020-03-02 13:13:36 -05:00
|
|
|
self.ops_completed_async += 1;
|
|
|
|
self.op_completed(bytes_received);
|
|
|
|
}
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
pub fn op_completed_async_unref(&mut self, bytes_received: usize) {
|
2020-03-02 13:13:36 -05:00
|
|
|
self.ops_completed_async_unref += 1;
|
|
|
|
self.op_completed(bytes_received);
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
|
|
|
|
use deno_core::BufVec;
|
|
|
|
use deno_core::Op;
|
|
|
|
use deno_core::OpFn;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
pub fn metrics_op(op_fn: Box<OpFn>) -> Box<OpFn> {
|
|
|
|
Box::new(move |op_state: Rc<RefCell<OpState>>, bufs: BufVec| -> Op {
|
|
|
|
// TODOs:
|
|
|
|
// * The 'bytes' metrics seem pretty useless, especially now that the
|
|
|
|
// distinction between 'control' and 'data' buffers has become blurry.
|
|
|
|
// * Tracking completion of async ops currently makes us put the boxed
|
|
|
|
// future into _another_ box. Keeping some counters may not be expensive
|
|
|
|
// in itself, but adding a heap allocation for every metric seems bad.
|
|
|
|
let mut buf_len_iter = bufs.iter().map(|buf| buf.len());
|
|
|
|
let bytes_sent_control = buf_len_iter.next().unwrap_or(0);
|
|
|
|
let bytes_sent_data = buf_len_iter.sum();
|
|
|
|
|
|
|
|
let op = (op_fn)(op_state.clone(), bufs);
|
|
|
|
|
2020-09-18 14:39:47 -04:00
|
|
|
let op_state_ = op_state.clone();
|
|
|
|
let mut s = op_state.borrow_mut();
|
|
|
|
let metrics = s.borrow_mut::<Metrics>();
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::FutureExt;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
|
|
|
match op {
|
|
|
|
Op::Sync(buf) => {
|
|
|
|
metrics.op_sync(bytes_sent_control, bytes_sent_data, buf.len());
|
|
|
|
Op::Sync(buf)
|
|
|
|
}
|
|
|
|
Op::Async(fut) => {
|
|
|
|
metrics.op_dispatched_async(bytes_sent_control, bytes_sent_data);
|
|
|
|
let fut = fut
|
|
|
|
.inspect(move |buf| {
|
2020-09-18 14:39:47 -04:00
|
|
|
let mut s = op_state_.borrow_mut();
|
|
|
|
let metrics = s.borrow_mut::<Metrics>();
|
2020-09-10 09:57:45 -04:00
|
|
|
metrics.op_completed_async(buf.len());
|
|
|
|
})
|
|
|
|
.boxed_local();
|
|
|
|
Op::Async(fut)
|
|
|
|
}
|
|
|
|
Op::AsyncUnref(fut) => {
|
|
|
|
metrics.op_dispatched_async_unref(bytes_sent_control, bytes_sent_data);
|
|
|
|
let fut = fut
|
|
|
|
.inspect(move |buf| {
|
2020-09-18 14:39:47 -04:00
|
|
|
let mut s = op_state_.borrow_mut();
|
|
|
|
let metrics = s.borrow_mut::<Metrics>();
|
2020-09-10 09:57:45 -04:00
|
|
|
metrics.op_completed_async_unref(buf.len());
|
|
|
|
})
|
|
|
|
.boxed_local();
|
|
|
|
Op::AsyncUnref(fut)
|
|
|
|
}
|
|
|
|
other => other,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|