deploy: add -automaxprocs to reduce CPU and memory resources

With the new `-automaxprocs` commandline parameter, Ceph-CSI will adjust
the GOMAXPROCS environment variable for the Golang runtime. The values
are based on the CPU quota that is given to the process. This can reduce
the number of threads that the Golang runtime spawns, which affects the
require amount of memory as well.

Updates: #5228
Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos
2025-03-21 10:06:45 +01:00
committed by mergify[bot]
parent 0cb6771115
commit 4db8b6222c
18 changed files with 1074 additions and 0 deletions

View File

@ -35,6 +35,7 @@ import (
"github.com/ceph/ceph-csi/internal/util"
"github.com/ceph/ceph-csi/internal/util/log"
"go.uber.org/automaxprocs/maxprocs"
"k8s.io/klog/v2"
ctrlLog "sigs.k8s.io/controller-runtime/pkg/log"
)
@ -77,6 +78,8 @@ func init() {
flag.StringVar(&conf.InstanceID, "instanceid", "default", "Unique ID distinguishing this instance of Ceph-CSI"+
" among other instances, when sharing Ceph clusters across CSI instances for provisioning")
flag.IntVar(&conf.PidLimit, "pidlimit", 0, "the PID limit to configure through cgroups")
flag.BoolVar(&conf.AutoMaxProcs, "automaxprocs", false,
"automatically set GOMAXPROCS to match Linux container CPU quota")
flag.BoolVar(&conf.IsControllerServer, "controllerserver", false, "start cephcsi controller server")
flag.BoolVar(&conf.IsNodeServer, "nodeserver", false, "start cephcsi node server")
flag.StringVar(
@ -215,6 +218,15 @@ func main() {
logAndExit(err.Error())
}
// maxprocs detects CPU quota based on cgroups, do this before setting PID-limit
if conf.AutoMaxProcs {
_, mpErr := maxprocs.Set(maxprocs.Logger(log.DefaultLog))
if mpErr != nil {
log.FatalLogMsg("failed to configure automaxprocs: %v", mpErr)
}
}
// set the PID limit (for native Ceph threads) after conf.AutoMaxProcs
setPIDLimit(&conf)
if conf.EnableProfiling || conf.Vtype == livenessType {