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-05-14 22:21:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/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 (
|
2014-05-22 20:50:51 +00:00
|
|
|
cpuCount = int64(runtime.NumCPU())
|
|
|
|
clockTicks = int64(system.GetClockTicks())
|
2014-04-21 17:26:03 +00:00
|
|
|
)
|
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
|
|
|
|
2014-05-22 20:50:51 +00:00
|
|
|
func (s *cpuacctGroup) Stats(d *data) (map[string]int64, error) {
|
2014-04-20 19:02:39 +00:00
|
|
|
var (
|
2014-05-22 20:50:51 +00:00
|
|
|
startCpu, lastCpu, startSystem, lastSystem, startUsage, lastUsage int64
|
|
|
|
percentage int64
|
|
|
|
paramData = make(map[string]int64)
|
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-05-05 23:56:53 +00:00
|
|
|
startUsageTime := time.Now()
|
2014-05-22 20:50:51 +00:00
|
|
|
if startUsage, err = getCgroupParamInt(path, "cpuacct.usage"); err != nil {
|
2014-05-05 23:56:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
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-05-05 23:56:53 +00:00
|
|
|
usageSampleDuration := time.Since(startUsageTime)
|
2014-05-22 20:50:51 +00:00
|
|
|
if lastUsage, err = getCgroupParamInt(path, "cpuacct.usage"); err != nil {
|
2014-05-05 23:56:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-21 03:45:08 +00:00
|
|
|
|
2014-04-21 03:27:05 +00:00
|
|
|
var (
|
|
|
|
deltaProc = lastCpu - startCpu
|
|
|
|
deltaSystem = lastSystem - startSystem
|
2014-05-05 23:56:53 +00:00
|
|
|
deltaUsage = lastUsage - startUsage
|
2014-04-21 03:27:05 +00:00
|
|
|
)
|
|
|
|
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-05-05 23:56:53 +00:00
|
|
|
|
|
|
|
// Delta usage is in nanoseconds of CPU time so get the usage (in cores) over the sample time.
|
2014-05-22 20:50:51 +00:00
|
|
|
paramData["usage"] = deltaUsage / usageSampleDuration.Nanoseconds()
|
2014-04-20 19:02:39 +00:00
|
|
|
return paramData, nil
|
|
|
|
}
|
|
|
|
|
2014-05-22 20:50:51 +00:00
|
|
|
// TODO(vmarmol): Use cgroups stats.
|
|
|
|
func (s *cpuacctGroup) getSystemCpuUsage(d *data) (int64, 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
|
|
|
|
2014-05-22 20:50:51 +00:00
|
|
|
var total int64
|
2014-04-21 03:45:08 +00:00
|
|
|
for _, i := range parts[1:8] {
|
2014-05-22 20:50:51 +00:00
|
|
|
v, err := strconv.ParseInt(i, 10, 64)
|
2014-04-21 03:27:05 +00:00
|
|
|
if err != nil {
|
2014-05-22 20:50:51 +00:00
|
|
|
return 0.0, fmt.Errorf("Unable to convert value %s to int: %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-05-22 20:50:51 +00:00
|
|
|
func (s *cpuacctGroup) getCpuUsage(d *data, path string) (int64, error) {
|
|
|
|
cpuTotal := int64(0)
|
2014-04-21 03:27:05 +00:00
|
|
|
f, err := os.Open(filepath.Join(path, "cpuacct.stat"))
|
2014-04-20 18:34:28 +00:00
|
|
|
if err != nil {
|
2014-05-22 20:50:51 +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() {
|
|
|
|
_, v, err := getCgroupParamKeyValue(sc.Text())
|
|
|
|
if err != nil {
|
2014-05-22 20:50:51 +00:00
|
|
|
return 0, err
|
2014-04-21 03:27:05 +00:00
|
|
|
}
|
|
|
|
// set the raw data in map
|
|
|
|
cpuTotal += v
|
|
|
|
}
|
|
|
|
return cpuTotal, nil
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|