From 8e9fe4712b9a96a4d43adbcd411dee912a17f5e2 Mon Sep 17 00:00:00 2001 From: Johannes Edmeier Date: Wed, 5 Feb 2025 16:35:04 +0100 Subject: [PATCH] fix: use allowed cpus from target container if a process is limited to a certain cpuset or some of the configured processors are not online, we need to pass the number of workers to stress-ng as otherwise stress-ng will always use the configured number of processors. --- exthost/action_stress.go | 16 ++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/exthost/action_stress.go b/exthost/action_stress.go index e8f5ed2..350b14a 100644 --- a/exthost/action_stress.go +++ b/exthost/action_stress.go @@ -13,6 +13,7 @@ import ( "github.com/steadybit/action-kit/go/action_kit_api/v2" "github.com/steadybit/action-kit/go/action_kit_commons/runc" "github.com/steadybit/action-kit/go/action_kit_commons/stress" + "github.com/steadybit/action-kit/go/action_kit_commons/utils" "github.com/steadybit/action-kit/go/action_kit_sdk" "github.com/steadybit/extension-kit" "github.com/steadybit/extension-kit/extutil" @@ -97,6 +98,8 @@ func (a *stressAction) Prepare(ctx context.Context, state *StressActionState, re return nil, extension_kit.ToError("Failed to prepare stress settings.", err) } + adaptCpuHosts(&opts) + state.StressOpts = opts state.Sidecar = stress.SidecarOpts{ TargetProcess: initProcess, @@ -110,6 +113,19 @@ func (a *stressAction) Prepare(ctx context.Context, state *StressActionState, re return nil, nil } +func adaptCpuHosts(s *stress.Opts) { + if s.CpuWorkers == nil || *s.CpuWorkers != 0 { + return + } + + //stress-ng will use all configured processors, we deem this to be wrong and expect all online cpus to be used. + if c, err := utils.ReadCpusAllowedCount("/proc/1/status"); err == nil { + s.CpuWorkers = extutil.Ptr(c) + } else { + log.Debug().Err(err).Msg("failed to read cpus allowed for pid 1") + } +} + func (a *stressAction) Start(ctx context.Context, state *StressActionState) (*action_kit_api.StartResult, error) { s, err := stress.New(ctx, a.runc, state.Sidecar, state.StressOpts) if err != nil { diff --git a/go.mod b/go.mod index cf90b75..28858fa 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.33.0 github.com/steadybit/action-kit/go/action_kit_api/v2 v2.9.7-0.20250131112718-3009f02a4b24 - github.com/steadybit/action-kit/go/action_kit_commons v1.2.17 + github.com/steadybit/action-kit/go/action_kit_commons v1.2.18 github.com/steadybit/action-kit/go/action_kit_sdk v1.1.14 github.com/steadybit/action-kit/go/action_kit_test v1.3.2 github.com/steadybit/discovery-kit/go/discovery_kit_api v1.6.0 diff --git a/go.sum b/go.sum index aa84ef0..2af8d8c 100644 --- a/go.sum +++ b/go.sum @@ -142,8 +142,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/steadybit/action-kit/go/action_kit_api/v2 v2.9.7-0.20250131112718-3009f02a4b24 h1:yPAEDf9TdBo7AreM9oNguDKqj2aJ4XZsRQupP4x3X9Y= github.com/steadybit/action-kit/go/action_kit_api/v2 v2.9.7-0.20250131112718-3009f02a4b24/go.mod h1:5Zs3wQtBtda+ASXKYk1sfcDBFe+F2+V2H4EbZzjXhJQ= -github.com/steadybit/action-kit/go/action_kit_commons v1.2.17 h1:NOKvnUA/iZo7mlcgSK+c8EHjxAEYCLSdyk8KVQSXmv8= -github.com/steadybit/action-kit/go/action_kit_commons v1.2.17/go.mod h1:GFLcaf/WluBIqnq+iSCEGkEqxnfB9h1I16IBR1T7kNQ= +github.com/steadybit/action-kit/go/action_kit_commons v1.2.18 h1:zg3c7b/Dl0M/odtNMbbhwVn/EbnvPZO14IPwbFy9WbY= +github.com/steadybit/action-kit/go/action_kit_commons v1.2.18/go.mod h1:GFLcaf/WluBIqnq+iSCEGkEqxnfB9h1I16IBR1T7kNQ= github.com/steadybit/action-kit/go/action_kit_sdk v1.1.14 h1:x94RX+vh9Iyc0tS6BhiSpvknj+xE36AV0Nc3D5Yuub0= github.com/steadybit/action-kit/go/action_kit_sdk v1.1.14/go.mod h1:Tp/klK5b7k+BCvb3JTSDZSNcnpFBaHauhndzOarnMW4= github.com/steadybit/action-kit/go/action_kit_test v1.3.2 h1:DFDznoWEbTGv+fiGYiRaq7tq5es9VTScjrWusRAbS08=