k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/memorymanager/state/state_mem.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 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 // NewMemoryState creates new State for keeping track of cpu/pod assignment 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 // GetMemoryState returns Memory Map stored in the State 43 func (s *stateMemory) GetMachineState() NUMANodeMap { 44 s.RLock() 45 defer s.RUnlock() 46 47 return s.machineState.Clone() 48 } 49 50 // GetMemoryBlocks returns memory assignments of a container 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 // GetMemoryAssignments returns ContainerMemoryAssignments 62 func (s *stateMemory) GetMemoryAssignments() ContainerMemoryAssignments { 63 s.RLock() 64 defer s.RUnlock() 65 66 return s.assignments.Clone() 67 } 68 69 // SetMachineState stores NUMANodeMap in State 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 // SetMemoryBlocks stores memory assignments of container 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 // SetMemoryAssignments sets ContainerMemoryAssignments by using the passed parameter 92 func (s *stateMemory) SetMemoryAssignments(assignments ContainerMemoryAssignments) { 93 s.Lock() 94 defer s.Unlock() 95 96 s.assignments = assignments.Clone() 97 } 98 99 // Delete deletes corresponding Blocks from ContainerMemoryAssignments 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 // ClearState clears machineState and ContainerMemoryAssignments 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 }