2020-02-12 13:53:27 -05:00
|
|
|
// +build memprofiler
|
|
|
|
|
|
2020-03-19 15:17:40 -04:00
|
|
|
package base
|
2020-02-12 13:53:27 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
"runtime/pprof"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-01 14:41:13 -04:00
|
|
|
func StartMemProfiler(ctx context.Context) {
|
|
|
|
|
const op = "base.StartMemProfiler"
|
2020-08-10 17:15:02 -04:00
|
|
|
profileDir := filepath.Join(os.TempDir(), "boundaryprof")
|
2021-01-07 10:02:32 -05:00
|
|
|
if err := os.MkdirAll(profileDir, 0o700); err != nil {
|
2021-08-04 13:56:54 -07:00
|
|
|
event.WriteError(ctx, op, err, "could not create profile directory")
|
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 {
|
2021-08-01 14:41:13 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfo("could not create memory profile"))
|
2020-02-12 13:53:27 -05:00
|
|
|
}
|
|
|
|
|
runtime.GC()
|
|
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
2021-08-02 09:58:58 -07:00
|
|
|
event.WriteError(ctx, op, err, "could not write memory profile")
|
2020-02-12 13:53:27 -05:00
|
|
|
}
|
|
|
|
|
f.Close()
|
2021-08-01 14:41:13 -04:00
|
|
|
event.WriteSysEvent(ctx, op, "wrote memory profile", "filename", filename)
|
2020-02-12 13:53:27 -05:00
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|