From 8fce59eab5cb2facfafca89e047b4b43ba44785f Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Mon, 20 Mar 2023 17:30:49 -0400 Subject: [PATCH] cmd/go: add a -debug-runtime-trace flag The runtime/trace package proved useful for investigating go command performance, and it makes sense (to me) to make this available for development behind an undocumented flag, at the cost of ~25KB of binary size. We could of course futher hide this functionality behind an experiment or build tag, if necessary. Updates #59157 Change-Id: I612320920ca935f1ee10bb6a803b7952f36c939b Reviewed-on: https://go-review.googlesource.com/c/go/+/477896 Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot Run-TryBot: Robert Findley --- src/cmd/go/internal/cfg/cfg.go | 5 +++-- src/cmd/go/internal/work/build.go | 1 + src/cmd/go/main.go | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 2037e7cf06..ed7bb6c4bb 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -97,8 +97,9 @@ var ( CmdName string // "build", "install", "list", "mod tidy", etc. - DebugActiongraph string // -debug-actiongraph flag (undocumented, unstable) - DebugTrace string // -debug-trace flag + DebugActiongraph string // -debug-actiongraph flag (undocumented, unstable) + DebugTrace string // -debug-trace flag + DebugRuntimeTrace string // -debug-runtime-trace flag (undocumented, unstable) // GoPathError is set when GOPATH is not set. it contains an // explanation why GOPATH is unset. diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 5a1915afb5..ac9718bfb3 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -332,6 +332,7 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) { // Undocumented, unstable debugging flags. cmd.Flag.StringVar(&cfg.DebugActiongraph, "debug-actiongraph", "", "") cmd.Flag.StringVar(&cfg.DebugTrace, "debug-trace", "", "") + cmd.Flag.StringVar(&cfg.DebugRuntimeTrace, "debug-runtime-trace", "", "") } // AddCoverFlags adds coverage-related flags to "cmd". If the diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go index 8d1c3c0e8b..6d3d5d4059 100644 --- a/src/cmd/go/main.go +++ b/src/cmd/go/main.go @@ -16,6 +16,7 @@ import ( "os" "path/filepath" "runtime" + rtrace "runtime/trace" "strings" "cmd/go/internal/base" @@ -220,6 +221,20 @@ func invoke(cmd *base.Command, args []string) { cmd.Flag.Parse(args[1:]) args = cmd.Flag.Args() } + + if cfg.DebugRuntimeTrace != "" { + f, err := os.Create(cfg.DebugRuntimeTrace) + if err != nil { + base.Fatalf("creating trace file: %v", err) + } + if err := rtrace.Start(f); err != nil { + base.Fatalf("starting event trace: %v", err) + } + defer func() { + rtrace.Stop() + }() + } + ctx := maybeStartTrace(context.Background()) ctx, span := trace.StartSpan(ctx, fmt.Sprint("Running ", cmd.Name(), " command")) cmd.Run(ctx, cmd, args) -- 2.48.1