...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/memorymanager/state/state.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/memorymanager/state

     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  	v1 "k8s.io/api/core/v1"
    21  )
    22  
    23  // MemoryTable contains memory information
    24  type MemoryTable struct {
    25  	TotalMemSize   uint64 `json:"total"`
    26  	SystemReserved uint64 `json:"systemReserved"`
    27  	Allocatable    uint64 `json:"allocatable"`
    28  	Reserved       uint64 `json:"reserved"`
    29  	Free           uint64 `json:"free"`
    30  }
    31  
    32  // NUMANodeState contains NUMA node related information
    33  type NUMANodeState struct {
    34  	// NumberOfAssignments contains a number memory assignments from this node
    35  	// When the container requires memory and hugepages it will increase number of assignments by two
    36  	NumberOfAssignments int `json:"numberOfAssignments"`
    37  	// MemoryTable contains NUMA node memory related information
    38  	MemoryMap map[v1.ResourceName]*MemoryTable `json:"memoryMap"`
    39  	// Cells contains the current NUMA node and all other nodes that are in a group with current NUMA node
    40  	// This parameter indicates if the current node is used for the multiple NUMA node memory allocation
    41  	// For example if some container has pinning 0,1,2, NUMA nodes 0,1,2 under the state will have
    42  	// this parameter equals to [0, 1, 2]
    43  	Cells []int `json:"cells"`
    44  }
    45  
    46  // NUMANodeMap contains memory information for each NUMA node.
    47  type NUMANodeMap map[int]*NUMANodeState
    48  
    49  // Clone returns a copy of NUMANodeMap
    50  func (nm NUMANodeMap) Clone() NUMANodeMap {
    51  	clone := make(NUMANodeMap)
    52  	for node, s := range nm {
    53  		if s == nil {
    54  			clone[node] = nil
    55  			continue
    56  		}
    57  
    58  		clone[node] = &NUMANodeState{}
    59  		clone[node].NumberOfAssignments = s.NumberOfAssignments
    60  		clone[node].Cells = append([]int{}, s.Cells...)
    61  
    62  		if s.MemoryMap == nil {
    63  			continue
    64  		}
    65  
    66  		clone[node].MemoryMap = map[v1.ResourceName]*MemoryTable{}
    67  		for memoryType, memoryTable := range s.MemoryMap {
    68  			clone[node].MemoryMap[memoryType] = &MemoryTable{
    69  				Allocatable:    memoryTable.Allocatable,
    70  				Free:           memoryTable.Free,
    71  				Reserved:       memoryTable.Reserved,
    72  				SystemReserved: memoryTable.SystemReserved,
    73  				TotalMemSize:   memoryTable.TotalMemSize,
    74  			}
    75  		}
    76  	}
    77  	return clone
    78  }
    79  
    80  // Block is a data structure used to represent a certain amount of memory
    81  type Block struct {
    82  	// NUMAAffinity contains the string that represents NUMA affinity bitmask
    83  	NUMAAffinity []int           `json:"numaAffinity"`
    84  	Type         v1.ResourceName `json:"type"`
    85  	Size         uint64          `json:"size"`
    86  }
    87  
    88  // ContainerMemoryAssignments stores memory assignments of containers
    89  type ContainerMemoryAssignments map[string]map[string][]Block
    90  
    91  // Clone returns a copy of ContainerMemoryAssignments
    92  func (as ContainerMemoryAssignments) Clone() ContainerMemoryAssignments {
    93  	clone := make(ContainerMemoryAssignments)
    94  	for pod := range as {
    95  		clone[pod] = make(map[string][]Block)
    96  		for container, blocks := range as[pod] {
    97  			clone[pod][container] = append([]Block{}, blocks...)
    98  		}
    99  	}
   100  	return clone
   101  }
   102  
   103  // Reader interface used to read current memory/pod assignment state
   104  type Reader interface {
   105  	// GetMachineState returns Memory Map stored in the State
   106  	GetMachineState() NUMANodeMap
   107  	// GetMemoryBlocks returns memory assignments of a container
   108  	GetMemoryBlocks(podUID string, containerName string) []Block
   109  	// GetMemoryAssignments returns ContainerMemoryAssignments
   110  	GetMemoryAssignments() ContainerMemoryAssignments
   111  }
   112  
   113  type writer interface {
   114  	// SetMachineState stores NUMANodeMap in State
   115  	SetMachineState(memoryMap NUMANodeMap)
   116  	// SetMemoryBlocks stores memory assignments of a container
   117  	SetMemoryBlocks(podUID string, containerName string, blocks []Block)
   118  	// SetMemoryAssignments sets ContainerMemoryAssignments by using the passed parameter
   119  	SetMemoryAssignments(assignments ContainerMemoryAssignments)
   120  	// Delete deletes corresponding Blocks from ContainerMemoryAssignments
   121  	Delete(podUID string, containerName string)
   122  	// ClearState clears machineState and ContainerMemoryAssignments
   123  	ClearState()
   124  }
   125  
   126  // State interface provides methods for tracking and setting memory/pod assignment
   127  type State interface {
   128  	Reader
   129  	writer
   130  }
   131  

View as plain text