2022-01-05 18:02:03 +00:00
|
|
|
//go:build memprofiler
|
2018-10-31 18:11:45 +00:00
|
|
|
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
memProfilerEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServerCommand) startMemProfiler() {
|
|
|
|
profileDir := filepath.Join(os.TempDir(), "vaultprof")
|
2021-04-08 16:43:39 +00:00
|
|
|
if err := os.MkdirAll(profileDir, 0o700); err != nil {
|
2018-10-31 18:11:45 +00:00
|
|
|
c.logger.Debug("could not create profile directory", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
filename := filepath.Join(profileDir, time.Now().UTC().Format("20060102_150405")) + ".pprof"
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Debug("could not create memory profile", "error", err)
|
|
|
|
}
|
|
|
|
runtime.GC()
|
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
|
|
c.logger.Debug("could not write memory profile", "error", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
c.logger.Debug("wrote memory profile", "filename", filename)
|
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|