Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to handle more than 4 fields in cpuacct.stat #231

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package cgroups

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -86,36 +88,41 @@ func (c *cpuacctController) percpuUsage(path string) ([]uint64, error) {

func (c *cpuacctController) getUsage(path string) (user uint64, kernel uint64, err error) {
statPath := filepath.Join(c.Path(path), "cpuacct.stat")
data, err := ioutil.ReadFile(statPath)
f, err := os.Open(statPath)
if err != nil {
return 0, 0, err
}
fields := strings.Fields(string(data))
if len(fields) != 4 {
return 0, 0, fmt.Errorf("%q is expected to have 4 fields", statPath)
defer f.Close()
var (
raw = make(map[string]uint64)
sc = bufio.NewScanner(f)
)
for sc.Scan() {
key, v, err := parseKV(sc.Text())
if err != nil {
return 0, 0, err
}
raw[key] = v
}
if err := sc.Err(); err != nil {
return 0, 0, err
}
for _, t := range []struct {
index int
name string
value *uint64
}{
{
index: 0,
name: "user",
value: &user,
},
{
index: 2,
name: "system",
value: &kernel,
},
} {
if fields[t.index] != t.name {
return 0, 0, fmt.Errorf("expected field %q but found %q in %q", t.name, fields[t.index], statPath)
}
v, err := strconv.ParseUint(fields[t.index+1], 10, 64)
if err != nil {
return 0, 0, err
v, ok := raw[t.name]
if !ok {
return 0, 0, fmt.Errorf("expected field %q but not found in %q", t.name, statPath)
}
*t.value = v
}
Expand Down
67 changes: 67 additions & 0 deletions cpuacct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cgroups

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

const cpuacctStatData = `user 1
system 2
sched_delay 3
`

func TestGetUsage(t *testing.T) {
mock, err := newMock()
if err != nil {
t.Fatal(err)
}
defer mock.delete()
cpuacct := NewCpuacct(mock.root)
if cpuacct == nil {
t.Fatal("cpuacct is nil")
}
err = os.Mkdir(filepath.Join(mock.root, string(Cpuacct), "test"), defaultDirPerm)
if err != nil {
t.Fatal(err)
}
current := filepath.Join(mock.root, string(Cpuacct), "test", "cpuacct.stat")
if err = ioutil.WriteFile(
current,
[]byte(cpuacctStatData),
defaultFilePerm,
); err != nil {
t.Fatal(err)
}
user, kernel, err := cpuacct.getUsage("test")
if err != nil {
t.Fatalf("can't get usage: %v", err)
}
index := []uint64{
user,
kernel,
}
for i, v := range index {
expected := ((uint64(i) + 1) * nanosecondsInSecond) / clockTicks
if v != expected {
t.Errorf("expected value at index %d to be %d but received %d", i, expected, v)
}
}
}