-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing_map.rs
More file actions
69 lines (59 loc) · 1.72 KB
/
Copy pathtracing_map.rs
File metadata and controls
69 lines (59 loc) · 1.72 KB
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
//! linux-parity: complete
//! linux-source: vendor/linux/kernel/trace/tracing_map.c
//! test-origin: linux:vendor/linux/kernel/trace/tracing_map.c
//! Bounded hash map used by histograms to aggregate values.
//!
//! Ref: vendor/linux/kernel/trace/tracing_map.c
extern crate alloc;
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use spin::Mutex;
pub struct TracingMap<K: Ord + Clone> {
inner: Mutex<BTreeMap<K, u64>>,
capacity: usize,
}
impl<K: Ord + Clone> TracingMap<K> {
pub fn new(capacity: usize) -> Self {
Self {
inner: Mutex::new(BTreeMap::new()),
capacity,
}
}
/// Returns `Err(-ENOSPC)` if the new key exceeds capacity.
pub fn add(&self, k: K, v: u64) -> Result<u64, i32> {
let mut g = self.inner.lock();
if !g.contains_key(&k) && g.len() == self.capacity {
return Err(-28); // -ENOSPC
}
let entry = g.entry(k).or_insert(0);
*entry += v;
Ok(*entry)
}
pub fn get(&self, k: &K) -> Option<u64> {
self.inner.lock().get(k).copied()
}
pub fn len(&self) -> usize {
self.inner.lock().len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_aggregates() {
let m: TracingMap<i32> = TracingMap::new(4);
m.add(1, 5).unwrap();
m.add(1, 10).unwrap();
assert_eq!(m.get(&1), Some(15));
}
#[test]
fn overflow_returns_enospc() {
let m: TracingMap<u32> = TracingMap::new(2);
m.add(1, 1).unwrap();
m.add(2, 1).unwrap();
// Existing keys still allowed to grow.
m.add(1, 1).unwrap();
// New key beyond capacity → -ENOSPC.
assert_eq!(m.add(3, 1).unwrap_err(), -28);
}
}