-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprof.go
44 lines (37 loc) · 1.09 KB
/
pprof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: LGPL-3.0-only
// pprof provides utilities for `runtime/pprof`.
//
// References:
// - <https://pkg.go.dev/runtime/pprof>
// - <https://github.com/google/pprof/blob/main/doc/README.md>
package pprof
import (
"io"
"os"
"runtime/pprof"
"github.com/cobratbq/goutils/std/errors"
io_ "github.com/cobratbq/goutils/std/io"
)
func StartCPUProfilingFileBacked(fileName string) (io.Closer, error) {
dest, fileErr := os.Create(fileName)
if fileErr != nil {
return nil, errors.Context(fileErr, "failed to open file for file-backed profiling")
}
profilerCloser, profilerErr := StartCPUProfiling(dest)
if profilerErr != nil {
dest.Close()
return nil, errors.Context(profilerErr, "failed to start CPU profiler (with file-backing)")
}
return io_.NewCloseSequence(profilerCloser, dest), nil
}
func StartCPUProfiling(output io.Writer) (io.Closer, error) {
if err := pprof.StartCPUProfile(output); err != nil {
return nil, err
}
return cpuProfilerCloser{}, nil
}
type cpuProfilerCloser struct{}
func (cpuProfilerCloser) Close() error {
pprof.StopCPUProfile()
return nil
}