2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
type BlkioGroup struct {
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-09 13:46:30 +08:00
|
|
|
func (s *BlkioGroup) Apply(d *data) error {
|
2015-01-19 14:29:02 +08:00
|
|
|
dir, err := d.join("blkio")
|
2015-03-24 03:14:03 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-08-21 01:32:01 +08:00
|
|
|
|
2015-02-27 12:09:42 +08:00
|
|
|
if err := s.Set(dir, d.c); err != nil {
|
|
|
|
return err
|
2015-02-25 17:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BlkioGroup) Set(path string, cgroup *configs.Cgroup) error {
|
|
|
|
if cgroup.BlkioWeight != 0 {
|
|
|
|
if err := writeFile(path, "blkio.weight", strconv.FormatInt(cgroup.BlkioWeight, 10)); err != nil {
|
2015-01-19 14:29:02 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 14:11:29 +08:00
|
|
|
if cgroup.BlkioWeightDevice != "" {
|
|
|
|
if err := writeFile(path, "blkio.weight_device", cgroup.BlkioWeightDevice); err != nil {
|
2015-04-17 15:27:21 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.BlkioThrottleReadBpsDevice != "" {
|
|
|
|
if err := writeFile(path, "blkio.throttle.read_bps_device", cgroup.BlkioThrottleReadBpsDevice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.BlkioThrottleWriteBpsDevice != "" {
|
|
|
|
if err := writeFile(path, "blkio.throttle.write_bps_device", cgroup.BlkioThrottleWriteBpsDevice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.BlkioThrottleReadIOpsDevice != "" {
|
|
|
|
if err := writeFile(path, "blkio.throttle.read_iops_device", cgroup.BlkioThrottleReadIOpsDevice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.BlkioThrottleWriteIOpsDevice != "" {
|
|
|
|
if err := writeFile(path, "blkio.throttle.write_iops_device", cgroup.BlkioThrottleWriteIOpsDevice); err != nil {
|
2015-04-08 14:11:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *BlkioGroup) Remove(d *data) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
return removePath(d.path("blkio"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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-05-28 08:01:08 +08:00
|
|
|
func splitBlkioStatLine(r rune) bool {
|
|
|
|
return r == ' ' || r == ':'
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2014-05-28 08:01:08 +08:00
|
|
|
func getBlkioStat(path string) ([]cgroups.BlkioStatEntry, error) {
|
|
|
|
var blkioStats []cgroups.BlkioStatEntry
|
|
|
|
f, err := os.Open(path)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
2014-07-10 00:39:38 +08:00
|
|
|
if os.IsNotExist(err) {
|
2014-07-09 05:25:55 +08:00
|
|
|
return blkioStats, nil
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-28 08:01:08 +08: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-05-15 06:21:44 +08:00
|
|
|
|
2014-05-28 08:01:08 +08:00
|
|
|
v, err := strconv.ParseUint(fields[0], 10, 64)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-28 08:01:08 +08: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-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
blkioStats = append(blkioStats, cgroups.BlkioStatEntry{Major: major, Minor: minor, Op: op, Value: v})
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
|
|
|
|
return blkioStats, nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *BlkioGroup) GetStats(path string, stats *cgroups.Stats) error {
|
2014-08-27 19:42:38 +08:00
|
|
|
// Try to read CFQ stats available on all CFQ enabled kernels first
|
|
|
|
if blkioStats, err := getBlkioStat(filepath.Join(path, "blkio.io_serviced_recursive")); err == nil && blkioStats != nil {
|
|
|
|
return getCFQStats(path, stats)
|
|
|
|
}
|
|
|
|
return getStats(path, stats) // Use generic stats as fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCFQStats(path string, stats *cgroups.Stats) error {
|
2014-05-28 08:01:08 +08:00
|
|
|
var blkioStats []cgroups.BlkioStatEntry
|
|
|
|
var err error
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2014-05-28 08:01:08 +08: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-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08: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
|
|
|
|
|
2014-10-20 17:25:03 +08:00
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_service_time_recursive")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoServiceTimeRecursive = blkioStats
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_wait_time_recursive")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoWaitTimeRecursive = blkioStats
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_merged_recursive")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoMergedRecursive = blkioStats
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.time_recursive")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoTimeRecursive = blkioStats
|
|
|
|
|
2014-05-28 08:01:08 +08:00
|
|
|
return nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-08-27 19:42:38 +08:00
|
|
|
|
|
|
|
func getStats(path string, stats *cgroups.Stats) error {
|
|
|
|
var blkioStats []cgroups.BlkioStatEntry
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.throttle.io_service_bytes")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoServiceBytesRecursive = blkioStats
|
|
|
|
|
|
|
|
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.throttle.io_serviced")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.BlkioStats.IoServicedRecursive = blkioStats
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|