]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.22] 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>
Wed, 24 Jul 2024 16:45:54 +0000 (16: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.

For #66782
For #68542
For #68546

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>
(cherry picked from commit e14aad1faf6f511e342d124681bc0146e426f9aa)
Reviewed-on: https://go-review.googlesource.com/c/go/+/600255
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.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: