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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
prelude! {}
use mem::Factory;
pub use data_parser::*;
peg::parser! {
grammar data_parser() for str {
rule ws() = quiet! {
[' ' | '\n' | '\t']+
}
rule _() = quiet! {
ws()?
}
rule integer() -> &'input str
= $(
"0"
/ ['1'..='9'] ['0'..='9']*
)
pub rule big_uint() -> BigUint
= quiet! {
n: integer() {?
BigUint::parse_bytes(n.as_bytes(), 10).ok_or("illegal integer (big uint)")
}
}
/ expected!("integer (big uint)")
pub rule u32() -> u32
= quiet! {
n: integer() {?
n.parse().map_err(|e| "illegal integer (u32)")
}
}
/ expected!("integer (u32)")
pub rule u64() -> u64
= quiet! {
n: integer() {?
n.parse().map_err(|e| "illegal integer (u64)")
}
}
/ expected!("integer (u64)")
pub rule usize() -> usize
= quiet! {
n: integer() {?
n.parse().map_err(|e| "illegal integer (usize)")
}
}
/ expected!("integer (usize)")
pub rule string() -> &'input str
= "`" s: $( (!['`'] [_])* ) "`" { s }
/ expected!("string (delimited by backquotes)")
pub rule str(f: &mut Factory) -> Str
= "`" s: $( (!['`'] [_])* ) "`" { f.register_str(s) }
/ expected!("string (delimited by backquotes)")
pub rule string_list() -> Vec<String>
= "[" list: ( (_ s: string() { s.to_string() })* ) _ "]" { list }
/ expected!("list of whitespace-separated, backquote-delimited strings")
pub rule str_list(f: &mut Factory) -> Vec<Str>
= "[" list: ( (_ s: str(f) { s })* ) _ "]" { list }
/ expected!("list of whitespace-separated, backquote-delimited strings")
pub rule loc(f: &mut Factory) -> Loc
= file: str(f)
_ ":"
_ line: usize()
_ ":"
_ col_start: usize()
_ "-"
_ col_end: usize()
{
Loc::new(file, line, (col_start, col_end))
}
/ expected!("file location")
pub rule counted_loc(f: &mut Factory) -> CLoc
= loc: loc(f) "#" count: usize() { CLoc::new(loc, count) }
pub rule loc_list(f: &mut Factory) -> Vec<CLoc>
= "[" list: ( (_ loc: counted_loc(f) { loc })* ) _ "]" { list }
/ expected!("list of whitespace-separated locations")
pub rule secs() -> (u64, u32)
= secs: u64() sub_sec: (
sub_sec: (
"."
heading_zeros_str: $(['0']*)
sub_sec_opt: u32()? {
(heading_zeros_str.len(), sub_sec_opt)
}
)? {
sub_sec.and_then(|(heading_zeros_count, sub_sec_opt)|
sub_sec_opt.map(
|sub_sec| (heading_zeros_count, sub_sec)
)
)
}
) {?
let sub_sec_res = sub_sec.map(
|(heading_zeros_count, sub_sec)| {
let digit_count = {
let (mut acc, mut len) = (sub_sec, 0);
while acc > 0 {
acc = acc / 10;
len += 1
}
len + heading_zeros_count
};
if digit_count > 9 {
Err("\
illegal date (seconds), \
precision below nanoseconds is not supported\
")
} else {
let sub_sec_offset = 9 - digit_count;
let mut sub_sec = sub_sec;
for _ in 0..sub_sec_offset {
sub_sec = sub_sec * 10
}
Ok(sub_sec)
}
}
).unwrap_or_else(
|| Ok(0u32)
);
sub_sec_res.map(|sub_sec|(secs, sub_sec))
}
/ expected!("seconds (float with at most nanosecond sub-second precision)")
pub rule lifetime() -> time::Lifetime
= secs: secs() {
time::Duration::new(secs.0, secs.1).into()
}
/ expected!("an amount of seconds (float, lifetime)")
pub rule since_start() -> time::SinceStart
= secs: secs() {
time::Duration::new(secs.0, secs.1).into()
}
/ expected!("an amount of seconds (float) since the start of the run")
pub rule since_start_opt() -> Option<time::SinceStart>
= "_" { None }
/ time: since_start() { Some(time) }
/ expected!("an optional amount of seconds (float) since the start of the run, `_` if none")
pub rule date() -> time::Date
= secs: secs() {?
let (secs, sub_secs) = secs;
i64::try_from(secs).map(
|secs| time::Date::from_timestamp(secs, sub_secs)
).map_err(|_| "illegal amount of seconds for a date")
}
pub rule uid() -> uid::Alloc
= quiet! {
uid: usize() { uid.into() }
}
/ expected!("UID (big uint)")
pub rule alloc_kind() -> AllocKind
= "Minor" { AllocKind::Minor }
/ "MajorPostponed" { AllocKind::MajorPostponed }
/ "Major" { AllocKind::Major }
/ "Serialized" { AllocKind::Serialized }
/ "_" { AllocKind::Unknown }
/ expected!("allocation kind")
pub rule new_alloc(f: &mut Factory) -> Builder
= uid: uid()
_ ":"
_ kind: alloc_kind()
_ size: u32()
_ callstack: loc_list(f)
_ labels: str_list(f)
_ toc: since_start()
_ tod: since_start_opt()
{
let labels = f.register_labels(labels);
let callstack = f.register_trace(callstack);
Builder::new(Some(uid), kind, size, callstack, labels, toc, tod)
}
/ expected!("allocation data")
pub rule diff_new_allocs(f: &mut Factory) -> Vec<Builder>
= "new" _ "{"
new_allocs: ( _ new_alloc: new_alloc(f) { new_alloc })*
_ "}" {
new_allocs
}
/ expected!("list of new allocations")
pub rule dead_alloc() -> (uid::Alloc, time::SinceStart)
= uid: uid() _ ":" _ secs: since_start() { (uid, secs) }
pub rule diff_dead_allocs() -> Vec<(uid::Alloc, time::SinceStart)>
= "dead" _ "{"
dead_allocs: (_ dead_alloc: dead_alloc() { dead_alloc })*
_ "}" {
dead_allocs
}
/ expected!("list of dead allocations")
pub rule diff(f: &mut Factory) -> Diff =
_ date: since_start()
_ new: diff_new_allocs(f)
_ dead: diff_dead_allocs()
_
{
Diff::new(date, new, dead)
}
pub rule callstack_is_reversed() -> bool =
"callstacks" _ ":" _ is_rev:(
"main" _ "to" _ "site" { false }
/ "site" _ "to" _ "main" { true }
/ expected!("either `main to site` or `site to main`")
) {
is_rev
}
/ { false }
pub rule init() -> Init =
_ "start" _ ":" _ start_time: date()
_ "word_size" _ ":" _ word_size: usize()
_ stack_is_rev: callstack_is_reversed()
_
{
Init::new(start_time, None, word_size, stack_is_rev)
}
}
}
pub trait Parseable: Sized {
type Info;
fn parse_with(text: impl AsRef<str>, info: &Self::Info) -> Res<Self>;
fn parse(text: impl AsRef<str>) -> Res<Self>
where
Self: Parseable<Info = ()>,
{
Self::parse_with(text, &())
}
}
macro_rules! implement {
(
Parseable($txt:ident) for {
$($inner:tt)*
}
$($tail:tt)*
) => {
implement! {
@($txt, _info: ()) $($inner)*
}
implement! {
$($tail)*
}
};
(
Parseable<$info_ty:ty>($txt:ident, $info:ident) for {
$($inner:tt)*
}
$($tail:tt)*
) => {
implement! {
@($txt, $info: $info_ty) $($inner)*
}
implement! {
$($tail)*
}
};
() => ();
(@($txt:ident, $info:ident: $info_ty:ty)
$ty:ty => $def:expr $( , $($tail:tt)*)?
) => {
impl Parseable for $ty {
type Info = $info_ty;
fn parse_with(text: impl AsRef<str>, $info: &Self::Info) -> Res<Self> {
let $txt = text.as_ref();
let res = $def?;
Ok(res)
}
}
$(
implement! {
@($txt, $info: $info_ty) $($tail)*
}
)?
};
(@($($stuff:tt)*)) => {};
}
implement! {
Parseable(text) for {
usize => usize(text),
u32 => u32(text),
u64 => u64(text),
time::SinceStart => since_start(text),
time::Lifetime => lifetime(text),
time::Date => date(text),
uid::Alloc => uid(text),
AllocKind => alloc_kind(text),
Init => init(text),
Loc => loc(text, &mut Factory::new(false)),
CLoc => counted_loc(text, &mut Factory::new(false)),
}
Parseable<Init>(text, init) for {
Builder => new_alloc(text, &mut Factory::new(init.callstack_is_rev)),
Diff => diff(text, &mut Factory::new(init.callstack_is_rev)),
}
}