From 105053fa19eba7f9761e1fdf1007905051583d44 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Fri, 20 Mar 2020 15:52:43 -0400 Subject: [PATCH] [dev.link] cmd/link: record per-phase memory profile We already have an option to record per-phase CPU profiles. If we're in "mem" benchmark mode, then it also makes sense to collect a heap profile of the live heap at the end of a phase. This CL adds that profile and changes the extensions of the profiles to "cpuprof" and "memprof" to make the distinction clear. Change-Id: Ia05b7fa18bccad954a875f7a55d9cff5ad8dfaaf Reviewed-on: https://go-review.googlesource.com/c/go/+/224617 Run-TryBot: Austin Clements Reviewed-by: Than McIntosh Reviewed-by: Cherry Zhang TryBot-Result: Gobot Gobot --- src/cmd/link/internal/benchmark/bench.go | 25 ++++++++++++++++--- src/cmd/link/internal/benchmark/bench_test.go | 4 +-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/cmd/link/internal/benchmark/bench.go b/src/cmd/link/internal/benchmark/bench.go index 52f98269a1..7b8492ec85 100644 --- a/src/cmd/link/internal/benchmark/bench.go +++ b/src/cmd/link/internal/benchmark/bench.go @@ -116,7 +116,7 @@ func (m *Metrics) Start(name string) { m.curMark = &mark{name: name} // Unlikely we need to a GC here, as one was likely just done in closeMark. if m.shouldPProf() { - f, err := os.Create(makePProfFilename(m.filebase, name)) + f, err := os.Create(makePProfFilename(m.filebase, name, "cpuprof")) if err != nil { panic(err) } @@ -143,6 +143,25 @@ func (m *Metrics) closeMark() { pprof.StopCPUProfile() m.pprofFile.Close() m.pprofFile = nil + if m.gc == GC { + // Collect a profile of the live heap. Do a + // second GC to force sweep completion so we + // get a complete snapshot of the live heap at + // the end of this phase. + runtime.GC() + f, err := os.Create(makePProfFilename(m.filebase, m.curMark.name, "memprof")) + if err != nil { + panic(err) + } + err = pprof.WriteHeapProfile(f) + if err != nil { + panic(err) + } + err = f.Close() + if err != nil { + panic(err) + } + } } m.marks = append(m.marks, m.curMark) m.curMark = nil @@ -171,6 +190,6 @@ func makeBenchString(name string) string { return string(ret) } -func makePProfFilename(filebase, name string) string { - return fmt.Sprintf("%s_%s.profile", filebase, makeBenchString(name)) +func makePProfFilename(filebase, name, typ string) string { + return fmt.Sprintf("%s_%s.%s", filebase, makeBenchString(name), typ) } diff --git a/src/cmd/link/internal/benchmark/bench_test.go b/src/cmd/link/internal/benchmark/bench_test.go index 48d4d74046..d8ec717c7c 100644 --- a/src/cmd/link/internal/benchmark/bench_test.go +++ b/src/cmd/link/internal/benchmark/bench_test.go @@ -39,8 +39,8 @@ func TestPProfFlag(t *testing.T) { } func TestPProfNames(t *testing.T) { - want := "foo_BenchmarkTest.profile" - if v := makePProfFilename("foo", "test"); v != want { + want := "foo_BenchmarkTest.cpuprof" + if v := makePProfFilename("foo", "test", "cpuprof"); v != want { t.Errorf("makePProfFilename() == %q, want %q", v, want) } } -- 2.50.0