forked from cloudflare/ebpf_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getpid_test.go
175 lines (137 loc) · 4.1 KB
/
getpid_test.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
package benchmark
import (
"fmt"
"os"
"testing"
"unsafe"
"github.com/aquasecurity/libbpfgo"
"github.com/cloudflare/ebpf_exporter/v2/util"
)
func init() {
libbpfgoCallbacks := libbpfgo.Callbacks{}
libbpfgoCallbacks.LogFilters = append(libbpfgoCallbacks.LogFilters, func(libLevel int, _ string) bool {
return libLevel == libbpfgo.LibbpfDebugLevel
})
libbpfgo.SetLoggerCbs(libbpfgoCallbacks)
}
func getpid() {
os.Getpid()
}
func BenchmarkGetpidWithoutAnyProbes(b *testing.B) {
b.Run("getpid", func(b *testing.B) {
for n := 0; n < b.N; n++ {
getpid()
}
})
}
func BenchmarkGetpidTracepointWithNoMap(b *testing.B) {
benchmarkWithProbe(b, "probes/tracepoint-empty.bpf.o", "getpid", getpid, false)
}
func BenchmarkGetpidTracepointWithSimpleMap(b *testing.B) {
benchmarkWithProbe(b, "probes/tracepoint-simple.bpf.o", "getpid", getpid, true)
}
func BenchmarkGetpidTracepointWithComplexMap(b *testing.B) {
benchmarkWithProbe(b, "probes/tracepoint-complex.bpf.o", "getpid", getpid, true)
}
func BenchmarkGetpidFentryWithNoMap(b *testing.B) {
benchmarkWithProbe(b, "probes/fentry-empty.bpf.o", "getpid", getpid, false)
}
func BenchmarkGetpidFentryWithSimpleMap(b *testing.B) {
benchmarkWithProbe(b, "probes/fentry-simple.bpf.o", "getpid", getpid, true)
}
func BenchmarkGetpidFentryWithComplexMap(b *testing.B) {
benchmarkWithProbe(b, "probes/fentry-complex.bpf.o", "getpid", getpid, true)
}
func BenchmarkGetpidKprobeWithNoMap(b *testing.B) {
benchmarkWithProbe(b, "probes/kprobe-empty.bpf.o", "getpid", getpid, false)
}
func BenchmarkGetpidKprobeWithSimpleMap(b *testing.B) {
benchmarkWithProbe(b, "probes/kprobe-simple.bpf.o", "getpid", getpid, true)
}
func BenchmarkGetpidKprobeWithComplexMap(b *testing.B) {
benchmarkWithProbe(b, "probes/kprobe-complex.bpf.o", "getpid", getpid, true)
}
func BenchmarkUprobeTargetWithoutAnyProbes(b *testing.B) {
b.Run("go", func(b *testing.B) {
for n := 0; n < b.N; n++ {
uprobeGo()
}
})
b.Run("cgo", func(b *testing.B) {
for n := 0; n < b.N; n++ {
uprobeCgo()
}
})
}
func BenchmarkUprobeWithNoMap(b *testing.B) {
benchmarkWithProbe(b, "probes/uprobe-empty.bpf.o", "cgo", uprobeCgo, false)
}
func BenchmarkUprobeWithSimpleMap(b *testing.B) {
benchmarkWithProbe(b, "probes/uprobe-simple.bpf.o", "cgo", uprobeCgo, true)
}
func BenchmarkUprobeWithComplexMap(b *testing.B) {
benchmarkWithProbe(b, "probes/uprobe-complex.bpf.o", "cgo", uprobeCgo, true)
}
func benchmarkWithProbe(b *testing.B, file string, target string, fn func(), checkMap bool) {
byteOrder := util.GetHostByteOrder()
m, link, err := setupGetpidProbe(file)
if err != nil {
b.Fatalf("Error setting up getpid probe: %v", err)
}
defer func() {
err := link.Destroy()
if err != nil {
b.Fatalf("Error destroying link: %v", err)
}
}()
defer m.Close()
b.Run(target, func(b *testing.B) {
for n := 0; n < b.N; n++ {
fn()
}
})
if !checkMap {
return
}
counts, err := m.GetMap("counts")
if err != nil {
b.Fatalf("Error getting map from bpf: %v", err)
}
keys := 0
value := uint64(0)
iter := counts.Iterator()
for iter.Next() {
keys++
valueBytes, err := counts.GetValue(unsafe.Pointer(&iter.Key()[0]))
if err != nil {
b.Fatalf("Error reading key from bpf map: %v", err)
}
value += byteOrder.Uint64(valueBytes)
}
if keys == 0 {
b.Fatal("No elements found in map")
}
if value < 1000 {
b.Fatalf("Cumulative count value is too low: %d", value)
}
b.Logf("keys = %d, value = %d", keys, value)
}
func setupGetpidProbe(name string) (*libbpfgo.Module, *libbpfgo.BPFLink, error) {
module, err := libbpfgo.NewModuleFromFile(name)
if err != nil {
return nil, nil, fmt.Errorf("error creating module from file %q: %v", name, err)
}
err = module.BPFLoadObject()
if err != nil {
return nil, nil, fmt.Errorf("error loading bpf object from file %q: %v", name, err)
}
prog, err := module.GetProgram("probe")
if err != nil {
return nil, nil, fmt.Errorf("error loading program from file %q: %v", name, err)
}
link, err := prog.AttachGeneric()
if err != nil {
return nil, nil, fmt.Errorf("error attaching probe from file %q: %v", name, err)
}
return module, link, nil
}