Take a profile if > 5GB mem usage

This commit is contained in:
Abhishek Pandey 2023-11-17 17:49:07 -08:00
parent 022ffd56b6
commit 6aac9b521f

View File

@ -15,7 +15,11 @@ import (
"github.com/pkg/profile" "github.com/pkg/profile"
) )
var profileTicker = time.NewTicker(120 * time.Second) var profileTicker = time.NewTicker(1 * time.Second)
var perMinuteMap = make(map[time.Time]int)
//var profileTicker = time.NewTicker(120 * time.Second)
var printTicker = time.NewTicker(1 * time.Second) var printTicker = time.NewTicker(1 * time.Second)
var profileCounter = 0 var profileCounter = 0
@ -27,6 +31,12 @@ func main() {
for { for {
select { select {
case <-profileTicker.C: case <-profileTicker.C:
var m runtime.MemStats
runtime.ReadMemStats(&m)
// if mem > 5GB and we havent captured a profile this min, capture it
t := time.Now().Truncate(time.Minute)
if m.HeapAlloc > 5*1024*1024*1024 && perMinuteMap[t] == 0 {
filename := "mem." + strconv.Itoa(profileCounter) + ".pprof" filename := "mem." + strconv.Itoa(profileCounter) + ".pprof"
f, _ := os.Create(filename) f, _ := os.Create(filename)
@ -37,6 +47,8 @@ func main() {
f.Close() f.Close()
profileCounter++ profileCounter++
perMinuteMap[t] = 1
}
} }
} }