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 <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Findley 2023-03-20 17:30:49 -04:00
parent 3aa7ada607
commit 8fce59eab5
3 changed files with 19 additions and 2 deletions

View File

@ -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.

View File

@ -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

View File

@ -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)