cpuacct.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright The containerd Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup1
  14. import (
  15. "bufio"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
  22. )
  23. const nanosecondsInSecond = 1000000000
  24. var clockTicks = getClockTicks()
  25. func NewCpuacct(root string) *cpuacctController {
  26. return &cpuacctController{
  27. root: filepath.Join(root, string(Cpuacct)),
  28. }
  29. }
  30. type cpuacctController struct {
  31. root string
  32. }
  33. func (c *cpuacctController) Name() Name {
  34. return Cpuacct
  35. }
  36. func (c *cpuacctController) Path(path string) string {
  37. return filepath.Join(c.root, path)
  38. }
  39. func (c *cpuacctController) Stat(path string, stats *v1.Metrics) error {
  40. user, kernel, err := c.getUsage(path)
  41. if err != nil {
  42. return err
  43. }
  44. total, err := readUint(filepath.Join(c.Path(path), "cpuacct.usage"))
  45. if err != nil {
  46. return err
  47. }
  48. percpu, err := c.percpuUsage(path)
  49. if err != nil {
  50. return err
  51. }
  52. stats.CPU.Usage.Total = total
  53. stats.CPU.Usage.User = user
  54. stats.CPU.Usage.Kernel = kernel
  55. stats.CPU.Usage.PerCPU = percpu
  56. return nil
  57. }
  58. func (c *cpuacctController) percpuUsage(path string) ([]uint64, error) {
  59. var usage []uint64
  60. data, err := os.ReadFile(filepath.Join(c.Path(path), "cpuacct.usage_percpu"))
  61. if err != nil {
  62. return nil, err
  63. }
  64. for _, v := range strings.Fields(string(data)) {
  65. u, err := strconv.ParseUint(v, 10, 64)
  66. if err != nil {
  67. return nil, err
  68. }
  69. usage = append(usage, u)
  70. }
  71. return usage, nil
  72. }
  73. func (c *cpuacctController) getUsage(path string) (user uint64, kernel uint64, err error) {
  74. statPath := filepath.Join(c.Path(path), "cpuacct.stat")
  75. f, err := os.Open(statPath)
  76. if err != nil {
  77. return 0, 0, err
  78. }
  79. defer f.Close()
  80. var (
  81. raw = make(map[string]uint64)
  82. sc = bufio.NewScanner(f)
  83. )
  84. for sc.Scan() {
  85. key, v, err := parseKV(sc.Text())
  86. if err != nil {
  87. return 0, 0, err
  88. }
  89. raw[key] = v
  90. }
  91. if err := sc.Err(); err != nil {
  92. return 0, 0, err
  93. }
  94. for _, t := range []struct {
  95. name string
  96. value *uint64
  97. }{
  98. {
  99. name: "user",
  100. value: &user,
  101. },
  102. {
  103. name: "system",
  104. value: &kernel,
  105. },
  106. } {
  107. v, ok := raw[t.name]
  108. if !ok {
  109. return 0, 0, fmt.Errorf("expected field %q but not found in %q", t.name, statPath)
  110. }
  111. *t.value = v
  112. }
  113. return (user * nanosecondsInSecond) / clockTicks, (kernel * nanosecondsInSecond) / clockTicks, nil
  114. }