-
Notifications
You must be signed in to change notification settings - Fork 10
/
sysfs.go
116 lines (105 loc) · 2.94 KB
/
sysfs.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
package manager
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
)
var (
// kprobePMUType is used to cache the kprobe PMY type value
kprobePMUType = struct {
once sync.Once
value uint32
err error
}{}
// uprobePMUType is used to cache the uprobe PMU type value
uprobePMUType = struct {
once sync.Once
value uint32
err error
}{}
)
func parsePMUEventType(eventType probeType) (uint32, error) {
PMUTypeFile := fmt.Sprintf("/sys/bus/event_source/devices/%s/type", eventType)
f, err := os.Open(PMUTypeFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return 0, fmt.Errorf("pmu type %s: %w", eventType, ErrNotSupported)
}
return 0, fmt.Errorf("couldn't open %s: %w", PMUTypeFile, err)
}
var t uint32
_, err = fmt.Fscanf(f, "%d\n", &t)
if err != nil {
return 0, fmt.Errorf("couldn't parse type at %s: %v", eventType, err)
}
return t, nil
}
// getPMUEventType reads a Performance Monitoring Unit's type (numeric identifier)
// from /sys/bus/event_source/devices/<pmu>/type.
func getPMUEventType(eventType probeType) (uint32, error) {
switch eventType {
case kprobe:
kprobePMUType.once.Do(func() {
kprobePMUType.value, kprobePMUType.err = parsePMUEventType(eventType)
})
return kprobePMUType.value, kprobePMUType.err
case uprobe:
uprobePMUType.once.Do(func() {
uprobePMUType.value, uprobePMUType.err = parsePMUEventType(eventType)
})
return uprobePMUType.value, uprobePMUType.err
default:
return 0, fmt.Errorf("unknown event type: %s", eventType)
}
}
var (
// kprobeRetProbeBit is used to cache the KProbe RetProbe bit value
kprobeRetProbeBit = struct {
once sync.Once
value uint64
err error
}{}
// uprobeRetProbeBit is used to cache the UProbe RetProbe bit value
uprobeRetProbeBit = struct {
once sync.Once
value uint64
err error
}{}
)
// parseRetProbeBit reads a Performance Monitoring Unit's retprobe bit
// from /sys/bus/event_source/devices/<pmu>/format/retprobe.
func parseRetProbeBit(eventType probeType) (uint64, error) {
p := filepath.Join("/sys/bus/event_source/devices/", eventType.String(), "/format/retprobe")
data, err := os.ReadFile(p)
if err != nil {
return 0, err
}
var rp uint64
n, err := fmt.Sscanf(string(bytes.TrimSpace(data)), "config:%d", &rp)
if err != nil {
return 0, fmt.Errorf("parse retprobe bit: %w", err)
}
if n != 1 {
return 0, fmt.Errorf("parse retprobe bit: expected 1 item, got %d", n)
}
return rp, nil
}
func getRetProbeBit(eventType probeType) (uint64, error) {
switch eventType {
case kprobe:
kprobeRetProbeBit.once.Do(func() {
kprobeRetProbeBit.value, kprobeRetProbeBit.err = parseRetProbeBit(eventType)
})
return kprobeRetProbeBit.value, kprobeRetProbeBit.err
case uprobe:
uprobeRetProbeBit.once.Do(func() {
uprobeRetProbeBit.value, uprobeRetProbeBit.err = parseRetProbeBit(eventType)
})
return uprobeRetProbeBit.value, uprobeRetProbeBit.err
default:
return 0, fmt.Errorf("unknown event type %s", eventType)
}
}