mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
The Go bootstrap toolchain requirement is now Go 1.17. We can finally delete all these pre-Go 1.17 workarounds. For #44505. Change-Id: I59d4dff1cde23da022892b5b6a116eb3dbad9ce4 Reviewed-on: https://go-review.googlesource.com/c/go/+/420903 Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Austin Clements <austin@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gc
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
tracepkg "runtime/trace"
|
|
|
|
"cmd/compile/internal/base"
|
|
)
|
|
|
|
func startProfile() {
|
|
if base.Flag.CPUProfile != "" {
|
|
f, err := os.Create(base.Flag.CPUProfile)
|
|
if err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
base.AtExit(pprof.StopCPUProfile)
|
|
}
|
|
if base.Flag.MemProfile != "" {
|
|
if base.Flag.MemProfileRate != 0 {
|
|
runtime.MemProfileRate = base.Flag.MemProfileRate
|
|
}
|
|
f, err := os.Create(base.Flag.MemProfile)
|
|
if err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
base.AtExit(func() {
|
|
// Profile all outstanding allocations.
|
|
runtime.GC()
|
|
// compilebench parses the memory profile to extract memstats,
|
|
// which are only written in the legacy pprof format.
|
|
// See golang.org/issue/18641 and runtime/pprof/pprof.go:writeHeap.
|
|
const writeLegacyFormat = 1
|
|
if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
})
|
|
} else {
|
|
// Not doing memory profiling; disable it entirely.
|
|
runtime.MemProfileRate = 0
|
|
}
|
|
if base.Flag.BlockProfile != "" {
|
|
f, err := os.Create(base.Flag.BlockProfile)
|
|
if err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
runtime.SetBlockProfileRate(1)
|
|
base.AtExit(func() {
|
|
pprof.Lookup("block").WriteTo(f, 0)
|
|
f.Close()
|
|
})
|
|
}
|
|
if base.Flag.MutexProfile != "" {
|
|
f, err := os.Create(base.Flag.MutexProfile)
|
|
if err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
runtime.SetMutexProfileFraction(1)
|
|
base.AtExit(func() {
|
|
pprof.Lookup("mutex").WriteTo(f, 0)
|
|
f.Close()
|
|
})
|
|
}
|
|
if base.Flag.TraceProfile != "" {
|
|
f, err := os.Create(base.Flag.TraceProfile)
|
|
if err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
if err := tracepkg.Start(f); err != nil {
|
|
base.Fatalf("%v", err)
|
|
}
|
|
base.AtExit(tracepkg.Stop)
|
|
}
|
|
}
|