Commit 00d4a6b3 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/internal/gc, cmd/internal/ld: add memprofilerate flag

Also call runtime.GC before exit to ensure
that the profiler picks up all allocations.

Fixes #10537.

Change-Id: Ibfbfc88652ac0ce30a6d1ae392f919df6c1e8126
Reviewed-on: https://go-review.googlesource.com/9261Reviewed-by: 's avatarDave Cheney <dave@cheney.net>
Reviewed-by: 's avatarMinux Ma <minux@golang.org>
Run-TryBot: Minux Ma <minux@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 23ce80ef
...@@ -233,6 +233,7 @@ func Main() { ...@@ -233,6 +233,7 @@ func Main() {
} }
obj.Flagstr("cpuprofile", "file: write cpu profile to file", &cpuprofile) obj.Flagstr("cpuprofile", "file: write cpu profile to file", &cpuprofile)
obj.Flagstr("memprofile", "file: write memory profile to file", &memprofile) obj.Flagstr("memprofile", "file: write memory profile to file", &memprofile)
obj.Flagint64("memprofilerate", "set runtime.MemProfileRate", &memprofilerate)
obj.Flagparse(usage) obj.Flagparse(usage)
if flag_dynlink { if flag_dynlink {
......
...@@ -3,6 +3,7 @@ package gc ...@@ -3,6 +3,7 @@ package gc
import ( import (
"cmd/internal/obj" "cmd/internal/obj"
"os" "os"
"runtime"
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
"strings" "strings"
...@@ -42,14 +43,6 @@ func plan9quote(s string) string { ...@@ -42,14 +43,6 @@ func plan9quote(s string) string {
return s return s
} }
// simulation of int(*s++) in C
func intstarstringplusplus(s string) (int, string) {
if s == "" {
return 0, ""
}
return int(s[0]), s[1:]
}
// strings.Compare, introduced in Go 1.5. // strings.Compare, introduced in Go 1.5.
func stringsCompare(a, b string) int { func stringsCompare(a, b string) int {
if a == b { if a == b {
...@@ -76,8 +69,11 @@ func Exit(code int) { ...@@ -76,8 +69,11 @@ func Exit(code int) {
os.Exit(code) os.Exit(code)
} }
var cpuprofile string var (
var memprofile string cpuprofile string
memprofile string
memprofilerate int64
)
func startProfile() { func startProfile() {
if cpuprofile != "" { if cpuprofile != "" {
...@@ -91,11 +87,15 @@ func startProfile() { ...@@ -91,11 +87,15 @@ func startProfile() {
AtExit(pprof.StopCPUProfile) AtExit(pprof.StopCPUProfile)
} }
if memprofile != "" { if memprofile != "" {
if memprofilerate != 0 {
runtime.MemProfileRate = int(memprofilerate)
}
f, err := os.Create(memprofile) f, err := os.Create(memprofile)
if err != nil { if err != nil {
Fatal("%v", err) Fatal("%v", err)
} }
AtExit(func() { AtExit(func() {
runtime.GC() // profile all outstanding allocations
if err := pprof.WriteHeapProfile(f); err != nil { if err := pprof.WriteHeapProfile(f); err != nil {
Fatal("%v", err) Fatal("%v", err)
} }
......
...@@ -140,6 +140,7 @@ func Ldmain() { ...@@ -140,6 +140,7 @@ func Ldmain() {
} }
obj.Flagstr("cpuprofile", "file: write cpu profile to file", &cpuprofile) obj.Flagstr("cpuprofile", "file: write cpu profile to file", &cpuprofile)
obj.Flagstr("memprofile", "file: write memory profile to file", &memprofile) obj.Flagstr("memprofile", "file: write memory profile to file", &memprofile)
obj.Flagint64("memprofilerate", "set runtime.MemProfileRate", &memprofilerate)
obj.Flagparse(usage) obj.Flagparse(usage)
startProfile() startProfile()
Ctxt.Bso = &Bso Ctxt.Bso = &Bso
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"io" "io"
"log" "log"
"os" "os"
"runtime"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"time" "time"
...@@ -300,8 +301,11 @@ func Exit(code int) { ...@@ -300,8 +301,11 @@ func Exit(code int) {
os.Exit(code) os.Exit(code)
} }
var cpuprofile string var (
var memprofile string cpuprofile string
memprofile string
memprofilerate int64
)
func startProfile() { func startProfile() {
if cpuprofile != "" { if cpuprofile != "" {
...@@ -315,11 +319,15 @@ func startProfile() { ...@@ -315,11 +319,15 @@ func startProfile() {
AtExit(pprof.StopCPUProfile) AtExit(pprof.StopCPUProfile)
} }
if memprofile != "" { if memprofile != "" {
if memprofilerate != 0 {
runtime.MemProfileRate = int(memprofilerate)
}
f, err := os.Create(memprofile) f, err := os.Create(memprofile)
if err != nil { if err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }
AtExit(func() { AtExit(func() {
runtime.GC() // profile all outstanding allocations
if err := pprof.WriteHeapProfile(f); err != nil { if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment