1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Refactor stats and add them to all subsystems

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-04-20 12:02:39 -07:00
parent 86e34ce59f
commit 9b65f16355
10 changed files with 88 additions and 61 deletions

View file

@ -26,6 +26,7 @@ var (
type subsystem interface {
Set(*data) error
Remove(*data) error
Stats(*data) (map[string]float64, error)
}
type data struct {

View file

@ -25,29 +25,33 @@ func (s *blkioGroup) Remove(d *data) error {
}
func (s *blkioGroup) Stats(d *data) (map[string]float64, error) {
paramData := make(map[string]float64)
var (
paramData = make(map[string]float64)
params = []string{
"sectors",
"io_service_bytes",
"io_serviced",
"io_queued",
}
)
path, err := d.path("blkio")
if err != nil {
return paramData, fmt.Errorf("Unable to read %s cgroup param: %s", path, err)
}
params := []string{
"sectors",
"io_service_bytes",
"io_serviced",
"io_queued",
return nil, err
}
for _, param := range params {
p := fmt.Sprintf("blkio.%s", param)
f, err := os.Open(filepath.Join(path, p))
f, err := os.Open(filepath.Join(path, fmt.Sprintf("blkio.%s", param)))
if err != nil {
return paramData, err
return nil, err
}
defer f.Close()
sc := bufio.NewScanner(f)
for sc.Scan() {
_, v, err := getCgroupParamKeyValue(sc.Text())
if err != nil {
return paramData, fmt.Errorf("Error parsing param data: %s", err)
return nil, err
}
paramData[param] = v
}

View file

@ -25,3 +25,7 @@ func (s *cpuGroup) Set(d *data) error {
func (s *cpuGroup) Remove(d *data) error {
return removePath(d.path("cpu"))
}
func (s *cpuGroup) Stats(d *data) (map[string]float64, error) {
return nil, ErrNotSupportStat
}

View file

@ -2,7 +2,6 @@ package fs
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -10,6 +9,7 @@ import (
"strings"
"github.com/dotcloud/docker/pkg/cgroups"
"github.com/dotcloud/docker/pkg/system"
)
type cpuacctGroup struct {
@ -28,62 +28,62 @@ func (s *cpuacctGroup) Remove(d *data) error {
}
func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
paramData := make(map[string]float64)
var (
uptime, startTime float64
paramData = make(map[string]float64)
cpuTotal = 0.0
)
path, err := d.path("cpuacct")
if err != nil {
return paramData, fmt.Errorf("Unable to read %s cgroup param: %s", path, err)
}
f, err := os.Open(filepath.Join(path, "cpuacct.stat"))
if err != nil {
return paramData, err
}
f, err := os.Open(filepath.Join(path, "cpuacct.stat"))
if err != nil {
return nil, err
}
defer f.Close()
sc := bufio.NewScanner(f)
cpuTotal := 0.0
for sc.Scan() {
t, v, err := getCgroupParamKeyValue(sc.Text())
if err != nil {
return paramData, fmt.Errorf("Error parsing param data: %s", err)
return paramData, err
}
// set the raw data in map
paramData[t] = v
cpuTotal += v
}
// calculate percentage from jiffies
// get sys uptime
uf, err := os.Open("/proc/uptime")
if err != nil {
return paramData, fmt.Errorf("Unable to open /proc/uptime")
if uptime, err = s.getUptime(); err != nil {
return nil, err
}
defer uf.Close()
uptimeData, err := ioutil.ReadAll(uf)
if err != nil {
return paramData, fmt.Errorf("Error reading /proc/uptime: %s", err)
if startTime, err = s.getProcStarttime(d); err != nil {
return nil, err
}
uptimeFields := strings.Fields(string(uptimeData))
uptime, err := strconv.ParseFloat(uptimeFields[0], 64)
if err != nil {
return paramData, fmt.Errorf("Error parsing cpu stats: %s", err)
}
// find starttime of process
pf, err := os.Open(filepath.Join(path, "cgroup.procs"))
if err != nil {
return paramData, fmt.Errorf("Error parsing cpu stats: %s", err)
}
defer pf.Close()
pr := bufio.NewReader(pf)
l, _, err := pr.ReadLine()
if err != nil {
return paramData, fmt.Errorf("Error reading param file: %s", err)
}
starttime, err := strconv.ParseFloat(string(l), 64)
if err != nil {
return paramData, fmt.Errorf("Unable to parse starttime: %s", err)
}
// get total elapsed seconds since proc start
seconds := uptime - (starttime / 100)
// finally calc percentage
cpuPercentage := 100.0 * ((cpuTotal / 100.0) / float64(seconds))
paramData["percentage"] = cpuPercentage
paramData["percentage"] = 100.0 * ((cpuTotal/100.0)/uptime - (startTime / 100))
return paramData, nil
}
func (s *cpuacctGroup) getUptime() (float64, error) {
f, err := os.Open("/proc/uptime")
if err != nil {
return 0, err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return 0, err
}
return strconv.ParseFloat(strings.Fields(string(data))[0], 64)
}
func (s *cpuacctGroup) getProcStarttime(d *data) (float64, error) {
rawStart, err := system.GetProcessStartTime(d.pid)
if err != nil {
return 0, err
}
return strconv.ParseFloat(rawStart, 64)
}

View file

@ -30,3 +30,7 @@ func (s *cpusetGroup) Set(d *data) error {
func (s *cpusetGroup) Remove(d *data) error {
return removePath(d.path("cpuset"))
}
func (s *cpusetGroup) Stats(d *data) (map[string]float64, error) {
return nil, ErrNotSupportStat
}

View file

@ -63,3 +63,7 @@ func (s *devicesGroup) Set(d *data) error {
func (s *devicesGroup) Remove(d *data) error {
return removePath(d.path("devices"))
}
func (s *devicesGroup) Stats(d *data) (map[string]float64, error) {
return nil, ErrNotSupportStat
}

View file

@ -18,3 +18,7 @@ func (s *freezerGroup) Set(d *data) error {
func (s *freezerGroup) Remove(d *data) error {
return removePath(d.path("freezer"))
}
func (s *freezerGroup) Stats(d *data) (map[string]float64, error) {
return nil, ErrNotSupportStat
}

View file

@ -2,7 +2,6 @@ package fs
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
@ -51,19 +50,20 @@ func (s *memoryGroup) Stats(d *data) (map[string]float64, error) {
paramData := make(map[string]float64)
path, err := d.path("memory")
if err != nil {
fmt.Errorf("Unable to read %s cgroup param: %s", path, err)
return paramData, err
return nil, err
}
f, err := os.Open(filepath.Join(path, "memory.stat"))
if err != nil {
return paramData, err
return nil, err
}
defer f.Close()
sc := bufio.NewScanner(f)
for sc.Scan() {
t, v, err := getCgroupParamKeyValue(sc.Text())
if err != nil {
return paramData, fmt.Errorf("Error parsing param data: %s", err)
return nil, err
}
paramData[t] = v
}

View file

@ -18,3 +18,7 @@ func (s *perfEventGroup) Set(d *data) error {
func (s *perfEventGroup) Remove(d *data) error {
return removePath(d.path("perf_event"))
}
func (s *perfEventGroup) Stats(d *data) (map[string]float64, error) {
return nil, ErrNotSupportStat
}

View file

@ -1,23 +1,25 @@
package fs
import (
"errors"
"fmt"
"strconv"
"strings"
)
var ErrNotSupportStat = errors.New("stats are not supported for subsystem")
// 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:
name := parts[0]
value, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return "", 0.0, fmt.Errorf("Unable to convert param value to float: %s", err)
}
return name, value, nil
return parts[0], value, nil
default:
return "", 0.0, fmt.Errorf("Unable to parse cgroup param: not enough parts; expected 2")
}