2014-04-19 04:55:06 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-04-20 18:34:28 +00:00
|
|
|
"bufio"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2014-04-19 04:55:06 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups"
|
2014-04-20 19:02:39 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-04-19 04:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cpuacctGroup struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cpuacctGroup) Set(d *data) error {
|
|
|
|
// we just want to join this group even though we don't set anything
|
|
|
|
if _, err := d.join("cpuacct"); err != nil && err != cgroups.ErrNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-19 05:17:31 +00:00
|
|
|
|
|
|
|
func (s *cpuacctGroup) Remove(d *data) error {
|
|
|
|
return removePath(d.path("cpuacct"))
|
|
|
|
}
|
2014-04-20 18:34:28 +00:00
|
|
|
|
|
|
|
func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
|
2014-04-20 19:02:39 +00:00
|
|
|
var (
|
|
|
|
uptime, startTime float64
|
|
|
|
paramData = make(map[string]float64)
|
|
|
|
cpuTotal = 0.0
|
|
|
|
)
|
|
|
|
|
2014-04-20 18:34:28 +00:00
|
|
|
path, err := d.path("cpuacct")
|
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return paramData, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
|
|
|
f, err := os.Open(filepath.Join(path, "cpuacct.stat"))
|
|
|
|
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() {
|
|
|
|
t, v, err := getCgroupParamKeyValue(sc.Text())
|
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return paramData, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
|
|
|
// set the raw data in map
|
|
|
|
paramData[t] = v
|
|
|
|
cpuTotal += v
|
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
|
|
|
|
if uptime, err = s.getUptime(); err != nil {
|
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
if startTime, err = s.getProcStarttime(d); err != nil {
|
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-21 00:36:20 +00:00
|
|
|
//paramData["percentage"] = 100.0 * ((cpuTotal/100.0)/uptime - (startTime / 100))
|
|
|
|
paramData["percentage"] = cpuTotal / (uptime - (startTime / 100))
|
2014-04-20 19:02:39 +00:00
|
|
|
|
|
|
|
return paramData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cpuacctGroup) getUptime() (float64, error) {
|
|
|
|
f, err := os.Open("/proc/uptime")
|
2014-04-20 18:34:28 +00:00
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return 0, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(f)
|
2014-04-20 18:34:28 +00:00
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return 0, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
return strconv.ParseFloat(strings.Fields(string(data))[0], 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cpuacctGroup) getProcStarttime(d *data) (float64, error) {
|
|
|
|
rawStart, err := system.GetProcessStartTime(d.pid)
|
2014-04-20 18:34:28 +00:00
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return 0, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
return strconv.ParseFloat(rawStart, 64)
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|