forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
289 lines (240 loc) · 7.19 KB
/
mod.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
mod aggregate;
mod exponential_histogram;
mod histogram;
mod last_value;
mod sum;
use core::fmt;
use std::marker::PhantomData;
use std::ops::{Add, AddAssign, Sub};
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
use std::sync::Mutex;
pub(crate) use aggregate::{AggregateBuilder, ComputeAggregation, Measure};
pub(crate) use exponential_histogram::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
/// Marks a type that can have a value added and retrieved atomically. Required since
/// different types have different backing atomic mechanisms
pub(crate) trait AtomicValue<T>: Sync + Send + 'static {
fn add(&self, value: T);
fn get_value(&self, reset: bool) -> T;
}
/// Keeps track if an atomic value has had a value set since the last reset
pub(crate) struct AtomicTracker<N, T: AtomicValue<N>> {
value: T,
has_value: AtomicBool,
_number: PhantomData<N>, // Required for the N generic to be considered used
}
/// Marks a type that can have an atomic tracker generated for it
pub(crate) trait AtomicallyUpdate<T> {
type AtomicValue: AtomicValue<T>;
fn new_atomic_tracker() -> AtomicTracker<T, Self::AtomicValue>;
}
pub(crate) trait Number<T>:
Add<Output = T>
+ AddAssign
+ Sub<Output = T>
+ PartialOrd
+ fmt::Debug
+ Clone
+ Copy
+ PartialEq
+ Default
+ Send
+ Sync
+ 'static
+ AtomicallyUpdate<T>
{
fn min() -> Self;
fn max() -> Self;
fn into_float(self) -> f64;
}
impl Number<i64> for i64 {
fn min() -> Self {
i64::MIN
}
fn max() -> Self {
i64::MAX
}
fn into_float(self) -> f64 {
// May have precision loss at high values
self as f64
}
}
impl Number<u64> for u64 {
fn min() -> Self {
u64::MIN
}
fn max() -> Self {
u64::MAX
}
fn into_float(self) -> f64 {
// May have precision loss at high values
self as f64
}
}
impl Number<f64> for f64 {
fn min() -> Self {
f64::MIN
}
fn max() -> Self {
f64::MAX
}
fn into_float(self) -> f64 {
self
}
}
impl AtomicValue<u64> for AtomicU64 {
fn add(&self, value: u64) {
self.fetch_add(value, Ordering::Relaxed);
}
fn get_value(&self, reset: bool) -> u64 {
if reset {
self.swap(0, Ordering::Relaxed)
} else {
self.load(Ordering::Relaxed)
}
}
}
impl AtomicallyUpdate<u64> for u64 {
type AtomicValue = AtomicU64;
fn new_atomic_tracker() -> AtomicTracker<u64, Self::AtomicValue> {
AtomicTracker::new(AtomicU64::new(0))
}
}
impl AtomicValue<i64> for AtomicI64 {
fn add(&self, value: i64) {
self.fetch_add(value, Ordering::Relaxed);
}
fn get_value(&self, reset: bool) -> i64 {
if reset {
self.swap(0, Ordering::Relaxed)
} else {
self.load(Ordering::Relaxed)
}
}
}
impl AtomicallyUpdate<i64> for i64 {
type AtomicValue = AtomicI64;
fn new_atomic_tracker() -> AtomicTracker<i64, Self::AtomicValue> {
AtomicTracker::new(AtomicI64::new(0))
}
}
pub(crate) struct F64AtomicValue {
inner: Mutex<f64>, // Floating points don't have true atomics, so we need to use mutex for them
}
impl F64AtomicValue {
pub(crate) fn new() -> Self {
F64AtomicValue {
inner: Mutex::new(0.0),
}
}
}
impl AtomicValue<f64> for F64AtomicValue {
fn add(&self, value: f64) {
let mut guard = self.inner.lock().expect("F64 mutex was poisoned");
*guard += value;
}
fn get_value(&self, reset: bool) -> f64 {
let mut guard = self.inner.lock().expect("F64 mutex was poisoned");
if reset {
let value = *guard;
*guard = 0.0;
value
} else {
*guard
}
}
}
impl AtomicallyUpdate<f64> for f64 {
type AtomicValue = F64AtomicValue;
fn new_atomic_tracker() -> AtomicTracker<f64, Self::AtomicValue> {
AtomicTracker::new(F64AtomicValue::new())
}
}
impl<N, T: AtomicValue<N>> AtomicTracker<N, T> {
fn new(value: T) -> Self {
AtomicTracker {
value,
has_value: AtomicBool::new(false),
_number: PhantomData,
}
}
pub(crate) fn add(&self, value: N) {
// Technically we lose atomicity from using 2 atomics. However, the `add()` is specifically
// designed mutate the value *then* set `has_value`, while the `get_value()` is designed
// to read `has_value` *then* read the value. This means that in a worst case race
// condition, the value added may get picked up from a previous `get_value()` call, but
// the `has_value` being true will be picked up in the next `get_value()` call. This really
// should only mean that the first export gets the added value, and the 2nd export will
// get a 0 value.
//
// This doesn't seem like a big deal, and we avoid the cost of locking.
self.value.add(value);
self.has_value.store(true, Ordering::Release);
}
pub(crate) fn get_value(&self, reset: bool) -> Option<N> {
let has_value = if reset {
self.has_value.swap(false, Ordering::AcqRel)
} else {
self.has_value.load(Ordering::Acquire)
};
if has_value {
Some(self.value.get_value(reset))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_add_and_get_u64_atomic_value() {
let atomic = u64::new_atomic_tracker();
atomic.add(15);
atomic.add(10);
let value = atomic.get_value(false).unwrap();
assert_eq!(value, 25);
}
#[test]
fn can_reset_u64_atomic_value() {
let atomic = u64::new_atomic_tracker();
atomic.add(15);
let value = atomic.get_value(true);
let value2 = atomic.get_value(false);
assert_eq!(value, Some(15), "Incorrect first value");
assert_eq!(value2, None, "Incorrect second value");
}
#[test]
fn can_add_and_get_i64_atomic_value() {
let atomic = i64::new_atomic_tracker();
atomic.add(15);
atomic.add(-10);
let value = atomic.get_value(false).unwrap();
assert_eq!(value, 5);
}
#[test]
fn can_reset_i64_atomic_value() {
let atomic = i64::new_atomic_tracker();
atomic.add(15);
let value = atomic.get_value(true);
let value2 = atomic.get_value(false);
assert_eq!(value, Some(15), "Incorrect first value");
assert_eq!(value2, None, "Incorrect second value");
}
#[test]
fn can_add_and_get_f64_atomic_value() {
let atomic = f64::new_atomic_tracker();
atomic.add(15.3);
atomic.add(10.4);
let value = atomic.get_value(false).unwrap();
assert!(f64::abs(25.7 - value) < 0.0001);
}
#[test]
fn can_reset_f64_atomic_value() {
let atomic = f64::new_atomic_tracker();
atomic.add(15.5);
let value = atomic.get_value(true).unwrap();
let value2 = atomic.get_value(false);
assert!(f64::abs(15.5 - value) < 0.0001, "Incorrect first value");
assert_eq!(value2, None, "Expected no value from second get_value call");
}
}