...
1
16
17 package state
18
19 import (
20 "sync"
21
22 "k8s.io/klog/v2"
23 )
24
25 type stateMemory struct {
26 sync.RWMutex
27 assignments ContainerMemoryAssignments
28 machineState NUMANodeMap
29 }
30
31 var _ State = &stateMemory{}
32
33
34 func NewMemoryState() State {
35 klog.InfoS("Initializing new in-memory state store")
36 return &stateMemory{
37 assignments: ContainerMemoryAssignments{},
38 machineState: NUMANodeMap{},
39 }
40 }
41
42
43 func (s *stateMemory) GetMachineState() NUMANodeMap {
44 s.RLock()
45 defer s.RUnlock()
46
47 return s.machineState.Clone()
48 }
49
50
51 func (s *stateMemory) GetMemoryBlocks(podUID string, containerName string) []Block {
52 s.RLock()
53 defer s.RUnlock()
54
55 if res, ok := s.assignments[podUID][containerName]; ok {
56 return append([]Block{}, res...)
57 }
58 return nil
59 }
60
61
62 func (s *stateMemory) GetMemoryAssignments() ContainerMemoryAssignments {
63 s.RLock()
64 defer s.RUnlock()
65
66 return s.assignments.Clone()
67 }
68
69
70 func (s *stateMemory) SetMachineState(nodeMap NUMANodeMap) {
71 s.Lock()
72 defer s.Unlock()
73
74 s.machineState = nodeMap.Clone()
75 klog.InfoS("Updated machine memory state")
76 }
77
78
79 func (s *stateMemory) SetMemoryBlocks(podUID string, containerName string, blocks []Block) {
80 s.Lock()
81 defer s.Unlock()
82
83 if _, ok := s.assignments[podUID]; !ok {
84 s.assignments[podUID] = map[string][]Block{}
85 }
86
87 s.assignments[podUID][containerName] = append([]Block{}, blocks...)
88 klog.InfoS("Updated memory state", "podUID", podUID, "containerName", containerName)
89 }
90
91
92 func (s *stateMemory) SetMemoryAssignments(assignments ContainerMemoryAssignments) {
93 s.Lock()
94 defer s.Unlock()
95
96 s.assignments = assignments.Clone()
97 }
98
99
100 func (s *stateMemory) Delete(podUID string, containerName string) {
101 s.Lock()
102 defer s.Unlock()
103
104 if _, ok := s.assignments[podUID]; !ok {
105 return
106 }
107
108 delete(s.assignments[podUID], containerName)
109 if len(s.assignments[podUID]) == 0 {
110 delete(s.assignments, podUID)
111 }
112 klog.V(2).InfoS("Deleted memory assignment", "podUID", podUID, "containerName", containerName)
113 }
114
115
116 func (s *stateMemory) ClearState() {
117 s.Lock()
118 defer s.Unlock()
119
120 s.machineState = NUMANodeMap{}
121 s.assignments = make(ContainerMemoryAssignments)
122 klog.V(2).InfoS("Cleared state")
123 }
124
View as plain text