mirror of
https://github.com/ipfs/kubo.git
synced 2025-05-17 15:06:47 +08:00
profile: add trace
This commit is contained in:
@ -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),
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"runtime/trace"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -27,6 +28,7 @@ const (
|
||||
CollectorCPU = "cpu"
|
||||
CollectorMutex = "mutex"
|
||||
CollectorBlock = "block"
|
||||
CollectorTrace = "trace"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -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 {
|
||||
@ -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()
|
||||
|
@ -22,6 +22,7 @@ func TestProfiler(t *testing.T) {
|
||||
CollectorCPU,
|
||||
CollectorMutex,
|
||||
CollectorBlock,
|
||||
CollectorTrace,
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
|
Reference in New Issue
Block a user