Files
threshold/internal/cmd/base/profiling_on.go
T

38 lines
878 B
Go
Raw Normal View History

2020-02-12 13:53:27 -05:00
// +build memprofiler
package base
2020-02-12 13:53:27 -05:00
import (
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
"github.com/hashicorp/go-hclog"
2020-02-12 13:53:27 -05:00
)
func StartMemProfiler(logger hclog.Logger) {
2020-08-10 17:15:02 -04:00
profileDir := filepath.Join(os.TempDir(), "boundaryprof")
if err := os.MkdirAll(profileDir, 0o700); err != nil {
logger.Debug("could not create profile directory", "error", err)
2020-02-12 13:53:27 -05:00
return
}
go func() {
for {
filename := filepath.Join(profileDir, time.Now().UTC().Format("20060102_150405")) + ".pprof"
f, err := os.Create(filename)
if err != nil {
logger.Debug("could not create memory profile", "error", err)
2020-02-12 13:53:27 -05:00
}
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
logger.Debug("could not write memory profile", "error", err)
2020-02-12 13:53:27 -05:00
}
f.Close()
logger.Debug("wrote memory profile", "filename", filename)
2020-02-12 13:53:27 -05:00
time.Sleep(5 * time.Minute)
}
}()
}