diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-12-16 13:31:37 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-12-25 10:12:41 +0100 |
| commit | 257f4cb9050d29a38a992b814bd6e79e6f1bca99 (patch) | |
| tree | 4f52317a4eb614821de2b930282e03d9f117535d /pkg/tool/cmdprof.go | |
| parent | 3bcdec13657598f6a6163c7ddecff58c2d3a2a71 (diff) | |
pkg/cmdprof: merge into pkg/tool
cmdprof functionality seems to fit well into pkg/tool.
Diffstat (limited to 'pkg/tool/cmdprof.go')
| -rw-r--r-- | pkg/tool/cmdprof.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/pkg/tool/cmdprof.go b/pkg/tool/cmdprof.go new file mode 100644 index 000000000..196a0cd24 --- /dev/null +++ b/pkg/tool/cmdprof.go @@ -0,0 +1,44 @@ +// Copyright 2020 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package tool + +import ( + "os" + "runtime" + "runtime/pprof" +) + +// installProfiling simplifies cpu/memory profiling for command line tools. +func installProfiling(cpuprof, memprof string) func() { + res := func() {} + if cpuprof != "" { + f, err := os.Create(cpuprof) + if err != nil { + Failf("failed to create cpuprofile file: %v", err) + } + if err := pprof.StartCPUProfile(f); err != nil { + Failf("failed to start cpu profile: %v", err) + } + res = func() { + pprof.StopCPUProfile() + f.Close() + } + } + if memprof != "" { + prev := res + res = func() { + prev() + f, err := os.Create(memprof) + if err != nil { + Failf("failed to create memprofile file: %v", err) + } + defer f.Close() + runtime.GC() + if err := pprof.WriteHeapProfile(f); err != nil { + Failf("failed to write mem profile: %v", err) + } + } + } + return res +} |
