utils.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // +build linux
  2. package fscommon
  3. import (
  4. "errors"
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "strings"
  9. )
  10. var (
  11. ErrNotValidFormat = errors.New("line is not a valid key value format")
  12. )
  13. // ParseUint converts a string to an uint64 integer.
  14. // Negative values are returned at zero as, due to kernel bugs,
  15. // some of the memory cgroup stats can be negative.
  16. func ParseUint(s string, base, bitSize int) (uint64, error) {
  17. value, err := strconv.ParseUint(s, base, bitSize)
  18. if err != nil {
  19. intValue, intErr := strconv.ParseInt(s, base, bitSize)
  20. // 1. Handle negative values greater than MinInt64 (and)
  21. // 2. Handle negative values lesser than MinInt64
  22. if intErr == nil && intValue < 0 {
  23. return 0, nil
  24. } else if intErr != nil && intErr.(*strconv.NumError).Err == strconv.ErrRange && intValue < 0 {
  25. return 0, nil
  26. }
  27. return value, err
  28. }
  29. return value, nil
  30. }
  31. // ParseKeyValue parses a space-separated "name value" kind of cgroup
  32. // parameter and returns its key as a string, and its value as uint64
  33. // (ParseUint is used to convert the value). For example,
  34. // "io_service_bytes 1234" will be returned as "io_service_bytes", 1234.
  35. func ParseKeyValue(t string) (string, uint64, error) {
  36. parts := strings.SplitN(t, " ", 3)
  37. if len(parts) != 2 {
  38. return "", 0, fmt.Errorf("line %q is not in key value format", t)
  39. }
  40. value, err := ParseUint(parts[1], 10, 64)
  41. if err != nil {
  42. return "", 0, fmt.Errorf("unable to convert to uint64: %v", err)
  43. }
  44. return parts[0], value, nil
  45. }
  46. // GetValueByKey reads a key-value pairs from the specified cgroup file,
  47. // and returns a value of the specified key. ParseUint is used for value
  48. // conversion.
  49. func GetValueByKey(path, file, key string) (uint64, error) {
  50. content, err := ReadFile(path, file)
  51. if err != nil {
  52. return 0, err
  53. }
  54. lines := strings.Split(string(content), "\n")
  55. for _, line := range lines {
  56. arr := strings.Split(line, " ")
  57. if len(arr) == 2 && arr[0] == key {
  58. return ParseUint(arr[1], 10, 64)
  59. }
  60. }
  61. return 0, nil
  62. }
  63. // GetCgroupParamUint reads a single uint64 value from the specified cgroup file.
  64. // If the value read is "max", the math.MaxUint64 is returned.
  65. func GetCgroupParamUint(path, file string) (uint64, error) {
  66. contents, err := GetCgroupParamString(path, file)
  67. if err != nil {
  68. return 0, err
  69. }
  70. contents = strings.TrimSpace(contents)
  71. if contents == "max" {
  72. return math.MaxUint64, nil
  73. }
  74. res, err := ParseUint(contents, 10, 64)
  75. if err != nil {
  76. return res, fmt.Errorf("unable to parse file %q", path+"/"+file)
  77. }
  78. return res, nil
  79. }
  80. // GetCgroupParamInt reads a single int64 value from specified cgroup file.
  81. // If the value read is "max", the math.MaxInt64 is returned.
  82. func GetCgroupParamInt(path, file string) (int64, error) {
  83. contents, err := ReadFile(path, file)
  84. if err != nil {
  85. return 0, err
  86. }
  87. contents = strings.TrimSpace(contents)
  88. if contents == "max" {
  89. return math.MaxInt64, nil
  90. }
  91. res, err := strconv.ParseInt(contents, 10, 64)
  92. if err != nil {
  93. return res, fmt.Errorf("unable to parse %q as a int from Cgroup file %q", contents, path+"/"+file)
  94. }
  95. return res, nil
  96. }
  97. // GetCgroupParamString reads a string from the specified cgroup file.
  98. func GetCgroupParamString(path, file string) (string, error) {
  99. contents, err := ReadFile(path, file)
  100. if err != nil {
  101. return "", err
  102. }
  103. return strings.TrimSpace(contents), nil
  104. }