forked from DataDog/chaos-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuset_manager.go
89 lines (72 loc) · 2.88 KB
/
cpuset_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.
package stress
import (
"fmt"
"github.com/DataDog/chaos-controller/cgroup"
"github.com/DataDog/chaos-controller/cpuset"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/util/intstr"
"runtime"
"sync"
)
type StresserManager interface {
TrackInjectorCores(cgroup cgroup.Manager, userRequestCount *intstr.IntOrString) (cpuset.CPUSet, error)
IsCoreAlreadyStressed(core int) bool
TrackCoreAlreadyStressed(core int, stresserPID int)
StresserPIDs() map[int]int
}
type cpuStressserManager struct {
mutex sync.RWMutex
coresToBeStressed cpuset.CPUSet
stresserPIDPerCore map[int]int
log *zap.SugaredLogger
}
func NewCPUStresserManager(log *zap.SugaredLogger) StresserManager {
return &cpuStressserManager{
coresToBeStressed: cpuset.NewCPUSet(),
stresserPIDPerCore: map[int]int{},
log: log,
}
}
func (manager *cpuStressserManager) setCoresToBeStressed(cores cpuset.CPUSet) {
manager.mutex.Lock()
defer manager.mutex.Unlock()
manager.coresToBeStressed = cores
oldMaxProcs := runtime.GOMAXPROCS(cores.Size())
manager.log.Infof("changed GOMAXPROCS value from %d to %d", oldMaxProcs, cores.Size())
}
func (manager *cpuStressserManager) IsCoreAlreadyStressed(core int) bool {
manager.mutex.RLock()
defer manager.mutex.RUnlock()
_, isCoreStressed := manager.stresserPIDPerCore[core]
return isCoreStressed
}
func (manager *cpuStressserManager) TrackCoreAlreadyStressed(core int, stresserPID int) {
manager.mutex.Lock()
defer manager.mutex.Unlock()
manager.stresserPIDPerCore[core] = stresserPID
}
func (manager *cpuStressserManager) StresserPIDs() map[int]int {
return manager.stresserPIDPerCore
}
func (manager *cpuStressserManager) TrackInjectorCores(cgroup cgroup.Manager, userRequestCount *intstr.IntOrString) (cpuset.CPUSet, error) {
// read cpuset allocated cores
manager.log.Infow("retrieving target cpuset allocated cores")
cpusetCores, err := cgroup.Read("cpuset", "cpuset.cpus")
if err != nil {
return cpuset.NewCPUSet(), fmt.Errorf("failed to read the target allocated cpus from the cpuset cgroup: %w", err)
}
// parse allocated cores
cores, err := cpuset.Parse(cpusetCores)
if err != nil {
return cpuset.NewCPUSet(), fmt.Errorf("error parsing cpuset allocated cores: %w", err)
}
coresToBeStressedCount, _ := intstr.GetScaledValueFromIntOrPercent(userRequestCount, cores.Size(), true)
coresToBeStressed := cpuset.NewCPUSet(cores.ToSlice()[:coresToBeStressedCount]...)
manager.log.Infof("retrieved coresToBeStressed after applying userRequestCount %s: %s", userRequestCount.StrVal, coresToBeStressed)
manager.setCoresToBeStressed(manager.coresToBeStressed.Union(coresToBeStressed))
return coresToBeStressed, nil
}