2014-04-19 04:55:06 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-04-20 18:34:28 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2014-04-21 01:18:17 +00:00
|
|
|
"io/ioutil"
|
2014-04-20 18:34:28 +00:00
|
|
|
"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 18:34:28 +00:00
|
|
|
func (s *blkioGroup) Stats(d *data) (map[string]float64, error) {
|
2014-04-20 19:02:39 +00:00
|
|
|
var (
|
|
|
|
paramData = make(map[string]float64)
|
|
|
|
params = []string{
|
2014-04-21 01:18:17 +00:00
|
|
|
"io_service_bytes_recursive",
|
|
|
|
"io_serviced_recursive",
|
|
|
|
"io_queued_recursive",
|
2014-04-20 19:02:39 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2014-04-20 18:34:28 +00:00
|
|
|
path, err := d.path("blkio")
|
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-04-21 01:18:17 +00:00
|
|
|
k, v, err := s.getSectors(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
paramData[fmt.Sprintf("blkio.sectors_recursive:%s", k)] = v
|
|
|
|
|
2014-04-20 18:34:28 +00:00
|
|
|
for _, param := range params {
|
2014-04-20 19:02:39 +00:00
|
|
|
f, err := os.Open(filepath.Join(path, fmt.Sprintf("blkio.%s", param)))
|
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
|
|
|
}
|
|
|
|
defer f.Close()
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-04-20 18:34:28 +00:00
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
for sc.Scan() {
|
2014-04-21 01:18:17 +00:00
|
|
|
// 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
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paramData, nil
|
|
|
|
}
|
2014-04-21 01:18:17 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|