hugepages_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 e2e_node
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "strconv"
  18. "strings"
  19. "time"
  20. apiv1 "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/api/resource"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/util/uuid"
  24. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  25. "k8s.io/kubernetes/pkg/kubelet/cm"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  28. imageutils "k8s.io/kubernetes/test/utils/image"
  29. . "github.com/onsi/ginkgo"
  30. . "github.com/onsi/gomega"
  31. )
  32. // makePodToVerifyHugePages returns a pod that verifies specified cgroup with hugetlb
  33. func makePodToVerifyHugePages(baseName string, hugePagesLimit resource.Quantity) *apiv1.Pod {
  34. // convert the cgroup name to its literal form
  35. cgroupFsName := ""
  36. cgroupName := cm.NewCgroupName(cm.RootCgroupName, defaultNodeAllocatableCgroup, baseName)
  37. if framework.TestContext.KubeletConfig.CgroupDriver == "systemd" {
  38. cgroupFsName = cgroupName.ToSystemd()
  39. } else {
  40. cgroupFsName = cgroupName.ToCgroupfs()
  41. }
  42. // this command takes the expected value and compares it against the actual value for the pod cgroup hugetlb.2MB.limit_in_bytes
  43. command := fmt.Sprintf("expected=%v; actual=$(cat /tmp/hugetlb/%v/hugetlb.2MB.limit_in_bytes); if [ \"$expected\" -ne \"$actual\" ]; then exit 1; fi; ", hugePagesLimit.Value(), cgroupFsName)
  44. e2elog.Logf("Pod to run command: %v", command)
  45. pod := &apiv1.Pod{
  46. ObjectMeta: metav1.ObjectMeta{
  47. Name: "pod" + string(uuid.NewUUID()),
  48. },
  49. Spec: apiv1.PodSpec{
  50. RestartPolicy: apiv1.RestartPolicyNever,
  51. Containers: []apiv1.Container{
  52. {
  53. Image: busyboxImage,
  54. Name: "container" + string(uuid.NewUUID()),
  55. Command: []string{"sh", "-c", command},
  56. VolumeMounts: []apiv1.VolumeMount{
  57. {
  58. Name: "sysfscgroup",
  59. MountPath: "/tmp",
  60. },
  61. },
  62. },
  63. },
  64. Volumes: []apiv1.Volume{
  65. {
  66. Name: "sysfscgroup",
  67. VolumeSource: apiv1.VolumeSource{
  68. HostPath: &apiv1.HostPathVolumeSource{Path: "/sys/fs/cgroup"},
  69. },
  70. },
  71. },
  72. },
  73. }
  74. return pod
  75. }
  76. // enableHugePagesInKubelet enables hugepages feature for kubelet
  77. func enableHugePagesInKubelet(f *framework.Framework) *kubeletconfig.KubeletConfiguration {
  78. oldCfg, err := getCurrentKubeletConfig()
  79. framework.ExpectNoError(err)
  80. newCfg := oldCfg.DeepCopy()
  81. if newCfg.FeatureGates == nil {
  82. newCfg.FeatureGates = make(map[string]bool)
  83. newCfg.FeatureGates["HugePages"] = true
  84. }
  85. // Update the Kubelet configuration.
  86. framework.ExpectNoError(setKubeletConfiguration(f, newCfg))
  87. // Wait for the Kubelet to be ready.
  88. Eventually(func() bool {
  89. nodeList := framework.GetReadySchedulableNodesOrDie(f.ClientSet)
  90. return len(nodeList.Items) == 1
  91. }, time.Minute, time.Second).Should(BeTrue())
  92. return oldCfg
  93. }
  94. // configureHugePages attempts to allocate 100Mi of 2Mi hugepages for testing purposes
  95. func configureHugePages() error {
  96. err := exec.Command("/bin/sh", "-c", "echo 50 > /proc/sys/vm/nr_hugepages").Run()
  97. if err != nil {
  98. return err
  99. }
  100. outData, err := exec.Command("/bin/sh", "-c", "cat /proc/meminfo | grep 'HugePages_Total' | awk '{print $2}'").Output()
  101. if err != nil {
  102. return err
  103. }
  104. numHugePages, err := strconv.Atoi(strings.TrimSpace(string(outData)))
  105. if err != nil {
  106. return err
  107. }
  108. e2elog.Logf("HugePages_Total is set to %v", numHugePages)
  109. if numHugePages == 50 {
  110. return nil
  111. }
  112. return fmt.Errorf("expected hugepages %v, but found %v", 50, numHugePages)
  113. }
  114. // releaseHugePages releases all pre-allocated hugepages
  115. func releaseHugePages() error {
  116. return exec.Command("/bin/sh", "-c", "echo 0 > /proc/sys/vm/nr_hugepages").Run()
  117. }
  118. // isHugePageSupported returns true if the default hugepagesize on host is 2Mi (i.e. 2048 kB)
  119. func isHugePageSupported() bool {
  120. outData, err := exec.Command("/bin/sh", "-c", "cat /proc/meminfo | grep 'Hugepagesize:' | awk '{print $2}'").Output()
  121. framework.ExpectNoError(err)
  122. pageSize, err := strconv.Atoi(strings.TrimSpace(string(outData)))
  123. framework.ExpectNoError(err)
  124. return pageSize == 2048
  125. }
  126. // pollResourceAsString polls for a specified resource and capacity from node
  127. func pollResourceAsString(f *framework.Framework, resourceName string) string {
  128. node, err := f.ClientSet.CoreV1().Nodes().Get(framework.TestContext.NodeName, metav1.GetOptions{})
  129. framework.ExpectNoError(err)
  130. amount := amountOfResourceAsString(node, resourceName)
  131. e2elog.Logf("amount of %v: %v", resourceName, amount)
  132. return amount
  133. }
  134. // amountOfResourceAsString returns the amount of resourceName advertised by a node
  135. func amountOfResourceAsString(node *apiv1.Node, resourceName string) string {
  136. val, ok := node.Status.Capacity[apiv1.ResourceName(resourceName)]
  137. if !ok {
  138. return ""
  139. }
  140. return val.String()
  141. }
  142. func runHugePagesTests(f *framework.Framework) {
  143. It("should assign hugepages as expected based on the Pod spec", func() {
  144. By("by running a G pod that requests hugepages")
  145. pod := f.PodClient().Create(&apiv1.Pod{
  146. ObjectMeta: metav1.ObjectMeta{
  147. Name: "pod" + string(uuid.NewUUID()),
  148. Namespace: f.Namespace.Name,
  149. },
  150. Spec: apiv1.PodSpec{
  151. Containers: []apiv1.Container{
  152. {
  153. Image: imageutils.GetPauseImageName(),
  154. Name: "container" + string(uuid.NewUUID()),
  155. Resources: apiv1.ResourceRequirements{
  156. Limits: apiv1.ResourceList{
  157. apiv1.ResourceName("cpu"): resource.MustParse("10m"),
  158. apiv1.ResourceName("memory"): resource.MustParse("100Mi"),
  159. apiv1.ResourceName("hugepages-2Mi"): resource.MustParse("50Mi"),
  160. },
  161. },
  162. },
  163. },
  164. },
  165. })
  166. podUID := string(pod.UID)
  167. By("checking if the expected hugetlb settings were applied")
  168. verifyPod := makePodToVerifyHugePages("pod"+podUID, resource.MustParse("50Mi"))
  169. f.PodClient().Create(verifyPod)
  170. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, verifyPod.Name, f.Namespace.Name)
  171. Expect(err).NotTo(HaveOccurred())
  172. })
  173. }
  174. // Serial because the test updates kubelet configuration.
  175. var _ = SIGDescribe("HugePages [Serial] [Feature:HugePages][NodeFeature:HugePages]", func() {
  176. f := framework.NewDefaultFramework("hugepages-test")
  177. Context("With config updated with hugepages feature enabled", func() {
  178. var oldCfg *kubeletconfig.KubeletConfiguration
  179. BeforeEach(func() {
  180. By("verifying hugepages are supported")
  181. if !isHugePageSupported() {
  182. framework.Skipf("skipping test because hugepages are not supported")
  183. return
  184. }
  185. By("configuring the host to reserve a number of pre-allocated hugepages")
  186. Eventually(func() error {
  187. err := configureHugePages()
  188. if err != nil {
  189. return err
  190. }
  191. return nil
  192. }, 30*time.Second, framework.Poll).Should(BeNil())
  193. By("enabling hugepages in kubelet")
  194. oldCfg = enableHugePagesInKubelet(f)
  195. By("restarting kubelet to pick up pre-allocated hugepages")
  196. restartKubelet()
  197. By("by waiting for hugepages resource to become available on the local node")
  198. Eventually(func() string {
  199. return pollResourceAsString(f, "hugepages-2Mi")
  200. }, 30*time.Second, framework.Poll).Should(Equal("100Mi"))
  201. })
  202. runHugePagesTests(f)
  203. AfterEach(func() {
  204. By("Releasing hugepages")
  205. Eventually(func() error {
  206. err := releaseHugePages()
  207. if err != nil {
  208. return err
  209. }
  210. return nil
  211. }, 30*time.Second, framework.Poll).Should(BeNil())
  212. if oldCfg != nil {
  213. By("Restoring old kubelet config")
  214. setOldKubeletConfig(f, oldCfg)
  215. }
  216. By("restarting kubelet to release hugepages")
  217. restartKubelet()
  218. By("by waiting for hugepages resource to not appear available on the local node")
  219. Eventually(func() string {
  220. return pollResourceAsString(f, "hugepages-2Mi")
  221. }, 30*time.Second, framework.Poll).Should(Equal("0"))
  222. })
  223. })
  224. })