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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1.1 KiB
Go
Raw Normal View History

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
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
"github.com/hashicorp/boundary/internal/event"
2020-02-12 13:53:27 -05:00
)
func StartMemProfiler(ctx context.Context) {
const op = "base.StartMemProfiler"
2020-08-10 17:15:02 -04:00
profileDir := filepath.Join(os.TempDir(), "boundaryprof")
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()
event.WriteSysEvent(ctx, op, "wrote memory profile", "filename", filename)
2020-02-12 13:53:27 -05:00
time.Sleep(5 * time.Minute)
}
}()
}