Skip to content

Commit

Permalink
profile: add trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo authored and hacdias committed Jan 12, 2024
1 parent 9343a95 commit fab6cfb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/commands/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ However, it could reveal:
profile.CollectorCPU,
profile.CollectorMutex,
profile.CollectorBlock,
profile.CollectorTrace,
}),
cmds.StringOption(profileTimeOption, "The amount of time spent profiling. If this is set to 0, then sampling profiles are skipped.").WithDefault("30s"),
cmds.IntOption(mutexProfileFractionOption, "The fraction 1/n of mutex contention events that are reported in the mutex profile.").WithDefault(4),
Expand Down
16 changes: 16 additions & 0 deletions profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"runtime"
"runtime/pprof"
"runtime/trace"
"sync"
"time"

Expand All @@ -27,6 +28,7 @@ const (
CollectorCPU = "cpu"
CollectorMutex = "mutex"
CollectorBlock = "block"
CollectorTrace = "trace"
)

var (
Expand Down Expand Up @@ -98,6 +100,11 @@ var collectors = map[string]collector{
collectFunc: blockProfile,
enabledFunc: func(opts Options) bool { return opts.ProfileDuration > 0 && opts.BlockProfileRate > 0 },
},
CollectorTrace: {
outputFile: "trace",
collectFunc: captureTrace,
enabledFunc: func(opts Options) bool { return opts.ProfileDuration > 0 },
},
}

type Options struct {
Expand Down Expand Up @@ -266,6 +273,15 @@ func profileCPU(ctx context.Context, opts Options, w io.Writer) error {
return waitOrCancel(ctx, opts.ProfileDuration)
}

func captureTrace(ctx context.Context, opts Options, w io.Writer) error {
err := trace.Start(w)
if err != nil {
return err
}
defer trace.Stop()
return waitOrCancel(ctx, opts.ProfileDuration)
}

func waitOrCancel(ctx context.Context, d time.Duration) error {
timer := time.NewTimer(d)
defer timer.Stop()
Expand Down
1 change: 1 addition & 0 deletions profile/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestProfiler(t *testing.T) {
CollectorCPU,
CollectorMutex,
CollectorBlock,
CollectorTrace,
}

cases := []struct {
Expand Down

0 comments on commit fab6cfb

Please sign in to comment.