-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathperf_test.go
361 lines (301 loc) · 7.04 KB
/
perf_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
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
// 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_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"unsafe"
"acln.ro/perf"
"golang.org/x/sys/unix"
)
func TestOpen(t *testing.T) {
t.Run("BadGroup", testOpenBadGroup)
t.Run("BadAttrType", testOpenBadAttrType)
t.Run("PopulatesLabel", testOpenPopulatesLabel)
t.Run("EventIDsDifferentByCPU", testEventIDsDifferentByCPU)
}
func testOpenBadGroup(t *testing.T) {
requires(t, paranoid(1), hardwarePMU)
ca := new(perf.Attr)
perf.CPUCycles.Configure(ca)
ca.CountFormat.Group = true
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cycles, err := perf.Open(ca, perf.CallingThread, perf.AnyCPU, nil)
if err != nil {
t.Fatal(err)
}
cycles.Close()
_, err = perf.Open(ca, perf.CallingThread, perf.AnyCPU, cycles)
if err == nil {
t.Fatal("successful Open with closed group *Event")
}
cycles = new(perf.Event) // uninitialized
_, err = perf.Open(ca, perf.CallingThread, perf.AnyCPU, cycles)
if err == nil {
t.Fatal("successful Open with closed group *Event")
}
}
func testOpenBadAttrType(t *testing.T) {
a := &perf.Attr{
Type: 42,
}
_, err := perf.Open(a, perf.CallingThread, perf.AnyCPU, nil)
if err == nil {
t.Fatal("got a valid *Event for bad Attr.Type 42")
}
}
func testOpenPopulatesLabel(t *testing.T) {
// TODO(acln): extend when we implement general label lookup
requires(t, paranoid(1), hardwarePMU)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ca := &perf.Attr{
Type: perf.HardwareEvent,
Config: uint64(perf.CPUCycles),
}
cycles, err := perf.Open(ca, perf.CallingThread, perf.AnyCPU, nil)
if err != nil {
t.Fatal(err)
}
defer cycles.Close()
c, err := cycles.Measure(getpidTrigger)
if err != nil {
t.Fatal(err)
}
if c.Label == "" {
t.Fatal("Open did not set label on *Attr")
}
}
func testEventIDsDifferentByCPU(t *testing.T) {
requires(t, paranoid(1))
if runtime.NumCPU() == 1 {
t.Skip("only one CPU")
}
ca := new(perf.Attr)
perf.CPUCycles.Configure(ca)
cycles0, err := perf.Open(ca, perf.CallingThread, 0, nil)
if err != nil {
t.Fatal(err)
}
defer cycles0.Close()
cycles1, err := perf.Open(ca, perf.CallingThread, 1, nil)
if err != nil {
t.Fatal(err)
}
defer cycles1.Close()
id0, err := cycles0.ID()
if err != nil {
t.Fatal(err)
}
id1, err := cycles1.ID()
if err != nil {
t.Fatal(err)
}
if id0 == id1 {
t.Fatalf("event has the same ID on different CPUs")
}
}
func TestMain(m *testing.M) {
if !perf.Supported() {
fmt.Fprintln(os.Stderr, "perf_event_open not supported")
os.Exit(2)
}
os.Exit(m.Run())
}
// perfTestEnv holds and caches information about the testing environment
// for package perf.
type perfTestEnv struct {
cap struct {
sync.Once
sysadmin bool
}
paranoid struct {
sync.Once
value int
}
debugfs struct {
sync.Once
mounted bool
readable bool
readErr error
}
pmu struct {
sync.Mutex
ok map[string]struct{}
missing map[string]error
}
}
func (env *perfTestEnv) capSysAdmin() bool {
env.cap.Once.Do(env.initCap)
return env.cap.sysadmin
}
type capHeader struct {
version uint32
pid int32
}
type capData struct {
effective uint32
_ uint32 // permitted
_ uint32 // inheritable
}
// constants from uapi/linux/capability.h
const (
capSysAdmin = 21
capV3 = 0x20080522
)
func (env *perfTestEnv) initCap() {
header := &capHeader{
version: capV3,
pid: int32(unix.Getpid()),
}
data := make([]capData, 2)
_, _, e := unix.Syscall(unix.SYS_CAPGET, uintptr(unsafe.Pointer(header)), uintptr(unsafe.Pointer(&data[0])), 0)
if e != 0 {
return
}
if data[0].effective&(1<<capSysAdmin) != 0 {
env.cap.sysadmin = true
}
}
func (env *perfTestEnv) paranoidLevel() int {
env.paranoid.Once.Do(env.initParanoid)
return env.paranoid.value
}
func (env *perfTestEnv) initParanoid() {
content, err := ioutil.ReadFile("/proc/sys/kernel/perf_event_paranoid")
if err != nil {
env.paranoid.value = 3
return
}
nr := strings.TrimSpace(string(content))
paranoid, err := strconv.ParseInt(nr, 10, 32)
if err != nil {
env.paranoid.value = 3
return
}
env.paranoid.value = int(paranoid)
}
func (env *perfTestEnv) initDebugfs() {
_, err := os.Stat("/sys/kernel/debug")
if err != nil {
return
}
env.debugfs.mounted = true
_, err = ioutil.ReadDir("/sys/kernel/debug")
if err != nil {
env.debugfs.readErr = err
return
}
env.debugfs.readable = true
}
func (env *perfTestEnv) debugfsMounted() bool {
env.debugfs.Once.Do(env.initDebugfs)
return env.debugfs.mounted
}
func (env *perfTestEnv) debugfsReadable() (bool, error) {
env.debugfs.Once.Do(env.initDebugfs)
return env.debugfs.readable, env.debugfs.readErr
}
func (env *perfTestEnv) havePMU(u string) (bool, error) {
env.pmu.Lock()
defer env.pmu.Unlock()
if env.pmu.ok == nil {
env.pmu.ok = map[string]struct{}{}
}
if env.pmu.missing == nil {
env.pmu.missing = map[string]error{}
}
if _, ok := env.pmu.ok[u]; ok {
return true, nil
}
if err, ok := env.pmu.missing[u]; ok {
return false, err
}
_, err := perf.LookupEventType(u)
if err != nil {
env.pmu.missing[u] = err
return false, err
}
env.pmu.ok[u] = struct{}{}
return true, nil
}
var testenv perfTestEnv
// paranoid specifies a perf_event_paranoid level requirement for a test.
//
// For example, a value of 1 for paranoid means that the test requires a
// perf_event_paranoid level of 1 or less.
type paranoid int
func (p paranoid) Evaluate() error {
if testenv.capSysAdmin() {
return nil
}
want, have := int(p), testenv.paranoidLevel()
if have > want {
return fmt.Errorf("want perf_event_paranoid <= %d, have %d", want, have)
}
return nil
}
// debugfsreq specifies a debugfs requirement for a test: debugfs must be
// mounted at /sys/kernel/debug, and it must be readable.
type debugfsreq struct{}
func (debugfsreq) Evaluate() error {
if !testenv.debugfsMounted() {
return errors.New("debugfs is not mounted at /sys/kernel/debug")
}
if ok, err := testenv.debugfsReadable(); !ok {
return fmt.Errorf("debugfs is not readable: %v", err)
}
return nil
}
var debugfs = debugfsreq{}
// pmu specifies a PMU requirement for a test.
type pmu string
var (
hardwarePMU = pmu("hardware")
softwarePMU = pmu("software")
tracepointPMU = pmu("tracepoint")
)
func (u pmu) Evaluate() error {
device := string(u)
if device == "hardware" {
device = "cpu" // TODO(acln): investigate
}
if ok, err := testenv.havePMU(device); !ok {
return fmt.Errorf("%s PMU not supported: %v", device, err)
}
return nil
}
type testRequirement interface {
Evaluate() error
}
func requires(t *testing.T, reqs ...testRequirement) {
t.Helper()
sb := new(strings.Builder)
unmet := 0
for _, req := range reqs {
if err := req.Evaluate(); err != nil {
if unmet > 0 {
sb.WriteString("; ")
}
fmt.Fprint(sb, err)
unmet++
}
}
switch unmet {
case 0:
return
case 1:
t.Skipf("unmet requirement: %s", sb.String())
default:
t.Skipf("unmet requirements: %s", sb.String())
}
}