]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/trace/v2: handle the -pprof flag
authorMichael Anthony Knyszek <mknyszek@google.com>
Thu, 11 Apr 2024 20:30:24 +0000 (20:30 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 11 Apr 2024 22:45:13 +0000 (22:45 +0000)
Turns out we ported all the profile generation, but forgot to actually
support the command line flags for them! This change fixes the issue by
handling the different kinds of profiles and writing them out to stdout.

Fixes #66782.

Change-Id: I7756fb4636ce8daaf11ed471be79c86ce3d463cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/578318
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/trace/v2/main.go

index 0a60ef04db0bccedb136bb61b9d786236e19c1cb..f2a54eea907b414cb748864ff7d0cfbb32bc712e 100644 (file)
@@ -28,6 +28,35 @@ func Main(traceFile, httpAddr, pprof string, debug int) error {
        }
        defer tracef.Close()
 
+       // Handle requests for profiles.
+       if pprof != "" {
+               parsed, err := parseTrace(tracef)
+               if err != nil {
+                       return err
+               }
+               var f traceviewer.ProfileFunc
+               switch pprof {
+               case "net":
+                       f = pprofByGoroutine(computePprofIO(), parsed)
+               case "sync":
+                       f = pprofByGoroutine(computePprofBlock(), parsed)
+               case "syscall":
+                       f = pprofByGoroutine(computePprofSyscall(), parsed)
+               case "sched":
+                       f = pprofByGoroutine(computePprofSched(), parsed)
+               default:
+                       return fmt.Errorf("unknown pprof type %s\n", pprof)
+               }
+               records, err := f(&http.Request{})
+               if err != nil {
+                       return fmt.Errorf("failed to generate pprof: %v\n", err)
+               }
+               if err := traceviewer.BuildProfile(records).Write(os.Stdout); err != nil {
+                       return fmt.Errorf("failed to generate pprof: %v\n", err)
+               }
+               return nil
+       }
+
        // Debug flags.
        switch debug {
        case 1: