-
Notifications
You must be signed in to change notification settings - Fork 10
/
hardware_profiler.go
243 lines (219 loc) · 7.84 KB
/
hardware_profiler.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
//go:build linux
// +build linux
package perf
import (
"fmt"
"sync"
"go.uber.org/multierr"
"golang.org/x/sys/unix"
)
type HardwareProfilerType int
const (
AllHardwareProfilers HardwareProfilerType = 0
CpuCyclesProfiler HardwareProfilerType = 1 << iota
CpuInstrProfiler HardwareProfilerType = 1 << iota
CacheRefProfiler HardwareProfilerType = 1 << iota
CacheMissesProfiler HardwareProfilerType = 1 << iota
BranchInstrProfiler HardwareProfilerType = 1 << iota
BranchMissesProfiler HardwareProfilerType = 1 << iota
BusCyclesProfiler HardwareProfilerType = 1 << iota
StalledCyclesBackendProfiler HardwareProfilerType = 1 << iota
StalledCyclesFrontendProfiler HardwareProfilerType = 1 << iota
RefCpuCyclesProfiler HardwareProfilerType = 1 << iota
)
type hardwareProfiler struct {
// map of perf counter type to file descriptor
profilers map[int]Profiler
profilersMu sync.RWMutex
}
// NewHardwareProfiler returns a new hardware profiler.
func NewHardwareProfiler(pid, cpu int, profilerSet HardwareProfilerType, opts ...int) (HardwareProfiler, error) {
var e error
profilers := map[int]Profiler{}
if profilerSet&CpuCyclesProfiler > 0 || profilerSet == AllHardwareProfilers {
cpuCycleProfiler, err := NewCPUCycleProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup CPU cycle profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_CPU_CYCLES] = cpuCycleProfiler
}
}
if profilerSet&CpuInstrProfiler > 0 || profilerSet == AllHardwareProfilers {
instrProfiler, err := NewInstrProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to CPU setup instruction profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_INSTRUCTIONS] = instrProfiler
}
}
if profilerSet&CacheRefProfiler > 0 || profilerSet == AllHardwareProfilers {
cacheRefProfiler, err := NewCacheRefProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup cache ref profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_CACHE_REFERENCES] = cacheRefProfiler
}
}
if profilerSet&CacheMissesProfiler > 0 || profilerSet == AllHardwareProfilers {
cacheMissesProfiler, err := NewCacheMissesProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup cache misses profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_CACHE_MISSES] = cacheMissesProfiler
}
}
if profilerSet&BranchInstrProfiler > 0 || profilerSet == AllHardwareProfilers {
branchInstrProfiler, err := NewBranchInstrProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup branch instruction profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = branchInstrProfiler
}
}
if profilerSet&BranchMissesProfiler > 0 || profilerSet == AllHardwareProfilers {
branchMissesProfiler, err := NewBranchMissesProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup branch miss profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_BRANCH_MISSES] = branchMissesProfiler
}
}
if profilerSet&BusCyclesProfiler > 0 || profilerSet == AllHardwareProfilers {
busCyclesProfiler, err := NewBusCyclesProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup bus cycles profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_BUS_CYCLES] = busCyclesProfiler
}
}
if profilerSet&StalledCyclesFrontendProfiler > 0 || profilerSet == AllHardwareProfilers {
stalledCyclesFrontProfiler, err := NewStalledCyclesFrontProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup stalled fronted cycles profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = stalledCyclesFrontProfiler
}
}
if profilerSet&StalledCyclesBackendProfiler > 0 || profilerSet == AllHardwareProfilers {
stalledCyclesBackProfiler, err := NewStalledCyclesBackProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup stalled backend cycles profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = stalledCyclesBackProfiler
}
}
if profilerSet&RefCpuCyclesProfiler > 0 || profilerSet == AllHardwareProfilers {
refCPUCyclesProfiler, err := NewRefCPUCyclesProfiler(pid, cpu, opts...)
if err != nil {
e = multierr.Append(e,
fmt.Errorf("Failed to setup ref CPU cycles profiler: pid (%d) cpu (%d) %q", pid, cpu, err))
} else {
profilers[unix.PERF_COUNT_HW_REF_CPU_CYCLES] = refCPUCyclesProfiler
}
}
return &hardwareProfiler{
profilers: profilers,
}, e
}
// HasProfilers returns if there are any configured profilers.
func (p *hardwareProfiler) HasProfilers() bool {
p.profilersMu.RLock()
defer p.profilersMu.RUnlock()
return len(p.profilers) >= 0
}
// Start is used to start the HardwareProfiler.
func (p *hardwareProfiler) Start() error {
if !p.HasProfilers() {
return ErrNoProfiler
}
var err error
p.profilersMu.RLock()
for _, profiler := range p.profilers {
err = multierr.Append(err, profiler.Start())
}
p.profilersMu.RUnlock()
return err
}
// Reset is used to reset the HardwareProfiler.
func (p *hardwareProfiler) Reset() error {
var err error
p.profilersMu.RLock()
for _, profiler := range p.profilers {
err = multierr.Append(err, profiler.Reset())
}
p.profilersMu.RUnlock()
return err
}
// Stop is used to reset the HardwareProfiler.
func (p *hardwareProfiler) Stop() error {
var err error
p.profilersMu.RLock()
for _, profiler := range p.profilers {
err = multierr.Append(err, profiler.Stop())
}
p.profilersMu.RUnlock()
return err
}
// Close is used to reset the HardwareProfiler.
func (p *hardwareProfiler) Close() error {
var err error
p.profilersMu.RLock()
for _, profiler := range p.profilers {
err = multierr.Append(err, profiler.Close())
}
p.profilersMu.RUnlock()
return err
}
// Profile is used to read the HardwareProfiler HardwareProfile it returns an
// error only if all profiles fail.
func (p *hardwareProfiler) Profile(hwProfile *HardwareProfile) error {
var err error
hwProfile.Reset()
p.profilersMu.RLock()
for profilerType, profiler := range p.profilers {
profileVal := ProfileValuePool.Get().(*ProfileValue)
err2 := profiler.Profile(profileVal)
err = multierr.Append(err, err2)
if err2 == nil {
if hwProfile.TimeEnabled == nil {
hwProfile.TimeEnabled = &profileVal.TimeEnabled
}
if hwProfile.TimeRunning == nil {
hwProfile.TimeRunning = &profileVal.TimeRunning
}
switch profilerType {
case unix.PERF_COUNT_HW_CPU_CYCLES:
hwProfile.CPUCycles = &profileVal.Value
case unix.PERF_COUNT_HW_INSTRUCTIONS:
hwProfile.Instructions = &profileVal.Value
case unix.PERF_COUNT_HW_CACHE_REFERENCES:
hwProfile.CacheRefs = &profileVal.Value
case unix.PERF_COUNT_HW_CACHE_MISSES:
hwProfile.CacheMisses = &profileVal.Value
case unix.PERF_COUNT_HW_BRANCH_INSTRUCTIONS:
hwProfile.BranchInstr = &profileVal.Value
case unix.PERF_COUNT_HW_BRANCH_MISSES:
hwProfile.BranchMisses = &profileVal.Value
case unix.PERF_COUNT_HW_BUS_CYCLES:
hwProfile.BusCycles = &profileVal.Value
case unix.PERF_COUNT_HW_STALLED_CYCLES_FRONTEND:
hwProfile.StalledCyclesFrontend = &profileVal.Value
case unix.PERF_COUNT_HW_STALLED_CYCLES_BACKEND:
hwProfile.StalledCyclesBackend = &profileVal.Value
case unix.PERF_COUNT_HW_REF_CPU_CYCLES:
hwProfile.RefCPUCycles = &profileVal.Value
}
}
}
p.profilersMu.RUnlock()
return err
}