utils.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package fs
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. )
  10. var (
  11. ErrNotSupportStat = errors.New("stats are not supported for subsystem")
  12. ErrNotValidFormat = errors.New("line is not a valid key value format")
  13. )
  14. // Parses a cgroup param and returns as name, value
  15. // i.e. "io_service_bytes 1234" will return as io_service_bytes, 1234
  16. func getCgroupParamKeyValue(t string) (string, int64, error) {
  17. parts := strings.Fields(t)
  18. switch len(parts) {
  19. case 2:
  20. value, err := strconv.ParseInt(parts[1], 10, 64)
  21. if err != nil {
  22. return "", 0, fmt.Errorf("Unable to convert param value to int: %s", err)
  23. }
  24. return parts[0], value, nil
  25. default:
  26. return "", 0, ErrNotValidFormat
  27. }
  28. }
  29. // Gets a single int64 value from the specified cgroup file.
  30. func getCgroupParamInt(cgroupPath, cgroupFile string) (int64, error) {
  31. contents, err := ioutil.ReadFile(filepath.Join(cgroupPath, cgroupFile))
  32. if err != nil {
  33. return -1, err
  34. }
  35. return strconv.ParseInt(strings.TrimSpace(string(contents)), 10, 64)
  36. }