From 6521880a8371f5c7406b2ce6e055b2d709a65cfb Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Thu, 22 May 2014 20:50:51 +0000 Subject: [PATCH] Make all cgroup stats output int64s instead of float64. Docker-DCO-1.1-Signed-off-by: Victor Marmol (github: vmarmol) --- cgroups/fs/apply_raw.go | 4 ++-- cgroups/fs/blkio.go | 8 ++++---- cgroups/fs/blkio_test.go | 34 +++++++++++++++---------------- cgroups/fs/cpu.go | 4 ++-- cgroups/fs/cpu_test.go | 8 ++++---- cgroups/fs/cpuacct.go | 43 ++++++++++++++++----------------------- cgroups/fs/cpuset.go | 2 +- cgroups/fs/devices.go | 2 +- cgroups/fs/freezer.go | 7 ++++--- cgroups/fs/memory.go | 6 +++--- cgroups/fs/memory_test.go | 2 +- cgroups/fs/perf_event.go | 2 +- cgroups/fs/test_util.go | 2 +- cgroups/fs/utils.go | 16 +++++++-------- cgroups/fs/utils_test.go | 10 ++++----- 15 files changed, 72 insertions(+), 78 deletions(-) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index fd52c607..be500781 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -26,7 +26,7 @@ var ( type subsystem interface { Set(*data) error Remove(*data) error - Stats(*data) (map[string]float64, error) + Stats(*data) (map[string]int64, error) } type data struct { @@ -74,7 +74,7 @@ func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) { return d, nil } -func GetStats(c *cgroups.Cgroup, subsystem string, pid int) (map[string]float64, error) { +func GetStats(c *cgroups.Cgroup, subsystem string, pid int) (map[string]int64, error) { cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu") if err != nil { return nil, err diff --git a/cgroups/fs/blkio.go b/cgroups/fs/blkio.go index 213d250d..5cbef69f 100644 --- a/cgroups/fs/blkio.go +++ b/cgroups/fs/blkio.go @@ -57,9 +57,9 @@ examples: 8:0 Total 0 Total 0 */ -func (s *blkioGroup) Stats(d *data) (map[string]float64, error) { +func (s *blkioGroup) Stats(d *data) (map[string]int64, error) { var ( - paramData = make(map[string]float64) + paramData = make(map[string]int64) params = []string{ "io_service_bytes_recursive", "io_serviced_recursive", @@ -91,7 +91,7 @@ func (s *blkioGroup) Stats(d *data) (map[string]float64, error) { fields := strings.Fields(sc.Text()) switch len(fields) { case 3: - v, err := strconv.ParseFloat(fields[2], 64) + v, err := strconv.ParseInt(fields[2], 10, 64) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func (s *blkioGroup) Stats(d *data) (map[string]float64, error) { return paramData, nil } -func (s *blkioGroup) getSectors(path string) (string, float64, error) { +func (s *blkioGroup) getSectors(path string) (string, int64, error) { f, err := os.Open(filepath.Join(path, "blkio.sectors_recursive")) if err != nil { return "", 0, err diff --git a/cgroups/fs/blkio_test.go b/cgroups/fs/blkio_test.go index 5279ac43..d0244ad7 100644 --- a/cgroups/fs/blkio_test.go +++ b/cgroups/fs/blkio_test.go @@ -43,29 +43,29 @@ func TestBlkioStats(t *testing.T) { } // Verify expected stats. - expectedStats := map[string]float64{ - "blkio.sectors_recursive:8:0": 1024.0, + expectedStats := map[string]int64{ + "blkio.sectors_recursive:8:0": 1024, // Serviced bytes. - "io_service_bytes_recursive:8:0:Read": 100.0, - "io_service_bytes_recursive:8:0:Write": 400.0, - "io_service_bytes_recursive:8:0:Sync": 200.0, - "io_service_bytes_recursive:8:0:Async": 300.0, - "io_service_bytes_recursive:8:0:Total": 500.0, + "io_service_bytes_recursive:8:0:Read": 100, + "io_service_bytes_recursive:8:0:Write": 400, + "io_service_bytes_recursive:8:0:Sync": 200, + "io_service_bytes_recursive:8:0:Async": 300, + "io_service_bytes_recursive:8:0:Total": 500, // Serviced requests. - "io_serviced_recursive:8:0:Read": 10.0, - "io_serviced_recursive:8:0:Write": 40.0, - "io_serviced_recursive:8:0:Sync": 20.0, - "io_serviced_recursive:8:0:Async": 30.0, - "io_serviced_recursive:8:0:Total": 50.0, + "io_serviced_recursive:8:0:Read": 10, + "io_serviced_recursive:8:0:Write": 40, + "io_serviced_recursive:8:0:Sync": 20, + "io_serviced_recursive:8:0:Async": 30, + "io_serviced_recursive:8:0:Total": 50, // Queued requests. - "io_queued_recursive:8:0:Read": 1.0, - "io_queued_recursive:8:0:Write": 4.0, - "io_queued_recursive:8:0:Sync": 2.0, - "io_queued_recursive:8:0:Async": 3.0, - "io_queued_recursive:8:0:Total": 5.0, + "io_queued_recursive:8:0:Read": 1, + "io_queued_recursive:8:0:Write": 4, + "io_queued_recursive:8:0:Sync": 2, + "io_queued_recursive:8:0:Async": 3, + "io_queued_recursive:8:0:Total": 5, } expectStats(t, expectedStats, stats) } diff --git a/cgroups/fs/cpu.go b/cgroups/fs/cpu.go index 6a7f66c7..ad3078b3 100644 --- a/cgroups/fs/cpu.go +++ b/cgroups/fs/cpu.go @@ -39,8 +39,8 @@ func (s *cpuGroup) Remove(d *data) error { return removePath(d.path("cpu")) } -func (s *cpuGroup) Stats(d *data) (map[string]float64, error) { - paramData := make(map[string]float64) +func (s *cpuGroup) Stats(d *data) (map[string]int64, error) { + paramData := make(map[string]int64) path, err := d.path("cpu") if err != nil { return nil, err diff --git a/cgroups/fs/cpu_test.go b/cgroups/fs/cpu_test.go index 698ae921..cacf2f4c 100644 --- a/cgroups/fs/cpu_test.go +++ b/cgroups/fs/cpu_test.go @@ -20,10 +20,10 @@ func TestCpuStats(t *testing.T) { t.Fatal(err) } - expected_stats := map[string]float64{ - "nr_periods": 2000.0, - "nr_throttled": 200.0, - "throttled_time": 42424242424.0, + expected_stats := map[string]int64{ + "nr_periods": 2000, + "nr_throttled": 200, + "throttled_time": 42424242424, } expectStats(t, expected_stats, stats) } diff --git a/cgroups/fs/cpuacct.go b/cgroups/fs/cpuacct.go index 3382440d..c52049f3 100644 --- a/cgroups/fs/cpuacct.go +++ b/cgroups/fs/cpuacct.go @@ -15,8 +15,8 @@ import ( ) var ( - cpuCount = float64(runtime.NumCPU()) - clockTicks = float64(system.GetClockTicks()) + cpuCount = int64(runtime.NumCPU()) + clockTicks = int64(system.GetClockTicks()) ) type cpuacctGroup struct { @@ -34,11 +34,11 @@ func (s *cpuacctGroup) Remove(d *data) error { return removePath(d.path("cpuacct")) } -func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { +func (s *cpuacctGroup) Stats(d *data) (map[string]int64, error) { var ( - startCpu, lastCpu, startSystem, lastSystem, startUsage, lastUsage float64 - percentage float64 - paramData = make(map[string]float64) + startCpu, lastCpu, startSystem, lastSystem, startUsage, lastUsage int64 + percentage int64 + paramData = make(map[string]int64) ) path, err := d.path("cpuacct") if startCpu, err = s.getCpuUsage(d, path); err != nil { @@ -48,7 +48,7 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { return nil, err } startUsageTime := time.Now() - if startUsage, err = getCgroupParamFloat64(path, "cpuacct.usage"); err != nil { + if startUsage, err = getCgroupParamInt(path, "cpuacct.usage"); err != nil { return nil, err } // sample for 100ms @@ -60,7 +60,7 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { return nil, err } usageSampleDuration := time.Since(startUsageTime) - if lastUsage, err = getCgroupParamFloat64(path, "cpuacct.usage"); err != nil { + if lastUsage, err = getCgroupParamInt(path, "cpuacct.usage"); err != nil { return nil, err } @@ -77,19 +77,12 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { paramData["percentage"] = percentage // Delta usage is in nanoseconds of CPU time so get the usage (in cores) over the sample time. - paramData["usage"] = deltaUsage / float64(usageSampleDuration.Nanoseconds()) + paramData["usage"] = deltaUsage / usageSampleDuration.Nanoseconds() return paramData, nil } -func (s *cpuacctGroup) getProcStarttime(d *data) (float64, error) { - rawStart, err := system.GetProcessStartTime(d.pid) - if err != nil { - return 0, err - } - return strconv.ParseFloat(rawStart, 64) -} - -func (s *cpuacctGroup) getSystemCpuUsage(d *data) (float64, error) { +// TODO(vmarmol): Use cgroups stats. +func (s *cpuacctGroup) getSystemCpuUsage(d *data) (int64, error) { f, err := os.Open("/proc/stat") if err != nil { @@ -106,11 +99,11 @@ func (s *cpuacctGroup) getSystemCpuUsage(d *data) (float64, error) { return 0, fmt.Errorf("invalid number of cpu fields") } - var total float64 + var total int64 for _, i := range parts[1:8] { - v, err := strconv.ParseFloat(i, 64) + v, err := strconv.ParseInt(i, 10, 64) if err != nil { - return 0.0, fmt.Errorf("Unable to convert value %s to float: %s", i, err) + return 0.0, fmt.Errorf("Unable to convert value %s to int: %s", i, err) } total += v } @@ -122,11 +115,11 @@ func (s *cpuacctGroup) getSystemCpuUsage(d *data) (float64, error) { return 0, fmt.Errorf("invalid stat format") } -func (s *cpuacctGroup) getCpuUsage(d *data, path string) (float64, error) { - cpuTotal := 0.0 +func (s *cpuacctGroup) getCpuUsage(d *data, path string) (int64, error) { + cpuTotal := int64(0) f, err := os.Open(filepath.Join(path, "cpuacct.stat")) if err != nil { - return 0.0, err + return 0, err } defer f.Close() @@ -134,7 +127,7 @@ func (s *cpuacctGroup) getCpuUsage(d *data, path string) (float64, error) { for sc.Scan() { _, v, err := getCgroupParamKeyValue(sc.Text()) if err != nil { - return 0.0, err + return 0, err } // set the raw data in map cpuTotal += v diff --git a/cgroups/fs/cpuset.go b/cgroups/fs/cpuset.go index f9f65ba4..af2dd528 100644 --- a/cgroups/fs/cpuset.go +++ b/cgroups/fs/cpuset.go @@ -38,7 +38,7 @@ func (s *cpusetGroup) Remove(d *data) error { return removePath(d.path("cpuset")) } -func (s *cpusetGroup) Stats(d *data) (map[string]float64, error) { +func (s *cpusetGroup) Stats(d *data) (map[string]int64, error) { return nil, ErrNotSupportStat } diff --git a/cgroups/fs/devices.go b/cgroups/fs/devices.go index fd9a39d9..00fea608 100644 --- a/cgroups/fs/devices.go +++ b/cgroups/fs/devices.go @@ -55,6 +55,6 @@ func (s *devicesGroup) Remove(d *data) error { return removePath(d.path("devices")) } -func (s *devicesGroup) Stats(d *data) (map[string]float64, error) { +func (s *devicesGroup) Stats(d *data) (map[string]int64, error) { return nil, ErrNotSupportStat } diff --git a/cgroups/fs/freezer.go b/cgroups/fs/freezer.go index f3b1985a..0738ec1f 100644 --- a/cgroups/fs/freezer.go +++ b/cgroups/fs/freezer.go @@ -35,9 +35,9 @@ func (s *freezerGroup) Remove(d *data) error { return removePath(d.path("freezer")) } -func (s *freezerGroup) Stats(d *data) (map[string]float64, error) { +func (s *freezerGroup) Stats(d *data) (map[string]int64, error) { var ( - paramData = make(map[string]float64) + paramData = make(map[string]int64) params = []string{ "parent_freezing", "self_freezing", @@ -50,6 +50,7 @@ func (s *freezerGroup) Stats(d *data) (map[string]float64, error) { return nil, err } + // TODO(vmarmol): This currently outputs nothing since the output is a string, fix. for _, param := range params { f, err := os.Open(filepath.Join(path, fmt.Sprintf("freezer.%s", param))) if err != nil { @@ -62,7 +63,7 @@ func (s *freezerGroup) Stats(d *data) (map[string]float64, error) { return nil, err } - v, err := strconv.ParseFloat(strings.TrimSuffix(string(data), "\n"), 64) + v, err := strconv.ParseInt(strings.TrimSuffix(string(data), "\n"), 10, 64) if err != nil { return nil, err } diff --git a/cgroups/fs/memory.go b/cgroups/fs/memory.go index 837640c0..9964f837 100644 --- a/cgroups/fs/memory.go +++ b/cgroups/fs/memory.go @@ -50,8 +50,8 @@ func (s *memoryGroup) Remove(d *data) error { return removePath(d.path("memory")) } -func (s *memoryGroup) Stats(d *data) (map[string]float64, error) { - paramData := make(map[string]float64) +func (s *memoryGroup) Stats(d *data) (map[string]int64, error) { + paramData := make(map[string]int64) path, err := d.path("memory") if err != nil { return nil, err @@ -79,7 +79,7 @@ func (s *memoryGroup) Stats(d *data) (map[string]float64, error) { "max_usage_in_bytes", } for _, param := range params { - value, err := getCgroupParamFloat64(path, fmt.Sprintf("memory.%s", param)) + value, err := getCgroupParamInt(path, fmt.Sprintf("memory.%s", param)) if err != nil { return nil, err } diff --git a/cgroups/fs/memory_test.go b/cgroups/fs/memory_test.go index 6c1fb735..190d437b 100644 --- a/cgroups/fs/memory_test.go +++ b/cgroups/fs/memory_test.go @@ -25,7 +25,7 @@ func TestMemoryStats(t *testing.T) { if err != nil { t.Fatal(err) } - expectedStats := map[string]float64{"cache": 512.0, "rss": 1024.0, "usage_in_bytes": 2048.0, "max_usage_in_bytes": 4096.0} + expectedStats := map[string]int64{"cache": 512, "rss": 1024, "usage_in_bytes": 2048, "max_usage_in_bytes": 4096} expectStats(t, expectedStats, stats) } diff --git a/cgroups/fs/perf_event.go b/cgroups/fs/perf_event.go index 063cec4b..1cf1aeef 100644 --- a/cgroups/fs/perf_event.go +++ b/cgroups/fs/perf_event.go @@ -19,6 +19,6 @@ func (s *perfEventGroup) Remove(d *data) error { return removePath(d.path("perf_event")) } -func (s *perfEventGroup) Stats(d *data) (map[string]float64, error) { +func (s *perfEventGroup) Stats(d *data) (map[string]int64, error) { return nil, ErrNotSupportStat } diff --git a/cgroups/fs/test_util.go b/cgroups/fs/test_util.go index 11b90b21..333386c5 100644 --- a/cgroups/fs/test_util.go +++ b/cgroups/fs/test_util.go @@ -61,7 +61,7 @@ func (c *cgroupTestUtil) writeFileContents(fileContents map[string]string) { } // Expect the specified stats. -func expectStats(t *testing.T, expected, actual map[string]float64) { +func expectStats(t *testing.T, expected, actual map[string]int64) { for stat, expectedValue := range expected { actualValue, ok := actual[stat] if !ok { diff --git a/cgroups/fs/utils.go b/cgroups/fs/utils.go index 8be65c97..7213b5d6 100644 --- a/cgroups/fs/utils.go +++ b/cgroups/fs/utils.go @@ -16,25 +16,25 @@ var ( // Parses a cgroup param and returns as name, value // i.e. "io_service_bytes 1234" will return as io_service_bytes, 1234 -func getCgroupParamKeyValue(t string) (string, float64, error) { +func getCgroupParamKeyValue(t string) (string, int64, error) { parts := strings.Fields(t) switch len(parts) { case 2: - value, err := strconv.ParseFloat(parts[1], 64) + value, err := strconv.ParseInt(parts[1], 10, 64) if err != nil { - return "", 0.0, fmt.Errorf("Unable to convert param value to float: %s", err) + return "", 0, fmt.Errorf("Unable to convert param value to int: %s", err) } return parts[0], value, nil default: - return "", 0.0, ErrNotValidFormat + return "", 0, ErrNotValidFormat } } -// Gets a single float64 value from the specified cgroup file. -func getCgroupParamFloat64(cgroupPath, cgroupFile string) (float64, error) { +// Gets a single int64 value from the specified cgroup file. +func getCgroupParamInt(cgroupPath, cgroupFile string) (int64, error) { contents, err := ioutil.ReadFile(filepath.Join(cgroupPath, cgroupFile)) if err != nil { - return -1.0, err + return -1, err } - return strconv.ParseFloat(strings.TrimSpace(string(contents)), 64) + return strconv.ParseInt(strings.TrimSpace(string(contents)), 10, 64) } diff --git a/cgroups/fs/utils_test.go b/cgroups/fs/utils_test.go index c8f1b017..4dd2243e 100644 --- a/cgroups/fs/utils_test.go +++ b/cgroups/fs/utils_test.go @@ -13,7 +13,7 @@ const ( floatString = "2048" ) -func TestGetCgroupParamsFloat64(t *testing.T) { +func TestGetCgroupParamsInt(t *testing.T) { // Setup tempdir. tempDir, err := ioutil.TempDir("", "cgroup_utils_test") if err != nil { @@ -27,7 +27,7 @@ func TestGetCgroupParamsFloat64(t *testing.T) { if err != nil { t.Fatal(err) } - value, err := getCgroupParamFloat64(tempDir, cgroupFile) + value, err := getCgroupParamInt(tempDir, cgroupFile) if err != nil { t.Fatal(err) } else if value != floatValue { @@ -39,7 +39,7 @@ func TestGetCgroupParamsFloat64(t *testing.T) { if err != nil { t.Fatal(err) } - value, err = getCgroupParamFloat64(tempDir, cgroupFile) + value, err = getCgroupParamInt(tempDir, cgroupFile) if err != nil { t.Fatal(err) } else if value != floatValue { @@ -51,7 +51,7 @@ func TestGetCgroupParamsFloat64(t *testing.T) { if err != nil { t.Fatal(err) } - _, err = getCgroupParamFloat64(tempDir, cgroupFile) + _, err = getCgroupParamInt(tempDir, cgroupFile) if err == nil { t.Fatal("Expecting error, got none") } @@ -61,7 +61,7 @@ func TestGetCgroupParamsFloat64(t *testing.T) { if err != nil { t.Fatal(err) } - _, err = getCgroupParamFloat64(tempDir, cgroupFile) + _, err = getCgroupParamInt(tempDir, cgroupFile) if err == nil { t.Fatal("Expecting error, got none") }