cpu.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // +build linux
  2. package fs2
  3. import (
  4. "bufio"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "github.com/opencontainers/runc/libcontainer/cgroups"
  9. "github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
  10. "github.com/opencontainers/runc/libcontainer/configs"
  11. )
  12. func setCpu(dirPath string, cgroup *configs.Cgroup) error {
  13. if cgroup.Resources.CpuWeight != 0 {
  14. if err := fscommon.WriteFile(dirPath, "cpu.weight", strconv.FormatUint(cgroup.Resources.CpuWeight, 10)); err != nil {
  15. return err
  16. }
  17. }
  18. if cgroup.Resources.CpuMax != "" {
  19. if err := fscommon.WriteFile(dirPath, "cpu.max", cgroup.Resources.CpuMax); err != nil {
  20. return err
  21. }
  22. }
  23. return nil
  24. }
  25. func statCpu(dirPath string, stats *cgroups.Stats) error {
  26. f, err := os.Open(filepath.Join(dirPath, "cpu.stat"))
  27. if err != nil {
  28. return err
  29. }
  30. defer f.Close()
  31. sc := bufio.NewScanner(f)
  32. for sc.Scan() {
  33. t, v, err := fscommon.GetCgroupParamKeyValue(sc.Text())
  34. if err != nil {
  35. return err
  36. }
  37. switch t {
  38. case "usage_usec":
  39. stats.CpuStats.CpuUsage.TotalUsage = v * 1000
  40. case "user_usec":
  41. stats.CpuStats.CpuUsage.UsageInUsermode = v * 1000
  42. case "system_usec":
  43. stats.CpuStats.CpuUsage.UsageInKernelmode = v * 1000
  44. }
  45. }
  46. return nil
  47. }