-
Notifications
You must be signed in to change notification settings - Fork 11
/
process_profile.go
584 lines (529 loc) · 15.5 KB
/
process_profile.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//go:build linux
// +build linux
package perf
import (
"encoding/binary"
"fmt"
"sync"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
const (
// PERF_SAMPLE_IDENTIFIER is not defined in x/sys/unix.
PERF_SAMPLE_IDENTIFIER = 1 << 16
// PERF_IOC_FLAG_GROUP is not defined in x/sys/unix.
PERF_IOC_FLAG_GROUP = 1 << 0
)
var (
// ErrNoProfiler is returned when no profiler is available for profiling.
ErrNoProfiler = fmt.Errorf("No profiler available")
bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 24, 24)
},
}
// ProfileValuePool is a sync.Pool of ProfileValue structs.
ProfileValuePool = sync.Pool{
New: func() interface{} {
return &ProfileValue{}
},
}
)
// Profiler is a profiler.
type Profiler interface {
Start() error
Reset() error
Stop() error
Close() error
Profile(*ProfileValue) error
}
// HardwareProfiler is a hardware profiler.
type HardwareProfiler interface {
Start() error
Reset() error
Stop() error
Close() error
Profile(*HardwareProfile) error
HasProfilers() bool
}
// HardwareProfile is returned by a HardwareProfiler. Depending on kernel
// configuration some fields may return nil.
type HardwareProfile struct {
CPUCycles *uint64 `json:"cpu_cycles,omitempty"`
Instructions *uint64 `json:"instructions,omitempty"`
CacheRefs *uint64 `json:"cache_refs,omitempty"`
CacheMisses *uint64 `json:"cache_misses,omitempty"`
BranchInstr *uint64 `json:"branch_instr,omitempty"`
BranchMisses *uint64 `json:"branch_misses,omitempty"`
BusCycles *uint64 `json:"bus_cycles,omitempty"`
StalledCyclesFrontend *uint64 `json:"stalled_cycles_frontend,omitempty"`
StalledCyclesBackend *uint64 `json:"stalled_cycles_backend,omitempty"`
RefCPUCycles *uint64 `json:"ref_cpu_cycles,omitempty"`
TimeEnabled *uint64 `json:"time_enabled,omitempty"`
TimeRunning *uint64 `json:"time_running,omitempty"`
}
// Reset sets all values to defaults and will nil any pointers.
func (p *HardwareProfile) Reset() {
p.CPUCycles = nil
p.Instructions = nil
p.CacheRefs = nil
p.CacheMisses = nil
p.BranchInstr = nil
p.BranchMisses = nil
p.BusCycles = nil
p.StalledCyclesFrontend = nil
p.StalledCyclesBackend = nil
p.RefCPUCycles = nil
p.TimeEnabled = nil
p.TimeRunning = nil
}
// SoftwareProfiler is a software profiler.
type SoftwareProfiler interface {
Start() error
Reset() error
Stop() error
Close() error
Profile(*SoftwareProfile) error
HasProfilers() bool
}
// SoftwareProfile is returned by a SoftwareProfiler.
type SoftwareProfile struct {
CPUClock *uint64 `json:"cpu_clock,omitempty"`
TaskClock *uint64 `json:"task_clock,omitempty"`
PageFaults *uint64 `json:"page_faults,omitempty"`
ContextSwitches *uint64 `json:"context_switches,omitempty"`
CPUMigrations *uint64 `json:"cpu_migrations,omitempty"`
MinorPageFaults *uint64 `json:"minor_page_faults,omitempty"`
MajorPageFaults *uint64 `json:"major_page_faults,omitempty"`
AlignmentFaults *uint64 `json:"alignment_faults,omitempty"`
EmulationFaults *uint64 `json:"emulation_faults,omitempty"`
TimeEnabled *uint64 `json:"time_enabled,omitempty"`
TimeRunning *uint64 `json:"time_running,omitempty"`
}
// Reset sets all values to defaults and will nil any pointers.
func (p *SoftwareProfile) Reset() {
p.CPUClock = nil
p.TaskClock = nil
p.PageFaults = nil
p.ContextSwitches = nil
p.CPUMigrations = nil
p.MinorPageFaults = nil
p.MajorPageFaults = nil
p.AlignmentFaults = nil
p.EmulationFaults = nil
p.TimeEnabled = nil
p.TimeRunning = nil
}
// CacheProfiler is a cache profiler.
type CacheProfiler interface {
Start() error
Reset() error
Stop() error
Close() error
Profile(*CacheProfile) error
HasProfilers() bool
}
// CacheProfile is returned by a CacheProfiler.
type CacheProfile struct {
L1DataReadHit *uint64 `json:"l1_data_read_hit,omitempty"`
L1DataReadMiss *uint64 `json:"l1_data_read_miss,omitempty"`
L1DataWriteHit *uint64 `json:"l1_data_write_hit,omitempty"`
L1InstrReadMiss *uint64 `json:"l1_instr_read_miss,omitempty"`
LastLevelReadHit *uint64 `json:"last_level_read_hit,omitempty"`
LastLevelReadMiss *uint64 `json:"last_level_read_miss,omitempty"`
LastLevelWriteHit *uint64 `json:"last_level_write_hit,omitempty"`
LastLevelWriteMiss *uint64 `json:"last_level_write_miss,omitempty"`
DataTLBReadHit *uint64 `json:"data_tlb_read_hit,omitempty"`
DataTLBReadMiss *uint64 `json:"data_tlb_read_miss,omitempty"`
DataTLBWriteHit *uint64 `json:"data_tlb_write_hit,omitempty"`
DataTLBWriteMiss *uint64 `json:"data_tlb_write_miss,omitempty"`
InstrTLBReadHit *uint64 `json:"instr_tlb_read_hit,omitempty"`
InstrTLBReadMiss *uint64 `json:"instr_tlb_read_miss,omitempty"`
BPUReadHit *uint64 `json:"bpu_read_hit,omitempty"`
BPUReadMiss *uint64 `json:"bpu_read_miss,omitempty"`
NodeReadHit *uint64 `json:"node_read_hit,omitempty"`
NodeReadMiss *uint64 `json:"node_read_miss,omitempty"`
NodeWriteHit *uint64 `json:"node_write_hit,omitempty"`
NodeWriteMiss *uint64 `json:"node_write_miss,omitempty"`
TimeEnabled *uint64 `json:"time_enabled,omitempty"`
TimeRunning *uint64 `json:"time_running,omitempty"`
}
// Reset sets all values to defaults and will nil any pointers.
func (p *CacheProfile) Reset() {
p.L1DataReadHit = nil
p.L1DataReadMiss = nil
p.L1DataWriteHit = nil
p.L1InstrReadMiss = nil
p.LastLevelReadHit = nil
p.LastLevelReadMiss = nil
p.LastLevelWriteHit = nil
p.LastLevelWriteMiss = nil
p.DataTLBReadHit = nil
p.DataTLBReadMiss = nil
p.DataTLBWriteHit = nil
p.DataTLBWriteMiss = nil
p.InstrTLBReadHit = nil
p.InstrTLBReadMiss = nil
p.BPUReadHit = nil
p.BPUReadMiss = nil
p.NodeReadHit = nil
p.NodeReadMiss = nil
p.NodeWriteHit = nil
p.NodeWriteMiss = nil
p.TimeEnabled = nil
p.TimeRunning = nil
}
// ProfileValue is a value returned by a profiler.
type ProfileValue struct {
Value uint64
TimeEnabled uint64
TimeRunning uint64
}
// profiler is used to profile a process.
type profiler struct {
fd int
}
// NewProfiler creates a new hardware profiler. It does not support grouping.
func NewProfiler(profilerType uint32, config uint64, pid, cpu int, opts ...int) (Profiler, error) {
eventAttr := &unix.PerfEventAttr{
Type: profilerType,
Config: config,
Size: uint32(unsafe.Sizeof(unix.PerfEventAttr{})),
Bits: unix.PerfBitDisabled | unix.PerfBitExcludeHv | unix.PerfBitInherit,
Read_format: unix.PERF_FORMAT_TOTAL_TIME_RUNNING | unix.PERF_FORMAT_TOTAL_TIME_ENABLED,
Sample_type: PERF_SAMPLE_IDENTIFIER,
}
var eventOps int
if len(opts) > 0 {
eventOps = opts[0]
}
fd, err := unix.PerfEventOpen(
eventAttr,
pid,
cpu,
-1,
eventOps,
)
if err != nil {
return nil, fmt.Errorf("Failed to open perf event for PerfEventAttr %+v: %q", eventAttr, err)
}
return &profiler{
fd: fd,
}, nil
}
// NewCPUCycleProfiler returns a Profiler that profiles CPU cycles.
func NewCPUCycleProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_CPU_CYCLES,
pid,
cpu,
opts...,
)
}
// NewInstrProfiler returns a Profiler that profiles CPU instructions.
func NewInstrProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_INSTRUCTIONS,
pid,
cpu,
opts...,
)
}
// NewCacheRefProfiler returns a Profiler that profiles cache references.
func NewCacheRefProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_CACHE_REFERENCES,
pid,
cpu,
opts...,
)
}
// NewCacheMissesProfiler returns a Profiler that profiles cache misses.
func NewCacheMissesProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_CACHE_MISSES,
pid,
cpu,
opts...,
)
}
// NewBranchInstrProfiler returns a Profiler that profiles branch instructions.
func NewBranchInstrProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
pid,
cpu,
opts...,
)
}
// NewBranchMissesProfiler returns a Profiler that profiles branch misses.
func NewBranchMissesProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_BRANCH_MISSES,
pid,
cpu,
opts...,
)
}
// NewBusCyclesProfiler returns a Profiler that profiles bus cycles.
func NewBusCyclesProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_BUS_CYCLES,
pid,
cpu,
opts...,
)
}
// NewStalledCyclesFrontProfiler returns a Profiler that profiles stalled
// frontend cycles.
func NewStalledCyclesFrontProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_STALLED_CYCLES_FRONTEND,
pid,
cpu,
opts...,
)
}
// NewStalledCyclesBackProfiler returns a Profiler that profiles stalled
// backend cycles.
func NewStalledCyclesBackProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_STALLED_CYCLES_BACKEND,
pid,
cpu,
opts...,
)
}
// NewRefCPUCyclesProfiler returns a Profiler that profiles CPU cycles, it
// is not affected by frequency scaling.
func NewRefCPUCyclesProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HARDWARE,
unix.PERF_COUNT_HW_REF_CPU_CYCLES,
pid,
cpu,
opts...,
)
}
// NewCPUClockProfiler returns a Profiler that profiles CPU clock speed.
func NewCPUClockProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_CPU_CLOCK,
pid,
cpu,
opts...,
)
}
// NewTaskClockProfiler returns a Profiler that profiles clock count of the
// running task.
func NewTaskClockProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_TASK_CLOCK,
pid,
cpu,
opts...,
)
}
// NewPageFaultProfiler returns a Profiler that profiles the number of page
// faults.
func NewPageFaultProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_PAGE_FAULTS,
pid,
cpu,
opts...,
)
}
// NewCtxSwitchesProfiler returns a Profiler that profiles the number of context
// switches.
func NewCtxSwitchesProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_CONTEXT_SWITCHES,
pid,
cpu,
opts...,
)
}
// NewCPUMigrationsProfiler returns a Profiler that profiles the number of times
// the process has migrated to a new CPU.
func NewCPUMigrationsProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_CPU_MIGRATIONS,
pid,
cpu,
opts...,
)
}
// NewMinorFaultsProfiler returns a Profiler that profiles the number of minor
// page faults.
func NewMinorFaultsProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_PAGE_FAULTS_MIN,
pid,
cpu,
opts...,
)
}
// NewMajorFaultsProfiler returns a Profiler that profiles the number of major
// page faults.
func NewMajorFaultsProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_PAGE_FAULTS_MAJ,
pid,
cpu,
opts...,
)
}
// NewAlignFaultsProfiler returns a Profiler that profiles the number of
// alignment faults.
func NewAlignFaultsProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_ALIGNMENT_FAULTS,
pid,
cpu,
opts...,
)
}
// NewEmulationFaultsProfiler returns a Profiler that profiles the number of
// alignment faults.
func NewEmulationFaultsProfiler(pid, cpu int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_SOFTWARE,
unix.PERF_COUNT_SW_EMULATION_FAULTS,
pid,
cpu,
opts...,
)
}
// NewL1DataProfiler returns a Profiler that profiles L1 cache data.
func NewL1DataProfiler(pid, cpu, op, result int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HW_CACHE,
uint64((unix.PERF_COUNT_HW_CACHE_L1D)|(op<<8)|(result<<16)),
pid,
cpu,
opts...,
)
}
// NewL1InstrProfiler returns a Profiler that profiles L1 instruction data.
func NewL1InstrProfiler(pid, cpu, op, result int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HW_CACHE,
uint64((unix.PERF_COUNT_HW_CACHE_L1I)|(op<<8)|(result<<16)),
pid,
cpu,
opts...,
)
}
// NewLLCacheProfiler returns a Profiler that profiles last level cache.
func NewLLCacheProfiler(pid, cpu, op, result int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HW_CACHE,
uint64((unix.PERF_COUNT_HW_CACHE_LL)|(op<<8)|(result<<16)),
pid,
cpu,
opts...,
)
}
// NewDataTLBProfiler returns a Profiler that profiles the data TLB.
func NewDataTLBProfiler(pid, cpu, op, result int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HW_CACHE,
uint64((unix.PERF_COUNT_HW_CACHE_DTLB)|(op<<8)|(result<<16)),
pid,
cpu,
opts...,
)
}
// NewInstrTLBProfiler returns a Profiler that profiles the instruction TLB.
func NewInstrTLBProfiler(pid, cpu, op, result int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HW_CACHE,
uint64((unix.PERF_COUNT_HW_CACHE_ITLB)|(op<<8)|(result<<16)),
pid,
cpu,
opts...,
)
}
// NewBPUProfiler returns a Profiler that profiles the BPU (branch prediction unit).
func NewBPUProfiler(pid, cpu, op, result int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HW_CACHE,
uint64((unix.PERF_COUNT_HW_CACHE_BPU)|(op<<8)|(result<<16)),
pid,
cpu,
opts...,
)
}
// NewNodeCacheProfiler returns a Profiler that profiles the node cache accesses.
func NewNodeCacheProfiler(pid, cpu, op, result int, opts ...int) (Profiler, error) {
return NewProfiler(
unix.PERF_TYPE_HW_CACHE,
uint64((unix.PERF_COUNT_HW_CACHE_NODE)|(op<<8)|(result<<16)),
pid,
cpu,
opts...,
)
}
// Reset is used to reset the counters of the profiler.
func (p *profiler) Reset() error {
return unix.IoctlSetInt(p.fd, unix.PERF_EVENT_IOC_RESET, 0)
}
// Start is used to Start the profiler.
func (p *profiler) Start() error {
return unix.IoctlSetInt(p.fd, unix.PERF_EVENT_IOC_ENABLE, 0)
}
// Stop is used to stop the profiler.
func (p *profiler) Stop() error {
return unix.IoctlSetInt(p.fd, unix.PERF_EVENT_IOC_DISABLE, 0)
}
// Profile returns the current Profile.
func (p *profiler) Profile(val *ProfileValue) error {
// The underlying struct that gets read from the profiler looks like:
/*
struct read_format {
u64 value; // The value of the event
u64 time_enabled; // if PERF_FORMAT_TOTAL_TIME_ENABLED
u64 time_running; // if PERF_FORMAT_TOTAL_TIME_RUNNING
u64 id; // if PERF_FORMAT_ID
};
*/
// read 24 bytes since PERF_FORMAT_TOTAL_TIME_ENABLED and
// PERF_FORMAT_TOTAL_TIME_RUNNING are always set.
// XXX: allow profile ids?
buf := bufPool.Get().([]byte)
_, err := syscall.Read(p.fd, buf)
if err != nil {
zero(buf)
bufPool.Put(buf)
return err
}
val.Value = binary.LittleEndian.Uint64(buf[0:8])
val.TimeEnabled = binary.LittleEndian.Uint64(buf[8:16])
val.TimeRunning = binary.LittleEndian.Uint64(buf[16:24])
zero(buf)
bufPool.Put(buf)
return nil
}
// Close is used to close the perf context.
func (p *profiler) Close() error {
return unix.Close(p.fd)
}