-
Notifications
You must be signed in to change notification settings - Fork 0
/
goperf.go
298 lines (251 loc) · 9.68 KB
/
goperf.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
package goperf
import (
"fmt"
"golang.org/x/sys/unix"
"golang.org/x/text/language"
"golang.org/x/text/message"
"strings"
"time"
"unsafe"
)
const (
pL1D = unix.PERF_COUNT_HW_CACHE_L1D
pL1I = unix.PERF_COUNT_HW_CACHE_L1I
pLL = unix.PERF_COUNT_HW_CACHE_LL
pDTLB = unix.PERF_COUNT_HW_CACHE_DTLB
pITLB = unix.PERF_COUNT_HW_CACHE_ITLB
pBPU = unix.PERF_COUNT_HW_CACHE_BPU
pNODE = unix.PERF_COUNT_HW_CACHE_NODE
)
const (
pREAD = unix.PERF_COUNT_HW_CACHE_OP_READ
pWRITE = unix.PERF_COUNT_HW_CACHE_OP_WRITE
pPREFETCH = unix.PERF_COUNT_HW_CACHE_OP_PREFETCH
)
const (
pACCESS = unix.PERF_COUNT_HW_CACHE_RESULT_ACCESS
pMISS = unix.PERF_COUNT_HW_CACHE_RESULT_MISS
)
type counter struct {
Name string
Type uint32
Config uint64
Enabled bool
}
var initialized bool
var running bool
var total time.Duration
var start time.Time
type item struct {
event counter
value float64
active float64
fd int
}
var items [len(Counters)]item
func cache(cache uint32, op uint32, result uint32) uint64 {
return uint64(cache | (op << 16) | (result << 16))
}
var Counters = [...]counter{
{"cpu-clock", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_CPU_CLOCK, true},
{"task-clock", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_TASK_CLOCK, true},
{"page-faults", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_PAGE_FAULTS, true},
{"context-switches", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_CONTEXT_SWITCHES, true},
{"cpu-migrations", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_CPU_MIGRATIONS, true},
{"page-fault-minor", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_PAGE_FAULTS_MIN, true},
{"page-fault-major", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_PAGE_FAULTS_MAJ, false},
{"alignment-faults", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_ALIGNMENT_FAULTS, false},
{"emulation-faults", unix.PERF_TYPE_SOFTWARE, unix.PERF_COUNT_SW_EMULATION_FAULTS, false},
{"cpu-cycles", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_CPU_CYCLES, true},
{"instructions", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_INSTRUCTIONS, true},
{"cache-references", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_CACHE_REFERENCES, false},
{"cache-misses", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_CACHE_MISSES, true},
{"branch-instructions", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_BRANCH_INSTRUCTIONS, false},
{"branch-misses", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_BRANCH_MISSES, false},
{"bus-cycles", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_BUS_CYCLES, false},
{"stalled-cycles-frontend", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_STALLED_CYCLES_FRONTEND, false},
{"stalled-cycles-backend", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_STALLED_CYCLES_BACKEND, false},
{"ref-cpu-cycles", unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_REF_CPU_CYCLES, false},
{"L1D-read-access", unix.PERF_TYPE_HW_CACHE, cache(pL1D, pREAD, pACCESS), false},
{"L1D-read-miss", unix.PERF_TYPE_HW_CACHE, cache(pL1D, pREAD, pMISS), true},
{"L1D-write-access", unix.PERF_TYPE_HW_CACHE, cache(pL1D, pWRITE, pACCESS), false},
{"L1D-write-miss", unix.PERF_TYPE_HW_CACHE, cache(pL1D, pWRITE, pMISS), false},
{"L1D-prefetch-access", unix.PERF_TYPE_HW_CACHE, cache(pL1D, pPREFETCH, pACCESS), false},
{"L1D-prefetch-miss", unix.PERF_TYPE_HW_CACHE, cache(pL1D, pPREFETCH, pMISS), false},
{"L1I-read-access", unix.PERF_TYPE_HW_CACHE, cache(pL1I, pREAD, pACCESS), false},
{"L1I-read-miss", unix.PERF_TYPE_HW_CACHE, cache(pL1I, pREAD, pMISS), true},
{"L1I-write-access", unix.PERF_TYPE_HW_CACHE, cache(pL1I, pWRITE, pACCESS), false},
{"L1I-write-miss", unix.PERF_TYPE_HW_CACHE, cache(pL1I, pWRITE, pMISS), false},
{"L1I-prefetch-access", unix.PERF_TYPE_HW_CACHE, cache(pL1I, pPREFETCH, pACCESS), false},
{"L1I-prefetch-miss", unix.PERF_TYPE_HW_CACHE, cache(pL1I, pPREFETCH, pMISS), false},
{"LL-read-access", unix.PERF_TYPE_HW_CACHE, cache(pLL, pREAD, pACCESS), false},
{"LL-read-miss", unix.PERF_TYPE_HW_CACHE, cache(pLL, pREAD, pMISS), false},
{"LL-write-access", unix.PERF_TYPE_HW_CACHE, cache(pLL, pWRITE, pACCESS), false},
{"LL-write-miss", unix.PERF_TYPE_HW_CACHE, cache(pLL, pWRITE, pMISS), false},
{"LL-prefetch-access", unix.PERF_TYPE_HW_CACHE, cache(pLL, pPREFETCH, pACCESS), false},
{"LL-prefetch-miss", unix.PERF_TYPE_HW_CACHE, cache(pLL, pPREFETCH, pMISS), false},
{"DTLB-read-access", unix.PERF_TYPE_HW_CACHE, cache(pDTLB, pREAD, pACCESS), false},
{"DTLB-read-miss", unix.PERF_TYPE_HW_CACHE, cache(pDTLB, pREAD, pMISS), false},
{"DTLB-write-access", unix.PERF_TYPE_HW_CACHE, cache(pDTLB, pWRITE, pACCESS), false},
{"DTLB-write-miss", unix.PERF_TYPE_HW_CACHE, cache(pDTLB, pWRITE, pMISS), false},
{"DTLB-prefetch-access", unix.PERF_TYPE_HW_CACHE, cache(pDTLB, pPREFETCH, pACCESS), false},
{"DTLB-prefetch-miss", unix.PERF_TYPE_HW_CACHE, cache(pDTLB, pPREFETCH, pMISS), false},
{"ITLB-read-access", unix.PERF_TYPE_HW_CACHE, cache(pITLB, pREAD, pACCESS), false},
{"ITLB-read-miss", unix.PERF_TYPE_HW_CACHE, cache(pITLB, pREAD, pMISS), false},
{"ITLB-write-access", unix.PERF_TYPE_HW_CACHE, cache(pITLB, pWRITE, pACCESS), false},
{"ITLB-write-miss", unix.PERF_TYPE_HW_CACHE, cache(pITLB, pWRITE, pMISS), false},
{"ITLB-prefetch-access", unix.PERF_TYPE_HW_CACHE, cache(pITLB, pPREFETCH, pACCESS), false},
{"ITLB-prefetch-miss", unix.PERF_TYPE_HW_CACHE, cache(pITLB, pPREFETCH, pMISS), false},
{"BPU-read-access", unix.PERF_TYPE_HW_CACHE, cache(pBPU, pREAD, pACCESS), false},
{"BPU-read-miss", unix.PERF_TYPE_HW_CACHE, cache(pBPU, pREAD, pMISS), false},
{"BPU-write-access", unix.PERF_TYPE_HW_CACHE, cache(pBPU, pWRITE, pACCESS), false},
{"BPU-write-miss", unix.PERF_TYPE_HW_CACHE, cache(pBPU, pWRITE, pMISS), false},
{"BPU-prefetch-access", unix.PERF_TYPE_HW_CACHE, cache(pBPU, pPREFETCH, pACCESS), false},
{"BPU-prefetch-miss", unix.PERF_TYPE_HW_CACHE, cache(pBPU, pPREFETCH, pMISS), false},
{"NODE-read-access", unix.PERF_TYPE_HW_CACHE, cache(pNODE, pREAD, pACCESS), false},
{"NODE-read-miss", unix.PERF_TYPE_HW_CACHE, cache(pNODE, pREAD, pMISS), false},
{"NODE-write-access", unix.PERF_TYPE_HW_CACHE, cache(pNODE, pWRITE, pACCESS), false},
{"NODE-write-miss", unix.PERF_TYPE_HW_CACHE, cache(pNODE, pWRITE, pMISS), false},
{"NODE-prefetch-access", unix.PERF_TYPE_HW_CACHE, cache(pNODE, pPREFETCH, pACCESS), false},
{"NODE-prefetch-miss", unix.PERF_TYPE_HW_CACHE, cache(pNODE, pPREFETCH, pMISS), false},
}
func clear() {
total = 0
running = false
initialized = false
for i := range items {
items[i].event = Counters[i]
items[i].active = 0
items[i].value = 0
items[i].fd = -1
}
}
type readFormat struct {
value uint64
timeEnabled uint64
timeRunning uint64
}
func set() {
flag := unix.PERF_FORMAT_TOTAL_TIME_RUNNING |
unix.PERF_FORMAT_TOTAL_TIME_ENABLED
for i := range items {
if !Counters[i].Enabled {
continue
}
t := unix.PerfEventAttr{
Type: items[i].event.Type,
Size: 0,
Config: items[i].event.Config,
Read_format: uint64(flag),
Bits: 3, // disabled & inherit
}
fd, err := unix.PerfEventOpen(&t, 0, -1, -1, unix.PERF_FLAG_FD_CLOEXEC)
if err != nil {
str := fmt.Errorf("perfEventOpen : %v", err)
panic(str)
}
items[i].fd = fd
}
}
// Enable a counter by its name, check Counters array for the list
// Hardware counters are limited on your CPU (~7 these days).
// Some performance counters cannot be enabled at the same time.
// Unsupported counters (either by OS or your CPU) will fail.
// Some counters can be scheduled at the same PMU on the CPU, so
// they will be multiplexed. You can check measurement time in the output
// to see this is the case.
func Enable(counter string) {
for i := range Counters {
if strings.EqualFold(Counters[i].Name, counter) {
Counters[i].Enabled = true
return
}
}
panic(fmt.Errorf("unknown counter : %s", counter))
}
// Disable a counter by its name, check Counters array for the list
func Disable(counter string) {
for i := range Counters {
if strings.EqualFold(Counters[i].Name, counter) {
Counters[i].Enabled = false
return
}
}
panic(fmt.Errorf("unknown counter : %s", counter))
}
func Start() {
if running {
panic("Already started")
}
if !initialized {
clear()
set()
initialized = true
}
err := unix.Prctl(unix.PR_TASK_PERF_EVENTS_ENABLE, 0, 0, 0, 0)
if err != nil {
panic(fmt.Errorf("prctl : %v", err))
}
start = time.Now()
running = true
}
func Pause() {
if !initialized {
panic("call Start() first")
}
if !running {
return
}
err := unix.Prctl(unix.PR_TASK_PERF_EVENTS_DISABLE, 0, 0, 0, 0)
if err != nil {
panic(fmt.Errorf("prctl : %v", err))
}
total = total + time.Now().Sub(start)
running = false
}
func End() {
if !initialized {
panic("call Start() first")
}
Pause()
readCounters()
for i := range items {
if Counters[i].Enabled {
_ = unix.Close(items[i].fd)
}
}
p := message.NewPrinter(language.English)
p.Printf("\n| %-25s | %-18s | %s \n", "Event", "Value", "Measurement time")
p.Printf("---------------------------------------------------------------\n")
p.Printf("| %-25s | %-18.2f | %s \n", "time (seconds)", float64(total)/1e9, "(100,00%)")
for i := range items {
if Counters[i].Enabled {
p.Printf("| %-25s | %-18.2f | (%.2f%%) \n", items[i].event.Name,
items[i].value, items[i].active*100)
}
}
clear()
}
func readCounters() {
var f readFormat
for i := range items {
n := 1.0
p := make([]byte, 64)
if !Counters[i].Enabled {
continue
}
rd, err := unix.Read(items[i].fd, p)
if err != nil {
panic(fmt.Errorf("failed to read counters : %v", err))
}
if rd != int(unsafe.Sizeof(f)) {
panic(fmt.Errorf("read less than expected"))
}
f = *((*readFormat)(unsafe.Pointer(&p[0])))
if f.timeEnabled > 0 && f.timeRunning > 0 {
n = float64(f.timeRunning / f.timeEnabled)
items[i].active = n
}
items[i].value = items[i].value + float64(f.value)*n
}
}