forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.rs
171 lines (152 loc) · 5.27 KB
/
metrics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::serde::Serialize;
#[derive(Default, Debug)]
pub struct RuntimeMetrics {
pub ops: HashMap<&'static str, OpMetrics>,
}
impl RuntimeMetrics {
pub fn combined_metrics(&self) -> OpMetrics {
let mut total = OpMetrics::default();
for metrics in self.ops.values() {
total.ops_dispatched += metrics.ops_dispatched;
total.ops_dispatched_sync += metrics.ops_dispatched_sync;
total.ops_dispatched_async += metrics.ops_dispatched_async;
total.ops_dispatched_async_unref += metrics.ops_dispatched_async_unref;
total.ops_completed += metrics.ops_completed;
total.ops_completed_sync += metrics.ops_completed_sync;
total.ops_completed_async += metrics.ops_completed_async;
total.ops_completed_async_unref += metrics.ops_completed_async_unref;
total.bytes_sent_control += metrics.bytes_sent_control;
total.bytes_sent_data += metrics.bytes_sent_data;
total.bytes_received += metrics.bytes_received;
}
total
}
}
#[derive(Default, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OpMetrics {
pub ops_dispatched: u64,
pub ops_dispatched_sync: u64,
pub ops_dispatched_async: u64,
pub ops_dispatched_async_unref: u64,
pub ops_completed: u64,
pub ops_completed_sync: u64,
pub ops_completed_async: u64,
pub ops_completed_async_unref: u64,
pub bytes_sent_control: u64,
pub bytes_sent_data: u64,
pub bytes_received: u64,
}
impl OpMetrics {
fn op_dispatched(
&mut self,
bytes_sent_control: usize,
bytes_sent_data: usize,
) {
self.ops_dispatched += 1;
self.bytes_sent_control += bytes_sent_control as u64;
self.bytes_sent_data += bytes_sent_data as u64;
}
fn op_completed(&mut self, bytes_received: usize) {
self.ops_completed += 1;
self.bytes_received += bytes_received as u64;
}
pub fn op_sync(
&mut self,
bytes_sent_control: usize,
bytes_sent_data: usize,
bytes_received: usize,
) {
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,
bytes_sent_control: usize,
bytes_sent_data: usize,
) {
self.ops_dispatched_async += 1;
self.op_dispatched(bytes_sent_control, bytes_sent_data)
}
pub fn op_dispatched_async_unref(
&mut self,
bytes_sent_control: usize,
bytes_sent_data: usize,
) {
self.ops_dispatched_async_unref += 1;
self.op_dispatched(bytes_sent_control, bytes_sent_data)
}
pub fn op_completed_async(&mut self, bytes_received: usize) {
self.ops_completed_async += 1;
self.op_completed(bytes_received);
}
pub fn op_completed_async_unref(&mut self, bytes_received: usize) {
self.ops_completed_async_unref += 1;
self.op_completed(bytes_received);
}
}
use deno_core::BufVec;
use deno_core::Op;
use deno_core::OpFn;
use deno_core::OpState;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
pub fn metrics_op(name: &'static str, 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);
let op_state_ = op_state.clone();
let mut s = op_state.borrow_mut();
let runtime_metrics = s.borrow_mut::<RuntimeMetrics>();
let metrics = if let Some(metrics) = runtime_metrics.ops.get_mut(name) {
metrics
} else {
runtime_metrics.ops.insert(name, OpMetrics::default());
runtime_metrics.ops.get_mut(name).unwrap()
};
use deno_core::futures::future::FutureExt;
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| {
let mut s = op_state_.borrow_mut();
let runtime_metrics = s.borrow_mut::<RuntimeMetrics>();
let metrics = runtime_metrics.ops.get_mut(name).unwrap();
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| {
let mut s = op_state_.borrow_mut();
let runtime_metrics = s.borrow_mut::<RuntimeMetrics>();
let metrics = runtime_metrics.ops.get_mut(name).unwrap();
metrics.op_completed_async_unref(buf.len());
})
.boxed_local();
Op::AsyncUnref(fut)
}
other => other,
}
})
}