2014-04-19 04:55:06 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-04-20 18:34:28 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-04-21 01:18:17 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-04-20 18:34:28 +00:00
|
|
|
|
2014-05-14 22:21:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/cgroups"
|
2014-04-19 04:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type blkioGroup struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *blkioGroup) Set(d *data) error {
|
|
|
|
// we just want to join this group even though we don't set anything
|
|
|
|
if _, err := d.join("blkio"); err != nil && err != cgroups.ErrNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-19 05:17:31 +00:00
|
|
|
|
|
|
|
func (s *blkioGroup) Remove(d *data) error {
|
|
|
|
return removePath(d.path("blkio"))
|
|
|
|
}
|
2014-04-20 18:34:28 +00:00
|
|
|
|
2014-04-21 00:36:20 +00:00
|
|
|
/*
|
|
|
|
examples:
|
|
|
|
|
|
|
|
blkio.sectors
|
|
|
|
8:0 6792
|
|
|
|
|
|
|
|
blkio.io_service_bytes
|
|
|
|
8:0 Read 1282048
|
|
|
|
8:0 Write 2195456
|
|
|
|
8:0 Sync 2195456
|
|
|
|
8:0 Async 1282048
|
|
|
|
8:0 Total 3477504
|
|
|
|
Total 3477504
|
|
|
|
|
|
|
|
blkio.io_serviced
|
|
|
|
8:0 Read 124
|
|
|
|
8:0 Write 104
|
|
|
|
8:0 Sync 104
|
|
|
|
8:0 Async 124
|
|
|
|
8:0 Total 228
|
|
|
|
Total 228
|
|
|
|
|
|
|
|
blkio.io_queued
|
|
|
|
8:0 Read 0
|
|
|
|
8:0 Write 0
|
|
|
|
8:0 Sync 0
|
|
|
|
8:0 Async 0
|
|
|
|
8:0 Total 0
|
|
|
|
Total 0
|
|
|
|
*/
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-05-28 00:01:08 +00:00
|
|
|
func splitBlkioStatLine(r rune) bool {
|
|
|
|
return r == ' ' || r == ':'
|
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-05-28 00:01:08 +00:00
|
|
|
func getBlkioStat(path string) ([]cgroups.BlkioStatEntry, error) {
|
|
|
|
var blkioStats []cgroups.BlkioStatEntry
|
|
|
|
f, err := os.Open(path)
|
2014-04-21 01:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-28 00:01:08 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
for sc.Scan() {
|
|
|
|
// format: dev type amount
|
|
|
|
fields := strings.FieldsFunc(sc.Text(), splitBlkioStatLine)
|
|
|
|
if len(fields) < 3 {
|
|
|
|
if len(fields) == 2 && fields[0] == "Total" {
|
|
|
|
// skip total line
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Invalid line found while parsing %s: %s", path, sc.Text())
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 01:18:17 +00:00
|
|
|
|
2014-05-28 00:01:08 +00:00
|
|
|
v, err := strconv.ParseUint(fields[0], 10, 64)
|
2014-04-20 18:34:28 +00:00
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-05-28 00:01:08 +00:00
|
|
|
major := v
|
|
|
|
|
|
|
|
v, err = strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
minor := v
|
|
|
|
|
|
|
|
op := ""
|
|
|
|
valueField := 2
|
|
|
|
if len(fields) == 4 {
|
|
|
|
op = fields[2]
|
|
|
|
valueField = 3
|
|
|
|
}
|
|
|
|
v, err = strconv.ParseUint(fields[valueField], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-05-28 00:01:08 +00:00
|
|
|
blkioStats = append(blkioStats, cgroups.BlkioStatEntry{Major: major, Minor: minor, Op: op, Value: v})
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-05-28 00:01:08 +00:00
|
|
|
|
|
|
|
return blkioStats, nil
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-21 01:18:17 +00:00
|
|
|
|
2014-05-28 00:01:08 +00:00
|
|
|
func (s *blkioGroup) GetStats(d *data, stats *cgroups.Stats) error {
|
|
|
|
var blkioStats []cgroups.BlkioStatEntry
|
|
|
|
var err error
|
|
|
|
path, err := d.path("blkio")
|
2014-04-21 01:18:17 +00:00
|
|
|
if err != nil {
|
2014-05-28 00:01:08 +00:00
|
|
|
return err
|
2014-04-21 01:18:17 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 00:01:08 +00:00
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.sectors_recursive")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.SectorsRecursive = blkioStats
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_service_bytes_recursive")); err != nil {
|
|
|
|
return err
|
2014-04-21 01:18:17 +00:00
|
|
|
}
|
2014-05-28 00:01:08 +00:00
|
|
|
stats.BlkioStats.IoServiceBytesRecursive = blkioStats
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_serviced_recursive")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoServicedRecursive = blkioStats
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_queued_recursive")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoQueuedRecursive = blkioStats
|
|
|
|
|
|
|
|
return nil
|
2014-04-21 01:18:17 +00:00
|
|
|
}
|