cpu.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package cgroup
  2. import (
  3. "path"
  4. )
  5. type ThrottlingStat struct {
  6. Periods uint64
  7. ThrottledPeriods uint64
  8. ThrottledTimeSeconds float64
  9. }
  10. func (cg Cgroup) ThrottledTimeSeconds() (float64, error) {
  11. vars, err := readVariablesFromFile(path.Join(cgRoot, "cpu", cg.subsystems["cpu"], "cpu.stat"))
  12. if err != nil {
  13. return 0, err
  14. }
  15. return float64(vars["throttled_time"]) / 1e9, nil
  16. }
  17. func (cg Cgroup) CpuUsageSeconds() (float64, error) {
  18. usageNs, err := readIntFromFile(path.Join(cgRoot, "cpuacct", cg.subsystems["cpuacct"], "cpuacct.usage"))
  19. if err != nil {
  20. return 0, err
  21. }
  22. return float64(usageNs) / 1e9, err
  23. }
  24. func (cg Cgroup) CpuQuotaCores() (float64, error) {
  25. periodUs, err := readIntFromFile(path.Join(cgRoot, "cpu", cg.subsystems["cpu"], "cpu.cfs_period_us"))
  26. if err != nil {
  27. return -1, err
  28. }
  29. quotaUs, err := readIntFromFile(path.Join(cgRoot, "cpu", cg.subsystems["cpu"], "cpu.cfs_quota_us"))
  30. if err != nil {
  31. return -1, err
  32. }
  33. if quotaUs < 0 {
  34. return -1, nil
  35. }
  36. return float64(quotaUs) / float64(periodUs), nil
  37. }