topology_hints_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 cpumanager
  14. import (
  15. "reflect"
  16. "sort"
  17. "testing"
  18. cadvisorapi "github.com/google/cadvisor/info/v1"
  19. v1 "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/state"
  22. "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
  23. "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
  24. "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
  25. "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
  26. )
  27. func TestGetTopologyHints(t *testing.T) {
  28. testPod1 := makePod("fakePod", "fakeContainer", "2", "2")
  29. testContainer1 := &testPod1.Spec.Containers[0]
  30. testPod2 := makePod("fakePod", "fakeContainer", "5", "5")
  31. testContainer2 := &testPod2.Spec.Containers[0]
  32. testPod3 := makePod("fakePod", "fakeContainer", "7", "7")
  33. testContainer3 := &testPod3.Spec.Containers[0]
  34. testPod4 := makePod("fakePod", "fakeContainer", "11", "11")
  35. testContainer4 := &testPod4.Spec.Containers[0]
  36. firstSocketMask, _ := bitmask.NewBitMask(0)
  37. secondSocketMask, _ := bitmask.NewBitMask(1)
  38. crossSocketMask, _ := bitmask.NewBitMask(0, 1)
  39. machineInfo := cadvisorapi.MachineInfo{
  40. NumCores: 12,
  41. Topology: []cadvisorapi.Node{
  42. {Id: 0,
  43. Cores: []cadvisorapi.Core{
  44. {Id: 0, Threads: []int{0, 6}},
  45. {Id: 1, Threads: []int{1, 7}},
  46. {Id: 2, Threads: []int{2, 8}},
  47. },
  48. },
  49. {Id: 1,
  50. Cores: []cadvisorapi.Core{
  51. {Id: 0, Threads: []int{3, 9}},
  52. {Id: 1, Threads: []int{4, 10}},
  53. {Id: 2, Threads: []int{5, 11}},
  54. },
  55. },
  56. },
  57. }
  58. numaNodeInfo := topology.NUMANodeInfo{
  59. 0: cpuset.NewCPUSet(0, 6, 1, 7, 2, 8),
  60. 1: cpuset.NewCPUSet(3, 9, 4, 10, 5, 11),
  61. }
  62. tcases := []struct {
  63. name string
  64. pod v1.Pod
  65. container v1.Container
  66. assignments state.ContainerCPUAssignments
  67. defaultCPUSet cpuset.CPUSet
  68. expectedHints []topologymanager.TopologyHint
  69. }{
  70. {
  71. name: "Request 2 CPUs, 4 available on NUMA 0, 6 available on NUMA 1",
  72. pod: *testPod1,
  73. container: *testContainer1,
  74. defaultCPUSet: cpuset.NewCPUSet(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
  75. expectedHints: []topologymanager.TopologyHint{
  76. {
  77. NUMANodeAffinity: firstSocketMask,
  78. Preferred: true,
  79. },
  80. {
  81. NUMANodeAffinity: secondSocketMask,
  82. Preferred: true,
  83. },
  84. {
  85. NUMANodeAffinity: crossSocketMask,
  86. Preferred: false,
  87. },
  88. },
  89. },
  90. {
  91. name: "Request 5 CPUs, 4 available on NUMA 0, 6 available on NUMA 1",
  92. pod: *testPod2,
  93. container: *testContainer2,
  94. defaultCPUSet: cpuset.NewCPUSet(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
  95. expectedHints: []topologymanager.TopologyHint{
  96. {
  97. NUMANodeAffinity: secondSocketMask,
  98. Preferred: true,
  99. },
  100. {
  101. NUMANodeAffinity: crossSocketMask,
  102. Preferred: false,
  103. },
  104. },
  105. },
  106. {
  107. name: "Request 7 CPUs, 4 available on NUMA 0, 6 available on NUMA 1",
  108. pod: *testPod3,
  109. container: *testContainer3,
  110. defaultCPUSet: cpuset.NewCPUSet(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
  111. expectedHints: []topologymanager.TopologyHint{
  112. {
  113. NUMANodeAffinity: crossSocketMask,
  114. Preferred: true,
  115. },
  116. },
  117. },
  118. {
  119. name: "Request 11 CPUs, 4 available on NUMA 0, 6 available on NUMA 1",
  120. pod: *testPod4,
  121. container: *testContainer4,
  122. defaultCPUSet: cpuset.NewCPUSet(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
  123. expectedHints: nil,
  124. },
  125. {
  126. name: "Request 2 CPUs, 1 available on NUMA 0, 1 available on NUMA 1",
  127. pod: *testPod1,
  128. container: *testContainer1,
  129. defaultCPUSet: cpuset.NewCPUSet(0, 3),
  130. expectedHints: []topologymanager.TopologyHint{
  131. {
  132. NUMANodeAffinity: crossSocketMask,
  133. Preferred: false,
  134. },
  135. },
  136. },
  137. {
  138. name: "Request more CPUs than available",
  139. pod: *testPod2,
  140. container: *testContainer2,
  141. defaultCPUSet: cpuset.NewCPUSet(0, 1, 2, 3),
  142. expectedHints: nil,
  143. },
  144. {
  145. name: "Regenerate Single-Node NUMA Hints if already allocated 1/2",
  146. pod: *testPod1,
  147. container: *testContainer1,
  148. assignments: state.ContainerCPUAssignments{
  149. string(testPod1.UID): map[string]cpuset.CPUSet{
  150. testContainer1.Name: cpuset.NewCPUSet(0, 6),
  151. },
  152. },
  153. defaultCPUSet: cpuset.NewCPUSet(),
  154. expectedHints: []topologymanager.TopologyHint{
  155. {
  156. NUMANodeAffinity: firstSocketMask,
  157. Preferred: true,
  158. },
  159. {
  160. NUMANodeAffinity: crossSocketMask,
  161. Preferred: false,
  162. },
  163. },
  164. },
  165. {
  166. name: "Regenerate Single-Node NUMA Hints if already allocated 1/2",
  167. pod: *testPod1,
  168. container: *testContainer1,
  169. assignments: state.ContainerCPUAssignments{
  170. string(testPod1.UID): map[string]cpuset.CPUSet{
  171. testContainer1.Name: cpuset.NewCPUSet(3, 9),
  172. },
  173. },
  174. defaultCPUSet: cpuset.NewCPUSet(),
  175. expectedHints: []topologymanager.TopologyHint{
  176. {
  177. NUMANodeAffinity: secondSocketMask,
  178. Preferred: true,
  179. },
  180. {
  181. NUMANodeAffinity: crossSocketMask,
  182. Preferred: false,
  183. },
  184. },
  185. },
  186. {
  187. name: "Regenerate Cross-NUMA Hints if already allocated",
  188. pod: *testPod4,
  189. container: *testContainer4,
  190. assignments: state.ContainerCPUAssignments{
  191. string(testPod4.UID): map[string]cpuset.CPUSet{
  192. testContainer4.Name: cpuset.NewCPUSet(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
  193. },
  194. },
  195. defaultCPUSet: cpuset.NewCPUSet(),
  196. expectedHints: []topologymanager.TopologyHint{
  197. {
  198. NUMANodeAffinity: crossSocketMask,
  199. Preferred: true,
  200. },
  201. },
  202. },
  203. {
  204. name: "Requested less than already allocated",
  205. pod: *testPod1,
  206. container: *testContainer1,
  207. assignments: state.ContainerCPUAssignments{
  208. string(testPod1.UID): map[string]cpuset.CPUSet{
  209. testContainer1.Name: cpuset.NewCPUSet(0, 6, 3, 9),
  210. },
  211. },
  212. defaultCPUSet: cpuset.NewCPUSet(),
  213. expectedHints: []topologymanager.TopologyHint{},
  214. },
  215. {
  216. name: "Requested more than already allocated",
  217. pod: *testPod4,
  218. container: *testContainer4,
  219. assignments: state.ContainerCPUAssignments{
  220. string(testPod4.UID): map[string]cpuset.CPUSet{
  221. testContainer4.Name: cpuset.NewCPUSet(0, 6, 3, 9),
  222. },
  223. },
  224. defaultCPUSet: cpuset.NewCPUSet(),
  225. expectedHints: []topologymanager.TopologyHint{},
  226. },
  227. }
  228. for _, tc := range tcases {
  229. topology, _ := topology.Discover(&machineInfo, numaNodeInfo)
  230. var activePods []*v1.Pod
  231. for p := range tc.assignments {
  232. pod := v1.Pod{}
  233. pod.UID = types.UID(p)
  234. for c := range tc.assignments[p] {
  235. container := v1.Container{}
  236. container.Name = c
  237. pod.Spec.Containers = append(pod.Spec.Containers, container)
  238. }
  239. activePods = append(activePods, &pod)
  240. }
  241. m := manager{
  242. policy: &staticPolicy{
  243. topology: topology,
  244. },
  245. state: &mockState{
  246. assignments: tc.assignments,
  247. defaultCPUSet: tc.defaultCPUSet,
  248. },
  249. topology: topology,
  250. activePods: func() []*v1.Pod { return activePods },
  251. podStatusProvider: mockPodStatusProvider{},
  252. sourcesReady: &sourcesReadyStub{},
  253. }
  254. hints := m.GetTopologyHints(&tc.pod, &tc.container)[string(v1.ResourceCPU)]
  255. if len(tc.expectedHints) == 0 && len(hints) == 0 {
  256. continue
  257. }
  258. sort.SliceStable(hints, func(i, j int) bool {
  259. return hints[i].LessThan(hints[j])
  260. })
  261. sort.SliceStable(tc.expectedHints, func(i, j int) bool {
  262. return tc.expectedHints[i].LessThan(tc.expectedHints[j])
  263. })
  264. if !reflect.DeepEqual(tc.expectedHints, hints) {
  265. t.Errorf("Expected in result to be %v , got %v", tc.expectedHints, hints)
  266. }
  267. }
  268. }