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
prelude! {}
use filter::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RawSubFilter {
Size(SizeFilter),
Lifetime(LifetimeFilter),
Label(LabelFilter),
Loc(LocFilter),
}
impl RawSubFilter {
pub fn kind(&self) -> FilterKind {
match self {
Self::Size(_) => FilterKind::Size,
Self::Lifetime(_) => FilterKind::Lifetime,
Self::Label(_) => FilterKind::Label,
Self::Loc(_) => FilterKind::Loc,
}
}
pub fn apply(&self, timestamp: &time::SinceStart, alloc: &Alloc) -> bool {
match self {
RawSubFilter::Size(filter) => filter.apply(&alloc.size),
RawSubFilter::Lifetime(filter) => {
let timestamp = alloc
.tod()
.map(|tod| std::cmp::min(tod, *timestamp))
.unwrap_or(*timestamp);
filter.apply_at(×tamp, &alloc.toc())
}
RawSubFilter::Label(filter) => filter.apply(&alloc.labels()),
RawSubFilter::Loc(filter) => filter.apply(&alloc.trace()),
}
}
pub fn change_kind(&mut self, kind: FilterKind) -> bool {
if self.kind() == kind {
return false;
}
*self = Self::from(kind);
true
}
pub fn update(&mut self, update: Update) -> Res<bool> {
macro_rules! fail {
() => {
bail!("cannot apply update `{}` to filter `{}`", update, self)
};
}
match self {
Self::Size(filter) => match update {
Update::Size(update) => filter.update(update),
_ => fail!(),
},
Self::Lifetime(filter) => match update {
Update::Lifetime(update) => filter.update(update),
_ => fail!(),
},
Self::Label(filter) => match update {
Update::Label(update) => filter.update(update),
_ => fail!(),
},
Self::Loc(filter) => match update {
Update::Loc(update) => filter.update(update),
_ => fail!(),
},
}
}
}
#[cfg(not(feature = "server"))]
static CREATOR_FLAG: bool = true;
#[cfg(feature = "server")]
static CREATOR_FLAG: bool = false;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubFilter {
uid: uid::SubFilter,
raw: RawSubFilter,
from_client: bool,
}
impl SubFilter {
fn from(uid: uid::SubFilter, raw: RawSubFilter) -> Self {
Self {
uid,
raw,
from_client: CREATOR_FLAG,
}
}
}
impl std::cmp::PartialOrd for SubFilter {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.uid.cmp(&other.uid))
}
}
impl std::cmp::Ord for SubFilter {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.uid.cmp(&other.uid)
}
}
impl SubFilter {
pub fn new(uid: uid::SubFilter, raw: RawSubFilter) -> Self {
Self::from(uid, raw)
}
pub fn uid(&self) -> uid::SubFilter {
self.uid
}
pub fn raw(&self) -> &RawSubFilter {
&self.raw
}
pub fn is_from_client(&self) -> bool {
self.from_client
}
pub fn sanitize(&mut self) {
if self.is_from_client() {
self.uid = uid::SubFilter::fresh()
}
}
}
pub enum Update {
Size(ord::SizeUpdate),
Lifetime(ord::LifetimeUpdate),
Label(label::LabelUpdate),
Loc(loc::LocUpdate),
}
base::implement! {
impl SubFilter {
Display {
|&self, fmt| write!(fmt, "{}({})", self.uid, self.raw)
}
Default {
Self::from(uid::SubFilter::fresh(), RawSubFilter::default()),
}
From {
from FilterKind => |kind| Self::from(
uid::SubFilter::fresh(), RawSubFilter::from(kind)
),
from SizeFilter => |filter| Self::from(
uid::SubFilter::fresh(), RawSubFilter::from(filter)
),
from LifetimeFilter => |filter| Self::from(
uid::SubFilter::fresh(), RawSubFilter::from(filter)
),
from LabelFilter => |filter| Self::from(
uid::SubFilter::fresh(), RawSubFilter::from(filter)
),
from LocFilter => |filter| Self::from(
uid::SubFilter::fresh(), RawSubFilter::from(filter)
),
from RawSubFilter => |filter| Self::from(
uid::SubFilter::fresh(), filter
),
}
Deref {
to RawSubFilter => |&self| &self.raw
}
DerefMut {
|&mut self| &mut self.raw
}
}
impl RawSubFilter {
Display {
|&self, fmt| match self {
Self::Size(filter) => write!(fmt, "size {}", filter),
Self::Lifetime(filter) => write!(fmt, "lifetime {}", filter),
Self::Label(filter) => write!(fmt, "labels {}", filter),
Self::Loc(filter) => write!(fmt, "callstack {}", filter),
}
}
Default {
SizeFilter::default().into()
}
From {
from FilterKind => |kind| match kind {
FilterKind::Size => SizeFilter::default().into(),
FilterKind::Lifetime => LifetimeFilter::default().into(),
FilterKind::Label => LabelFilter::default().into(),
FilterKind::Loc => LocFilter::default().into(),
},
from SizeFilter => |filter| Self::Size(filter),
from LifetimeFilter => |filter| Self::Lifetime(filter),
from LabelFilter => |filter| Self::Label(filter),
from LocFilter => |filter| Self::Loc(filter),
}
}
impl Update {
Display {
|&self, fmt| match self {
Self::Size(update) => update.fmt(fmt),
Self::Lifetime(update) => update.fmt(fmt),
Self::Label(update) => update.fmt(fmt),
Self::Loc(update) => update.fmt(fmt),
}
}
}
}