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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#[derive(Debug)]
pub struct ThreadId {
pub pid: i32,
pub tid: i32
}
#[derive(Debug)]
pub struct Cpu {
pub cpu: u32,
pub res: u32
}
#[derive(Debug)]
pub struct SampleId {
pub ptid: ThreadId,
pub time: u64,
pub id: u64,
pub stream_id: u64,
pub cpu: Cpu,
pub identifier: u64
}
#[derive(Debug)]
pub struct Event {
pub header: EventHeader,
pub data: EventData,
}
#[derive(Debug)]
pub enum EventData {
MMAP(MMAPRecord),
Lost(LostRecord),
Comm(CommRecord),
Exit(ExitRecord),
Throttle(ThrottleRecord),
Unthrottle(UnthrottleRecord),
Fork(ForkRecord),
Sample(SampleRecord),
MMAP2(MMAP2Record),
BuildId(BuildIdRecord),
None,
}
#[derive(Debug)]
pub struct EventHeader {
pub event_type: EventType,
pub misc: u16,
pub size: u16,
}
impl EventHeader {
pub fn size(&self) -> usize {
self.size as usize
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum EventType {
Mmap,
Lost,
Comm,
Exit,
Throttle,
Unthrottle,
Fork,
Read,
Sample,
Mmap2,
BuildId,
FinishedRound,
Unknown(u32)
}
impl EventType {
pub fn new(event_type: u32) -> EventType {
match event_type {
1 => EventType::Mmap,
2 => EventType::Lost,
3 => EventType::Comm,
4 => EventType::Exit,
5 => EventType::Throttle,
6 => EventType::Unthrottle,
7 => EventType::Fork,
8 => EventType::Read,
9 => EventType::Sample,
10 => EventType::Mmap2,
67 => EventType::BuildId,
68 => EventType::FinishedRound,
_ => EventType::Unknown(event_type)
}
}
pub fn is_unknown(&self) -> bool {
match *self {
EventType::Unknown(_) => true,
_ => false
}
}
}
#[derive(Debug)]
pub struct ForkRecord {
pub pid: u32,
pub ppid: u32,
pub tid: u32,
pub ptid: u32,
pub time: u64,
}
#[derive(Debug)]
pub struct ExitRecord {
pub pid: u32,
pub ppid: u32,
pub tid: u32,
pub ptid: u32,
pub time: u64
}
#[derive(Debug)]
pub struct ThrottleRecord {
pub time: u64,
pub id: u64,
pub stream_id: u64
}
#[derive(Debug)]
pub struct UnthrottleRecord {
pub time: u64,
pub id: u64,
pub stream_id: u64
}
#[derive(Debug)]
pub struct MMAPRecord {
pub pid: i32,
pub tid: u32,
pub addr: u64,
pub len: u64,
pub pgoff: u64,
pub filename: String
}
#[derive(Debug)]
pub struct MMAP2Record {
pub ptid: ThreadId,
pub addr: u64,
pub len: u64,
pub pgoff: u64,
pub maj: u32,
pub min: u32,
pub ino: u64,
pub ino_generation: u64,
pub prot: u32,
pub flags: u32,
pub filename: String,
}
#[derive(Default, Debug)]
pub struct ReadFormat {
pub time_enabled: Option<u64>,
pub time_running: Option<u64>,
pub values: Vec<(u64, Option<u64>)>
}
#[derive(Debug)]
pub struct ReadRecord {
pub pid: u32,
pub tid: u32,
pub value: ReadFormat,
}
#[derive(Debug)]
pub struct BranchEntry {
pub from: u64,
pub to: u64,
pub flags: u64,
}
#[derive(Debug)]
pub struct SampleRecord {
pub sample_id: Option<u64>,
pub ip: Option<u64>,
pub ptid: Option<ThreadId>,
pub time: Option<u64>,
pub addr: Option<u64>,
pub id: Option<u64>,
pub stream_id: Option<u64>,
pub cpu: Option<Cpu>,
pub period: Option<u64>,
pub v: Option<ReadFormat>,
pub ips: Option<Vec<u64>>,
pub raw: Option<Vec<u8>>,
pub lbr: Option<Vec<BranchEntry>>,
pub abi_user: Option<u64>,
pub regs_user: Option<Vec<u64>>,
pub user_stack: Option<Vec<u8>>,
pub dyn_size: Option<u64>,
pub weight: Option<u64>,
pub data_src: Option<u64>,
pub transaction: Option<u64>,
pub abi: Option<u64>,
pub regs_intr: Option<Vec<u64>>
}
#[derive(Debug)]
pub struct CommRecord {
pub ptid: ThreadId,
pub comm: String,
}
#[derive(Debug)]
pub struct LostRecord {
}
#[derive(Debug)]
pub struct BuildIdRecord {
pub pid: i32,
pub build_id: Vec<u8>,
pub filename: String,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum HeaderFlag {
NrCpus,
Arch,
Version,
OsRelease,
Hostname,
BuildId,
TracingData,
BranchStack,
NumaTopology,
CpuTopology,
EventDesc,
CmdLine,
TotalMem,
CpuId,
CpuDesc,
GroupDesc,
PmuMappings
}
#[derive(Debug)]
pub struct HeaderFlags {
pub nrcpus: bool,
pub arch: bool,
pub version: bool,
pub osrelease: bool,
pub hostname: bool,
pub build_id: bool,
pub tracing_data: bool,
pub branch_stack: bool,
pub numa_topology: bool,
pub cpu_topology: bool,
pub event_desc: bool,
pub cmdline: bool,
pub total_mem: bool,
pub cpuid: bool,
pub cpudesc: bool,
pub group_desc: bool,
pub pmu_mappings: bool,
}
impl HeaderFlags {
pub fn collect(&self) -> Vec<HeaderFlag> {
let mut flags = Vec::with_capacity(17);
if self.tracing_data {
flags.push(HeaderFlag::TracingData);
}
if self.build_id {
flags.push(HeaderFlag::BuildId);
}
if self.hostname {
flags.push(HeaderFlag::Hostname);
}
if self.osrelease {
flags.push(HeaderFlag::OsRelease);
}
if self.version {
flags.push(HeaderFlag::Version);
}
if self.arch {
flags.push(HeaderFlag::Arch);
}
if self.nrcpus {
flags.push(HeaderFlag::NrCpus);
}
if self.cpudesc {
flags.push(HeaderFlag::CpuDesc);
}
if self.cpuid {
flags.push(HeaderFlag::CpuId);
}
if self.total_mem {
flags.push(HeaderFlag::TotalMem);
}
if self.cmdline {
flags.push(HeaderFlag::CmdLine);
}
if self.event_desc {
flags.push(HeaderFlag::EventDesc);
}
if self.cpu_topology {
flags.push(HeaderFlag::CpuTopology);
}
if self.numa_topology {
flags.push(HeaderFlag::NumaTopology);
}
if self.branch_stack {
flags.push(HeaderFlag::BranchStack);
}
if self.pmu_mappings {
flags.push(HeaderFlag::PmuMappings);
}
if self.group_desc {
flags.push(HeaderFlag::GroupDesc);
}
flags
}
}
#[derive(Debug)]
pub struct PerfFileHeader {
pub size: u64,
pub attr_size: u64,
pub attrs: PerfFileSection,
pub data: PerfFileSection,
pub event_types: PerfFileSection,
pub flags: HeaderFlags,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct EventAttr {
pub attr_type: u32,
pub size: u32,
pub config: u64,
pub sample_period_freq: u64,
pub sample_type: SampleFormatFlags,
pub read_format: ReadFormatFlags,
pub settings: EventAttrFlags,
pub wakeup_events_watermark: u32,
pub bp_type: u32,
pub config1_or_bp_addr: u64,
pub config2_or_bp_len: u64,
pub branch_sample_type: u64,
pub sample_regs_user: u64,
pub sample_stack_user: u32,
pub clock_id: i32,
pub sample_regs_intr: u64,
pub aux_watermark: u32,
pub reserved: u32
}
impl EventAttr {
pub fn attr_type(&self) -> EventAttrType {
EventAttrType::new(self.attr_type)
}
}
impl Default for EventAttr {
fn default() -> EventAttr {
use std::mem;
unsafe { mem::zeroed::<EventAttr>() }
}
}
#[derive(Debug)]
pub enum EventAttrType {
Hardware,
Software,
TracePoint,
HwCache,
Raw,
Breakpoint,
Unknown(u32)
}
impl EventAttrType {
pub fn new(attr_type: u32) -> EventAttrType {
match attr_type {
0 => EventAttrType::Hardware,
1 => EventAttrType::Software,
2 => EventAttrType::TracePoint,
3 => EventAttrType::HwCache,
4 => EventAttrType::Raw,
5 => EventAttrType::Breakpoint,
_ => EventAttrType::Unknown(attr_type)
}
}
}
bitflags!{
pub flags ReadFormatFlags: u64 {
const FORMAT_TOTAL_TIME_ENABLED = 1,
const FORMAT_TOTAL_TIME_RUNNING = 2,
const FORMAT_ID = 4,
const FORMAT_GROUP = 8,
}
}
impl ReadFormatFlags {
pub fn has_total_time_enabled(&self) -> bool {
self.contains(FORMAT_TOTAL_TIME_ENABLED)
}
pub fn has_total_time_running(&self) -> bool {
self.contains(FORMAT_TOTAL_TIME_RUNNING)
}
pub fn has_id(&self) -> bool {
self.contains(FORMAT_ID)
}
pub fn has_group(&self) -> bool {
self.contains(FORMAT_GROUP)
}
}
bitflags!{
pub flags SampleFormatFlags: u64 {
const PERF_SAMPLE_IP = 1 << 0,
const PERF_SAMPLE_TID = 1 << 1,
const PERF_SAMPLE_TIME = 1 << 2,
const PERF_SAMPLE_ADDR = 1 << 3,
const PERF_SAMPLE_READ = 1 << 4,
const PERF_SAMPLE_CALLCHAIN = 1 << 5,
const PERF_SAMPLE_ID = 1 << 6,
const PERF_SAMPLE_CPU = 1 << 7,
const PERF_SAMPLE_PERIOD = 1 << 8,
const PERF_SAMPLE_STREAM_ID = 1 << 9,
const PERF_SAMPLE_RAW = 1 << 10,
const PERF_SAMPLE_BRANCH_STACK = 1 << 11,
const PERF_SAMPLE_REGS_USER = 1 << 12,
const PERF_SAMPLE_STACK_USER = 1 << 13,
const PERF_SAMPLE_WEIGHT = 1 << 14,
const PERF_SAMPLE_DATA_SRC = 1 << 15,
const PERF_SAMPLE_IDENTIFIER = 1 << 16,
const PERF_SAMPLE_TRANSACTION = 1 << 17,
const PERF_SAMPLE_REGS_INTR = 1 << 18,
}
}
impl SampleFormatFlags {
pub fn has_ip(&self) -> bool {
self.contains(PERF_SAMPLE_IP)
}
pub fn has_tid(&self) -> bool {
self.contains(PERF_SAMPLE_TID)
}
pub fn has_time(&self) -> bool {
self.contains(PERF_SAMPLE_TIME)
}
pub fn has_addr(&self) -> bool {
self.contains(PERF_SAMPLE_ADDR)
}
pub fn has_read(&self) -> bool {
self.contains(PERF_SAMPLE_READ)
}
pub fn has_callchain(&self) -> bool {
self.contains(PERF_SAMPLE_CALLCHAIN)
}
pub fn has_sample_id(&self) -> bool {
self.contains(PERF_SAMPLE_ID)
}
pub fn has_cpu(&self) -> bool {
self.contains(PERF_SAMPLE_CPU)
}
pub fn has_period(&self) -> bool {
self.contains(PERF_SAMPLE_PERIOD)
}
pub fn has_stream_id(&self) -> bool {
self.contains(PERF_SAMPLE_STREAM_ID)
}
pub fn has_raw(&self) -> bool {
self.contains(PERF_SAMPLE_RAW)
}
pub fn has_branch_stack(&self) -> bool {
self.contains(PERF_SAMPLE_BRANCH_STACK)
}
pub fn has_regs_user(&self) -> bool {
self.contains(PERF_SAMPLE_REGS_USER)
}
pub fn has_stack_user(&self) -> bool {
self.contains(PERF_SAMPLE_STACK_USER)
}
pub fn has_weight(&self) -> bool {
self.contains(PERF_SAMPLE_WEIGHT)
}
pub fn has_data_src(&self) -> bool {
self.contains(PERF_SAMPLE_DATA_SRC)
}
pub fn has_identifier(&self) -> bool {
self.contains(PERF_SAMPLE_IDENTIFIER)
}
pub fn has_transaction(&self) -> bool {
self.contains(PERF_SAMPLE_TRANSACTION)
}
pub fn has_regs_intr(&self) -> bool {
self.contains(PERF_SAMPLE_REGS_INTR)
}
}
bitflags! {
pub flags EventAttrFlags: u64 {
const EVENT_ATTR_DISABLED = 1 << 0,
const EVENT_ATTR_INHERIT = 1 << 1,
const EVENT_ATTR_PINNED = 1 << 2,
const EVENT_ATTR_EXCLUSIVE = 1 << 3,
const EVENT_ATTR_EXCLUDE_USER = 1 << 4,
const EVENT_ATTR_EXCLUDE_KERNEL = 1 << 5,
const EVENT_ATTR_EXCLUDE_HV = 1 << 6,
const EVENT_ATTR_EXCLUDE_IDLE = 1 << 7,
const EVENT_ATTR_MMAP = 1 << 8,
const EVENT_ATTR_COMM = 1 << 9,
const EVENT_ATTR_FREQ = 1 << 10,
const EVENT_ATTR_INHERIT_STAT = 1 << 11,
const EVENT_ATTR_ENABLE_ON_EXEC = 1 << 12,
const EVENT_ATTR_TASK = 1 << 13,
const EVENT_ATTR_WATERMARK = 1 << 14,
const EVENT_ATTR_SAMPLE_IP_ARBITRARY_SKID = 0 << 15,
const EVENT_ATTR_SAMPLE_IP_CONSTANT_SKID = 1 << 15,
const EVENT_ATTR_SAMPLE_IP_REQ_ZERO_SKID = 2 << 15,
const EVENT_ATTR_SAMPLE_IP_ZERO_SKID = 3 << 15,
const EVENT_ATTR_MMAP_DATA = 1 << 17,
const EVENT_ATTR_SAMPLE_ID_ALL = 1 << 18,
const EVENT_ATTR_EXCLUDE_HOST = 1 << 19,
const EVENT_ATTR_EXCLUDE_GUEST = 1 << 20,
const EVENT_ATTR_EXCLUDE_CALLCHAIN_KERNEL = 1 << 21,
const EVENT_ATTR_EXCLUDE_CALLCHAIN_USER = 1 << 22,
const EVENT_ATTR_MMAP2 = 1 << 23,
}
}
#[derive(Debug, Clone, Copy)]
pub struct PerfFileSection {
pub offset: u64,
pub size: u64
}
impl PerfFileSection {
pub fn start(&self) -> usize {
self.offset as usize
}
pub fn end(&self) -> usize {
(self.offset + self.size) as usize
}
}
#[derive(Debug)]
pub struct NrCpus {
pub online: u32,
pub available: u32
}
#[derive(Debug)]
pub struct EventDesc {
pub attr: EventAttr,
pub event_string: String,
pub ids: Vec<u64>
}
#[derive(Debug)]
pub struct CpuTopology {
pub cores: Vec<String>,
pub threads: Vec<String>
}
#[derive(Debug)]
pub struct NumaNode {
pub node_nr: u32,
pub mem_total: u64,
pub mem_free: u64,
pub cpus: String
}
#[derive(Debug)]
pub struct PmuMapping {
pub pmu_type: u32,
pub pmu_name: String
}
#[derive(Debug)]
pub struct GroupDesc {
pub string: String,
pub leader_idx: u32,
pub nr_members: u32
}