From 54b9e1d3cbf549eb9ba1ca48f8f07c4130d01ca3 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 5 Jul 2021 12:31:26 +0300 Subject: [PATCH] lib/cgroup: set GOGC to 50 by default if it isn't set This should reduce memory usage for typical VictoriaMetrics workloads by up to 50% --- lib/cgroup/cpu.go | 6 +++--- lib/cgroup/mem.go | 30 ++++++++++++++++++++++++++++++ lib/httpserver/metrics.go | 1 + 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/cgroup/cpu.go b/lib/cgroup/cpu.go index b151aa925..46c706099 100644 --- a/lib/cgroup/cpu.go +++ b/lib/cgroup/cpu.go @@ -7,16 +7,16 @@ import ( "runtime" "strconv" "strings" - "sync" ) // AvailableCPUs returns the number of available CPU cores for the app. func AvailableCPUs() int { - availableCPUsOnce.Do(updateGOMAXPROCSToCPUQuota) return runtime.GOMAXPROCS(-1) } -var availableCPUsOnce sync.Once +func init() { + updateGOMAXPROCSToCPUQuota() +} // updateGOMAXPROCSToCPUQuota updates GOMAXPROCS to cgroup CPU quota if GOMAXPROCS isn't set in environment var. func updateGOMAXPROCSToCPUQuota() { diff --git a/lib/cgroup/mem.go b/lib/cgroup/mem.go index c4641a4b8..2fa34a11a 100644 --- a/lib/cgroup/mem.go +++ b/lib/cgroup/mem.go @@ -1,9 +1,39 @@ package cgroup import ( + "os" + "runtime/debug" "strconv" ) +// GetGOGC returns GOGC value for the currently running process. +// +// See https://golang.org/pkg/runtime/#hdr-Environment_Variables for more details about GOGC +func GetGOGC() int { + return gogc +} + +func init() { + initGOGC() +} + +func initGOGC() { + if v := os.Getenv("GOGC"); v != "" { + n, err := strconv.Atoi(v) + if err != nil { + n = 100 + } + gogc = n + } else { + // Set GOGC to 50% by default if it isn't set yet. + // This should reduce memory usage for typical workloads for VictoriaMetrics components. + gogc = 50 + debug.SetGCPercent(gogc) + } +} + +var gogc int + // GetMemoryLimit returns cgroup memory limit func GetMemoryLimit() int64 { // Try determining the amount of memory inside docker container. diff --git a/lib/httpserver/metrics.go b/lib/httpserver/metrics.go index a14603710..0b822f5d5 100644 --- a/lib/httpserver/metrics.go +++ b/lib/httpserver/metrics.go @@ -51,6 +51,7 @@ func writePrometheusMetrics(w io.Writer) { fmt.Fprintf(w, "vm_allowed_memory_bytes %d\n", memory.Allowed()) fmt.Fprintf(w, "vm_available_memory_bytes %d\n", memory.Allowed()+memory.Remaining()) fmt.Fprintf(w, "vm_available_cpu_cores %d\n", cgroup.AvailableCPUs()) + fmt.Fprintf(w, "vm_gogc %d\n", cgroup.GetGOGC()) // Export start time and uptime in seconds fmt.Fprintf(w, "vm_app_start_timestamp %d\n", startTime.Unix())