Add cpu and mem profiling options

Following https://golang.org/pkg/runtime/pprof/. When attempting to build
images in https://github.com/linuxkit/kubernetes CI the process is mysteriously
being SIGKILL'd, which I think might be down to OOMing due to the resource
limits placed on the build container.

I haven't done so yet but I'm intending to use these options to investigate and
they seem potentially useful in any case, even if this turns out to be a
red-herring.

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell 2017-12-06 15:54:18 +00:00
parent 656bd87fd2
commit 9558740c11

View File

@ -5,6 +5,8 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"github.com/moby/tool/src/moby"
log "github.com/sirupsen/logrus"
@ -60,6 +62,8 @@ func main() {
}
flagQuiet := flag.Bool("q", false, "Quiet execution")
flagVerbose := flag.Bool("v", false, "Verbose execution")
flagCPUProfile := flag.String("cpuprofile", "", "write cpu profile to `file`")
flagMemProfile := flag.String("memprofile", "", "write mem profile to `file`")
// config and cache directory
flagConfigDir := flag.String("config", defaultMobyConfigDir(), "Configuration directory")
@ -101,6 +105,17 @@ func main() {
}
moby.MobyDir = mobyDir
if *flagCPUProfile != "" {
f, err := os.Create(*flagCPUProfile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
switch args[0] {
case "build":
build(args[1:])
@ -113,4 +128,16 @@ func main() {
flag.Usage()
os.Exit(1)
}
if *flagMemProfile != "" {
f, err := os.Create(*flagMemProfile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}