]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/gc, cmd/internal/ld: add memprofilerate flag
authorJosh Bleecher Snyder <josharian@gmail.com>
Thu, 23 Apr 2015 00:53:32 +0000 (17:53 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 27 Apr 2015 22:21:40 +0000 (22:21 +0000)
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/9261
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Minux Ma <minux@golang.org>
Run-TryBot: Minux Ma <minux@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/internal/gc/lex.go
src/cmd/internal/gc/util.go
src/cmd/internal/ld/pobj.go
src/cmd/internal/ld/util.go

index 5600d90df2b65df24fc79d5f26c8f99de0ba4b16..e055894f793094be8c797002835a1b8e4e0bd0c1 100644 (file)
@@ -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 {
index 7b593dc42c49ab9b1fde1dd9423ba5a29e0acc44..5dc6561b48c50053709e37a5e3ab349aa2ebe140 100644 (file)
@@ -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)
                        }
index c56e20eadc33fc61678179516585cbe3ef339b12..dbfe8a044afe2b44351076bd325831f2957e8f7b 100644 (file)
@@ -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
index 3461ae862c8ed700aea03f7e331fe9cc7764e4ea..ea6ca1589c9f516da2a4a14823f715f71f294e3c 100644 (file)
@@ -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)
                        }