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%
This commit is contained in:
Aliaksandr Valialkin 2021-07-05 12:31:26 +03:00
parent 28a778dc9f
commit 377bb06b47
3 changed files with 34 additions and 3 deletions

View File

@ -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() {

View File

@ -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.

View File

@ -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())