github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state/state_mem.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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  	"github.com/kubewharf/katalyst-core/pkg/util/machine"
    25  )
    26  
    27  // cpuPluginState is an in-memory implementation of State;
    28  // everytime we want to read or write states, those requests will always
    29  // go to in-memory State, and then go to disk State, i.e. in write-back mode
    30  type cpuPluginState struct {
    31  	sync.RWMutex
    32  
    33  	cpuTopology *machine.CPUTopology
    34  
    35  	podEntries     PodEntries
    36  	machineState   NUMANodeMap
    37  	socketTopology map[int]string
    38  }
    39  
    40  var _ State = &cpuPluginState{}
    41  
    42  func GetDefaultMachineState(topology *machine.CPUTopology) NUMANodeMap {
    43  	if topology == nil {
    44  		return nil
    45  	}
    46  
    47  	defaultMachineState := make(NUMANodeMap)
    48  	for _, numaNode := range topology.CPUDetails.NUMANodes().ToSliceInt() {
    49  		defaultMachineState[numaNode] = &NUMANodeState{
    50  			DefaultCPUSet:   topology.CPUDetails.CPUsInNUMANodes(numaNode).Clone(),
    51  			AllocatedCPUSet: machine.NewCPUSet(),
    52  			PodEntries:      make(PodEntries),
    53  		}
    54  	}
    55  	return defaultMachineState
    56  }
    57  
    58  func NewCPUPluginState(topology *machine.CPUTopology) State {
    59  	klog.InfoS("[cpu_plugin] initializing new cpu plugin in-memory state store")
    60  	return &cpuPluginState{
    61  		podEntries:     make(PodEntries),
    62  		machineState:   GetDefaultMachineState(topology),
    63  		socketTopology: topology.GetSocketTopology(),
    64  		cpuTopology:    topology,
    65  	}
    66  }
    67  
    68  func (s *cpuPluginState) GetMachineState() NUMANodeMap {
    69  	s.RLock()
    70  	defer s.RUnlock()
    71  
    72  	return s.machineState.Clone()
    73  }
    74  
    75  func (s *cpuPluginState) GetAllocationInfo(podUID string, containerName string) *AllocationInfo {
    76  	s.RLock()
    77  	defer s.RUnlock()
    78  
    79  	if res, ok := s.podEntries[podUID][containerName]; ok {
    80  		return res.Clone()
    81  	}
    82  	return nil
    83  }
    84  
    85  func (s *cpuPluginState) GetPodEntries() PodEntries {
    86  	s.RLock()
    87  	defer s.RUnlock()
    88  
    89  	return s.podEntries.Clone()
    90  }
    91  
    92  func (s *cpuPluginState) SetMachineState(numaNodeMap NUMANodeMap) {
    93  	s.Lock()
    94  	defer s.Unlock()
    95  
    96  	s.machineState = numaNodeMap.Clone()
    97  	klog.InfoS("[cpu_plugin] Updated cpu plugin machine state", "numaNodeMap", numaNodeMap.String())
    98  }
    99  
   100  func (s *cpuPluginState) SetAllocationInfo(podUID string, containerName string, allocationInfo *AllocationInfo) {
   101  	s.Lock()
   102  	defer s.Unlock()
   103  
   104  	if _, ok := s.podEntries[podUID]; !ok {
   105  		s.podEntries[podUID] = make(ContainerEntries)
   106  	}
   107  
   108  	s.podEntries[podUID][containerName] = allocationInfo.Clone()
   109  	klog.InfoS("[cpu_plugin] updated cpu plugin pod entries",
   110  		"podUID", podUID,
   111  		"containerName", containerName,
   112  		"allocationInfo", allocationInfo.String())
   113  }
   114  
   115  func (s *cpuPluginState) SetPodEntries(podEntries PodEntries) {
   116  	s.Lock()
   117  	defer s.Unlock()
   118  
   119  	s.podEntries = podEntries.Clone()
   120  	klog.InfoS("[cpu_plugin] Updated cpu plugin pod entries",
   121  		"podEntries", podEntries.String())
   122  }
   123  
   124  func (s *cpuPluginState) Delete(podUID string, containerName string) {
   125  	s.Lock()
   126  	defer s.Unlock()
   127  
   128  	if _, ok := s.podEntries[podUID]; !ok {
   129  		return
   130  	}
   131  
   132  	delete(s.podEntries[podUID], containerName)
   133  	if len(s.podEntries[podUID]) == 0 {
   134  		delete(s.podEntries, podUID)
   135  	}
   136  	klog.V(2).InfoS("[cpu_plugin] deleted container entry",
   137  		"podUID", podUID,
   138  		"containerName", containerName)
   139  }
   140  
   141  func (s *cpuPluginState) ClearState() {
   142  	s.Lock()
   143  	defer s.Unlock()
   144  
   145  	s.machineState = GetDefaultMachineState(s.cpuTopology)
   146  	s.socketTopology = s.cpuTopology.GetSocketTopology()
   147  	s.podEntries = make(PodEntries)
   148  	klog.V(2).InfoS("[cpu_plugin] cleared state")
   149  }