Add external function to get cgroup stats
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
ab15aa234c
commit
dcbca94904
3 changed files with 70 additions and 10 deletions
|
@ -11,15 +11,15 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
subsystems = []subsystem{
|
||||
&devicesGroup{},
|
||||
&memoryGroup{},
|
||||
&cpuGroup{},
|
||||
&cpusetGroup{},
|
||||
&cpuacctGroup{},
|
||||
&blkioGroup{},
|
||||
&perfEventGroup{},
|
||||
&freezerGroup{},
|
||||
subsystems = map[string]subsystem{
|
||||
"devices": &devicesGroup{},
|
||||
"memory": &memoryGroup{},
|
||||
"cpu": &cpuGroup{},
|
||||
"cpuset": &cpusetGroup{},
|
||||
"cpuacct": &cpuacctGroup{},
|
||||
"blkio": &blkioGroup{},
|
||||
"perf_event": &perfEventGroup{},
|
||||
"freezer": &freezerGroup{},
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -73,6 +73,35 @@ func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
|
|||
return d, nil
|
||||
}
|
||||
|
||||
func GetStats(c *cgroups.Cgroup, subsystem string, pid int) (map[string]float64, error) {
|
||||
cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cgroupRoot = filepath.Dir(cgroupRoot)
|
||||
|
||||
if _, err := os.Stat(cgroupRoot); err != nil {
|
||||
return nil, fmt.Errorf("cgroups fs not found")
|
||||
}
|
||||
|
||||
cgroup := c.Name
|
||||
if c.Parent != "" {
|
||||
cgroup = filepath.Join(c.Parent, cgroup)
|
||||
}
|
||||
|
||||
d := &data{
|
||||
root: cgroupRoot,
|
||||
cgroup: cgroup,
|
||||
c: c,
|
||||
pid: pid,
|
||||
}
|
||||
sys, exists := subsystems[subsystem]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("subsystem %s does not exist", subsystem)
|
||||
}
|
||||
return sys.Stats(d)
|
||||
}
|
||||
|
||||
func (raw *data) path(subsystem string) (string, error) {
|
||||
initPath, err := cgroups.GetInitCgroupDir(subsystem)
|
||||
if err != nil {
|
||||
|
|
|
@ -24,6 +24,36 @@ func (s *blkioGroup) Remove(d *data) error {
|
|||
return removePath(d.path("blkio"))
|
||||
}
|
||||
|
||||
/*
|
||||
examples:
|
||||
|
||||
blkio.sectors
|
||||
8:0 6792
|
||||
|
||||
blkio.io_service_bytes
|
||||
8:0 Read 1282048
|
||||
8:0 Write 2195456
|
||||
8:0 Sync 2195456
|
||||
8:0 Async 1282048
|
||||
8:0 Total 3477504
|
||||
Total 3477504
|
||||
|
||||
blkio.io_serviced
|
||||
8:0 Read 124
|
||||
8:0 Write 104
|
||||
8:0 Sync 104
|
||||
8:0 Async 124
|
||||
8:0 Total 228
|
||||
Total 228
|
||||
|
||||
blkio.io_queued
|
||||
8:0 Read 0
|
||||
8:0 Write 0
|
||||
8:0 Sync 0
|
||||
8:0 Async 0
|
||||
8:0 Total 0
|
||||
Total 0
|
||||
*/
|
||||
func (s *blkioGroup) Stats(d *data) (map[string]float64, error) {
|
||||
var (
|
||||
paramData = make(map[string]float64)
|
||||
|
|
|
@ -61,7 +61,8 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
|
|||
if startTime, err = s.getProcStarttime(d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paramData["percentage"] = 100.0 * ((cpuTotal/100.0)/uptime - (startTime / 100))
|
||||
//paramData["percentage"] = 100.0 * ((cpuTotal/100.0)/uptime - (startTime / 100))
|
||||
paramData["percentage"] = cpuTotal / (uptime - (startTime / 100))
|
||||
|
||||
return paramData, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue