diff options
Diffstat (limited to 'internal/cpu/stresser.go')
| -rw-r--r-- | internal/cpu/stresser.go | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/internal/cpu/stresser.go b/internal/cpu/stresser.go index 906467a..62ae3b7 100644 --- a/internal/cpu/stresser.go +++ b/internal/cpu/stresser.go @@ -13,12 +13,13 @@ import ( // Stresser uses pointer receivers because it contains a sync.Mutex // and shared mutable state that must not be copied. type Stresser struct { - config Config - cancel context.CancelFunc - wg sync.WaitGroup - mu sync.Mutex - current Mode - rand *rand.Rand + config Config + cancel context.CancelFunc + wg sync.WaitGroup + mu sync.Mutex + current Mode + currentCPUs int + rand *rand.Rand } func New(cfg Config) *Stresser { @@ -34,6 +35,11 @@ func New(cfg Config) *Stresser { if cfg.Mode == "" { cfg.Mode = ModePrime } + if !cfg.ChaosEnabled && cfg.MinCPUs <= 0 { + cfg.MinCPUs = cfg.Workers + cfg.MaxCPUs = cfg.Workers + } + cfg.normalize() return &Stresser{ config: cfg, rand: rand.New(rand.NewSource(time.Now().UnixNano())), @@ -44,8 +50,12 @@ func (s *Stresser) Start() { ctx, cancel := context.WithCancel(context.Background()) s.cancel = cancel - s.wg.Add(s.config.Workers) - for i := 0; i < s.config.Workers; i++ { + s.mu.Lock() + s.currentCPUs = s.determineCPUCount() + s.mu.Unlock() + + s.wg.Add(s.currentCPUs) + for i := 0; i < s.currentCPUs; i++ { go s.worker(ctx, i) } |
