-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusdt_integration_test.go
More file actions
220 lines (187 loc) · 5.25 KB
/
usdt_integration_test.go
File metadata and controls
220 lines (187 loc) · 5.25 KB
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
// Copyright The Parca Authors
// SPDX-License-Identifier: Apache-2.0
//go:build linux
package usdt
import (
"os"
"testing"
"time"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
usdttest "github.com/parca-dev/usdt/test"
)
// TestUSDTProbeLifecycleSingle is a full end-to-end test:
// parse probes from self, load BPF, populate spec map, attach individual
// programs, fire probes, verify results via BPF map.
func TestUSDTProbeLifecycleSingle(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("requires root to load BPF programs")
}
setup := newIntegrationSetup(t)
defer setup.close()
// Attach each probe with its own dedicated BPF program.
progs := make([]*ebpf.Program, len(setup.probes))
for i, p := range setup.probes {
prog := setup.progByName(t, p.Name)
if prog == nil {
t.Fatalf("no BPF program for probe %s", p.Name)
}
progs[i] = prog
}
// Cookies carry the probe_id (1-8) that the BPF programs use to key
// into the results map. Spec IDs go in the high 32 bits.
userCookies := make([]uint64, len(setup.probes))
for i := range userCookies {
userCookies[i] = uint64(i + 1)
}
cookies := MergeCookies(setup.specIDs, userCookies)
pl, err := AttachUprobes(setup.exe, setup.objs.BpfUsdtSpecs, setup.probes, progs, cookies)
if err != nil {
t.Fatalf("AttachUprobes: %v", err)
}
defer pl.Close()
setup.fireAndVerify(t)
}
// TestUSDTProbeLifecycleMulti tests multi-probe attachment where a single
// dispatcher program handles all probes, using the cookie for dispatch.
func TestUSDTProbeLifecycleMulti(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("requires root to load BPF programs")
}
setup := newIntegrationSetup(t)
defer setup.close()
// All probes share the multi-probe dispatcher program.
progs := make([]*ebpf.Program, len(setup.probes))
for i := range progs {
progs[i] = setup.objs.UsdtTestMulti
}
userCookies := make([]uint64, len(setup.probes))
for i := range userCookies {
userCookies[i] = uint64(i + 1)
}
cookies := MergeCookies(setup.specIDs, userCookies)
pl, err := AttachUprobes(setup.exe, setup.objs.BpfUsdtSpecs, setup.probes, progs, cookies)
if err != nil {
t.Fatalf("AttachUprobes: %v", err)
}
defer pl.Close()
setup.fireAndVerify(t)
}
// ---------------------------------------------------------------------------
// test helpers
// ---------------------------------------------------------------------------
type integrationSetup struct {
objs bpfUsdtObjects
exe *link.Executable
probes []Probe
specIDs []uint32
}
func newIntegrationSetup(t *testing.T) *integrationSetup {
t.Helper()
// Ensure probes are compiled into the binary.
usdttest.CallTestProbes()
exePath, err := os.Executable()
if err != nil {
t.Fatalf("os.Executable: %v", err)
}
allProbes, err := ParseProbesFromFile(exePath)
if err != nil {
t.Fatalf("ParseProbesFromFile: %v", err)
}
// Keep only testprov probes, in the order the BPF test programs expect.
probeOrder := []string{
"simple_probe", "memory_probe", "const_probe", "mixed_probe",
"int32_args", "int64_args", "mixed_refs", "uint8_args",
}
byName := make(map[string]Probe, len(allProbes))
for _, p := range allProbes {
if p.Provider == "testprov" {
byName[p.Name] = p
}
}
var probes []Probe
for _, name := range probeOrder {
p, ok := byName[name]
if !ok {
t.Fatalf("probe %q not found in binary", name)
}
probes = append(probes, p)
t.Logf("probe %s: location=0x%x args=%q", p.Name, p.Location, p.Arguments)
}
// Load BPF objects.
var objs bpfUsdtObjects
if err := loadBpfUsdtObjects(&objs, nil); err != nil {
t.Fatalf("loadBpfUsdtObjects: %v", err)
}
// Populate the spec map with argument metadata.
specIDs, err := PopulateSpecMap(objs.BpfUsdtSpecs, probes, 1)
if err != nil {
objs.Close()
t.Fatalf("PopulateSpecMap: %v", err)
}
exe, err := link.OpenExecutable(exePath)
if err != nil {
objs.Close()
t.Fatalf("OpenExecutable: %v", err)
}
return &integrationSetup{
objs: objs,
exe: exe,
probes: probes,
specIDs: specIDs,
}
}
func (s *integrationSetup) close() {
s.objs.Close()
}
func (s *integrationSetup) progByName(t *testing.T, name string) *ebpf.Program {
t.Helper()
switch name {
case "simple_probe":
return s.objs.SimpleProbe
case "memory_probe":
return s.objs.MemoryProbe
case "const_probe":
return s.objs.ConstProbe
case "mixed_probe":
return s.objs.MixedProbe
case "int32_args":
return s.objs.Int32Args
case "int64_args":
return s.objs.Int64Args
case "mixed_refs":
return s.objs.MixedRefs
case "uint8_args":
return s.objs.Uint8Args
default:
return nil
}
}
func (s *integrationSetup) fireAndVerify(t *testing.T) {
t.Helper()
// Fire probes several times.
for range 10 {
usdttest.CallTestProbes()
time.Sleep(10 * time.Millisecond)
}
passed, failed := 0, 0
for i, p := range s.probes {
probeID := uint32(i + 1)
var val uint64
if err := s.objs.UsdtTestResults.Lookup(&probeID, &val); err != nil {
t.Logf("probe %s (id=%d): no result (may not have fired)", p.Name, probeID)
continue
}
if val == 1 {
t.Logf("probe %s: PASS", p.Name)
passed++
} else {
t.Errorf("probe %s: FAIL (arguments did not match)", p.Name)
failed++
}
}
t.Logf("%d passed, %d failed out of %d probes", passed, failed, len(s.probes))
if passed == 0 {
t.Fatal("no probes fired")
}
}