-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathperf.go
1389 lines (1193 loc) · 39.6 KB
/
perf.go
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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package perf
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
// Special pid values for Open.
const (
// CallingThread configures the event to measure the calling thread.
CallingThread = 0
// AllThreads configures the event to measure all threads on the
// specified CPU.
AllThreads = -1
)
// AnyCPU configures the specified process/thread to be measured on any CPU.
const AnyCPU = -1
// Event states.
const (
eventStateUninitialized = 0
eventStateOK = 1
eventStateClosed = 2
)
// Event is an active perf event.
type Event struct {
// state is the state of the event. See eventState* constants.
state int32
// perffd is the perf event file descriptor.
perffd int
// id is the unique event ID.
id uint64
// group contains other events in the event group, if this event is
// an event group leader. The order is the order in which the events
// were added to the group.
group []*Event
// groupByID maps group event IDs to the events themselves. The
// reason why this mapping is needed is explained in ReadRecord.
groupByID map[uint64]*Event
// owned contains other events in the event group, which the caller
// has no access to. The Event owns them all, Close closes them all.
owned []*Event
// a is the set of attributes the Event was configured with. It is
// a clone of the original, save for the Label field, which may have
// been set, if the original *Attr didn't set it.
a *Attr
// noReadRecord is true if ReadRecord is disabled for the event.
// See SetOutput and ReadRecord.
noReadRecord bool
// ring is the (entire) memory mapped ring buffer.
ring []byte
// ringdata is the data region of the ring buffer.
ringdata []byte
// meta is the metadata page: &ring[0].
meta *unix.PerfEventMmapPage
// wakeupfd is an event file descriptor (see eventfd(2)). It is used to
// unblock calls to ReadRawRecord when the associated context expires.
wakeupfd int
// pollreq communicates requests from ReadRawRecord to the poll goroutine
// associated with the ring.
pollreq chan pollreq
// pollresp receives responses from the poll goroutine associated
// with the ring, back to ReadRawRecord.
pollresp chan pollresp
}
// Open opens the event configured by attr.
//
// The pid and cpu parameters specify which thread and CPU to monitor:
//
// * if pid == CallingThread and cpu == AnyCPU, the event measures
// the calling thread on any CPU
//
// * if pid == CallingThread and cpu >= 0, the event measures
// the calling thread only when running on the specified CPU
//
// * if pid > 0 and cpu == AnyCPU, the event measures the specified
// thread on any CPU
//
// * if pid > 0 and cpu >= 0, the event measures the specified thread
// only when running on the specified CPU
//
// * if pid == AllThreads and cpu >= 0, the event measures all threads
// on the specified CPU
//
// * finally, the pid == AllThreads and cpu == AnyCPU setting is invalid
//
// If group is non-nil, the returned Event is made part of the group
// associated with the specified group Event.
func Open(a *Attr, pid, cpu int, group *Event) (*Event, error) {
return open(a, pid, cpu, group, 0)
}
// OpenCGroup is like Open, but activates per-container system-wide
// monitoring. If cgroupfs is mounted on /dev/cgroup, and the group to
// monitor is called "test", then cgroupfd must be a file descriptor opened
// on /dev/cgroup/test.
func OpenCGroup(a *Attr, cgroupfd, cpu int, group *Event) (*Event, error) {
return open(a, cgroupfd, cpu, group, unix.PERF_FLAG_PID_CGROUP)
}
func open(a *Attr, pid, cpu int, group *Event, flags int) (*Event, error) {
groupfd := -1
if group != nil {
if err := group.ok(); err != nil {
return nil, err
}
groupfd = group.perffd
}
flags |= unix.PERF_FLAG_FD_CLOEXEC
fd, err := unix.PerfEventOpen(a.sysAttr(), pid, cpu, groupfd, flags)
if err != nil {
return nil, os.NewSyscallError("perf_event_open", err)
}
if err := unix.SetNonblock(fd, true); err != nil {
unix.Close(fd)
return nil, os.NewSyscallError("setnonblock", err)
}
// Clone the *Attr so the caller can't change it from under our feet.
ac := new(Attr)
*ac = *a // ok to copy since no slices
if ac.Label == "" {
evID := eventID{
Type: uint64(a.Type),
Config: uint64(a.Config),
}
ac.Label = lookupLabel(evID).Name
}
ev := &Event{
state: eventStateOK,
perffd: fd,
a: ac,
}
id, err := ev.ID()
if err != nil {
return nil, err
}
ev.id = id
if group != nil {
if group.groupByID == nil {
group.groupByID = map[uint64]*Event{}
}
group.group = append(group.group, ev)
group.groupByID[id] = ev
}
return ev, nil
}
// DefaultNumPages is the number of pages used by MapRing. There is no
// fundamental logic to this number. We use it because that is what the perf
// tool does.
const DefaultNumPages = 128
// MapRing maps the ring buffer attached to the event into memory.
//
// This enables reading records via ReadRecord / ReadRawRecord.
func (ev *Event) MapRing() error {
return ev.MapRingNumPages(DefaultNumPages)
}
// MapRingNumPages is like MapRing, but allows the caller to The size of
// the data portion of the ring is num pages. The total size of the ring
// is num+1 pages, because an additional metadata page is mapped before the
// data portion of the ring.
func (ev *Event) MapRingNumPages(num int) error {
if err := ev.ok(); err != nil {
return err
}
if ev.ring != nil {
return nil
}
pgSize := unix.Getpagesize()
size := (1 + num) * pgSize
const prot = unix.PROT_READ | unix.PROT_WRITE
const flags = unix.MAP_SHARED
ring, err := unix.Mmap(ev.perffd, 0, size, prot, flags)
if err != nil {
return os.NewSyscallError("mmap", err)
}
meta := (*unix.PerfEventMmapPage)(unsafe.Pointer(&ring[0]))
if meta.Data_offset == 0 && meta.Data_size == 0 {
atomic.StoreUint64(&meta.Data_offset, uint64(pgSize))
atomic.StoreUint64(&meta.Data_size, uint64(num*pgSize))
}
ringdata := ring[meta.Data_offset:]
wakeupfd, err := unix.Eventfd(0, unix.EFD_CLOEXEC|unix.EFD_NONBLOCK)
if err != nil {
return os.NewSyscallError("eventfd", err)
}
ev.ring = ring
ev.meta = meta
ev.ringdata = ringdata
ev.wakeupfd = wakeupfd
ev.pollreq = make(chan pollreq)
ev.pollresp = make(chan pollresp)
go ev.poll()
return nil
}
func (ev *Event) ok() error {
if ev == nil {
return os.ErrInvalid
}
switch ev.state {
case eventStateUninitialized:
return os.ErrInvalid
case eventStateOK:
return nil
default: // eventStateClosed
return os.ErrClosed
}
}
// FD returns the file descriptor associated with the event.
func (ev *Event) FD() (int, error) {
if err := ev.ok(); err != nil {
return -1, err
}
return ev.perffd, nil
}
// Measure disables the event, resets it, enables it, runs f, disables it again,
// then reads the Count associated with the event.
func (ev *Event) Measure(f func()) (Count, error) {
if err := ev.Disable(); err != nil {
return Count{}, err
}
if err := ev.Reset(); err != nil {
return Count{}, err
}
doEnableRunDisable(uintptr(ev.perffd), f)
return ev.ReadCount()
}
// MeasureGroup is like Measure, but for event groups.
func (ev *Event) MeasureGroup(f func()) (GroupCount, error) {
if err := ev.Disable(); err != nil {
return GroupCount{}, err
}
if err := ev.Reset(); err != nil {
return GroupCount{}, err
}
doEnableRunDisable(uintptr(ev.perffd), f)
return ev.ReadGroupCount()
}
// Enable enables the event.
func (ev *Event) Enable() error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlNoArg(unix.PERF_EVENT_IOC_ENABLE)
return wrapIoctlError("PERF_EVENT_IOC_ENABLE", err)
}
// Disable disables the event. If ev is a group leader, Disable disables
// all events in the group.
func (ev *Event) Disable() error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlInt(unix.PERF_EVENT_IOC_DISABLE, 0)
return wrapIoctlError("PERF_EVENT_IOC_DISABLE", err)
}
// TODO(acln): add support for PERF_IOC_FLAG_GROUP and for event followers
// to disable the entire group?
// Refresh adds delta to a counter associated with the event. This counter
// decrements every time the event overflows. Once the counter reaches zero,
// the event is disabled. Calling Refresh with delta == 0 is considered
// undefined behavior.
func (ev *Event) Refresh(delta int) error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlInt(unix.PERF_EVENT_IOC_REFRESH, uintptr(delta))
return wrapIoctlError("PERF_EVENT_IOC_REFRESH", err)
}
// Reset resets the counters associated with the event.
func (ev *Event) Reset() error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlNoArg(unix.PERF_EVENT_IOC_RESET)
return wrapIoctlError("PERF_EVENT_IOC_RESET", err)
}
// UpdatePeriod updates the overflow period for the event. On older kernels,
// the new period does not take effect until after the next overflow.
func (ev *Event) UpdatePeriod(p uint64) error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlPointer(unix.PERF_EVENT_IOC_PERIOD, unsafe.Pointer(&p))
return wrapIoctlError("PERF_EVENT_IOC_PERIOD", err)
}
// SetOutput tells the kernel to send records to the specified
// target Event rather than ev.
//
// If target is nil, output from ev is ignored.
//
// Some restrictions apply:
//
// 1) Calling SetOutput on an *Event will fail with EINVAL if MapRing was
// called on that event previously. 2) If ev and target are not CPU-wide
// events, they must be on the same CPU. 3) If ev and target are CPU-wide
// events, they must refer to the same task. 4) ev and target must use the
// same clock.
//
// An additional restriction of the Go API also applies:
//
// In order to use ReadRecord on the target Event, the following settings on
// ev and target must match: Options.SampleIDAll, SampleFormat.Identifier,
// SampleFormat.IP, SampleFormat.Tid, SampleFormat.Time, SampleFormat.Addr,
// SampleFormat.ID, SampleFormat.StreamID. Furthermore, SampleFormat.StreamID
// must be set. SetOutput nevertheless succeeds even if this condition is
// not met, because callers can still use ReadRawRecord instead of ReadRecord.
func (ev *Event) SetOutput(target *Event) error {
if err := ev.ok(); err != nil {
return err
}
var targetfd int
if target == nil {
targetfd = -1
} else {
if err := target.ok(); err != nil {
return err
}
if !target.canReadRecordFrom(ev) {
target.noReadRecord = true
}
targetfd = target.perffd
}
err := ev.ioctlInt(unix.PERF_EVENT_IOC_SET_OUTPUT, uintptr(targetfd))
return wrapIoctlError("PERF_EVENT_IOC_SET_OUTPUT", err)
}
// canReadRecordFrom returns a boolean indicating whether ev, as a leader,
// can read records produced by f, a follower.
func (ev *Event) canReadRecordFrom(f *Event) bool {
lf := ev.a.SampleFormat
ff := f.a.SampleFormat
return lf.Identifier == ff.Identifier &&
lf.IP == ff.IP &&
lf.Tid == ff.Tid &&
lf.Time == ff.Time &&
lf.Addr == ff.Addr &&
lf.ID == ff.ID &&
lf.StreamID == ff.StreamID &&
ff.StreamID
}
// BUG(acln): PERF_EVENT_IOC_SET_FILTER is not implemented
// ID returns the unique event ID value for ev.
func (ev *Event) ID() (uint64, error) {
if err := ev.ok(); err != nil {
return 0, err
}
var val uint64
err := ev.ioctlPointer(unix.PERF_EVENT_IOC_ID, unsafe.Pointer(&val))
return val, wrapIoctlError("PERF_EVENT_IOC_ID", err)
}
// SetBPF attaches a BPF program to ev, which must be a kprobe tracepoint
// event. progfd is the file descriptor associated with the BPF program.
func (ev *Event) SetBPF(progfd uint32) error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlInt(unix.PERF_EVENT_IOC_SET_BPF, uintptr(progfd))
return wrapIoctlError("PERF_EVENT_IOC_SET_BPF", err)
}
// PauseOutput pauses the output from ev.
func (ev *Event) PauseOutput() error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlInt(unix.PERF_EVENT_IOC_PAUSE_OUTPUT, 1)
return wrapIoctlError("PEF_EVENT_IOC_PAUSE_OUTPUT", err)
}
// ResumeOutput resumes output from ev.
func (ev *Event) ResumeOutput() error {
if err := ev.ok(); err != nil {
return err
}
err := ev.ioctlInt(unix.PERF_EVENT_IOC_PAUSE_OUTPUT, 0)
return wrapIoctlError("PEF_EVENT_IOC_PAUSE_OUTPUT", err)
}
// QueryBPF queries the event for BPF program file descriptors attached to
// the same tracepoint as ev. max is the maximum number of file descriptors
// to return.
func (ev *Event) QueryBPF(max uint32) ([]uint32, error) {
if err := ev.ok(); err != nil {
return nil, err
}
buf := make([]uint32, 2+max)
buf[0] = max
err := ev.ioctlPointer(unix.PERF_EVENT_IOC_QUERY_BPF, unsafe.Pointer(&buf[0]))
if err != nil {
return nil, wrapIoctlError("PERF_EVENT_IOC_QUERY_BPF", err)
}
count := buf[1]
fds := make([]uint32, count)
copy(fds, buf[2:2+count])
return fds, nil
}
// BUG(acln): PERF_EVENT_IOC_MODIFY_ATTRIBUTES is not implemented
func (ev *Event) ioctlNoArg(number int) error {
return ev.ioctlInt(number, 0)
}
func (ev *Event) ioctlInt(number int, arg uintptr) error {
_, _, e := unix.Syscall(unix.SYS_IOCTL, uintptr(ev.perffd), uintptr(number), arg)
if e != 0 {
return e
}
return nil
}
func (ev *Event) ioctlPointer(number int, arg unsafe.Pointer) error {
_, _, e := unix.Syscall(unix.SYS_IOCTL, uintptr(ev.perffd), uintptr(number), uintptr(arg))
if e != 0 {
return e
}
return nil
}
func wrapIoctlError(ioctl string, err error) error {
if err == nil {
return nil
}
return &ioctlError{ioctl: ioctl, err: err}
}
type ioctlError struct {
ioctl string
err error
}
func (e *ioctlError) Error() string {
return fmt.Sprintf("%s: %v", e.ioctl, e.err)
}
func (e *ioctlError) Unwrap() error { return e.err }
// Close closes the event. Close must not be called concurrently with any
// other methods on the Event.
func (ev *Event) Close() error {
if ev.ring != nil {
close(ev.pollreq)
<-ev.pollresp
unix.Munmap(ev.ring)
unix.Close(ev.wakeupfd)
}
for _, ev := range ev.owned {
ev.Close()
}
ev.state = eventStateClosed
return unix.Close(ev.perffd)
}
// Attr configures a perf event.
type Attr struct {
// Label is a human readable label associated with the event.
// For convenience, the Label is included in Count and GroupCount
// measurements read from events.
//
// When an event is opened, if Label is the empty string, then a
// Label is computed (if possible) based on the Type and Config
// fields. Otherwise, if the Label user-defined (not the empty
// string), it is included verbatim.
//
// For most events, the computed Label matches the label specified by
// ``perf list'' for the same event (but see Bugs).
Label string
// Type is the major type of the event.
Type EventType
// Config is the type-specific event configuration.
Config uint64
// Sample configures the sample period or sample frequency for
// overflow packets, based on Options.Freq: if Options.Freq is set,
// Sample is interpreted as "sample frequency", otherwise it is
// interpreted as "sample period".
//
// See also SetSample{Period,Freq}.
Sample uint64
// SampleFormat configures information requested in sample records,
// on the memory mapped ring buffer.
SampleFormat SampleFormat
// CountFormat specifies the format of counts read from the
// Event using ReadCount or ReadGroupCount. See the CountFormat
// documentation for more details.
CountFormat CountFormat
// Options contains more fine grained event configuration.
Options Options
// Wakeup configures wakeups on the ring buffer associated with the
// event. If Options.Watermark is set, Wakeup is interpreted as the
// number of bytes before wakeup. Otherwise, it is interpreted as
// "wake up every N events".
//
// See also SetWakeup{Events,Watermark}.
Wakeup uint32
// BreakpointType is the breakpoint type, if Type == BreakpointEvent.
BreakpointType uint32
// Config1 is used for events that need an extra register or otherwise
// do not fit in the regular config field.
//
// For breakpoint events, Config1 is the breakpoint address.
// For kprobes, it is the kprobe function. For uprobes, it is the
// uprobe path.
Config1 uint64
// Config2 is a further extension of the Config1 field.
//
// For breakpoint events, it is the length of the breakpoint.
// For kprobes, when the kprobe function is NULL, it is the address of
// the kprobe. For both kprobes and uprobes, it is the probe offset.
Config2 uint64
// BranchSampleFormat specifies what branches to include in the
// branch record, if SampleFormat.BranchStack is set.
BranchSampleFormat BranchSampleFormat
// SampleRegistersUser is the set of user registers to dump on samples.
SampleRegistersUser uint64
// SampleStackUser is the size of the user stack to dump on samples.
SampleStackUser uint32
// ClockID is the clock ID to use with samples, if Options.UseClockID
// is set.
//
// TODO(acln): What are the values for this? CLOCK_MONOTONIC and such?
// Investigate. Can we choose a clock that can be compared to Go's
// clock in a meaningful way? If so, should we add special support
// for that?
ClockID int32
// SampleRegistersIntr is the set of register to dump for each sample.
// See asm/perf_regs.h for details.
SampleRegistersIntr uint64
// AuxWatermark is the watermark for the aux area.
AuxWatermark uint32
// SampleMaxStack is the maximum number of frame pointers in a
// callchain. The value must be < MaxStack().
SampleMaxStack uint16
}
func (a Attr) sysAttr() *unix.PerfEventAttr {
return &unix.PerfEventAttr{
Type: uint32(a.Type),
Size: uint32(unsafe.Sizeof(unix.PerfEventAttr{})),
Config: a.Config,
Sample: a.Sample,
Sample_type: a.SampleFormat.marshal(),
Read_format: a.CountFormat.marshal(),
Bits: a.Options.marshal(),
Wakeup: a.Wakeup,
Bp_type: a.BreakpointType,
Ext1: a.Config1,
Ext2: a.Config2,
Branch_sample_type: a.BranchSampleFormat.marshal(),
Sample_regs_user: a.SampleRegistersUser,
Sample_stack_user: a.SampleStackUser,
Clockid: a.ClockID,
Sample_regs_intr: a.SampleRegistersIntr,
Aux_watermark: a.AuxWatermark,
Sample_max_stack: a.SampleMaxStack,
}
}
// Configure implements the Configurator interface. It overwrites target
// with a. See also (*Group).Add.
func (a *Attr) Configure(target *Attr) error {
*target = *a
return nil
}
// SetSamplePeriod configures the sampling period for the event.
//
// It sets attr.Sample to p and disables a.Options.Freq.
func (a *Attr) SetSamplePeriod(p uint64) {
a.Sample = p
a.Options.Freq = false
}
// SetSampleFreq configures the sampling frequency for the event.
//
// It sets attr.Sample to f and enables a.Options.Freq.
func (a *Attr) SetSampleFreq(f uint64) {
a.Sample = f
a.Options.Freq = true
}
// SetWakeupEvents configures the event to wake up every n events.
//
// It sets a.Wakeup to n and disables a.Options.Watermark.
func (a *Attr) SetWakeupEvents(n uint32) {
a.Wakeup = n
a.Options.Watermark = false
}
// SetWakeupWatermark configures the number of bytes in overflow records
// before wakeup.
//
// It sets a.Wakeup to n and enables a.Options.Watermark.
func (a *Attr) SetWakeupWatermark(n uint32) {
a.Wakeup = n
a.Options.Watermark = true
}
// LookupEventType probes /sys/bus/event_source/devices/<device>/type
// for the EventType value associated with the specified PMU.
func LookupEventType(pmu string) (EventType, error) {
path := filepath.Join("/sys/bus/event_source/devices", pmu, "type")
et, err := readUint(path, 32)
return EventType(et), err
}
// EventType is the overall type of a performance event.
type EventType uint32
// Supported event types.
const (
HardwareEvent EventType = unix.PERF_TYPE_HARDWARE
SoftwareEvent EventType = unix.PERF_TYPE_SOFTWARE
TracepointEvent EventType = unix.PERF_TYPE_TRACEPOINT
HardwareCacheEvent EventType = unix.PERF_TYPE_HW_CACHE
RawEvent EventType = unix.PERF_TYPE_RAW
BreakpointEvent EventType = unix.PERF_TYPE_BREAKPOINT
)
// HardwareCounter is a hardware performance counter.
type HardwareCounter uint64
// Hardware performance counters.
const (
CPUCycles HardwareCounter = unix.PERF_COUNT_HW_CPU_CYCLES
Instructions HardwareCounter = unix.PERF_COUNT_HW_INSTRUCTIONS
CacheReferences HardwareCounter = unix.PERF_COUNT_HW_CACHE_REFERENCES
CacheMisses HardwareCounter = unix.PERF_COUNT_HW_CACHE_MISSES
BranchInstructions HardwareCounter = unix.PERF_COUNT_HW_BRANCH_INSTRUCTIONS
BranchMisses HardwareCounter = unix.PERF_COUNT_HW_BRANCH_MISSES
BusCycles HardwareCounter = unix.PERF_COUNT_HW_BUS_CYCLES
StalledCyclesFrontend HardwareCounter = unix.PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
StalledCyclesBackend HardwareCounter = unix.PERF_COUNT_HW_STALLED_CYCLES_BACKEND
RefCPUCycles HardwareCounter = unix.PERF_COUNT_HW_REF_CPU_CYCLES
)
var hardwareLabels = map[HardwareCounter]eventLabel{
CPUCycles: {Name: "cpu-cycles", Alias: "cycles"},
Instructions: {Name: "instructions"},
CacheReferences: {Name: "cache-references"},
CacheMisses: {Name: "cache-misses"},
BranchInstructions: {Name: "branch-instructions", Alias: "branches"},
BranchMisses: {Name: "branch-misses", Alias: "branch-misses"},
BusCycles: {Name: "bus-cycles"},
StalledCyclesFrontend: {Name: "stalled-cycles-frontend", Alias: "idle-cycles-frontend"},
StalledCyclesBackend: {Name: "stalled-cycles-backend", Alias: "idle-cycles-backend"},
RefCPUCycles: {Name: "ref-cycles"},
}
func (hwc HardwareCounter) String() string {
return hwc.eventLabel().Name
}
func (hwc HardwareCounter) eventLabel() eventLabel {
return hardwareLabels[hwc]
}
// Configure configures attr to measure hwc. It sets the Label, Type, and
// Config fields on attr.
func (hwc HardwareCounter) Configure(attr *Attr) error {
attr.Label = hwc.String()
attr.Type = HardwareEvent
attr.Config = uint64(hwc)
return nil
}
// AllHardwareCounters returns a slice of all known hardware counters.
func AllHardwareCounters() []Configurator {
return []Configurator{
CPUCycles,
Instructions,
CacheReferences,
CacheMisses,
BranchInstructions,
BranchMisses,
BusCycles,
StalledCyclesFrontend,
StalledCyclesBackend,
RefCPUCycles,
}
}
// SoftwareCounter is a software performance counter.
type SoftwareCounter uint64
// Software performance counters.
const (
CPUClock SoftwareCounter = unix.PERF_COUNT_SW_CPU_CLOCK
TaskClock SoftwareCounter = unix.PERF_COUNT_SW_TASK_CLOCK
PageFaults SoftwareCounter = unix.PERF_COUNT_SW_PAGE_FAULTS
ContextSwitches SoftwareCounter = unix.PERF_COUNT_SW_CONTEXT_SWITCHES
CPUMigrations SoftwareCounter = unix.PERF_COUNT_SW_CPU_MIGRATIONS
MinorPageFaults SoftwareCounter = unix.PERF_COUNT_SW_PAGE_FAULTS_MIN
MajorPageFaults SoftwareCounter = unix.PERF_COUNT_SW_PAGE_FAULTS_MAJ
AlignmentFaults SoftwareCounter = unix.PERF_COUNT_SW_ALIGNMENT_FAULTS
EmulationFaults SoftwareCounter = unix.PERF_COUNT_SW_EMULATION_FAULTS
Dummy SoftwareCounter = unix.PERF_COUNT_SW_DUMMY
BPFOutput SoftwareCounter = unix.PERF_COUNT_SW_BPF_OUTPUT
)
var softwareLabels = map[SoftwareCounter]eventLabel{
CPUClock: {Name: "cpu-clock"},
TaskClock: {Name: "task-clock"},
PageFaults: {Name: "page-faults", Alias: "faults"},
ContextSwitches: {Name: "context-switches", Alias: "cs"},
CPUMigrations: {Name: "cpu-migrations", Alias: "migrations"},
MinorPageFaults: {Name: "minor-faults"},
MajorPageFaults: {Name: "major-faults"},
AlignmentFaults: {Name: "alignment-faults"},
EmulationFaults: {Name: "emulation-faults"},
Dummy: {Name: "dummy"},
BPFOutput: {Name: "bpf-output"},
}
func (swc SoftwareCounter) String() string {
return swc.eventLabel().Name
}
func (swc SoftwareCounter) eventLabel() eventLabel {
return softwareLabels[swc]
}
// Configure configures attr to measure swc. It sets attr.Type and attr.Config.
func (swc SoftwareCounter) Configure(attr *Attr) error {
attr.Label = swc.eventLabel().Name
attr.Type = SoftwareEvent
attr.Config = uint64(swc)
return nil
}
// AllSoftwareCounters returns a slice of all known software counters.
func AllSoftwareCounters() []Configurator {
return []Configurator{
CPUClock,
TaskClock,
PageFaults,
ContextSwitches,
CPUMigrations,
MinorPageFaults,
MajorPageFaults,
AlignmentFaults,
EmulationFaults,
Dummy,
BPFOutput,
}
}
// Cache identifies a cache.
type Cache uint64
// Caches.
const (
L1D Cache = unix.PERF_COUNT_HW_CACHE_L1D
L1I Cache = unix.PERF_COUNT_HW_CACHE_L1I
LL Cache = unix.PERF_COUNT_HW_CACHE_LL
DTLB Cache = unix.PERF_COUNT_HW_CACHE_DTLB
ITLB Cache = unix.PERF_COUNT_HW_CACHE_ITLB
BPU Cache = unix.PERF_COUNT_HW_CACHE_BPU
NODE Cache = unix.PERF_COUNT_HW_CACHE_NODE
)
// AllCaches returns a slice of all known cache types.
func AllCaches() []Cache {
return []Cache{L1D, L1I, LL, DTLB, ITLB, BPU, NODE}
}
// CacheOp is a cache operation.
type CacheOp uint64
// Cache operations.
const (
Read CacheOp = unix.PERF_COUNT_HW_CACHE_OP_READ
Write CacheOp = unix.PERF_COUNT_HW_CACHE_OP_WRITE
Prefetch CacheOp = unix.PERF_COUNT_HW_CACHE_OP_PREFETCH
)
// AllCacheOps returns a slice of all known cache operations.
func AllCacheOps() []CacheOp {
return []CacheOp{Read, Write, Prefetch}
}
// CacheOpResult is the result of a cache operation.
type CacheOpResult uint64
// Cache operation results.
const (
Access CacheOpResult = unix.PERF_COUNT_HW_CACHE_RESULT_ACCESS
Miss CacheOpResult = unix.PERF_COUNT_HW_CACHE_RESULT_MISS
)
// AllCacheOpResults returns a slice of all known cache operation results.
func AllCacheOpResults() []CacheOpResult {
return []CacheOpResult{Access, Miss}
}
// A HardwareCacheCounter groups a cache, a cache operation, and an operation
// result. It measures the number of results for the specified op, on the
// specified cache.
type HardwareCacheCounter struct {
Cache Cache
Op CacheOp
Result CacheOpResult
}
// Configure configures attr to measure hwcc. It sets attr.Type and attr.Config.
func (hwcc HardwareCacheCounter) Configure(attr *Attr) error {
attr.Type = HardwareCacheEvent
attr.Config = uint64(hwcc.Cache) | uint64(hwcc.Op<<8) | uint64(hwcc.Result<<16)
return nil
}
// HardwareCacheCounters returns cache counters which measure the cartesian
// product of the specified caches, operations and results.
func HardwareCacheCounters(caches []Cache, ops []CacheOp, results []CacheOpResult) []Configurator {
counters := make([]Configurator, 0, len(caches)*len(ops)*len(results))
for _, cache := range caches {
for _, op := range ops {
for _, result := range results {
c := HardwareCacheCounter{
Cache: cache,
Op: op,
Result: result,
}
counters = append(counters, c)
}
}
}
return counters
}
// Tracepoint returns a Configurator for the specified category and event.
// The returned Configurator sets attr.Type and attr.Config.
func Tracepoint(category, event string) Configurator {
return configuratorFunc(func(attr *Attr) error {
cfg, err := LookupTracepointConfig(category, event)
if err != nil {
return err
}
attr.Label = fmt.Sprintf("%s:%s", category, event)
attr.Type = TracepointEvent
attr.Config = cfg
return nil
})
}
// LookupTracepointConfig probes
// /sys/kernel/debug/tracing/events/<category>/<event>/id for the Attr.Config
// value associated with the specified category and event.
func LookupTracepointConfig(category, event string) (uint64, error) {
p := filepath.Join("/sys/kernel/debug/tracing/events", category, event, "id")
return readUint(p, 64)
}
// Breakpoint returns a Configurator for a breakpoint event.
//
// typ is the type of the breakpoint.
//
// addr is the address of the breakpoint. For execution breakpoints, this
// is the memory address of the instruction of interest; for read and write
// breakpoints, it is the memory address of the memory location of interest.
//
// length is the length of the breakpoint being measured.
//
// The returned Configurator sets the Type, BreakpointType, Config1, and
// Config2 fields on attr.
func Breakpoint(typ BreakpointType, addr uint64, length BreakpointLength) Configurator {
return configuratorFunc(func(attr *Attr) error {
attr.Type = BreakpointEvent
attr.BreakpointType = uint32(typ)
attr.Config1 = addr
attr.Config2 = uint64(length)
return nil
})
}
// BreakpointType is the type of a breakpoint.
type BreakpointType uint32
// Breakpoint types. Values are |-ed together. The combination of
// BreakpointTypeR or BreakpointTypeW with BreakpointTypeX is invalid.
const (
BreakpointTypeEmpty BreakpointType = 0x0
BreakpointTypeR BreakpointType = 0x1
BreakpointTypeW BreakpointType = 0x2
BreakpointTypeRW BreakpointType = BreakpointTypeR | BreakpointTypeW
BreakpointTypeX BreakpointType = 0x4
)
// BreakpointLength is the length of the breakpoint being measured.
type BreakpointLength uint64
// Breakpoint length values.
const (
BreakpointLength1 BreakpointLength = 1
BreakpointLength2 BreakpointLength = 2
BreakpointLength4 BreakpointLength = 4
BreakpointLength8 BreakpointLength = 8
)
// ExecutionBreakpointLength returns the length of an execution breakpoint.
func ExecutionBreakpointLength() BreakpointLength {
// TODO(acln): is this correct? The man page says to set this to
// sizeof(long). Is sizeof(C long) == sizeof(Go uintptr) on all
// platforms of interest?
var x uintptr
return BreakpointLength(unsafe.Sizeof(x))
}
// ExecutionBreakpoint returns a Configurator for an execution breakpoint
// at the specified address.
func ExecutionBreakpoint(addr uint64) Configurator {
return Breakpoint(BreakpointTypeX, addr, ExecutionBreakpointLength())
}
// Options contains low level event configuration options.