topology_hints.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2019 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 devicemanager
  14. import (
  15. "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/util/sets"
  17. "k8s.io/klog"
  18. "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
  19. "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
  20. )
  21. // GetTopologyHints implements the TopologyManager HintProvider Interface which
  22. // ensures the Device Manager is consulted when Topology Aware Hints for each
  23. // container are created.
  24. func (m *ManagerImpl) GetTopologyHints(pod *v1.Pod, container *v1.Container) map[string][]topologymanager.TopologyHint {
  25. // Garbage collect any stranded device resources before providing TopologyHints
  26. m.UpdateAllocatedDevices()
  27. // Loop through all device resources and generate TopologyHints for them..
  28. deviceHints := make(map[string][]topologymanager.TopologyHint)
  29. for resourceObj, requestedObj := range container.Resources.Limits {
  30. resource := string(resourceObj)
  31. requested := int(requestedObj.Value())
  32. // Only consider resources associated with a device plugin.
  33. if m.isDevicePluginResource(resource) {
  34. // Only consider devices that actually container topology information.
  35. if aligned := m.deviceHasTopologyAlignment(resource); !aligned {
  36. klog.Infof("[devicemanager] Resource '%v' does not have a topology preference", resource)
  37. deviceHints[resource] = nil
  38. continue
  39. }
  40. // Short circuit to regenerate the same hints if there are already
  41. // devices allocated to the Container. This might happen after a
  42. // kubelet restart, for example.
  43. allocated := m.podDevices.containerDevices(string(pod.UID), container.Name, resource)
  44. if allocated.Len() > 0 {
  45. if allocated.Len() != requested {
  46. klog.Errorf("[devicemanager] Resource '%v' already allocated to (pod %v, container %v) with different number than request: requested: %d, allocated: %d", resource, string(pod.UID), container.Name, requested, allocated.Len())
  47. deviceHints[resource] = []topologymanager.TopologyHint{}
  48. continue
  49. }
  50. klog.Infof("[devicemanager] Regenerating TopologyHints for resource '%v' already allocated to (pod %v, container %v)", resource, string(pod.UID), container.Name)
  51. deviceHints[resource] = m.generateDeviceTopologyHints(resource, allocated, requested)
  52. continue
  53. }
  54. // Get the list of available devices, for which TopologyHints should be generated.
  55. available := m.getAvailableDevices(resource)
  56. if available.Len() < requested {
  57. klog.Errorf("[devicemanager] Unable to generate topology hints: requested number of devices unavailable for '%s': requested: %d, available: %d", resource, requested, available.Len())
  58. deviceHints[resource] = []topologymanager.TopologyHint{}
  59. continue
  60. }
  61. // Generate TopologyHints for this resource given the current
  62. // request size and the list of available devices.
  63. deviceHints[resource] = m.generateDeviceTopologyHints(resource, available, requested)
  64. }
  65. }
  66. return deviceHints
  67. }
  68. func (m *ManagerImpl) deviceHasTopologyAlignment(resource string) bool {
  69. // If any device has Topology set, we assume they care about alignment.
  70. for device := range m.allDevices[resource] {
  71. if m.allDevices[resource][device].Topology != nil {
  72. return true
  73. }
  74. }
  75. return false
  76. }
  77. func (m *ManagerImpl) getAvailableDevices(resource string) sets.String {
  78. // Strip all devices in use from the list of healthy ones.
  79. return m.healthyDevices[resource].Difference(m.allocatedDevices[resource])
  80. }
  81. func (m *ManagerImpl) generateDeviceTopologyHints(resource string, devices sets.String, request int) []topologymanager.TopologyHint {
  82. // Initialize minAffinitySize to include all NUMA Nodes
  83. minAffinitySize := len(m.numaNodes)
  84. // Iterate through all combinations of NUMA Nodes and build hints from them.
  85. hints := []topologymanager.TopologyHint{}
  86. bitmask.IterateBitMasks(m.numaNodes, func(mask bitmask.BitMask) {
  87. // First, update minAffinitySize for the current request size.
  88. devicesInMask := 0
  89. for _, device := range m.allDevices[resource] {
  90. if device.Topology == nil {
  91. continue
  92. }
  93. for _, node := range device.Topology.Nodes {
  94. if mask.IsSet(int(node.ID)) {
  95. devicesInMask++
  96. break
  97. }
  98. }
  99. }
  100. if devicesInMask >= request && mask.Count() < minAffinitySize {
  101. minAffinitySize = mask.Count()
  102. }
  103. // Then check to see if we have enough devices available on the current
  104. // NUMA Node combination to satisfy the device request.
  105. numMatching := 0
  106. for d := range devices {
  107. if m.allDevices[resource][d].Topology == nil {
  108. continue
  109. }
  110. for _, node := range m.allDevices[resource][d].Topology.Nodes {
  111. if mask.IsSet(int(node.ID)) {
  112. numMatching++
  113. break
  114. }
  115. }
  116. }
  117. // If we don't, then move onto the next combination.
  118. if numMatching < request {
  119. return
  120. }
  121. // Otherwise, create a new hint from the NUMA mask and add it to the
  122. // list of hints. We set all hint preferences to 'false' on the first
  123. // pass through.
  124. hints = append(hints, topologymanager.TopologyHint{
  125. NUMANodeAffinity: mask,
  126. Preferred: false,
  127. })
  128. })
  129. // Loop back through all hints and update the 'Preferred' field based on
  130. // counting the number of bits sets in the affinity mask and comparing it
  131. // to the minAffinity. Only those with an equal number of bits set will be
  132. // considered preferred.
  133. for i := range hints {
  134. if hints[i].NUMANodeAffinity.Count() == minAffinitySize {
  135. hints[i].Preferred = true
  136. }
  137. }
  138. return hints
  139. }