state_mem.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(containerID string) (cpuset.CPUSet, bool) {
  34. s.RLock()
  35. defer s.RUnlock()
  36. res, ok := s.assignments[containerID]
  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(containerID string) cpuset.CPUSet {
  45. if res, ok := s.GetCPUSet(containerID); 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(containerID string, cset cpuset.CPUSet) {
  56. s.Lock()
  57. defer s.Unlock()
  58. s.assignments[containerID] = cset
  59. klog.Infof("[cpumanager] updated desired cpuset (container id: %s, cpuset: \"%s\")", containerID, cset)
  60. }
  61. func (s *stateMemory) SetDefaultCPUSet(cset cpuset.CPUSet) {
  62. s.Lock()
  63. defer s.Unlock()
  64. s.defaultCPUSet = cset
  65. klog.Infof("[cpumanager] updated default cpuset: \"%s\"", cset)
  66. }
  67. func (s *stateMemory) SetCPUAssignments(a ContainerCPUAssignments) {
  68. s.Lock()
  69. defer s.Unlock()
  70. s.assignments = a.Clone()
  71. klog.Infof("[cpumanager] updated cpuset assignments: \"%v\"", a)
  72. }
  73. func (s *stateMemory) Delete(containerID string) {
  74. s.Lock()
  75. defer s.Unlock()
  76. delete(s.assignments, containerID)
  77. klog.V(2).Infof("[cpumanager] deleted cpuset assignment (container id: %s)", containerID)
  78. }
  79. func (s *stateMemory) ClearState() {
  80. s.Lock()
  81. defer s.Unlock()
  82. s.defaultCPUSet = cpuset.CPUSet{}
  83. s.assignments = make(ContainerCPUAssignments)
  84. klog.V(2).Infof("[cpumanager] cleared state")
  85. }