state_mem.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package state
  14. import (
  15. "sync"
  16. "k8s.io/klog"
  17. "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
  18. )
  19. type stateMemory struct {
  20. sync.RWMutex
  21. assignments ContainerCPUAssignments
  22. defaultCPUSet cpuset.CPUSet
  23. }
  24. var _ State = &stateMemory{}
  25. // NewMemoryState creates new State for keeping track of cpu/pod assignment
  26. func NewMemoryState() State {
  27. klog.Infof("[cpumanager] initializing new in-memory state store")
  28. return &stateMemory{
  29. assignments: ContainerCPUAssignments{},
  30. defaultCPUSet: cpuset.NewCPUSet(),
  31. }
  32. }
  33. func (s *stateMemory) GetCPUSet(podUID string, containerName string) (cpuset.CPUSet, bool) {
  34. s.RLock()
  35. defer s.RUnlock()
  36. res, ok := s.assignments[podUID][containerName]
  37. return res.Clone(), ok
  38. }
  39. func (s *stateMemory) GetDefaultCPUSet() cpuset.CPUSet {
  40. s.RLock()
  41. defer s.RUnlock()
  42. return s.defaultCPUSet.Clone()
  43. }
  44. func (s *stateMemory) GetCPUSetOrDefault(podUID string, containerName string) cpuset.CPUSet {
  45. if res, ok := s.GetCPUSet(podUID, containerName); ok {
  46. return res
  47. }
  48. return s.GetDefaultCPUSet()
  49. }
  50. func (s *stateMemory) GetCPUAssignments() ContainerCPUAssignments {
  51. s.RLock()
  52. defer s.RUnlock()
  53. return s.assignments.Clone()
  54. }
  55. func (s *stateMemory) SetCPUSet(podUID string, containerName string, cset cpuset.CPUSet) {
  56. s.Lock()
  57. defer s.Unlock()
  58. if _, ok := s.assignments[podUID]; !ok {
  59. s.assignments[podUID] = make(map[string]cpuset.CPUSet)
  60. }
  61. s.assignments[podUID][containerName] = cset
  62. klog.Infof("[cpumanager] updated desired cpuset (pod: %s, container: %s, cpuset: \"%s\")", podUID, containerName, cset)
  63. }
  64. func (s *stateMemory) SetDefaultCPUSet(cset cpuset.CPUSet) {
  65. s.Lock()
  66. defer s.Unlock()
  67. s.defaultCPUSet = cset
  68. klog.Infof("[cpumanager] updated default cpuset: \"%s\"", cset)
  69. }
  70. func (s *stateMemory) SetCPUAssignments(a ContainerCPUAssignments) {
  71. s.Lock()
  72. defer s.Unlock()
  73. s.assignments = a.Clone()
  74. klog.Infof("[cpumanager] updated cpuset assignments: \"%v\"", a)
  75. }
  76. func (s *stateMemory) Delete(podUID string, containerName string) {
  77. s.Lock()
  78. defer s.Unlock()
  79. delete(s.assignments[podUID], containerName)
  80. if len(s.assignments[podUID]) == 0 {
  81. delete(s.assignments, podUID)
  82. }
  83. klog.V(2).Infof("[cpumanager] deleted cpuset assignment (pod: %s, container: %s)", podUID, containerName)
  84. }
  85. func (s *stateMemory) ClearState() {
  86. s.Lock()
  87. defer s.Unlock()
  88. s.defaultCPUSet = cpuset.CPUSet{}
  89. s.assignments = make(ContainerCPUAssignments)
  90. klog.V(2).Infof("[cpumanager] cleared state")
  91. }