-
Notifications
You must be signed in to change notification settings - Fork 6
/
group_test.go
148 lines (126 loc) · 2.89 KB
/
group_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
// 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 (
"context"
"runtime"
"testing"
"time"
"acln.ro/perf"
)
func TestGroup(t *testing.T) {
t.Run("Count", testGroupCount)
t.Run("Record", testGroupRecord)
}
func testGroupCount(t *testing.T) {
requires(t, paranoid(1), hardwarePMU, softwarePMU)
da := new(perf.Attr)
perf.Dummy.Configure(da)
g := perf.Group{
CountFormat: perf.CountFormat{
Enabled: true,
Running: true,
},
}
g.Add(perf.CPUCycles, perf.Instructions, da)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ev, err := g.Open(perf.CallingThread, perf.AnyCPU)
if err != nil {
t.Fatalf("Open: %v", err)
}
sum := int64(0)
gc, err := ev.MeasureGroup(func() {
for i := int64(0); i < 50000; i++ {
sum += i
}
})
if err != nil {
t.Fatalf("MeasureGroup: %v", err)
}
t.Logf("got sum %d in %d %s and %d %s", sum, gc.Values[0].Value, gc.Values[0].Label, gc.Values[1].Value, gc.Values[1].Label)
}
func testGroupRecord(t *testing.T) {
requires(t, tracepointPMU, debugfs) // TODO(acln): paranoid
ga := &perf.Attr{
Options: perf.Options{
Disabled: true,
},
SampleFormat: perf.SampleFormat{
Tid: true,
Time: true,
CPU: true,
IP: true,
StreamID: true,
},
}
ga.SetSamplePeriod(1)
ga.SetWakeupEvents(1)
gtp := perf.Tracepoint("syscalls", "sys_enter_getpid")
if err := gtp.Configure(ga); err != nil {
t.Fatal(err)
}
wa := &perf.Attr{
SampleFormat: perf.SampleFormat{
Tid: true,
Time: true,
CPU: true,
IP: true,
StreamID: true,
},
}
wa.SetSamplePeriod(1)
wa.SetWakeupEvents(1)
wtp := perf.Tracepoint("syscalls", "sys_enter_write")
if err := wtp.Configure(wa); err != nil {
t.Fatal(err)
}
g := perf.Group{
CountFormat: perf.CountFormat{
Enabled: true,
Running: true,
},
}
g.Add(ga, wa)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ev, err := g.Open(perf.CallingThread, perf.AnyCPU)
if err != nil {
t.Fatal(err)
}
defer ev.Close()
gc, err := ev.MeasureGroup(func() {
getpidTrigger()
writeTrigger()
})
if err != nil {
t.Fatal(err)
}
for _, got := range gc.Values {
if got.Value != 1 {
t.Fatalf("want 1 hit for %q, got %d", got.Label, got.Value)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
grec, err := ev.ReadRecord(ctx)
if err != nil {
t.Fatal(err)
}
gsr, ok := grec.(*perf.SampleGroupRecord)
if !ok {
t.Fatalf("got %T, want *perf.SampleGroupRecord", grec)
}
wrec, err := ev.ReadRecord(ctx)
if err != nil {
t.Fatal(err)
}
wsr, ok := wrec.(*perf.SampleGroupRecord)
if !ok {
t.Fatalf("got %T, want *perf.SampleGroupRecord", wrec)
}
if gip, wip := gsr.IP, wsr.IP; gip == wip {
t.Fatalf("equal IP 0x%x for samples of different events", wip)
}
}