MCPcopy Create free account
hub / github.com/containerd/cgroups / getUsage

Method getUsage

cgroup1/cpuacct.go:88–129  ·  view source on GitHub ↗
(path string)

Source from the content-addressed store, hash-verified

86}
87
88func (c *cpuacctController) getUsage(path string) (user uint64, kernel uint64, err error) {
89 statPath := filepath.Join(c.Path(path), "cpuacct.stat")
90 f, err := os.Open(statPath)
91 if err != nil {
92 return 0, 0, err
93 }
94 defer f.Close()
95 var (
96 raw = make(map[string]uint64)
97 sc = bufio.NewScanner(f)
98 )
99 for sc.Scan() {
100 key, v, err := parseKV(sc.Text())
101 if err != nil {
102 return 0, 0, err
103 }
104 raw[key] = v
105 }
106 if err := sc.Err(); err != nil {
107 return 0, 0, err
108 }
109 for _, t := range []struct {
110 name string
111 value *uint64
112 }{
113 {
114 name: "user",
115 value: &user,
116 },
117 {
118 name: "system",
119 value: &kernel,
120 },
121 } {
122 v, ok := raw[t.name]
123 if !ok {
124 return 0, 0, fmt.Errorf("expected field %q but not found in %q", t.name, statPath)
125 }
126 *t.value = v
127 }
128 return (user * nanosecondsInSecond) / clockTicks, (kernel * nanosecondsInSecond) / clockTicks, nil
129}

Callers 2

StatMethod · 0.95
TestGetUsageFunction · 0.80

Calls 2

PathMethod · 0.95
parseKVFunction · 0.70

Tested by 1

TestGetUsageFunction · 0.64