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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
use Result;
use cratebindings as b;
pub
pub use *;
/// This macro will not be used under the `latest` feature, as all syscall
/// features are supported.
pub use unsupported;
/// Event options.
/// Privilege levels.
/// Controls the inherit behavior.
// https://github.com/torvalds/linux/blob/v6.13/kernel/events/core.c#L12535
/// Counter behavior when calling [`execve`](https://man7.org/linux/man-pages/man2/execve.2.html).
/// Controls the format of [`Stat`][crate::count::Stat].
// Details about overflow:
// https://github.com/torvalds/linux/blob/v6.13/kernel/events/core.c#L9958
// https://github.com/torvalds/linux/blob/v6.13/kernel/events/core.c#L5944
// https://github.com/torvalds/linux/blob/v6.13/kernel/events/core.c#L10036
/// Controls when to generate a [sample record][crate::sample::record::sample::Sample].
///
/// Defaults to `Count(0)` (no sample mode in `perf record` command),
/// set it to the desired rate to generate sample records.
///
/// The maximum sample rate is specified in `/proc/sys/kernel/perf_event_max_sample_rate`,
/// [`Throttle`][crate::sample::record::throttle::Throttle] record will be generated if
/// the limit has been reached.
///
/// Meanwhile, `/proc/sys/kernel/perf_cpu_time_max_percent` limits the CPU time allowed
/// to handle sampling (0 means unlimited). Sampling also will be throttled if this limit
/// has been reached.
///
/// # Event overflow
///
/// The kernel maintains an unsigned counter with an appropriate negative initial value,
/// which will finally overflows since every event increase it by one. Then sampling will
/// be triggered and that counter will be reset to prepare for the next overflow. This is
/// what this option actually controls.
///
/// In addition to asynchronous iterators with [wake up][WakeUp] option, overflow can
/// also be captured by enabling I/O signaling from the perf event fd, which indicates
/// `POLL_IN` on each overflow.
///
/// Here is an example:
///
/// ```rust
/// // Fork to avoid signal handler conflicts.
/// # unsafe {
/// # let child = libc::fork();
/// # if child > 0 {
/// # let mut code = 0;
/// # libc::waitpid(child, &mut code as _, 0);
/// # assert_eq!(code, 0);
/// # return;
/// # }
/// # }
/// #
/// # unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) };
/// #
/// # let result = std::panic::catch_unwind(|| {
/// use std::mem::MaybeUninit;
/// use std::os::fd::AsRawFd;
/// use std::ptr::null_mut;
/// use std::sync::atomic::AtomicBool;
/// use std::sync::atomic::Ordering;
///
/// use perf_event_open::config::{Cpu, Opts, Proc, SampleOn};
/// use perf_event_open::count::Counter;
/// use perf_event_open::event::sw::Software;
///
/// static IN: AtomicBool = AtomicBool::new(false);
///
/// let event = Software::TaskClock;
/// let target = (Proc::CURRENT, Cpu::ALL);
/// let mut opts = Opts::default();
/// opts.sample_on = SampleOn::Count(1_000_000); // 1ms
///
/// let counter = Counter::new(event, target, opts).unwrap();
///
/// // Enable I/O signals from perf event fd to the current process.
/// let fd = counter.file().as_raw_fd();
/// unsafe {
/// libc::fcntl(fd, libc::F_SETFL, libc::O_ASYNC);
/// // The value of `F_SETSIG` is 10, and libc crate does not have
/// // that binding (same as `POLL_IN` below).
/// libc::fcntl(fd, 10, libc::SIGIO);
/// libc::fcntl(fd, libc::F_SETOWN, libc::getpid());
/// }
///
/// fn handler(num: i32, info: *const libc::siginfo_t) {
/// assert_eq!(num, libc::SIGIO);
/// let si_code = unsafe { *info }.si_code;
/// assert_eq!(si_code, 1); // POLL_IN
/// IN.store(true, Ordering::Relaxed);
/// }
/// let act = libc::sigaction {
/// sa_sigaction: handler as _,
/// sa_mask: unsafe { MaybeUninit::zeroed().assume_init() },
/// sa_flags: libc::SA_SIGINFO,
/// sa_restorer: None,
/// };
/// unsafe { libc::sigaction(libc::SIGIO, &act as _, null_mut()) };
///
/// let sampler = counter.sampler(5).unwrap();
/// counter.enable().unwrap();
///
/// while !IN.load(Ordering::Relaxed) {
/// std::hint::spin_loop();
/// }
///
/// println!("{:-?}", sampler.iter().next());
/// # });
/// # if result.is_err() {
/// # unsafe { libc::abort() };
/// # }
///
/// # unsafe { libc::exit(0) };
/// ```
///
/// For more information on I/O signals, see also
/// [`Sampler::enable_counter_with`][crate::sample::Sampler::enable_counter_with].
/// Controls the amount of sample skid.
///
/// Skid is how many instructions execute between an event of interest happening and
/// the kernel being able to stop and record the event.
///
/// Smaller skid is better and allows more accurate reporting of which events correspond
/// to which instructions, but hardware is often limited with how small this can be.
///
/// This affects the precision of [`code_addr`][crate::sample::record::sample::Sample::code_addr].
/// Controls the format of [sample record][crate::sample::record::sample::Sample].
/// LBR options.
/// Branch target privilege levels.
/// Branch types.
/// Controls the format of [LBR entry][crate::sample::record::sample::Entry].
/// Semantic wrapper for options that require a size (in bytes).
;
/// Controls how weight values are represented.
/// Call chain options.
/// Register mask that defines the set of CPU registers to dump on samples.
///
/// The layout of the register mask is architecture-specific and is described
/// in the kernel header file `arch/<arch>/include/uapi/asm/perf_regs.h`.
;
/// Generate extra record types.
/// Controls the format of [`RecordId`][crate::sample::record::RecordId].
/// Wake up options for asynchronous iterators.
/// When to wake up asynchronous iterators.
///
/// "wake up" means notifying the async runtime to schedule the
/// asynchronous iterator's future to be pulled in the next round.
///
/// For performance reasons, we may not want to wake up asynchronous
/// iterators as soon as data is available. With this option we can
/// configure the number of bytes or samples that triggers the wake
/// up.
///
/// If we specify the [`Proc`] instead of [`All`], asynchronous iterators
/// will be woken up when the target process exits.
///
/// # Examples
///
/// ```rust
/// # tokio_test::block_on(async {
/// use perf_event_open::config::{Cpu, Opts, Proc, SampleOn, WakeUpOn};
/// use perf_event_open::count::Counter;
/// use perf_event_open::event::sw::Software;
///
/// let event = Software::TaskClock;
/// let target = (Proc::ALL, Cpu(0));
///
/// let mut opts = Opts::default();
/// opts.sample_on = SampleOn::Freq(1000);
/// // Wake up asynchronous iterators on every sample.
/// opts.wake_up.on = WakeUpOn::Samples(1);
///
/// let counter = Counter::new(event, target, opts).unwrap();
/// let sampler = counter.sampler(5).unwrap();
///
/// counter.enable().unwrap();
///
/// let mut iter = sampler.iter().into_async().unwrap();
/// println!("{:-?}", iter.next().await);
/// # });
/// ```
/// Semantic wrapper for signal data to pass.
///
/// This data will be copied to user's signal handler (through `si_perf`
/// in the `siginfo_t`) to disambiguate which event triggered the signal.
///
/// Since `linux-5.13`: <https://github.com/torvalds/linux/commit/97ba62b278674293762c3d91f724f1bb922f04e0>
;
/// Available internal Linux timers.
/// [`Mmap`][crate::sample::record::mmap::Mmap] record options.
/// Carry [`BuildId`][crate::sample::record::mmap::Info::BuildId] instead of
/// [`Device`][crate::sample::record::mmap::Info::Device] in [`Mmap`][crate::sample::record::mmap::Mmap] records
/// if possible.
///
/// The Build ID is carried if memory is mapped to an ELF file containing
/// a Build ID. Otherwise, device info is used as a fallback.
///
/// Since `linux-5.12`: <https://github.com/torvalds/linux/commit/88a16a1309333e43d328621ece3e9fa37027e8eb>
;
/*
EventConifg::ty u32 type_
size_of::<Attr> u32 size
EventConifg::config u64 config
SampleOn u64 __bindgen_anon_1 sample method union
{Sample, RecordId}Format u64 sample_type
Opts::stat_format u64 read_format
- ZST _bitfield_align_1
(See below) u64 _bitfield_1 option bits
WakeUpOn u32 __bindgen_anon_2 wakeup on union
Breakpoint::ty u32 bp_type
EventConifg::config1 u64 __bindgen_anon_3 config1 union
EventConifg::config2 u64 __bindgen_anon_4 config2 union
Lbr u64 branch_sample_type
SampleFormat::user_regs u64 sample_regs_user
SampleFormat::user_stack u32 sample_stack_user
Clock i32 clockid
SampleFormat::intr_regs u64 sample_regs_intr
WakeUp::on_aux_bytes u32 aux_watermark
SampleFormat::call_chain u16 sample_max_stack
- u16 __reserved_2
SampleFormat::aux u32 aux_sample_size
(See below) u32 __bindgen_anon_5 aux action bits
SigData u64 sig_data
EventConfig::config3 u64 config3
*/
/*
Opts::auto_start 1 disabled off by default
Opts::inherit 1 inherit children inherit it
Opts::pin_on_pmu 1 pinned must always be on PMU
Opts::only_group 1 exclusive only group on PMU
Priv::user 1 exclude_user don't count user
Priv::kernel 1 exclude_kernel ditto kernel
Priv::hv 1 exclude_hv ditto hypervisor
Priv::idle 1 exclude_idle don't count when idle
ExtraRecord::mmap 1 mmap include mmap data
ExtraRecord::comm 1 comm include comm data
SampleOn::Freq 1 freq use freq, not period
ExtraRecord::read 1 inherit_stat per task counts
Opts::on_execve 1 enable_on_exec next exec enables
ExtraRecord::task 1 task trace fork/exit
WakeUpOn 1 watermark wakeup_watermark
IpSkid 2 precise_ip skid constraint
ExtraRecord::mmap 1 mmap_data non-exec mmap data
Opts::record_id_all 1 sample_id_all sample_type all events
Priv::host 1 exclude_host don't count in host
Priv::guest 1 exclude_guest don't count in guest
SampleFormat::call_chain 1 exclude_callchain_kernel exclude kernel callchains
SampleFormat::call_chain 1 exclude_callchain_user exclude user callchains
ExtraRecord::mmap 1 mmap2 include mmap with inode data
false 1 comm_exec flag comm events that are due to an exec
Opts::clock_time 1 use_clockid use @clockid for time fields
ExtraRecord::ctx_switch 1 context_switch context switch data
false 1 write_backward Write ring-buffer from end to beginning
ExtraRecord::namespaces 1 namespaces include namespaces data
ExtraRecord::ksymbol 1 ksymbol include ksymbol events
ExtraRecord::bpf_event 1 bpf_event include bpf events
sibling::Opts::aux_output 1 aux_output generate AUX records instead of events
ExtraRecord::cgroup 1 cgroup include cgroup events
ExtraRecord::text_poke 1 text_poke include text poke events
ExtraRecord::mmap 1 build_id use build id in mmap2 events
Inherit 1 inherit_thread children only inherit if cloned with CLONE_THREAD
Opts::on_execve 1 remove_on_exec event is removed from task on exec
Opts::sigtrap_on_sample 1 sigtrap send synchronous SIGTRAP on event
- 26 __reserved_1
*/
/*
Opts::pause_aux 1 aux_start_paused start AUX area tracing paused
sibling::AuxTracer 1 aux_pause on overflow, pause AUX area tracing
sibling::AuxTracer 1 aux_resume on overflow, resume AUX area tracing
- 29 __reserved_3
*/