2014-04-20 18:34:28 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-04-20 19:02:39 +00:00
|
|
|
"errors"
|
2014-04-20 18:34:28 +00:00
|
|
|
"fmt"
|
2014-04-24 22:59:37 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2014-04-20 18:34:28 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-04-21 01:18:17 +00:00
|
|
|
var (
|
|
|
|
ErrNotSupportStat = errors.New("stats are not supported for subsystem")
|
|
|
|
ErrNotValidFormat = errors.New("line is not a valid key value format")
|
|
|
|
)
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-04-20 18:34:28 +00:00
|
|
|
// 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:
|
|
|
|
value, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0.0, fmt.Errorf("Unable to convert param value to float: %s", err)
|
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
return parts[0], value, nil
|
2014-04-20 18:34:28 +00:00
|
|
|
default:
|
2014-04-21 01:18:17 +00:00
|
|
|
return "", 0.0, ErrNotValidFormat
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-24 22:59:37 +00:00
|
|
|
|
|
|
|
// Gets a single float64 value from the specified cgroup file.
|
|
|
|
func getCgroupParamFloat64(cgroupPath, cgroupFile string) (float64, error) {
|
|
|
|
contents, err := ioutil.ReadFile(filepath.Join(cgroupPath, cgroupFile))
|
|
|
|
if err != nil {
|
|
|
|
return -1.0, err
|
|
|
|
}
|
|
|
|
return strconv.ParseFloat(strings.TrimSpace(string(contents)), 64)
|
|
|
|
}
|