From 61f156d5215b2c9d38e26bbd732c6e9cb9a3208e Mon Sep 17 00:00:00 2001 From: Rohit Jnagal Date: Tue, 29 Apr 2014 00:18:18 +0000 Subject: [PATCH 1/2] Add cpu throttling stats. Docker-DCO-1.1-Signed-off-by: Rohit Jnagal (github: rjnagal) --- pkg/cgroups/fs/cpu.go | 27 ++++++++++++++++++++++--- pkg/cgroups/fs/cpu_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 pkg/cgroups/fs/cpu_test.go diff --git a/pkg/cgroups/fs/cpu.go b/pkg/cgroups/fs/cpu.go index 2664811851..6a7f66c72d 100644 --- a/pkg/cgroups/fs/cpu.go +++ b/pkg/cgroups/fs/cpu.go @@ -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 } diff --git a/pkg/cgroups/fs/cpu_test.go b/pkg/cgroups/fs/cpu_test.go new file mode 100644 index 0000000000..ed9c0defd2 --- /dev/null +++ b/pkg/cgroups/fs/cpu_test.go @@ -0,0 +1,40 @@ +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.") + } +} From d724242297bf2981ad9c7745e5b130ab7fa8f067 Mon Sep 17 00:00:00 2001 From: Rohit Jnagal Date: Tue, 29 Apr 2014 00:32:05 +0000 Subject: [PATCH 2/2] Another test to check for invalid stats. Docker-DCO-1.1-Signed-off-by: Rohit Jnagal (github: rjnagal) --- pkg/cgroups/fs/cpu_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/cgroups/fs/cpu_test.go b/pkg/cgroups/fs/cpu_test.go index ed9c0defd2..698ae921d8 100644 --- a/pkg/cgroups/fs/cpu_test.go +++ b/pkg/cgroups/fs/cpu_test.go @@ -38,3 +38,20 @@ func TestNoCpuStatFile(t *testing.T) { 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.") + } +}