From ab15aa234c0915980ee2e941d3bfc78e932f0c14 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Sun, 20 Apr 2014 12:02:39 -0700 Subject: [PATCH] Refactor stats and add them to all subsystems Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- cgroups/fs/apply_raw.go | 1 + cgroups/fs/blkio.go | 28 ++++++++------ cgroups/fs/cpu.go | 4 ++ cgroups/fs/cpuacct.go | 84 ++++++++++++++++++++-------------------- cgroups/fs/cpuset.go | 4 ++ cgroups/fs/devices.go | 4 ++ cgroups/fs/freezer.go | 4 ++ cgroups/fs/memory.go | 10 ++--- cgroups/fs/perf_event.go | 4 ++ cgroups/fs/utils.go | 6 ++- 10 files changed, 88 insertions(+), 61 deletions(-) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index cdb2b35..ecc4bb6 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -26,6 +26,7 @@ var ( type subsystem interface { Set(*data) error Remove(*data) error + Stats(*data) (map[string]float64, error) } type data struct { diff --git a/cgroups/fs/blkio.go b/cgroups/fs/blkio.go index fae8d5a..abf8fb6 100644 --- a/cgroups/fs/blkio.go +++ b/cgroups/fs/blkio.go @@ -25,29 +25,33 @@ func (s *blkioGroup) Remove(d *data) error { } func (s *blkioGroup) Stats(d *data) (map[string]float64, error) { - paramData := make(map[string]float64) + var ( + paramData = make(map[string]float64) + params = []string{ + "sectors", + "io_service_bytes", + "io_serviced", + "io_queued", + } + ) + path, err := d.path("blkio") if err != nil { - return paramData, fmt.Errorf("Unable to read %s cgroup param: %s", path, err) - } - params := []string{ - "sectors", - "io_service_bytes", - "io_serviced", - "io_queued", + return nil, err } + for _, param := range params { - p := fmt.Sprintf("blkio.%s", param) - f, err := os.Open(filepath.Join(path, p)) + f, err := os.Open(filepath.Join(path, fmt.Sprintf("blkio.%s", param))) if err != nil { - return paramData, err + return nil, err } defer f.Close() + sc := bufio.NewScanner(f) for sc.Scan() { _, v, err := getCgroupParamKeyValue(sc.Text()) if err != nil { - return paramData, fmt.Errorf("Error parsing param data: %s", err) + return nil, err } paramData[param] = v } diff --git a/cgroups/fs/cpu.go b/cgroups/fs/cpu.go index f458d79..5534443 100644 --- a/cgroups/fs/cpu.go +++ b/cgroups/fs/cpu.go @@ -25,3 +25,7 @@ func (s *cpuGroup) Set(d *data) error { func (s *cpuGroup) Remove(d *data) error { return removePath(d.path("cpu")) } + +func (s *cpuGroup) Stats(d *data) (map[string]float64, error) { + return nil, ErrNotSupportStat +} diff --git a/cgroups/fs/cpuacct.go b/cgroups/fs/cpuacct.go index 621592e..b0b14a1 100644 --- a/cgroups/fs/cpuacct.go +++ b/cgroups/fs/cpuacct.go @@ -2,7 +2,6 @@ package fs import ( "bufio" - "fmt" "io/ioutil" "os" "path/filepath" @@ -10,6 +9,7 @@ import ( "strings" "github.com/dotcloud/docker/pkg/cgroups" + "github.com/dotcloud/docker/pkg/system" ) type cpuacctGroup struct { @@ -28,62 +28,62 @@ func (s *cpuacctGroup) Remove(d *data) error { } func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { - paramData := make(map[string]float64) + var ( + uptime, startTime float64 + paramData = make(map[string]float64) + cpuTotal = 0.0 + ) + path, err := d.path("cpuacct") - if err != nil { - return paramData, fmt.Errorf("Unable to read %s cgroup param: %s", path, err) - } - f, err := os.Open(filepath.Join(path, "cpuacct.stat")) if err != nil { return paramData, err } + f, err := os.Open(filepath.Join(path, "cpuacct.stat")) + if err != nil { + return nil, err + } defer f.Close() + sc := bufio.NewScanner(f) - cpuTotal := 0.0 for sc.Scan() { t, v, err := getCgroupParamKeyValue(sc.Text()) if err != nil { - return paramData, fmt.Errorf("Error parsing param data: %s", err) + return paramData, err } // set the raw data in map paramData[t] = v cpuTotal += v } - // calculate percentage from jiffies - // get sys uptime - uf, err := os.Open("/proc/uptime") - if err != nil { - return paramData, fmt.Errorf("Unable to open /proc/uptime") + + if uptime, err = s.getUptime(); err != nil { + return nil, err } - defer uf.Close() - uptimeData, err := ioutil.ReadAll(uf) - if err != nil { - return paramData, fmt.Errorf("Error reading /proc/uptime: %s", err) + if startTime, err = s.getProcStarttime(d); err != nil { + return nil, err } - uptimeFields := strings.Fields(string(uptimeData)) - uptime, err := strconv.ParseFloat(uptimeFields[0], 64) - if err != nil { - return paramData, fmt.Errorf("Error parsing cpu stats: %s", err) - } - // find starttime of process - pf, err := os.Open(filepath.Join(path, "cgroup.procs")) - if err != nil { - return paramData, fmt.Errorf("Error parsing cpu stats: %s", err) - } - defer pf.Close() - pr := bufio.NewReader(pf) - l, _, err := pr.ReadLine() - if err != nil { - return paramData, fmt.Errorf("Error reading param file: %s", err) - } - starttime, err := strconv.ParseFloat(string(l), 64) - if err != nil { - return paramData, fmt.Errorf("Unable to parse starttime: %s", err) - } - // get total elapsed seconds since proc start - seconds := uptime - (starttime / 100) - // finally calc percentage - cpuPercentage := 100.0 * ((cpuTotal / 100.0) / float64(seconds)) - paramData["percentage"] = cpuPercentage + paramData["percentage"] = 100.0 * ((cpuTotal/100.0)/uptime - (startTime / 100)) + return paramData, nil } + +func (s *cpuacctGroup) getUptime() (float64, error) { + f, err := os.Open("/proc/uptime") + if err != nil { + return 0, err + } + defer f.Close() + + data, err := ioutil.ReadAll(f) + if err != nil { + return 0, err + } + return strconv.ParseFloat(strings.Fields(string(data))[0], 64) +} + +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) +} diff --git a/cgroups/fs/cpuset.go b/cgroups/fs/cpuset.go index 3d3b15f..8a13c56 100644 --- a/cgroups/fs/cpuset.go +++ b/cgroups/fs/cpuset.go @@ -30,3 +30,7 @@ func (s *cpusetGroup) Set(d *data) error { func (s *cpusetGroup) Remove(d *data) error { return removePath(d.path("cpuset")) } + +func (s *cpusetGroup) Stats(d *data) (map[string]float64, error) { + return nil, ErrNotSupportStat +} diff --git a/cgroups/fs/devices.go b/cgroups/fs/devices.go index fc5d83b..a2f91ed 100644 --- a/cgroups/fs/devices.go +++ b/cgroups/fs/devices.go @@ -63,3 +63,7 @@ func (s *devicesGroup) Set(d *data) error { func (s *devicesGroup) Remove(d *data) error { return removePath(d.path("devices")) } + +func (s *devicesGroup) Stats(d *data) (map[string]float64, error) { + return nil, ErrNotSupportStat +} diff --git a/cgroups/fs/freezer.go b/cgroups/fs/freezer.go index 05bc584..878a3a5 100644 --- a/cgroups/fs/freezer.go +++ b/cgroups/fs/freezer.go @@ -18,3 +18,7 @@ func (s *freezerGroup) Set(d *data) error { func (s *freezerGroup) Remove(d *data) error { return removePath(d.path("freezer")) } + +func (s *freezerGroup) Stats(d *data) (map[string]float64, error) { + return nil, ErrNotSupportStat +} diff --git a/cgroups/fs/memory.go b/cgroups/fs/memory.go index 26281c1..cf4bf5a 100644 --- a/cgroups/fs/memory.go +++ b/cgroups/fs/memory.go @@ -2,7 +2,6 @@ package fs import ( "bufio" - "fmt" "os" "path/filepath" "strconv" @@ -51,19 +50,20 @@ func (s *memoryGroup) Stats(d *data) (map[string]float64, error) { paramData := make(map[string]float64) path, err := d.path("memory") if err != nil { - fmt.Errorf("Unable to read %s cgroup param: %s", path, err) - return paramData, err + return nil, err } + f, err := os.Open(filepath.Join(path, "memory.stat")) if err != nil { - return paramData, err + return nil, err } defer f.Close() + sc := bufio.NewScanner(f) for sc.Scan() { t, v, err := getCgroupParamKeyValue(sc.Text()) if err != nil { - return paramData, fmt.Errorf("Error parsing param data: %s", err) + return nil, err } paramData[t] = v } diff --git a/cgroups/fs/perf_event.go b/cgroups/fs/perf_event.go index b5ec6c6..789b3e5 100644 --- a/cgroups/fs/perf_event.go +++ b/cgroups/fs/perf_event.go @@ -18,3 +18,7 @@ func (s *perfEventGroup) Set(d *data) error { func (s *perfEventGroup) Remove(d *data) error { return removePath(d.path("perf_event")) } + +func (s *perfEventGroup) Stats(d *data) (map[string]float64, error) { + return nil, ErrNotSupportStat +} diff --git a/cgroups/fs/utils.go b/cgroups/fs/utils.go index 260cf13..6a0838f 100644 --- a/cgroups/fs/utils.go +++ b/cgroups/fs/utils.go @@ -1,23 +1,25 @@ package fs import ( + "errors" "fmt" "strconv" "strings" ) +var ErrNotSupportStat = errors.New("stats are not supported for subsystem") + // 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) { parts := strings.Fields(t) switch len(parts) { case 2: - name := parts[0] value, err := strconv.ParseFloat(parts[1], 64) if err != nil { return "", 0.0, fmt.Errorf("Unable to convert param value to float: %s", err) } - return name, value, nil + return parts[0], value, nil default: return "", 0.0, fmt.Errorf("Unable to parse cgroup param: not enough parts; expected 2") }