2022-05-09 10:16:10 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2021-09-27 16:24:00 -04:00
|
|
|
//go:build memprofiler
|
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 (
|
2022-05-09 10:16:10 -04:00
|
|
|
"context"
|
2020-02-12 13:53:27 -05:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
"runtime/pprof"
|
|
|
|
|
"time"
|
2022-05-09 10:16:10 -04:00
|
|
|
|
2023-09-12 08:15:34 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/event"
|
2020-02-12 13:53:27 -05:00
|
|
|
)
|
|
|
|
|
|
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 {
|
2022-05-09 10:16:10 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("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 {
|
2022-05-09 10:16:10 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("could not create memory profile"))
|
2020-02-12 13:53:27 -05:00
|
|
|
}
|
|
|
|
|
runtime.GC()
|
|
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
2022-05-09 10:16:10 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("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)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|