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() {
}
obj.Flagstr("cpuprofile", "file: write cpu profile to file", &cpuprofile)
obj.Flagstr("memprofile", "file: write memory profile to file", &memprofile)
obj.Flagint64("memprofilerate", "set runtime.MemProfileRate", &memprofilerate)
obj.Flagparse(usage)
if flag_dynlink {
......
......@@ -3,6 +3,7 @@ package gc
import (
"cmd/internal/obj"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
......@@ -42,14 +43,6 @@ func plan9quote(s string) string {
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.
func stringsCompare(a, b string) int {
if a == b {
......@@ -76,8 +69,11 @@ func Exit(code int) {
os.Exit(code)
}
var cpuprofile string
var memprofile string
var (
cpuprofile string
memprofile string
memprofilerate int64
)
func startProfile() {
if cpuprofile != "" {
......@@ -91,11 +87,15 @@ func startProfile() {
AtExit(pprof.StopCPUProfile)
}
if memprofile != "" {
if memprofilerate != 0 {
runtime.MemProfileRate = int(memprofilerate)
}
f, err := os.Create(memprofile)
if err != nil {
Fatal("%v", err)
}
AtExit(func() {
runtime.GC() // profile all outstanding allocations
if err := pprof.WriteHeapProfile(f); err != nil {
Fatal("%v", err)
}
......
......@@ -140,6 +140,7 @@ func Ldmain() {
}
obj.Flagstr("cpuprofile", "file: write cpu profile to file", &cpuprofile)
obj.Flagstr("memprofile", "file: write memory profile to file", &memprofile)
obj.Flagint64("memprofilerate", "set runtime.MemProfileRate", &memprofilerate)
obj.Flagparse(usage)
startProfile()
Ctxt.Bso = &Bso
......
......@@ -11,6 +11,7 @@ import (
"io"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"time"
......@@ -300,8 +301,11 @@ func Exit(code int) {
os.Exit(code)
}
var cpuprofile string
var memprofile string
var (
cpuprofile string
memprofile string
memprofilerate int64
)
func startProfile() {
if cpuprofile != "" {
......@@ -315,11 +319,15 @@ func startProfile() {
AtExit(pprof.StopCPUProfile)
}
if memprofile != "" {
if memprofilerate != 0 {
runtime.MemProfileRate = int(memprofilerate)
}
f, err := os.Create(memprofile)
if err != nil {
log.Fatalf("%v", err)
}
AtExit(func() {
runtime.GC() // profile all outstanding allocations
if err := pprof.WriteHeapProfile(f); err != nil {
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