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
//! This module contains the actual structs.
//!
//! This is mostly to separate them from the support code of this crate.

// Having a file named aux causes errors on windows so we rename it here.
#[path = "aux_record.rs"]
mod aux;
mod aux_output_hw_id;
mod bpf_event;
mod cgroup;
mod comm;
mod exit;
mod itrace_start;
mod ksymbol;
mod lost;
mod lost_samples;
mod mmap;
mod mmap2;
mod namespaces;
mod read;
mod sample;
mod switch_cpu_wide;
mod text_poke;
mod throttle;

use perf_event_open_sys::bindings::perf_event_header;

pub use self::aux::*;
pub use self::aux_output_hw_id::*;
pub use self::bpf_event::*;
pub use self::cgroup::*;
pub use self::comm::*;
pub use self::exit::*;
pub use self::itrace_start::*;
pub use self::ksymbol::*;
pub use self::lost::*;
pub use self::lost_samples::*;
pub use self::mmap::*;
pub use self::mmap2::*;
pub use self::namespaces::*;
pub use self::read::*;
pub use self::sample::*;
pub use self::switch_cpu_wide::*;
pub use self::text_poke::*;
pub use self::throttle::*;

/// FORK records indicate that a process called [`fork(2)`] successfully.
///
/// This struct corresponds to `PERF_RECORD_FORK`. See the [manpage] for more
/// documentation.
///
/// [`fork(2)`]: https://man7.org/linux/man-pages/man2/fork.2.html
/// [manpage]: http://man7.org/linux/man-pages/man2/perf_event_open.2.html
pub type Fork = Exit;

mod sample_id {
    option_struct! {
        ##[copy]
        pub(super) struct SampleId : u8 {
            pub pid: u32,
            pub tid: u32,
            pub time: u64,
            pub id: u64,
            pub stream_id: u64,
            pub cpu: u32,
        }
    }
}

use std::borrow::Cow;
use std::fmt;

use crate::prelude::*;

/// A subset of the sample fields that can be recorded in non-SAMPLE records.
///
/// This will be empty by default unless `sample_id_all` was set when
/// configuring the perf event counter.
#[derive(Copy, Clone, Default)]
pub struct SampleId(sample_id::SampleId);

impl SampleId {
    /// The process ID that generated this event.
    pub fn pid(&self) -> Option<u32> {
        self.0.pid().copied()
    }

    /// The thread ID that generated this event.
    pub fn tid(&self) -> Option<u32> {
        self.0.tid().copied()
    }

    /// The time at which this event was recorded.
    ///
    /// The clock used to record the time depends on how the clock was
    /// configured when setting up the counter.
    pub fn time(&self) -> Option<u64> {
        self.0.time().copied()
    }

    /// The unique kernel-assigned ID for the leader of this counter group.
    pub fn id(&self) -> Option<u64> {
        self.0.id().copied()
    }

    /// The unique kernel-assigned ID for the counter that generated this event.
    pub fn stream_id(&self) -> Option<u64> {
        self.0.stream_id().copied()
    }

    /// The CPU on which this event was recorded.
    pub fn cpu(&self) -> Option<u32> {
        self.0.cpu().copied()
    }

    /// Get the length in bytes that this struct would need to be parsed from
    /// the provided parser.
    pub fn estimate_len<E: Endian>(config: &ParseConfig<E>) -> usize {
        let sty = config.sample_type();

        if !config.sample_id_all() {
            return 0;
        }

        let flags = SampleFlags::TID
            | SampleFlags::TIME
            | SampleFlags::ID
            | SampleFlags::STREAM_ID
            | SampleFlags::CPU
            | SampleFlags::IDENTIFIER;

        (sty & flags).bits().count_ones() as usize * std::mem::size_of::<u64>()
    }
}

impl<'p> Parse<'p> for SampleId {
    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
    where
        E: Endian,
        B: ParseBuf<'p>,
    {
        let config = p.config();
        let sty = config.sample_type();

        if !config.sample_id_all() {
            return Ok(Self::default());
        }

        let pid = p.parse_if(sty.contains(SampleFlags::TID))?;
        let tid = p.parse_if(sty.contains(SampleFlags::TID))?;
        let time = p.parse_if(sty.contains(SampleFlags::TIME))?;
        let id = p.parse_if(sty.contains(SampleFlags::ID))?;
        let stream_id = p.parse_if(sty.contains(SampleFlags::STREAM_ID))?;
        let cpu = p.parse_if_with(sty.contains(SampleFlags::CPU), |p| {
            Ok((p.parse_u32()?, p.parse_u32()?).0)
        })?;
        let identifier = p.parse_if(sty.contains(SampleFlags::IDENTIFIER))?;

        Ok(Self(sample_id::SampleId::new(
            pid,
            tid,
            time,
            id.or(identifier),
            stream_id,
            cpu,
        )))
    }
}

impl fmt::Debug for SampleId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// A record emitted by the linux kernel.
///
/// This enum contains every supported record type emitted by the kernel.
/// Depending on how the perf event counter was configured only a few of these
/// will be emitted by any one counter.
#[derive(Clone, Debug)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum Record<'a> {
    Mmap(Mmap<'a>),
    Lost(Lost),
    Comm(Comm<'a>),
    Exit(Exit),
    Throttle(Throttle),
    Unthrottle(Throttle),
    Fork(Fork),
    Read(Read),
    Sample(Box<Sample<'a>>),
    Mmap2(Mmap2<'a>),
    Aux(Aux),
    ITraceStart(ITraceStart),
    LostSamples(LostSamples),
    Switch,
    SwitchCpuWide(SwitchCpuWide),
    Namespaces(Namespaces<'a>),
    KSymbol(KSymbol<'a>),
    BpfEvent(BpfEvent),
    CGroup(CGroup<'a>),
    TextPoke(TextPoke<'a>),
    AuxOutputHwId(AuxOutputHwId),

    /// A record type that is unknown to this crate.
    ///
    /// Note that just because a record is parsed as unknown in one release of
    /// this crate does not mean it will continue to be parsed in future
    /// releases. Adding a new variant to this enum is not considered to be a
    /// breaking change.
    ///
    /// If you find yourself using the unknown variant to parse valid records
    /// emitted by the kernel please file an issue or create a PR to add support
    /// for them.
    Unknown {
        ty: u32,
        data: Cow<'a, [u8]>,
    },
}

macro_rules! record_from {
    ($ty:ident) => {
        impl<'a> From<$ty> for Record<'a> {
            fn from(value: $ty) -> Self {
                Self::$ty(value)
            }
        }
    };
    ($ty:ident<$lt:lifetime>) => {
        impl<$lt> From<$ty<$lt>> for Record<$lt> {
            fn from(value: $ty<$lt>) -> Self {
                Self::$ty(value)
            }
        }
    };
}

record_from!(Mmap<'a>);
record_from!(Lost);
record_from!(Comm<'a>);
// These are both the same struct
// record_from!(Exit);
// record_from!(Fork);
record_from!(Read);
record_from!(Mmap2<'a>);
record_from!(Aux);
record_from!(ITraceStart);
record_from!(LostSamples);
record_from!(SwitchCpuWide);
record_from!(Namespaces<'a>);
record_from!(KSymbol<'a>);
record_from!(BpfEvent);
record_from!(CGroup<'a>);
record_from!(TextPoke<'a>);
record_from!(AuxOutputHwId);

impl<'a> From<Sample<'a>> for Record<'a> {
    fn from(value: Sample<'a>) -> Self {
        Self::Sample(Box::new(value))
    }
}

struct RecordVisitor;

impl<'a> crate::Visitor<'a> for RecordVisitor {
    type Output = Record<'a>;

    fn visit_unimplemented(self, metadata: crate::RecordMetadata) -> Self::Output {
        panic!(
            "parsing for records of type {} is not implemented",
            metadata.ty()
        );
    }

    fn visit_mmap(self, record: Mmap<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_lost(self, record: Lost, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_comm(self, record: Comm<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_exit(self, record: Exit, _: crate::RecordMetadata) -> Self::Output {
        Record::Exit(record)
    }

    fn visit_throttle(self, record: Throttle, _: crate::RecordMetadata) -> Self::Output {
        Record::Throttle(record)
    }

    fn visit_unthrottle(self, record: Throttle, _: crate::RecordMetadata) -> Self::Output {
        Record::Unthrottle(record)
    }

    fn visit_fork(self, record: Fork, _: crate::RecordMetadata) -> Self::Output {
        Record::Fork(record)
    }

    fn visit_read(self, record: Read, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_sample(self, record: Sample<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_mmap2(self, record: Mmap2<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_aux(self, record: Aux, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_itrace_start(self, record: ITraceStart, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_lost_samples(self, record: LostSamples, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_switch(self, _: crate::RecordMetadata) -> Self::Output {
        Record::Switch
    }

    fn visit_switch_cpu_wide(
        self,
        record: SwitchCpuWide,
        _: crate::RecordMetadata,
    ) -> Self::Output {
        record.into()
    }

    fn visit_namespaces(self, record: Namespaces<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_ksymbol(self, record: KSymbol<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_bpf_event(self, record: BpfEvent, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_cgroup(self, record: CGroup<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_text_poke(self, record: TextPoke<'a>, _: crate::RecordMetadata) -> Self::Output {
        record.into()
    }

    fn visit_aux_output_hw_id(
        self,
        record: AuxOutputHwId,
        _: crate::RecordMetadata,
    ) -> Self::Output {
        record.into()
    }

    fn visit_unknown(self, data: Cow<'a, [u8]>, metadata: crate::RecordMetadata) -> Self::Output {
        Record::Unknown {
            ty: metadata.ty(),
            data,
        }
    }
}

impl<'p> Record<'p> {
    /// Parse a `Record` using a [`perf_event_header`] that has already been
    /// parsed.
    pub fn parse_with_header<B, E>(
        p: &mut Parser<B, E>,
        header: perf_event_header,
    ) -> ParseResult<Self>
    where
        E: Endian,
        B: ParseBuf<'p>,
    {
        p.parse_record_with_header(RecordVisitor, header)
    }
}

impl<'p> Parse<'p> for Record<'p> {
    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
    where
        E: Endian,
        B: ParseBuf<'p>,
    {
        p.parse_record(RecordVisitor)
    }
}