Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional "Heap ballast" to alter GC behaviour. #1489

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/cortex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"runtime"

"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
Expand All @@ -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()
Expand All @@ -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)

Expand All @@ -60,6 +66,7 @@ func main() {
level.Error(util.Logger).Log("msg", "error running Cortex", "err", err)
}

runtime.KeepAlive(ballast)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha! I tried doing this in a different project awhile ago and did not know about KeepAlive.

err = t.Stop()
util.CheckFatal("initializing cortex", err)
}
Expand Down