Merge pull request #5455 from rjnagal/cgroup-stats
Add throttling stats for cpu cgroup
This commit is contained in:
commit
f6024af3e8
2 changed files with 81 additions and 3 deletions
|
@ -1,6 +1,9 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
@ -37,7 +40,25 @@ func (s *cpuGroup) Remove(d *data) error {
|
|||
}
|
||||
|
||||
func (s *cpuGroup) Stats(d *data) (map[string]float64, error) {
|
||||
// we can reuse the cpuacct subsystem to get the cpu stats
|
||||
sys := subsystems["cpuacct"]
|
||||
return sys.Stats(d)
|
||||
paramData := make(map[string]float64)
|
||||
path, err := d.path("cpu")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f, err := os.Open(filepath.Join(path, "cpu.stat"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
t, v, err := getCgroupParamKeyValue(sc.Text())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paramData[t] = v
|
||||
}
|
||||
return paramData, nil
|
||||
}
|
||||
|
|
57
cgroups/fs/cpu_test.go
Normal file
57
cgroups/fs/cpu_test.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCpuStats(t *testing.T) {
|
||||
helper := NewCgroupTestUtil("cpu", t)
|
||||
defer helper.cleanup()
|
||||
cpuStatContent := `nr_periods 2000
|
||||
nr_throttled 200
|
||||
throttled_time 42424242424`
|
||||
helper.writeFileContents(map[string]string{
|
||||
"cpu.stat": cpuStatContent,
|
||||
})
|
||||
|
||||
cpu := &cpuGroup{}
|
||||
stats, err := cpu.Stats(helper.CgroupData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected_stats := map[string]float64{
|
||||
"nr_periods": 2000.0,
|
||||
"nr_throttled": 200.0,
|
||||
"throttled_time": 42424242424.0,
|
||||
}
|
||||
expectStats(t, expected_stats, stats)
|
||||
}
|
||||
|
||||
func TestNoCpuStatFile(t *testing.T) {
|
||||
helper := NewCgroupTestUtil("cpu", t)
|
||||
defer helper.cleanup()
|
||||
|
||||
cpu := &cpuGroup{}
|
||||
_, err := cpu.Stats(helper.CgroupData)
|
||||
if err == nil {
|
||||
t.Fatal("Expected to fail, but did not.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCpuStat(t *testing.T) {
|
||||
helper := NewCgroupTestUtil("cpu", t)
|
||||
defer helper.cleanup()
|
||||
cpuStatContent := `nr_periods 2000
|
||||
nr_throttled 200
|
||||
throttled_time fortytwo`
|
||||
helper.writeFileContents(map[string]string{
|
||||
"cpu.stat": cpuStatContent,
|
||||
})
|
||||
|
||||
cpu := &cpuGroup{}
|
||||
_, err := cpu.Stats(helper.CgroupData)
|
||||
if err == nil {
|
||||
t.Fatal("Expected failed stat parsing.")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue