-
Notifications
You must be signed in to change notification settings - Fork 90
/
state_mem.go
149 lines (117 loc) · 3.83 KB
/
state_mem.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"sync"
"k8s.io/klog/v2"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)
// cpuPluginState is an in-memory implementation of State;
// everytime we want to read or write states, those requests will always
// go to in-memory State, and then go to disk State, i.e. in write-back mode
type cpuPluginState struct {
sync.RWMutex
cpuTopology *machine.CPUTopology
podEntries PodEntries
machineState NUMANodeMap
socketTopology map[int]string
}
var _ State = &cpuPluginState{}
func GetDefaultMachineState(topology *machine.CPUTopology) NUMANodeMap {
if topology == nil {
return nil
}
defaultMachineState := make(NUMANodeMap)
for _, numaNode := range topology.CPUDetails.NUMANodes().ToSliceInt() {
defaultMachineState[numaNode] = &NUMANodeState{
DefaultCPUSet: topology.CPUDetails.CPUsInNUMANodes(numaNode).Clone(),
AllocatedCPUSet: machine.NewCPUSet(),
PodEntries: make(PodEntries),
}
}
return defaultMachineState
}
func NewCPUPluginState(topology *machine.CPUTopology) State {
klog.InfoS("[cpu_plugin] initializing new cpu plugin in-memory state store")
return &cpuPluginState{
podEntries: make(PodEntries),
machineState: GetDefaultMachineState(topology),
socketTopology: topology.GetSocketTopology(),
cpuTopology: topology,
}
}
func (s *cpuPluginState) GetMachineState() NUMANodeMap {
s.RLock()
defer s.RUnlock()
return s.machineState.Clone()
}
func (s *cpuPluginState) GetAllocationInfo(podUID string, containerName string) *AllocationInfo {
s.RLock()
defer s.RUnlock()
if res, ok := s.podEntries[podUID][containerName]; ok {
return res.Clone()
}
return nil
}
func (s *cpuPluginState) GetPodEntries() PodEntries {
s.RLock()
defer s.RUnlock()
return s.podEntries.Clone()
}
func (s *cpuPluginState) SetMachineState(numaNodeMap NUMANodeMap) {
s.Lock()
defer s.Unlock()
s.machineState = numaNodeMap.Clone()
klog.InfoS("[cpu_plugin] Updated cpu plugin machine state", "numaNodeMap", numaNodeMap.String())
}
func (s *cpuPluginState) SetAllocationInfo(podUID string, containerName string, allocationInfo *AllocationInfo) {
s.Lock()
defer s.Unlock()
if _, ok := s.podEntries[podUID]; !ok {
s.podEntries[podUID] = make(ContainerEntries)
}
s.podEntries[podUID][containerName] = allocationInfo.Clone()
klog.InfoS("[cpu_plugin] updated cpu plugin pod entries",
"podUID", podUID,
"containerName", containerName,
"allocationInfo", allocationInfo.String())
}
func (s *cpuPluginState) SetPodEntries(podEntries PodEntries) {
s.Lock()
defer s.Unlock()
s.podEntries = podEntries.Clone()
klog.InfoS("[cpu_plugin] Updated cpu plugin pod entries",
"podEntries", podEntries.String())
}
func (s *cpuPluginState) Delete(podUID string, containerName string) {
s.Lock()
defer s.Unlock()
if _, ok := s.podEntries[podUID]; !ok {
return
}
delete(s.podEntries[podUID], containerName)
if len(s.podEntries[podUID]) == 0 {
delete(s.podEntries, podUID)
}
klog.V(2).InfoS("[cpu_plugin] deleted container entry",
"podUID", podUID,
"containerName", containerName)
}
func (s *cpuPluginState) ClearState() {
s.Lock()
defer s.Unlock()
s.machineState = GetDefaultMachineState(s.cpuTopology)
s.socketTopology = s.cpuTopology.GetSocketTopology()
s.podEntries = make(PodEntries)
klog.V(2).InfoS("[cpu_plugin] cleared state")
}