hugepages.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sysutils
  15. import (
  16. "io/ioutil"
  17. "path/filepath"
  18. "regexp"
  19. "sort"
  20. "strconv"
  21. "strings"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/errors"
  24. )
  25. type SHugepageInfo struct {
  26. SizeKb int
  27. Total int
  28. Free int
  29. }
  30. func (h SHugepageInfo) BytesMb() int64 {
  31. return int64(h.Total) * int64(h.SizeKb) / 1024
  32. }
  33. type THugepages []SHugepageInfo
  34. func (a THugepages) Len() int { return len(a) }
  35. func (a THugepages) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  36. func (a THugepages) Less(i, j int) bool { return a[i].SizeKb < a[j].SizeKb }
  37. func (a THugepages) BytesMb() int64 {
  38. ret := int64(0)
  39. for _, h := range a {
  40. ret += h.BytesMb()
  41. }
  42. return ret
  43. }
  44. func (a THugepages) PageSizes() []int {
  45. ret := make([]int, 0)
  46. for _, h := range a {
  47. if h.Total > 0 {
  48. ret = append(ret, h.SizeKb)
  49. }
  50. }
  51. return ret
  52. }
  53. func fetchHugepageInfo(sizeKb int, dir string) (SHugepageInfo, error) {
  54. info := SHugepageInfo{
  55. SizeKb: sizeKb,
  56. }
  57. cont, err := ioutil.ReadFile(filepath.Join(dir, "nr_hugepages"))
  58. if err != nil {
  59. return info, errors.Wrap(err, "FileGetContents nr_hugepages")
  60. }
  61. total, _ := strconv.Atoi(strings.TrimSpace(string(cont)))
  62. cont, err = ioutil.ReadFile(filepath.Join(dir, "free_hugepages"))
  63. if err != nil {
  64. return info, errors.Wrap(err, "FileGetContents free_hugepages")
  65. }
  66. free, _ := strconv.Atoi(strings.TrimSpace(string(cont)))
  67. info.Total = int(total)
  68. info.Free = int(free)
  69. return info, nil
  70. }
  71. func GetHugepages() (THugepages, error) {
  72. const hugepageDir = "/sys/kernel/mm/hugepages"
  73. files, err := ioutil.ReadDir(hugepageDir)
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "ReadDir %s", hugepageDir)
  76. }
  77. re := regexp.MustCompile(`hugepages-(\d+)kB`)
  78. infos := make(THugepages, 0)
  79. for _, dir := range files {
  80. if !dir.IsDir() {
  81. continue
  82. }
  83. ms := re.FindAllStringSubmatch(dir.Name(), -1)
  84. if len(ms) > 0 && len(ms[0]) > 1 {
  85. sizeKb, _ := strconv.Atoi(ms[0][1])
  86. if sizeKb > 0 {
  87. info, err := fetchHugepageInfo(sizeKb, filepath.Join(hugepageDir, dir.Name()))
  88. if err != nil {
  89. log.Errorf("fetchHugepageInfo %s fail %s", dir.Name(), err)
  90. } else {
  91. infos = append(infos, info)
  92. }
  93. }
  94. }
  95. }
  96. sort.Sort(infos)
  97. return infos, nil
  98. }