]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: extend profiling-per-package-into-directory to other profiling flags
authorDavid Chase <drchase@google.com>
Wed, 15 Nov 2023 19:31:30 +0000 (14:31 -0500)
committerDavid Chase <drchase@google.com>
Wed, 15 Nov 2023 21:42:06 +0000 (21:42 +0000)
Also allow specification of "directory" with a trailing
path separator on the name.  Updated suffix ".mprof" to ".memprof",
others are similarly disambiguated.

Change-Id: I2f3f44a436893730dbfe70b6815dff1e74885404
Reviewed-on: https://go-review.googlesource.com/c/go/+/542715
Run-TryBot: David Chase <drchase@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/gc/util.go

index 5615d60b57f6b5f8b2f02c345681a81abfafadb2..b82a983d9feaf211bbaa2073bc38177a79d2f44c 100644 (file)
@@ -11,13 +11,28 @@ import (
        "runtime"
        "runtime/pprof"
        tracepkg "runtime/trace"
+       "strings"
 
        "cmd/compile/internal/base"
 )
 
+func profileName(fn, suffix string) string {
+       if strings.HasSuffix(fn, string(os.PathSeparator)) {
+               err := os.MkdirAll(fn, 0755)
+               if err != nil {
+                       base.Fatalf("%v", err)
+               }
+       }
+       if fi, statErr := os.Stat(fn); statErr == nil && fi.IsDir() {
+               fn = filepath.Join(fn, url.PathEscape(base.Ctxt.Pkgpath)+suffix)
+       }
+       return fn
+}
+
 func startProfile() {
        if base.Flag.CPUProfile != "" {
-               f, err := os.Create(base.Flag.CPUProfile)
+               fn := profileName(base.Flag.CPUProfile, ".cpuprof")
+               f, err := os.Create(fn)
                if err != nil {
                        base.Fatalf("%v", err)
                }
@@ -40,8 +55,14 @@ func startProfile() {
                // gzipFormat is what most people want, otherwise
                var format = textFormat
                fn := base.Flag.MemProfile
+               if strings.HasSuffix(fn, string(os.PathSeparator)) {
+                       err := os.MkdirAll(fn, 0755)
+                       if err != nil {
+                               base.Fatalf("%v", err)
+                       }
+               }
                if fi, statErr := os.Stat(fn); statErr == nil && fi.IsDir() {
-                       fn = filepath.Join(fn, url.PathEscape(base.Ctxt.Pkgpath)+".mprof")
+                       fn = filepath.Join(fn, url.PathEscape(base.Ctxt.Pkgpath)+".memprof")
                        format = gzipFormat
                }
 
@@ -62,7 +83,7 @@ func startProfile() {
                runtime.MemProfileRate = 0
        }
        if base.Flag.BlockProfile != "" {
-               f, err := os.Create(base.Flag.BlockProfile)
+               f, err := os.Create(profileName(base.Flag.BlockProfile, ".blockprof"))
                if err != nil {
                        base.Fatalf("%v", err)
                }
@@ -73,7 +94,7 @@ func startProfile() {
                })
        }
        if base.Flag.MutexProfile != "" {
-               f, err := os.Create(base.Flag.MutexProfile)
+               f, err := os.Create(profileName(base.Flag.MutexProfile, ".mutexprof"))
                if err != nil {
                        base.Fatalf("%v", err)
                }
@@ -84,7 +105,7 @@ func startProfile() {
                })
        }
        if base.Flag.TraceProfile != "" {
-               f, err := os.Create(base.Flag.TraceProfile)
+               f, err := os.Create(profileName(base.Flag.TraceProfile, ".trace"))
                if err != nil {
                        base.Fatalf("%v", err)
                }