This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
profile.go
292 lines (251 loc) · 7.37 KB
/
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
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
"slices"
"time"
pprof "github.com/google/pprof/profile"
"github.com/stealthrocket/timecraft/format"
"github.com/stealthrocket/timecraft/internal/print/human"
"github.com/stealthrocket/timecraft/internal/print/jsonprint"
"github.com/stealthrocket/timecraft/internal/print/yamlprint"
"github.com/stealthrocket/timecraft/internal/stream"
"github.com/stealthrocket/timecraft/internal/timecraft"
"github.com/stealthrocket/timecraft/internal/timemachine"
"github.com/stealthrocket/wzprof"
"github.com/tetratelabs/wazero/experimental"
"golang.org/x/exp/maps"
)
const profileUsage = `
Usage: timecraft profile [options] <process id>
The profile command provides the ability to generate performance profiles
from records of an execution timeline. The profiles can be scopped to a time
range of interest and written to files in the format understood by pprof.
For resources on how to use pprof, see:
- https://go.dev/blog/pprof
- https://jvns.ca/blog/2017/09/24/profiling-go-with-pprof/
Example:
$ timecraft profile --export memory:mem.out f6e9acbc-0543-47df-9413-b99f569cfa3b
==> writing memory profile to mem.out
...
$ go tool pprof -http :4040 mem.out
(web page opens in browser)
Options:
-c, --config path Path to the timecraft configuration file (overrides TIMECRAFTCONFIG)
-d, --duration duration Amount of time that the profiler will be running for (default to the process up time)
--export type:path Exports the generated profiles, type is one of cpu or memory (may be repeated)
-h, --help Show this usage information
-o, --output format Output format, one of: text, json, yaml
-q, --quiet Only display the profile ids
-t, --start-time time Time at which the profiler gets started (default to 1 minute)
`
func profile(ctx context.Context, args []string) error {
var (
exports = stringMap{}
output = outputFormat("text")
startTime = human.Time{}
duration = human.Duration(1 * time.Minute)
quiet = false
)
flagSet := newFlagSet("timecraft profile", profileUsage)
customVar(flagSet, &exports, "export")
customVar(flagSet, &output, "o", "output")
customVar(flagSet, &duration, "d", "duration")
customVar(flagSet, &startTime, "t", "start-time")
boolVar(flagSet, &quiet, "q", "quiet")
args, err := parseFlags(flagSet, args)
if err != nil {
return err
}
if len(args) != 1 {
return errors.New(`expected exactly one process id as argument`)
}
exportedProfileTypes := maps.Keys(exports)
slices.Sort(exportedProfileTypes)
for _, typ := range exportedProfileTypes {
switch typ {
case "cpu", "memory":
default:
return fmt.Errorf(`unsupported profile type: %s`, typ)
}
}
processID, err := parseProcessID(args[0])
if err != nil {
return err
}
config, err := timecraft.LoadConfig()
if err != nil {
return err
}
registry, err := timecraft.OpenRegistry(config)
if err != nil {
return err
}
runtime, err := timecraft.NewRuntime(ctx, config)
if err != nil {
return err
}
defer runtime.Close(ctx)
replay := timecraft.NewReplay(registry, runtime, processID)
recordReader, processStartTime, err := replay.RecordReader(ctx)
if err != nil {
return err
}
if startTime.IsZero() {
startTime = human.Time(processStartTime)
}
moduleCode, function, err := replay.ModuleCode(ctx)
if err != nil {
return err
}
p := wzprof.ProfilingFor(moduleCode)
records := &recordProfiler{
records: recordReader,
startTime: time.Time(startTime),
endTime: time.Time(startTime).Add(time.Duration(duration)),
sampleRate: 1.0,
// Enable profiling of time spent in host functions because we don't have
// any I/O wait during a replay, so it gives a useful perspective of the
// CPU time spent processing the host call invocation.
cpu: p.CPUProfiler(wzprof.HostTime(true)),
mem: p.MemoryProfiler(),
}
ctx = context.WithValue(ctx,
experimental.FunctionListenerFactoryKey{},
experimental.MultiFunctionListenerFactory(
wzprof.Flag(&records.started, records.cpu),
wzprof.Flag(&records.started, records.mem),
),
)
compiledModule, err := runtime.CompileModule(ctx, moduleCode)
if err != nil {
return err
}
defer compiledModule.Close(ctx)
err = p.Prepare(compiledModule)
if err != nil {
return err
}
if err := replay.ReplayRecordsModule(ctx, function, compiledModule, records); err != nil {
return err
}
records.stop()
desc, err := createProfiles(registry, processID, records.cpuProfile, records.memProfile)
if err != nil {
return err
}
for _, typ := range exportedProfileTypes {
var p *pprof.Profile
switch typ {
case "cpu":
p = records.cpuProfile
case "memory":
p = records.memProfile
}
if p != nil {
path := exports[typ]
perrorf("==> writing %s profile to %s", typ, path)
if err := wzprof.WriteProfile(path, p); err != nil {
return err
}
}
}
var writer stream.WriteCloser[*format.Descriptor]
switch output {
case "json":
writer = jsonprint.NewWriter[*format.Descriptor](os.Stdout)
case "yaml":
writer = yamlprint.NewWriter[*format.Descriptor](os.Stdout)
default:
writer = getProfiles(ctx, os.Stdout, registry, quiet)
}
defer writer.Close()
_, err = stream.Copy[*format.Descriptor](writer, stream.NewReader(desc...))
return err
}
type recordProfiler struct {
records stream.Reader[timemachine.Record]
cpu *wzprof.CPUProfiler
mem *wzprof.MemoryProfiler
cpuProfile *pprof.Profile
memProfile *pprof.Profile
firstTimestamp time.Time
lastTimestamp time.Time
startTime time.Time
endTime time.Time
started bool
stopped bool
sampleRate float64
}
func (r *recordProfiler) Read(records []timemachine.Record) (int, error) {
if len(records) == 0 {
return 0, nil
}
if r.stopped {
return 0, io.EOF
}
n, err := r.records.Read(records[:1])
if n > 0 {
r.lastTimestamp = records[0].Time
if !r.started && !r.lastTimestamp.Before(r.startTime) {
r.firstTimestamp = r.lastTimestamp
r.start()
}
if !r.stopped && !r.lastTimestamp.Before(r.endTime) {
r.stop()
}
}
return n, err
}
func (r *recordProfiler) start() {
if !r.started {
r.started = true
r.cpu.StartProfile()
}
}
func (r *recordProfiler) stop() {
if !r.stopped {
r.stopped = true
r.cpuProfile = r.cpu.StopProfile(r.sampleRate)
r.memProfile = r.mem.NewProfile(r.sampleRate)
r.cpuProfile.TimeNanos = r.startTime.UnixNano()
r.memProfile.TimeNanos = r.startTime.UnixNano()
duration := r.lastTimestamp.Sub(r.firstTimestamp)
r.cpuProfile.DurationNanos = int64(duration)
r.memProfile.DurationNanos = int64(duration)
}
}
func createProfiles(reg *timemachine.Registry, processID format.UUID, profiles ...*pprof.Profile) ([]*format.Descriptor, error) {
mapping := []*pprof.Mapping{{
ID: 1,
File: "module.wasm",
}}
ch := make(chan stream.Optional[*format.Descriptor])
for _, p := range profiles {
p.Mapping = mapping
profileType := "memory"
for _, sample := range p.SampleType {
if sample.Type == "cpu" || sample.Type == "samples" {
profileType = "cpu"
break
}
}
go func(profile *pprof.Profile) {
ch <- stream.Opt(reg.CreateProfile(context.TODO(), processID, profileType, profile))
}(p)
}
descriptors := make([]*format.Descriptor, 0, len(profiles))
var lastErr error
for range profiles {
d, err := (<-ch).Value()
if err != nil {
lastErr = err
} else {
descriptors = append(descriptors, d)
}
}
return descriptors, lastErr
}