2014-04-19 04:55:06 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-04-20 18:34:28 +00:00
|
|
|
"bufio"
|
2014-04-21 03:27:05 +00:00
|
|
|
"fmt"
|
2014-04-20 18:34:28 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-04-21 03:27:05 +00:00
|
|
|
"runtime"
|
2014-04-20 18:34:28 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-04-21 03:27:05 +00:00
|
|
|
"time"
|
2014-04-20 18:34:28 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2014-04-21 17:26:03 +00:00
|
|
|
var (
|
|
|
|
cpuCount = float64(runtime.NumCPU())
|
|
|
|
clockTicks = float64(system.GetClockTicks())
|
|
|
|
)
|
2014-04-21 03:45:08 +00:00
|
|
|
|
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 (
|
2014-04-21 03:27:05 +00:00
|
|
|
startCpu, lastCpu, startSystem, lastSystem float64
|
2014-04-21 03:45:08 +00:00
|
|
|
percentage float64
|
2014-04-21 03:27:05 +00:00
|
|
|
paramData = make(map[string]float64)
|
2014-04-20 19:02:39 +00:00
|
|
|
)
|
2014-04-20 18:34:28 +00:00
|
|
|
path, err := d.path("cpuacct")
|
2014-04-21 03:27:05 +00:00
|
|
|
if startCpu, err = s.getCpuUsage(d, path); err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-21 03:27:05 +00:00
|
|
|
if startSystem, err = s.getSystemCpuUsage(d); err != nil {
|
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-21 03:27:05 +00:00
|
|
|
// sample for 100ms
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if lastCpu, err = s.getCpuUsage(d, path); err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-21 03:27:05 +00:00
|
|
|
if lastSystem, err = s.getSystemCpuUsage(d); err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-21 03:45:08 +00:00
|
|
|
|
2014-04-21 03:27:05 +00:00
|
|
|
var (
|
|
|
|
deltaProc = lastCpu - startCpu
|
|
|
|
deltaSystem = lastSystem - startSystem
|
|
|
|
)
|
|
|
|
if deltaSystem > 0.0 {
|
2014-04-21 17:26:03 +00:00
|
|
|
percentage = ((deltaProc / deltaSystem) * clockTicks) * cpuCount
|
2014-04-21 03:27:05 +00:00
|
|
|
}
|
2014-04-21 03:45:08 +00:00
|
|
|
// NOTE: a percentage over 100% is valid for POSIX because that means the
|
|
|
|
// processes is using multiple cores
|
2014-04-21 03:27:05 +00:00
|
|
|
paramData["percentage"] = percentage
|
2014-04-20 19:02:39 +00:00
|
|
|
return paramData, nil
|
|
|
|
}
|
|
|
|
|
2014-04-21 03:27:05 +00:00
|
|
|
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-21 03:27:05 +00:00
|
|
|
return strconv.ParseFloat(rawStart, 64)
|
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-04-21 03:27:05 +00:00
|
|
|
func (s *cpuacctGroup) getSystemCpuUsage(d *data) (float64, error) {
|
2014-04-21 03:45:08 +00:00
|
|
|
|
2014-04-21 03:27:05 +00:00
|
|
|
f, err := os.Open("/proc/stat")
|
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-21 03:27:05 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
for sc.Scan() {
|
2014-04-21 03:45:08 +00:00
|
|
|
parts := strings.Fields(sc.Text())
|
|
|
|
switch parts[0] {
|
|
|
|
case "cpu":
|
|
|
|
if len(parts) < 8 {
|
|
|
|
return 0, fmt.Errorf("invalid number of cpu fields")
|
2014-04-21 03:27:05 +00:00
|
|
|
}
|
2014-04-21 03:45:08 +00:00
|
|
|
|
|
|
|
var total float64
|
|
|
|
for _, i := range parts[1:8] {
|
|
|
|
v, err := strconv.ParseFloat(i, 64)
|
2014-04-21 03:27:05 +00:00
|
|
|
if err != nil {
|
2014-04-21 03:45:08 +00:00
|
|
|
return 0.0, fmt.Errorf("Unable to convert value %s to float: %s", i, err)
|
2014-04-21 03:27:05 +00:00
|
|
|
}
|
2014-04-21 03:45:08 +00:00
|
|
|
total += v
|
2014-04-21 03:27:05 +00:00
|
|
|
}
|
2014-04-21 03:45:08 +00:00
|
|
|
return total, nil
|
|
|
|
default:
|
|
|
|
continue
|
2014-04-21 03:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-21 03:45:08 +00:00
|
|
|
return 0, fmt.Errorf("invalid stat format")
|
2014-04-20 19:02:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 03:27:05 +00:00
|
|
|
func (s *cpuacctGroup) getCpuUsage(d *data, path string) (float64, error) {
|
|
|
|
cpuTotal := 0.0
|
|
|
|
f, err := os.Open(filepath.Join(path, "cpuacct.stat"))
|
2014-04-20 18:34:28 +00:00
|
|
|
if err != nil {
|
2014-04-21 03:27:05 +00:00
|
|
|
return 0.0, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-21 03:27:05 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
for sc.Scan() {
|
|
|
|
_, v, err := getCgroupParamKeyValue(sc.Text())
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
// set the raw data in map
|
|
|
|
cpuTotal += v
|
|
|
|
}
|
|
|
|
return cpuTotal, nil
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|