From eaa20ffaf0e59b54383172b1f27fc2d897db5275 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 2 Jul 2019 22:23:47 +0000 Subject: [PATCH] Add optional "Heap ballast" to alter GC behaviour. This allows the heap size to be made bigger, to make Go's GC run less often and use less CPU. See https://github.com/golang/go/issues/23044 Signed-off-by: Bryan Boreham --- cmd/cortex/main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/cortex/main.go b/cmd/cortex/main.go index 689d2f6c5ba..8ab0cd44b32 100644 --- a/cmd/cortex/main.go +++ b/cmd/cortex/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "os" + "runtime" "github.com/go-kit/kit/log/level" "github.com/pkg/errors" @@ -27,9 +28,11 @@ func main() { cfg cortex.Config configFile = "" eventSampleRate int + ballastBytes int ) flag.StringVar(&configFile, "config.file", "", "Configuration file to load.") flag.IntVar(&eventSampleRate, "event.sample-rate", 0, "How often to sample observability events (0 = never).") + flag.IntVar(&ballastBytes, "mem-ballast-size-bytes", 0, "Size of memory ballast to allocate.") flagext.RegisterFlags(&cfg) flag.Parse() @@ -44,6 +47,9 @@ func main() { // Parse a second time, as command line flags should take precedent over the config file. flag.Parse() + // Allocate a block of memory to alter GC behaviour. See https://github.com/golang/go/issues/23044 + ballast := make([]byte, ballastBytes) + util.InitLogger(&cfg.Server) util.InitEvents(eventSampleRate) @@ -60,6 +66,7 @@ func main() { level.Error(util.Logger).Log("msg", "error running Cortex", "err", err) } + runtime.KeepAlive(ballast) err = t.Stop() util.CheckFatal("initializing cortex", err) }