mirror of
https://codeberg.org/superseriousbusiness/gotosocial.git
synced 2024-12-27 03:18:16 +03:00
acc333c40b
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.
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
var sysCPU struct {
|
|
once sync.Once
|
|
err error
|
|
num int
|
|
}
|
|
|
|
// PossibleCPUs returns the max number of CPUs a system may possibly have
|
|
// Logical CPU numbers must be of the form 0-n
|
|
func PossibleCPUs() (int, error) {
|
|
sysCPU.once.Do(func() {
|
|
sysCPU.num, sysCPU.err = parseCPUsFromFile("/sys/devices/system/cpu/possible")
|
|
})
|
|
|
|
return sysCPU.num, sysCPU.err
|
|
}
|
|
|
|
func parseCPUsFromFile(path string) (int, error) {
|
|
spec, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
n, err := parseCPUs(string(spec))
|
|
if err != nil {
|
|
return 0, fmt.Errorf("can't parse %s: %v", path, err)
|
|
}
|
|
|
|
return n, nil
|
|
}
|
|
|
|
// parseCPUs parses the number of cpus from a string produced
|
|
// by bitmap_list_string() in the Linux kernel.
|
|
// Multiple ranges are rejected, since they can't be unified
|
|
// into a single number.
|
|
// This is the format of /sys/devices/system/cpu/possible, it
|
|
// is not suitable for /sys/devices/system/cpu/online, etc.
|
|
func parseCPUs(spec string) (int, error) {
|
|
if strings.Trim(spec, "\n") == "0" {
|
|
return 1, nil
|
|
}
|
|
|
|
var low, high int
|
|
n, err := fmt.Sscanf(spec, "%d-%d\n", &low, &high)
|
|
if n != 2 || err != nil {
|
|
return 0, fmt.Errorf("invalid format: %s", spec)
|
|
}
|
|
if low != 0 {
|
|
return 0, fmt.Errorf("CPU spec doesn't start at zero: %s", spec)
|
|
}
|
|
|
|
// cpus is 0 indexed
|
|
return high + 1, nil
|
|
}
|