From 3bd149f766f24d1cb767d151b852791ca2eedc98 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Sun, 20 Apr 2014 18:18:17 -0700 Subject: [PATCH] Fix parsing of blkio files Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- cgroups/fs/blkio.go | 47 +++++++++++++++++++++++++++++++++++++-------- cgroups/fs/utils.go | 7 +++++-- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/cgroups/fs/blkio.go b/cgroups/fs/blkio.go index 151f2b4..79e14fa 100644 --- a/cgroups/fs/blkio.go +++ b/cgroups/fs/blkio.go @@ -3,8 +3,11 @@ package fs import ( "bufio" "fmt" + "io/ioutil" "os" "path/filepath" + "strconv" + "strings" "github.com/dotcloud/docker/pkg/cgroups" ) @@ -58,10 +61,9 @@ func (s *blkioGroup) Stats(d *data) (map[string]float64, error) { var ( paramData = make(map[string]float64) params = []string{ - "sectors", - "io_service_bytes", - "io_serviced", - "io_queued", + "io_service_bytes_recursive", + "io_serviced_recursive", + "io_queued_recursive", } ) @@ -70,6 +72,12 @@ func (s *blkioGroup) Stats(d *data) (map[string]float64, error) { return nil, err } + k, v, err := s.getSectors(path) + if err != nil { + return nil, err + } + paramData[fmt.Sprintf("blkio.sectors_recursive:%s", k)] = v + for _, param := range params { f, err := os.Open(filepath.Join(path, fmt.Sprintf("blkio.%s", param))) if err != nil { @@ -79,12 +87,35 @@ func (s *blkioGroup) Stats(d *data) (map[string]float64, error) { sc := bufio.NewScanner(f) for sc.Scan() { - _, v, err := getCgroupParamKeyValue(sc.Text()) - if err != nil { - return nil, err + // format: dev type amount + fields := strings.Fields(sc.Text()) + switch len(fields) { + case 3: + v, err := strconv.ParseFloat(fields[2], 64) + if err != nil { + return nil, err + } + paramData[fmt.Sprintf("%s:%s:%s", param, fields[0], fields[1])] = v + case 2: + // this is the total line, skip + default: + return nil, ErrNotValidFormat } - paramData[param] = v } } return paramData, nil } + +func (s *blkioGroup) getSectors(path string) (string, float64, error) { + f, err := os.Open(filepath.Join(path, "blkio.sectors_recursive")) + if err != nil { + return "", 0, err + } + defer f.Close() + + data, err := ioutil.ReadAll(f) + if err != nil { + return "", 0, err + } + return getCgroupParamKeyValue(string(data)) +} diff --git a/cgroups/fs/utils.go b/cgroups/fs/utils.go index 6a0838f..f4c4846 100644 --- a/cgroups/fs/utils.go +++ b/cgroups/fs/utils.go @@ -7,7 +7,10 @@ import ( "strings" ) -var ErrNotSupportStat = errors.New("stats are not supported for subsystem") +var ( + ErrNotSupportStat = errors.New("stats are not supported for subsystem") + ErrNotValidFormat = errors.New("line is not a valid key value format") +) // Parses a cgroup param and returns as name, value // i.e. "io_service_bytes 1234" will return as io_service_bytes, 1234 @@ -21,6 +24,6 @@ func getCgroupParamKeyValue(t string) (string, float64, error) { } return parts[0], value, nil default: - return "", 0.0, fmt.Errorf("Unable to parse cgroup param: not enough parts; expected 2") + return "", 0.0, ErrNotValidFormat } }