profile: add trace
Jorropo committed
Nov 30, 2023 at 11:49 UTC
2738b49c1a22b742993f424725aa378fc53deec1
3 files changed
+18
core/commands/profile.go
+1
@@ -85,6 +85,7 @@ However, it could reveal:
85
profile.CollectorCPU,
86
profile.CollectorMutex,
87
profile.CollectorBlock,
88
+ profile.CollectorTrace,
89
}),
90
cmds.StringOption(profileTimeOption, "The amount of time spent profiling. If this is set to 0, then sampling profiles are skipped.").WithDefault("30s"),
91
cmds.IntOption(mutexProfileFractionOption, "The fraction 1/n of mutex contention events that are reported in the mutex profile.").WithDefault(4),
profile/profile.go
+16
@@ -10,6 +10,7 @@ import (
10
"os"
11
"runtime"
12
"runtime/pprof"
13
+ "runtime/trace"
14
"sync"
15
"time"
16
@@ -27,6 +28,7 @@ const (
28
CollectorCPU = "cpu"
29
CollectorMutex = "mutex"
30
CollectorBlock = "block"
31
+ CollectorTrace = "trace"
32
)
33
34
var (
@@ -98,6 +100,11 @@ var collectors = map[string]collector{
100
collectFunc: blockProfile,
101
enabledFunc: func(opts Options) bool { return opts.ProfileDuration > 0 && opts.BlockProfileRate > 0 },
102
},
103
+ CollectorTrace: {
104
+ outputFile: "trace",
105
+ collectFunc: captureTrace,
106
+ enabledFunc: func(opts Options) bool { return opts.ProfileDuration > 0 },
107
+ },
108
}
109
110
type Options struct {
@@ -266,6 +273,15 @@ func profileCPU(ctx context.Context, opts Options, w io.Writer) error {
273
return waitOrCancel(ctx, opts.ProfileDuration)
274
}
275
276
+func captureTrace(ctx context.Context, opts Options, w io.Writer) error {
277
+ err := trace.Start(w)
278
+ if err != nil {
279
+ return err
280
+ }
281
+ defer trace.Stop()
282
+ return waitOrCancel(ctx, opts.ProfileDuration)
283
+}
284
+
285
func waitOrCancel(ctx context.Context, d time.Duration) error {
286
timer := time.NewTimer(d)
287
defer timer.Stop()
profile/profile_test.go
+1
@@ -22,6 +22,7 @@ func TestProfiler(t *testing.T) {
22
CollectorCPU,
23
CollectorMutex,
24
CollectorBlock,
25
+ CollectorTrace,
26
}
27
28
cases := []struct {