-
Notifications
You must be signed in to change notification settings - Fork 11
/
group_profiler.go
200 lines (175 loc) · 4.74 KB
/
group_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
//go:build linux
// +build linux
package perf
import (
"encoding/binary"
"fmt"
"sync"
"syscall"
"go.uber.org/multierr"
"golang.org/x/sys/unix"
)
// ErrNoLeader is returned when a leader of a GroupProfiler is not defined.
var ErrNoLeader = fmt.Errorf("No leader defined")
// GroupProfileValue is returned from a GroupProfiler.
type GroupProfileValue struct {
Events uint64
TimeEnabled uint64
TimeRunning uint64
Values []uint64
}
// GroupProfiler is used to setup a group profiler.
type GroupProfiler interface {
Start() error
Reset() error
Stop() error
Close() error
HasProfilers() bool
Profile(*GroupProfileValue) error
}
// groupProfiler implements the GroupProfiler interface.
type groupProfiler struct {
fds []int // leader is always element 0
profilersMu sync.RWMutex
bufPool sync.Pool
}
// NewGroupProfiler returns a GroupProfiler.
func NewGroupProfiler(pid, cpu, opts int, eventAttrs ...unix.PerfEventAttr) (GroupProfiler, error) {
fds := make([]int, len(eventAttrs))
for i, eventAttr := range eventAttrs {
// common configs
eventAttr.Size = EventAttrSize
eventAttr.Sample_type = PERF_SAMPLE_IDENTIFIER
// Leader fd must be opened first
if i == 0 {
// leader specific configs
eventAttr.Bits = unix.PerfBitDisabled | unix.PerfBitExcludeHv
eventAttr.Read_format = unix.PERF_FORMAT_TOTAL_TIME_RUNNING | unix.PERF_FORMAT_TOTAL_TIME_ENABLED | unix.PERF_FORMAT_GROUP
fd, err := unix.PerfEventOpen(
&eventAttr,
pid,
cpu,
-1,
opts,
)
if err != nil {
return nil, fmt.Errorf("Failed to open perf event with PerfEventAttr %+v %q", eventAttr, err)
}
fds[i] = fd
continue
}
// non leader configs
eventAttr.Read_format = unix.PERF_FORMAT_TOTAL_TIME_RUNNING | unix.PERF_FORMAT_TOTAL_TIME_ENABLED | unix.PERF_FORMAT_GROUP
eventAttr.Bits = unix.PerfBitExcludeHv
fd, err := unix.PerfEventOpen(
&eventAttr,
pid,
cpu,
fds[0],
opts,
)
if err != nil {
err = fmt.Errorf("Failed to open perf event with PerfEventAtter %+v: %q", eventAttr, err)
// cleanup any old Fds
for ii, fd2 := range fds {
if ii == i {
break
}
err = multierr.Append(err, unix.Close(fd2))
}
return nil, err
}
fds[i] = fd
}
bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 24+8*len(fds))
},
}
return &groupProfiler{
fds: fds,
bufPool: bufPool}, nil
}
// HasProfilers returns if there are any configured profilers.
func (p *groupProfiler) HasProfilers() bool {
p.profilersMu.RLock()
defer p.profilersMu.RUnlock()
return len(p.fds) >= 0
}
// Start is used to start the GroupProfiler.
func (p *groupProfiler) Start() error {
if !p.HasProfilers() {
return ErrNoLeader
}
p.profilersMu.RLock()
defer p.profilersMu.RUnlock()
return unix.IoctlSetInt(p.fds[0], unix.PERF_EVENT_IOC_ENABLE, unix.PERF_IOC_FLAG_GROUP)
}
// Reset is used to reset the GroupProfiler.
func (p *groupProfiler) Reset() error {
if !p.HasProfilers() {
return ErrNoLeader
}
p.profilersMu.RLock()
defer p.profilersMu.RUnlock()
return unix.IoctlSetInt(p.fds[0], unix.PERF_EVENT_IOC_RESET, unix.PERF_IOC_FLAG_GROUP)
}
// Stop is used to stop the GroupProfiler.
func (p *groupProfiler) Stop() error {
if !p.HasProfilers() {
return ErrNoLeader
}
p.profilersMu.RLock()
defer p.profilersMu.RUnlock()
return unix.IoctlSetInt(p.fds[0], unix.PERF_EVENT_IOC_DISABLE, unix.PERF_IOC_FLAG_GROUP)
}
// Close is used to close the GroupProfiler.
func (p *groupProfiler) Close() error {
var err error
p.profilersMu.RLock()
for _, fd := range p.fds {
err = multierr.Append(err, unix.Close(fd))
}
p.profilersMu.RUnlock()
return err
}
// Profile is used to return the GroupProfileValue of the GroupProfiler.
func (p *groupProfiler) Profile(val *GroupProfileValue) error {
p.profilersMu.RLock()
defer p.profilersMu.RUnlock()
nEvents := len(p.fds)
if nEvents == 0 {
return ErrNoLeader
}
// read format of the raw event looks like this:
/*
struct read_format {
u64 nr; // The number of events /
u64 time_enabled; // if PERF_FORMAT_TOTAL_TIME_ENABLED
u64 time_running; // if PERF_FORMAT_TOTAL_TIME_RUNNING
struct {
u64 value; // The value of the event
u64 id; // if PERF_FORMAT_ID
} values[nr];
};
*/
buf := p.bufPool.Get().([]byte)
_, err := syscall.Read(p.fds[0], buf)
if err != nil {
zero(buf)
p.bufPool.Put(buf)
return err
}
val.Events = binary.LittleEndian.Uint64(buf[0:8])
val.TimeEnabled = binary.LittleEndian.Uint64(buf[8:16])
val.TimeRunning = binary.LittleEndian.Uint64(buf[16:24])
val.Values = make([]uint64, len(p.fds))
offset := 24
for i := range p.fds {
val.Values[i] = binary.LittleEndian.Uint64(buf[offset : offset+8])
offset += 8
}
zero(buf)
p.bufPool.Put(buf)
return nil
}