]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add a -debug-runtime-trace flag
authorRobert Findley <rfindley@google.com>
Mon, 20 Mar 2023 21:30:49 +0000 (17:30 -0400)
committerRobert Findley <rfindley@google.com>
Tue, 21 Mar 2023 21:15:43 +0000 (21:15 +0000)
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>

src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/work/build.go
src/cmd/go/main.go

index 2037e7cf063bc3b6809c2e619e86fa8b6c57e410..ed7bb6c4bbfe5ecd06e35da9e8288b92f644df26 100644 (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.
index 5a1915afb58e5f7465bca4a4028c8b663478ca5f..ac9718bfb3b1660195261fd618af32ef041fdd00 100644 (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
index 8d1c3c0e8ba204d52ed40eb5f41168be9703b6a8..6d3d5d405930d850978dc27b852f6d8a69eb798d 100644 (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)