Skip to content

Commit

Permalink
cmd/compile: extend profiling-per-package-into-directory to other pro…
Browse files Browse the repository at this point in the history
…filing flags

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 <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
dr2chase committed Nov 15, 2023
1 parent 0ce94d7 commit 2f5bd4e
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/cmd/compile/internal/gc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 2f5bd4e

Please sign in to comment.