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
use std::fmt::{self, Display};
use std::num::NonZeroU64;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
type HashMap<K, V> =
std::collections::HashMap<K, V, std::hash::BuildHasherDefault<fxhash::FxHasher>>;
#[derive(Debug, PartialEq, Copy, Clone, Hash, Eq)]
pub enum Id {
Name(&'static str),
Number(u64),
}
impl Id {
pub fn readable_name(&self) -> String {
match self {
Id::Name(s) => {
let mut s = s.replace('_', " ");
s.make_ascii_lowercase();
s
}
Id::Number(n) => n.to_string(),
}
}
}
impl From<&'static str> for Id {
#[inline]
fn from(s: &'static str) -> Self {
Id::Name(s)
}
}
impl From<u64> for Id {
#[inline]
fn from(id: u64) -> Self {
Id::Number(id)
}
}
impl From<NonZeroU64> for Id {
#[inline]
fn from(id: NonZeroU64) -> Self {
Id::Number(id.into())
}
}
impl Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Id::Number(n) => write!(f, "{}", n),
Id::Name(n) => write!(f, "{}", n),
}
}
}
#[derive(Clone, Copy)]
pub enum TraceEvent {
Add(usize),
Sub(usize),
Reset(usize),
}
pub trait MemoryTrace {
fn trace(&self, event: TraceEvent);
fn snapshot(&self) -> MemoryTraceSnapshot;
fn sub_trace(&self, id: Id) -> Arc<dyn MemoryTrace + Send + Sync>;
fn add_sub_trace(&mut self, id: Id, trace: Arc<dyn MemoryTrace + Send + Sync>);
fn sum(&self) -> usize;
}
pub struct MemoryTraceNode {
pub id: Id,
trace: AtomicUsize,
children: HashMap<Id, Arc<dyn MemoryTrace + Send + Sync>>,
}
impl MemoryTraceNode {
pub fn new(id: impl Into<Id>) -> MemoryTraceNode {
MemoryTraceNode {
id: id.into(),
trace: std::sync::atomic::AtomicUsize::default(),
children: HashMap::default(),
}
}
}
impl MemoryTrace for MemoryTraceNode {
fn trace(&self, event: TraceEvent) {
match event {
TraceEvent::Add(val) => {
self.trace.fetch_add(val, Ordering::Relaxed);
}
TraceEvent::Sub(val) => {
self.trace.fetch_sub(val, Ordering::Relaxed);
}
TraceEvent::Reset(val) => {
self.trace.swap(val, Ordering::Relaxed);
}
}
}
fn snapshot(&self) -> MemoryTraceSnapshot {
MemoryTraceSnapshot {
id: self.id,
trace: self.trace.load(Ordering::Relaxed),
children: self.children.values().map(|c| c.snapshot()).collect(),
}
}
fn sub_trace(&self, id: Id) -> Arc<dyn MemoryTrace + Send + Sync> {
self.children.get(&id).cloned().unwrap()
}
fn add_sub_trace(&mut self, id: Id, trace: Arc<dyn MemoryTrace + Send + Sync>) {
self.children.insert(id, trace);
}
fn sum(&self) -> usize {
let sum: usize = self.children.values().map(|c| c.sum()).sum();
sum + self.trace.load(Ordering::Relaxed)
}
}
pub struct MemoryTraceSnapshot {
pub id: Id,
pub trace: usize,
pub children: Vec<MemoryTraceSnapshot>,
}
#[macro_export]
macro_rules! mem_trace {
($name: ident) => {
{
use tikv_alloc::trace::MemoryTraceNode;
std::sync::Arc::new(MemoryTraceNode::new(stringify!($name)))
}
};
($name: ident, [$($child:tt),+]) => {
{
use tikv_alloc::trace::MemoryTrace;
let mut node = mem_trace!($name);
$(
let child = mem_trace!($child);
std::sync::Arc::get_mut(&mut node).unwrap().add_sub_trace(child.id, child);
)*
node
}
};
(($name: ident, [$($child:tt),+])) => {
mem_trace!($name, [$($child),*])
}
}
#[cfg(test)]
mod tests {
use crate::{
self as tikv_alloc,
trace::{Id, MemoryTrace, TraceEvent},
};
#[test]
fn test_id_readable_name() {
let id = Id::Name("test_id");
assert_eq!(id.readable_name(), "test id");
let id = Id::Number(100);
assert_eq!(id.readable_name(), "100");
}
#[test]
fn test_mem_trace_macro() {
let trace = mem_trace!(root, [(mid1, [leaf1, leaf2]), (mid2, [leaf3])]);
trace
.sub_trace(Id::Name("mid1"))
.sub_trace(Id::Name("leaf2"))
.trace(TraceEvent::Add(1));
trace
.sub_trace(Id::Name("mid2"))
.sub_trace(Id::Name("leaf3"))
.trace(TraceEvent::Add(10));
trace
.sub_trace(Id::Name("mid2"))
.trace(TraceEvent::Add(100));
assert_eq!(111, trace.sum());
}
}