-
Notifications
You must be signed in to change notification settings - Fork 5
/
perforator.go
210 lines (179 loc) · 4.83 KB
/
perforator.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
package perforator
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/zyedidia/perf"
"github.com/zyedidia/perforator/bininfo"
"github.com/zyedidia/perforator/utrace"
)
// Events is a specification for which perf events should be tracked. A Base
// set of events is tracked using standard perf, and sets of groups of events
// may also be given to avoid multiplexing between events in the same group.
type Events struct {
Base []perf.Configurator
Groups [][]perf.Configurator
}
// Run executes the given command with tracing for certain events enabled. A
// structure with all perf metrics is returned.
func Run(target string, args []string,
regionNames []string,
events Events,
attropts perf.Options,
immediate func() MetricsWriter,
ignoreMissingRegions bool,
rangeInnerDelimiter string,
excludeClones bool) (TotalMetrics, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
path, err := exec.LookPath(target)
if err != nil {
return TotalMetrics{}, fmt.Errorf("lookpath: %w", err)
}
f, err := os.Open(path)
if err != nil {
return TotalMetrics{}, fmt.Errorf("open: %w", err)
}
bin, err := bininfo.Read(f, f.Name())
if err != nil {
return TotalMetrics{}, fmt.Errorf("elf-read: %w", err)
}
var regions []utrace.Region
var regionIds []int
addregion := func(reg utrace.Region, id int) {
regions = append(regions, reg)
regionIds = append(regionIds, id)
}
for i, name := range regionNames {
if strings.Contains(name, "-") {
reg, err := ParseRegion(name, bin, rangeInnerDelimiter)
if err != nil {
return TotalMetrics{}, fmt.Errorf("region-parse: %w", err)
}
logger.Printf("%s: 0x%x-0x%x\n", name, reg.StartAddr, reg.EndAddr)
addregion(reg, i)
} else {
fnpc, fnerr := bin.FuncToPC(name, excludeClones)
if fnerr == nil {
logger.Printf("%s: 0x%x\n", name, fnpc)
addregion(&utrace.FuncRegion{
Addr: fnpc,
}, i)
}
inlinings, err := bin.InlinedFuncToPCs(name, excludeClones)
if len(inlinings) == 0 {
logger.Printf("%s not inlined (error: %s)\n", name, err)
}
if err != nil {
if fnerr != nil {
if err != nil && !ignoreMissingRegions {
return TotalMetrics{}, fmt.Errorf("func-lookup: %w, inlined-func-lookup: %s", fnerr, err)
}
}
continue
}
for _, in := range inlinings {
logger.Printf("%s (inlined): 0x%x-0x%x\n", name, in.Low, in.High)
addregion(&utrace.AddressRegion{
StartAddr: in.Low,
EndAddr: in.High,
}, i)
}
}
}
prog, pid, err := utrace.NewProgram(bin, target, args, regions)
if err != nil {
return TotalMetrics{}, err
}
fa := &perf.Attr{
CountFormat: perf.CountFormat{
Enabled: true,
Running: true,
},
Options: attropts,
}
fa.Options.Disabled = true
base := make([]*perf.Attr, len(events.Base))
for i, c := range events.Base {
attr := *fa
c.Configure(&attr)
base[i] = &attr
}
groups := make([][]*perf.Attr, len(events.Groups))
for i, group := range events.Groups {
for _, c := range group {
attr := *fa
c.Configure(&attr)
groups[i] = append(groups[i], &attr)
}
}
total := make(TotalMetrics, 0)
ptable := make(map[int][]Profiler)
ptable[pid], err = makeProfilers(pid, len(regions), base, groups, fa)
if err != nil {
return total, err
}
for {
var ws utrace.Status
p, evs, err := prog.Wait(&ws)
if err == utrace.ErrFinishedTrace {
break
}
if err != nil {
return total, fmt.Errorf("wait: %w", err)
}
profilers, ok := ptable[p.Pid()]
if !ok {
ptable[p.Pid()], err = makeProfilers(p.Pid(), len(regions), base, groups, fa)
if err != nil {
return total, err
}
}
for _, ev := range evs {
switch ev.State {
case utrace.RegionStart:
logger.Printf("%d: Profiler %d enabled\n", p.Pid(), ev.Id)
profilers[ev.Id].Disable()
profilers[ev.Id].Reset()
profilers[ev.Id].Enable()
case utrace.RegionEnd:
profilers[ev.Id].Disable()
logger.Printf("%d: Profiler %d disabled\n", p.Pid(), ev.Id)
nm := NamedMetrics{
Metrics: profilers[ev.Id].Metrics(),
Name: regionNames[regionIds[ev.Id]],
}
total = append(total, nm)
writer := immediate()
if writer != nil {
nm.WriteTo(writer)
}
}
}
err = prog.Continue(p, ws)
if err != nil {
return total, fmt.Errorf("trace-continue: %w", err)
}
}
return total, nil
}
func makeProfilers(pid, n int, attrs []*perf.Attr, groups [][]*perf.Attr, fa *perf.Attr) ([]Profiler, error) {
profilers := make([]Profiler, n)
for i := 0; i < n; i++ {
mprof, err := NewMultiProfiler(attrs, pid, perf.AnyCPU)
if err != nil {
return nil, fmt.Errorf("profiler: %w", err)
}
for _, gattrs := range groups {
gprof, err := NewGroupProfiler(gattrs, pid, perf.AnyCPU)
if err != nil {
return nil, fmt.Errorf("profiler: %w", err)
}
mprof.profilers = append(mprof.profilers, gprof)
}
profilers[i] = mprof
}
return profilers, nil
}