mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[feature] Inherit resource limits from cgroups (#1336)
When GTS is running in a container runtime which has configured CPU or memory limits or under an init system that uses cgroups to impose CPU and memory limits the values the Go runtime sees for GOMAXPROCS and GOMEMLIMIT are still based on the host resources, not the cgroup. At least for the throttling middlewares which use GOMAXPROCS to configure their queue size, this can result in GTS running with values too big compared to the resources that will actuall be available to it. This introduces 2 dependencies which can pick up resource contraints from the current cgroup and tune the Go runtime accordingly. This should result in the different queues being appropriately sized and in general more predictable performance. These dependencies are a no-op on non-Linux systems or if running in a cgroup that doesn't set a limit on CPU or memory. The automatic tuning of GOMEMLIMIT can be disabled by either explicitly setting GOMEMLIMIT yourself or by setting AUTOMEMLIMIT=off. The automatic tuning of GOMAXPROCS can similarly be counteracted by setting GOMAXPROCS yourself.
This commit is contained in:
committed by
GitHub
parent
627b8eeae6
commit
acc333c40b
63
vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go
generated
vendored
Normal file
63
vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go
generated
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package memlimit
|
||||
|
||||
import (
|
||||
"github.com/containerd/cgroups"
|
||||
v2 "github.com/containerd/cgroups/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
cgroupMountPoint = "/sys/fs/cgroup"
|
||||
)
|
||||
|
||||
// FromCgroup returns the memory limit based on the cgroups version on this system.
|
||||
func FromCgroup() (uint64, error) {
|
||||
switch cgroups.Mode() {
|
||||
case cgroups.Legacy:
|
||||
return FromCgroupV1()
|
||||
case cgroups.Hybrid, cgroups.Unified:
|
||||
return FromCgroupV2()
|
||||
}
|
||||
return 0, ErrNoCgroup
|
||||
}
|
||||
|
||||
// FromCgroupV1 returns the memory limit from the cgroup v1.
|
||||
func FromCgroupV1() (uint64, error) {
|
||||
cg, err := cgroups.Load(cgroups.SingleSubsystem(cgroups.V1, cgroups.Memory), cgroups.RootPath)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
metrics, err := cg.Stat(cgroups.IgnoreNotExist)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else if metrics.Memory == nil {
|
||||
return 0, ErrNoLimit
|
||||
}
|
||||
|
||||
return metrics.Memory.HierarchicalMemoryLimit, nil
|
||||
}
|
||||
|
||||
// FromCgroupV2 returns the memory limit from the cgroup v2.
|
||||
func FromCgroupV2() (uint64, error) {
|
||||
path, err := v2.NestedGroupPath("")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
m, err := v2.LoadManager(cgroupMountPoint, path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
stats, err := m.Stat()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else if stats.Memory == nil {
|
||||
return 0, ErrNoLimit
|
||||
}
|
||||
|
||||
return stats.Memory.UsageLimit, nil
|
||||
}
|
16
vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups_unsupported.go
generated
vendored
Normal file
16
vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups_unsupported.go
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package memlimit
|
||||
|
||||
func FromCgroup() (uint64, error) {
|
||||
return 0, ErrCgroupsNotSupported
|
||||
}
|
||||
|
||||
func FromCgroupV1() (uint64, error) {
|
||||
return 0, ErrCgroupsNotSupported
|
||||
}
|
||||
|
||||
func FromCgroupV2() (uint64, error) {
|
||||
return 0, ErrCgroupsNotSupported
|
||||
}
|
105
vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
generated
vendored
Normal file
105
vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
generated
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
package memlimit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
envGOMEMLIMIT = "GOMEMLIMIT"
|
||||
envAUTOMEMLIMIT = "AUTOMEMLIMIT"
|
||||
envAUTOMEMLIMIT_DEBUG = "AUTOMEMLIMIT_DEBUG"
|
||||
|
||||
defaultAUTOMEMLIMIT = 0.9
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNoLimit is returned when the memory limit is not set.
|
||||
ErrNoLimit = errors.New("memory is not limited")
|
||||
// ErrNoCgroup is returned when the process is not in cgroup.
|
||||
ErrNoCgroup = errors.New("process is not in cgroup")
|
||||
// ErrCgroupsNotSupported is returned when the system does not support cgroups.
|
||||
ErrCgroupsNotSupported = errors.New("cgroups is not supported on this system")
|
||||
|
||||
logger = log.New(io.Discard, "", log.LstdFlags)
|
||||
)
|
||||
|
||||
// SetGoMemLimitWithEnv sets GOMEMLIMIT with the value from the environment variable.
|
||||
// You can configure how much memory of the cgroup's memory limit to set as GOMEMLIMIT
|
||||
// through AUTOMEMLIMIT in the half-open range (0.0,1.0].
|
||||
//
|
||||
// If AUTOMEMLIMIT is not set, it defaults to 0.9. (10% is the headroom for memory sources the Go runtime is unaware of.)
|
||||
// If GOMEMLIMIT is already set or AUTOMEMLIMIT=off, this function does nothing.
|
||||
func SetGoMemLimitWithEnv() {
|
||||
if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" {
|
||||
logger = log.Default()
|
||||
}
|
||||
|
||||
if val, ok := os.LookupEnv(envGOMEMLIMIT); ok {
|
||||
logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val)
|
||||
return
|
||||
}
|
||||
|
||||
ratio := defaultAUTOMEMLIMIT
|
||||
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
|
||||
if val == "off" {
|
||||
logger.Printf("AUTOMEMLIMIT is set to off, skipping\n")
|
||||
return
|
||||
}
|
||||
_ratio, err := strconv.ParseFloat(val, 64)
|
||||
if err != nil {
|
||||
logger.Printf("cannot parse AUTOMEMLIMIT: %s\n", val)
|
||||
return
|
||||
}
|
||||
ratio = _ratio
|
||||
}
|
||||
if ratio <= 0 || ratio > 1 {
|
||||
logger.Printf("invalid AUTOMEMLIMIT: %f\n", ratio)
|
||||
return
|
||||
}
|
||||
|
||||
limit, err := SetGoMemLimit(ratio)
|
||||
if err != nil {
|
||||
logger.Printf("failed to set GOMEMLIMIT: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Printf("GOMEMLIMIT=%d\n", limit)
|
||||
}
|
||||
|
||||
// SetGoMemLimit sets GOMEMLIMIT with the value from the cgroup's memory limit and given ratio.
|
||||
func SetGoMemLimit(ratio float64) (int64, error) {
|
||||
return SetGoMemLimitWithProvider(FromCgroup, ratio)
|
||||
}
|
||||
|
||||
// Provider is a function that returns the memory limit.
|
||||
type Provider func() (uint64, error)
|
||||
|
||||
// SetGoMemLimitWithProvider sets GOMEMLIMIT with the value from the given provider and ratio.
|
||||
func SetGoMemLimitWithProvider(provider Provider, ratio float64) (int64, error) {
|
||||
limit, err := provider()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
goMemLimit := cappedFloat2Int(float64(limit) * ratio)
|
||||
debug.SetMemoryLimit(goMemLimit)
|
||||
return goMemLimit, nil
|
||||
}
|
||||
|
||||
func cappedFloat2Int(f float64) int64 {
|
||||
if f > math.MaxInt64 {
|
||||
return math.MaxInt64
|
||||
}
|
||||
return int64(f)
|
||||
}
|
||||
// Limit is a helper Provider function that returns the given limit.
|
||||
func Limit(limit uint64) func() (uint64, error) {
|
||||
return func() (uint64, error) {
|
||||
return limit, nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user