Skip to content

Commit

Permalink
cmd/go: add a -debug-runtime-trace flag
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
findleyr committed Mar 21, 2023
1 parent 3aa7ada commit 8fce59e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/cmd/go/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/cmd/go/internal/work/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"path/filepath"
"runtime"
rtrace "runtime/trace"
"strings"

"cmd/go/internal/base"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8fce59e

Please sign in to comment.