diff --git a/lib/cgroup/cpu.go b/lib/cgroup/cpu.go index b151aa9259..46c706099f 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 c4641a4b89..2fa34a11a5 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 a146037105..0b822f5d50 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())