From 257f4cb9050d29a38a992b814bd6e79e6f1bca99 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 16 Dec 2020 13:31:37 +0100 Subject: pkg/cmdprof: merge into pkg/tool cmdprof functionality seems to fit well into pkg/tool. --- pkg/tool/cmdprof.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pkg/tool/cmdprof.go (limited to 'pkg/tool/cmdprof.go') 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 +} -- cgit mrf-deployment