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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*<LICENSE>
    This file is part of Memthol.

    Copyright (C) 2020 OCamlPro.

    Memthol is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Memthol is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Memthol.  If not, see <https://www.gnu.org/licenses/>.
*/

//! AST for memtrace's CTF format.

prelude! {}

// /// A span for some type.
// pub type Span<T> {
//     /// Beginning of the span.
//     pub begin: T,
//     /// End of the span.
//     pub end: T,
// }

// impl<T> Span<T>
// where
//     T: PartialOrd + Ord,
// {
//     /// Constructor.
//     ///
//     /// Fails if `begin > end`.
//     pub fn new(begin: T, end: T) -> Res<Self> {
//         if begin > end {
//             bail!("non-monotonous values")
//         }
//         Ok(Self { begin, end })
//     }

//     /// Checks whether the input value is in the span.
//     pub fn contains(&self, value: T) -> bool {
//         (self.begin <= value) && (value <= self.end)
//     }

//     /// Checks whether the input value reference is in the span.
//     pub fn contains_ref(&self, value: impl AsRef<T>) -> bool
//     where
//         for<'a> &'a T: PartialOrd + Ord,
//     {
//         let value = value.as_ref();
//         (&self.begin <= value) && (value <= &self.end)
//     }
// }

// impl<T> Span<T> {
//     /// Map over the bounds of the span.
//     pub fn map<U>(self, f: impl Fn(T) -> U) -> Span<U> {
//         Span {
//             begin: f(self.begin),
//             end: f(self.end),
//         }
//     }

//     /// Reference version of a span.
//     pub fn as_ref(&self) -> Span<&T> {
//         Span {
//             begin: &self.begin,
//             end: &self.end,
//         }
//     }
// }

// impl Span<Clock> {
//     /// Pretty printer for a span of `u64` durations.
//     pub fn pretty_time(&self) -> Span<time::Duration> {
//         Span {
//             begin: time::Duration::from_millis(self.begin),
//             end: time::Duration::from_millis(self.end),
//         }
//     }
// }

// impl<T> fmt::Display for Span<T>
// where
//     T: fmt::Display,
// {
//     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
//         write!(fmt, "[{}, {}]", self.begin, self.end)
//     }
// }

/// Header-related AST types.
pub mod header {
    prelude! {}

    /// Plain header, factors common data between [`Ctf`] and [`Packet`].
    ///
    /// [`Ctf`]: Ctf (Ctf struct)
    /// [`Packet`]: Packet (Packet struct)
    #[derive(Debug, Clone)]
    pub struct Header {
        /// Size of the content of the packet/stream, **without the header**.
        pub content_size: u32,
        /// Size of the content of the packet/stream, **with the header**.
        pub total_content_size: u32,
        /// Header timestamp.
        pub timestamp: Range<Clock>,
        /// Allocation UID range created in whatever the header represents.
        pub alloc_id: Range<AllocUid>,
        /// Associated PID.
        pub pid: Pid,
        /// Memtrace version in use.
        pub version: u16,
    }
    impl Header {
        /// True if the element this header is for has a context.
        ///
        /// Only true in memtrace `v2.*` and above.
        pub fn has_context(&self) -> bool {
            self.version >= 2
        }
    }

    /// CTF header, top-level header of a memtrace dump.
    #[derive(Debug, Clone)]
    pub struct Ctf {
        /// Actual header.
        header: Header,
        /// True if the dump uses big-endian encoding.
        big_e: bool,
    }
    impl ops::Deref for Ctf {
        type Target = Header;
        fn deref(&self) -> &Header {
            &self.header
        }
    }

    impl Ctf {
        /// Constructor.
        pub fn new(header: Header, big_e: bool) -> Self {
            Self { header, big_e }
        }
        /// Header accessor.
        pub fn header(&self) -> &Header {
            &self.header
        }
        /// True if the dump uses big-endian encoding.
        pub fn is_be(&self) -> bool {
            self.big_e
        }
    }

    /// Packet header, contains information about a sequence of events.
    #[derive(Debug, Clone)]
    pub struct Packet {
        /// Actual header.
        header: Header,
        /// Cache-check structure.
        cache_check: ast::CacheCheck,
        /// Packet ID.
        id: usize,
    }
    impl ops::Deref for Packet {
        type Target = Header;
        fn deref(&self) -> &Header {
            &self.header
        }
    }

    impl Packet {
        /// Constructor.
        pub fn new(id: usize, header: Header, cache_check: ast::CacheCheck) -> Self {
            Self {
                id,
                header,
                cache_check,
            }
        }
        /// Header accessor.
        pub fn header(&self) -> &Header {
            &self.header
        }
        /// Cache-checker.
        pub fn cache_check(&self) -> &ast::CacheCheck {
            &self.cache_check
        }
        /// Packet id.
        pub fn id(&self) -> usize {
            self.id
        }
    }

    /// An event header.
    #[derive(Debug, Clone)]
    pub struct Event {
        /// Timestamp of the event.
        timestamp: u32,
        /// Event code.
        code: u8,
    }
    impl Event {
        /// Constructor.
        pub fn new(timestamp: u32, code: u8) -> Self {
            Self { timestamp, code }
        }
        /// Timestamp accessor.
        pub fn timestamp(&self) -> u32 {
            self.timestamp
        }
        /// Event code accessor.
        pub fn code(&self) -> u8 {
            self.code
        }
    }
}

/// Event-related types.
pub mod event {
    use super::*;
    // prelude! {}

    /// Code for info events.
    const INFO_CODE: u32 = 0;
    /// Code for location events.
    const LOCS_CODE: u32 = 1;
    /// Code for allocation events.
    const ALLOC_CODE: u32 = 2;
    /// Code for promotion events.
    const PROMOTION_CODE: u32 = 3;
    /// Code for collection events.
    const COLLECTION_CODE: u32 = 4;

    /// Relative codes encoding small allocations.
    const SMALL_ALLOC_REDUCED_CODES: Range<u32> = Range::new(1, 16);
    /// Offset for small allocation codes.
    const SMALL_ALLOC_OFFSET: u32 = 100;

    /// Absolute codes encoding small allocations.
    const SMALL_ALLOC_CODES: Range<u32> = Range::new(
        SMALL_ALLOC_REDUCED_CODES.lbound + SMALL_ALLOC_OFFSET,
        SMALL_ALLOC_REDUCED_CODES.ubound + SMALL_ALLOC_OFFSET,
    );

    /// Event kind.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum Kind {
        /// Info event.
        Info,
        /// Locations event.
        Locs,
        /// Allocation event.
        Alloc,
        /// Promotion event.
        Promotion,
        /// Collection event.
        Collection,
        /// Stores a value between `1` and `16`.
        SmallAlloc(u32),
    }
    impl Kind {
        /// Checks the invariant for the `SmallAlloc` variant.
        fn small_alloc_invariant(code: u32) {
            if !SMALL_ALLOC_REDUCED_CODES.contains(code) {
                panic!(
                    "illegal small allocation reduced code, expected {} <= {} <= {}",
                    SMALL_ALLOC_REDUCED_CODES.lbound, code, SMALL_ALLOC_REDUCED_CODES.ubound
                )
            }
        }

        /// True if the event is an info event.
        pub fn is_info(self) -> bool {
            self == Self::Info
        }

        /// Constructor from an event code.
        pub fn from_code(code: u32) -> Res<Self> {
            let res = if code == INFO_CODE {
                Self::Info
            } else if code == LOCS_CODE {
                Self::Locs
            } else if code == ALLOC_CODE {
                Self::Alloc
            } else if code == PROMOTION_CODE {
                Self::Promotion
            } else if code == COLLECTION_CODE {
                Self::Collection
            } else if SMALL_ALLOC_CODES.contains(code) {
                let reduced_code = code - SMALL_ALLOC_OFFSET;
                Self::small_alloc_invariant(reduced_code);
                Self::SmallAlloc(reduced_code)
            } else {
                bail!("unexpected event code `{}`", code)
            };
            Ok(res)
        }

        /// Event code of an event kind.
        pub fn code(self) -> u32 {
            match self {
                Self::Info => INFO_CODE,
                Self::Locs => LOCS_CODE,
                Self::Alloc => ALLOC_CODE,
                Self::Promotion => PROMOTION_CODE,
                Self::Collection => COLLECTION_CODE,
                Self::SmallAlloc(n) => {
                    Self::small_alloc_invariant(n);
                    n + 100
                }
            }
        }
    }

    /// An event, decoded version.
    #[derive(Debug, Clone)]
    pub enum Event<'data> {
        /// Location event.
        Locs(Locs<'data>),
        /// Allocation event.
        Alloc(Alloc),
        /// Promotion event.
        Promotion(u64),
        /// Collection event.
        Collection(u64),
    }
    impl<'data> Event<'data> {
        /// One-word description of the event.
        pub fn name(&self) -> &'static str {
            match self {
                Self::Locs(_) => "locations",
                Self::Alloc(_) => "allocation",
                Self::Promotion(_) => "promotion",
                Self::Collection(_) => "collection",
            }
        }

        /// Verbose description of the event.
        pub fn desc(&self) -> String {
            let name = self.name();
            match self {
                Self::Alloc(alloc) => format!(
                    "{}({} @ {})",
                    name,
                    alloc.id,
                    alloc.alloc_time.display_micros(),
                ),
                Self::Collection(id) => format!("{}({})", name, id),
                Self::Promotion(id) => format!("{}({})", name, id),
                _ => name.into(),
            }
        }
    }

    /// Information event.
    ///
    /// This kind of event is expected to appear exactly once at the beginning, right after the CTF
    /// (top-level) header.
    #[derive(Debug, Clone)]
    pub struct Info<'data> {
        /// Sample rate.
        pub sample_rate: f64,
        /// Word size.
        pub word_size: u8,
        /// Executable name.
        pub exe_name: String,
        /// Name of the host system.
        pub host_name: String,
        /// Parameters for the executable.
        pub exe_params: String,
        /// Process PID.
        pub pid: u64,
        /// Context.
        pub context: Option<&'data str>,
    }
    impl<'data> Info<'data> {
        /// Code for this event.
        pub const fn event_code() -> u32 {
            INFO_CODE
        }
        /// Name of this event.
        pub const fn name() -> &'static str {
            "trace_info"
        }

        /// Turns itself into an `Init`.
        pub fn to_init(&self, start_time: time::Date) -> alloc_data::Init {
            alloc_data::Init::new(
                start_time,
                None,
                convert(self.word_size, "ctf parser: word_size"),
                false,
            )
            .sample_rate(self.sample_rate)
        }
    }

    /// Types of allocation
    #[derive(Debug, Clone, Copy)]
    pub enum AllocSource {
        /// Minor heap allocation
        Minor,
        /// Major heap allocation
        Major,
        /// External allocation
        External
    }

    /// Allocation event.
    #[derive(Debug, Clone)]
    pub struct Alloc {
        /// Event UID.
        pub id: u64,
        /// Size of the allocation.
        pub len: usize,
        /// Timestamp at which the allocation occured.
        pub alloc_time: time::Duration,
        /// Sample count.
        pub nsamples: usize,
        /// Initial allocation place
        pub source: AllocSource,
        /// Backtrace of the allocation.
        pub backtrace: Vec<usize>,
        /// Backtrace common prefix *w.r.t.* the previous backtrace.
        pub common_pref_len: usize,
    }
    impl Alloc {
        /// Code of this event.
        pub const fn event_code() -> u32 {
            ALLOC_CODE
        }
        /// Name of this event.
        pub const fn name() -> &'static str {
            "alloc"
        }
    }

    /// Promotion event.
    #[derive(Debug, Clone)]
    pub struct Promotion {
        /// Allocation UID delta.
        ///
        /// Specifies the difference between the UID of the last allocation and the UID that is
        /// being promoted.
        pub id_delta: u64,
    }
    impl Promotion {
        /// Code of this event.
        pub const fn event_code() -> u32 {
            PROMOTION_CODE
        }
        /// Name of this event.
        pub const fn name() -> &'static str {
            "promote"
        }
    }

    /// Collection event.
    #[derive(Debug, Clone)]
    pub struct Collection {
        /// Allocation UID delta.
        ///
        /// Specifies the difference between the UID of the last allocation and the UID that is
        /// being collected.
        pub id_delta: u64,
    }
    impl Collection {
        /// Code of this event.
        pub const fn event_code() -> u32 {
            COLLECTION_CODE
        }
        /// Name of this event.
        pub const fn name() -> &'static str {
            "collect"
        }
    }
}

/// A collection of locations.
#[derive(Debug, Clone)]
pub struct Locs<'data> {
    /// ID of the locations.
    pub id: u64,
    /// Locations.
    pub locs: Vec<loc::Location<'data>>,
}

/// Cache-check data.
#[derive(Debug, Clone)]
pub struct CacheCheck {
    /// Currently unused.
    pub ix: u16,
    /// Currently unused.
    pub pred: u16,
    /// Currently unused.
    pub value: u64,
}