Skip to content

Commit

Permalink
Added flag for periodic GC invocation to reduce overall memory consum…
Browse files Browse the repository at this point in the history
…ption in pooling scenarios (db1000nx100) (#565)

Co-authored-by: Ivan Shekelman <ivan.shekelman@proteantecs.com>
  • Loading branch information
millfreedom and Ivan Shekelman authored Aug 13, 2022
1 parent 636d61b commit 13f4584
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"net/http"
pprofhttp "net/http/pprof"
"os"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -60,6 +61,8 @@ func main() {
"possible values are: json, console, simple\n"+
"simple is the most human readable format if you only look at the output in your terminal")
lessStats := flag.Bool("less-stats", utils.GetEnvBoolDefault("LESS_STATS", false), "group target stats by protocols - in case you have too many targets")
periodicGCEnabled := flag.Bool("periodic-gc", utils.GetEnvBoolDefault("PERIODIC_GC", false),
"set to true if you want to run periodic garbage collection(useful in pooling scenarios, like db1000nx100)")

flag.Parse()

Expand Down Expand Up @@ -89,6 +92,7 @@ func main() {
logger.Warn("failed to increase rlimit", zap.Error(err))
}

go periodicGC(periodicGCEnabled, runnerConfigOptions.RefreshTimeout, logger)
go ota.WatchUpdates(logger, otaConfig)
setUpPprof(logger, *pprof, *debug)
rand.Seed(time.Now().UnixNano())
Expand All @@ -104,6 +108,27 @@ func main() {
job.NewRunner(runnerConfigOptions, jobsGlobalConfig, reporter).Run(ctx, logger)
}

func periodicGC(enabled *bool, period time.Duration, log *zap.Logger) {
if !*enabled {
return
}
var m runtime.MemStats
for {
<-time.After(period)
runtime.ReadMemStats(&m)
memBefore := m.Alloc
start := time.Now()
runtime.GC()
runtime.ReadMemStats(&m)
log.Info("GC finished",
zap.Duration("GC took(Sec)", time.Since(start)),
zap.Uint64("previous(MiB)", utils.ToMiB(memBefore)),
zap.Uint64("current(MiB)", utils.ToMiB(m.Alloc)),
zap.Uint64("recovered(MiB)", utils.ToMiB(memBefore-m.Alloc)),
)
}
}

func newZapLogger(debug bool, logLevel string, logFormat string) (*zap.Logger, error) {
cfg := zap.NewProductionConfig()
if debug {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,7 @@ func Unmarshal(input []byte, output any, format string) error {

return nil
}

func ToMiB(bytes uint64) uint64 {
return bytes / 1048576
}

0 comments on commit 13f4584

Please sign in to comment.