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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use core::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, RwLock};
use std::usize;
pub fn aggregate() -> (AggregateSink, AggregateSource) {
let agg = Aggregator::new();
(agg.as_sink(), agg.as_source())
}
#[derive(Debug)]
enum InnerScores {
Event {
hit: AtomicUsize
},
Value {
hit: AtomicUsize,
sum: AtomicUsize,
max: AtomicUsize,
min: AtomicUsize,
},
}
#[derive(Debug, Clone, Copy)]
pub enum ScoresSnapshot {
NoData,
Event {
hit: u64
},
Value {
hit: u64,
sum: u64,
max: u64,
min: u64,
},
}
#[derive(Debug)]
pub struct MetricScores {
pub kind: Kind,
pub name: String,
score: InnerScores,
}
#[inline]
fn compare_and_swap<F>(counter: &AtomicUsize, new_value: usize, retry: F) where F: Fn(usize) -> bool {
let mut loaded = counter.load(Ordering::Acquire);
while retry(loaded) {
if counter.compare_and_swap(loaded, new_value, Ordering::Release) == new_value {
break;
}
loaded = counter.load(Ordering::Acquire);
}
}
impl MetricScores {
pub fn write(&self, value: usize) -> () {
match &self.score {
&InnerScores::Event { ref hit, .. } => {
hit.fetch_add(1, Ordering::SeqCst);
}
&InnerScores::Value { ref hit, ref sum, ref max, ref min, .. } => {
compare_and_swap(max, value, |loaded| value > loaded);
compare_and_swap(min, value, |loaded| value < loaded);
sum.fetch_add(value, Ordering::Acquire);
hit.fetch_add(1, Ordering::Acquire);
}
}
}
pub fn read_and_reset(&self) -> ScoresSnapshot {
match self.score {
InnerScores::Event { ref hit } => {
match hit.swap(0, Ordering::Release) as u64 {
0 => ScoresSnapshot::NoData,
hit => ScoresSnapshot::Event { hit }
}
}
InnerScores::Value { ref hit, ref sum, ref max, ref min, .. } => {
match hit.swap(0, Ordering::Release) as u64 {
0 => ScoresSnapshot::NoData,
hit => ScoresSnapshot::Value {
hit,
sum: sum.swap(0, Ordering::Release) as u64,
max: max.swap(usize::MIN, Ordering::Release) as u64,
min: min.swap(usize::MAX, Ordering::Release) as u64,
}
}
}
}
}
}
#[derive(Clone)]
pub struct AggregateSource(Arc<RwLock<Vec<Arc<MetricScores>>>>);
impl AggregateSource {
pub fn for_each<F>(&self, ops: F) where F: Fn(&MetricScores) {
for metric in self.0.read().unwrap().iter() {
ops(&metric)
}
}
}
pub struct Aggregator {
metrics: Arc<RwLock<Vec<Arc<MetricScores>>>>,
}
impl Aggregator {
pub fn new() -> Aggregator {
Aggregator::with_capacity(0)
}
pub fn with_capacity(size: usize) -> Aggregator {
Aggregator { metrics: Arc::new(RwLock::new(Vec::with_capacity(size))) }
}
}
pub trait AsSource {
fn as_source(&self) -> AggregateSource;
}
impl AsSource for Aggregator {
fn as_source(&self) -> AggregateSource {
AggregateSource(self.metrics.clone())
}
}
impl AsSink<Aggregate, AggregateSink> for Aggregator {
fn as_sink(&self) -> AggregateSink {
AggregateSink(self.metrics.clone())
}
}
pub type Aggregate = Arc<MetricScores>;
#[derive(Clone)]
pub struct AggregateSink(Arc<RwLock<Vec<Aggregate>>>);
impl Sink<Aggregate> for AggregateSink {
#[allow(unused_variables)]
fn new_metric(&self, kind: Kind, name: &str, sampling: Rate) -> Aggregate {
let name = name.to_string();
let metric = Arc::new(MetricScores {
kind,
name,
score: match kind {
Kind::Event => InnerScores::Event { hit: AtomicUsize::new(0) },
_ => InnerScores::Value {
hit: AtomicUsize::new(0),
sum: AtomicUsize::new(0),
max: AtomicUsize::new(usize::MIN),
min: AtomicUsize::new(usize::MAX),
},
},
});
self.0.write().unwrap().push(metric.clone());
metric
}
fn new_scope(&self) -> ScopeFn<Aggregate> {
Arc::new(|cmd| match cmd {
Scope::Write(metric, value) => metric.write(value as usize),
Scope::Flush => {}
})
}
}
#[cfg(feature = "bench")]
mod microbench {
use super::*;
use ::*;
use test::Bencher;
#[bench]
fn time_bench_write_event(b: &mut Bencher) {
let (sink, source) = aggregate();
let metric = sink.new_metric(Kind::Event, &"event_a", 1.0);
let scope = sink.new_scope();
b.iter(|| scope(Scope::Write(&metric, 1)));
}
#[bench]
fn time_bench_write_count(b: &mut Bencher) {
let (sink, source) = aggregate();
let metric = sink.new_metric(Kind::Count, &"count_a", 1.0);
let scope = sink.new_scope();
b.iter(|| scope(Scope::Write(&metric, 1)));
}
#[bench]
fn time_bench_read_event(b: &mut Bencher) {
let (sink, source) = aggregate();
let metric = sink.new_metric(Kind::Event, &"event_a", 1.0);
b.iter(|| metric.read_and_reset());
}
#[bench]
fn time_bench_read_count(b: &mut Bencher) {
let (sink, source) = aggregate();
let metric = sink.new_metric(Kind::Count, &"count_a", 1.0);
b.iter(|| metric.read_and_reset());
}
}