Skip to content

Commit 6113dac

Browse files
committed
cmd/compile: make -memprofilerate work
There are multiple things preventing the -memprofilerate flag from working right now: - CmdFlags.MemProfileRate has type int64, which is currently not supported by the compiler's reflection-based registerFlags. Unfortunately, rather than letting you know this, registerFlags simply ignores this field. - Nothing consumes CmdFlags.MemProfileRate anyway. startProfile instead uses a package-local memprofilerate variable that is never set to anything. Fix this by making CmdFlags.MemProfileRate an int (that's what runtime.MemProfileRate is anyway) and using it in startProfile. While we're here, prevent similar flag parsing bugs in the future by making registerFlags panic if it encounters a flag field of unsupported type. Change-Id: Ib9a1fcd8f4c5e9d7175a4fabc375f31e79774f9a Reviewed-on: https://go-review.googlesource.com/c/go/+/359955 Trust: Austin Clements <austin@google.com> Trust: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Reviewed-by: David Chase <drchase@google.com>
1 parent f582778 commit 6113dac

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/cmd/compile/internal/base/flag.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type CmdFlags struct {
109109
Live CountFlag "help:\"debug liveness analysis\""
110110
MSan bool "help:\"build code compatible with C/C++ memory sanitizer\""
111111
MemProfile string "help:\"write memory profile to `file`\""
112-
MemProfileRate int64 "help:\"set runtime.MemProfileRate to `rate`\""
112+
MemProfileRate int "help:\"set runtime.MemProfileRate to `rate`\""
113113
MutexProfile string "help:\"write mutex profile to `file`\""
114114
NoLocalImports bool "help:\"reject local (relative) imports\""
115115
Pack bool "help:\"write to file.a instead of file.o\""
@@ -330,6 +330,8 @@ func registerFlags() {
330330
case funcType:
331331
f := v.Field(i).Interface().(func(string))
332332
objabi.Flagfn1(name, help, f)
333+
default:
334+
panic(fmt.Sprintf("base.Flag.%s has unexpected type %s", f.Name, f.Type))
333335
}
334336
}
335337
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import (
1212
"cmd/compile/internal/base"
1313
)
1414

15-
var (
16-
memprofilerate int64
17-
traceHandler func(string)
18-
)
15+
var traceHandler func(string)
1916

2017
func startProfile() {
2118
if base.Flag.CPUProfile != "" {
@@ -29,8 +26,8 @@ func startProfile() {
2926
base.AtExit(pprof.StopCPUProfile)
3027
}
3128
if base.Flag.MemProfile != "" {
32-
if memprofilerate != 0 {
33-
runtime.MemProfileRate = int(memprofilerate)
29+
if base.Flag.MemProfileRate != 0 {
30+
runtime.MemProfileRate = base.Flag.MemProfileRate
3431
}
3532
f, err := os.Create(base.Flag.MemProfile)
3633
if err != nil {

0 commit comments

Comments
 (0)