Squashed commit of the following:
commit 75af1649b063abbc5d662fd2f8bc4ff62c927687 Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 01:32:42 2014 -0400 more refactor commit 43b36d0f15d634497127bcb17dacaa70ae92e903 Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 01:11:49 2014 -0400 refactored cgroup param parsing to util func commit e3738b0168a075bd92ec828879b0e46bdbbe3845 Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 00:57:19 2014 -0400 dat error checking commit 57872bcc59403ecd308cfe97c78f73d6ca58d165 Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 00:43:25 2014 -0400 proper use of fmt.Errorf commit 43dad6acc0cb21aac2b04ce074699879898ee820 Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 00:36:45 2014 -0400 proper placement of defer commit b7f20b934b2bc92cd39397dbc608b77bff28493c Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 00:34:39 2014 -0400 defers, error checking, panic avoidance commit 7a9a6ff267f8806dfe6676486f73fe89b72968fb Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 00:22:00 2014 -0400 data param to use container info instead of host commit 0e0cf7309be1644687160d6519db792b23cd26e9 Author: Evan Hazlett <ejhazlett@gmail.com> Date: Sun Apr 20 00:11:29 2014 -0400 added stats for cpuacct, memory, and blkio Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
810cf722cc
commit
b5b44ad439
4 changed files with 155 additions and 0 deletions
|
@ -1,6 +1,11 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/dotcloud/docker/pkg/cgroups"
|
||||
)
|
||||
|
||||
|
@ -18,3 +23,34 @@ func (s *blkioGroup) Set(d *data) error {
|
|||
func (s *blkioGroup) Remove(d *data) error {
|
||||
return removePath(d.path("blkio"))
|
||||
}
|
||||
|
||||
func (s *blkioGroup) Stats(d *data) (map[string]float64, error) {
|
||||
paramData := make(map[string]float64)
|
||||
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",
|
||||
}
|
||||
for _, param := range params {
|
||||
p := fmt.Sprintf("blkio.%s", param)
|
||||
f, err := os.Open(filepath.Join(path, p))
|
||||
if err != nil {
|
||||
return paramData, 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)
|
||||
}
|
||||
paramData[param] = v
|
||||
}
|
||||
}
|
||||
return paramData, nil
|
||||
}
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/dotcloud/docker/pkg/cgroups"
|
||||
)
|
||||
|
||||
|
@ -18,3 +26,64 @@ func (s *cpuacctGroup) Set(d *data) error {
|
|||
func (s *cpuacctGroup) Remove(d *data) error {
|
||||
return removePath(d.path("cpuacct"))
|
||||
}
|
||||
|
||||
func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
|
||||
paramData := make(map[string]float64)
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
// 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")
|
||||
}
|
||||
defer uf.Close()
|
||||
uptimeData, err := ioutil.ReadAll(uf)
|
||||
if err != nil {
|
||||
return paramData, fmt.Errorf("Error reading /proc/uptime: %s", 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
|
||||
return paramData, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
@ -43,3 +46,26 @@ func (s *memoryGroup) Set(d *data) error {
|
|||
func (s *memoryGroup) Remove(d *data) error {
|
||||
return removePath(d.path("memory"))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
f, err := os.Open(filepath.Join(path, "memory.stat"))
|
||||
if err != nil {
|
||||
return paramData, 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)
|
||||
}
|
||||
paramData[t] = v
|
||||
}
|
||||
return paramData, nil
|
||||
}
|
||||
|
|
24
cgroups/fs/utils.go
Normal file
24
cgroups/fs/utils.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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
|
||||
default:
|
||||
return "", 0.0, fmt.Errorf("Unable to parse cgroup param: not enough parts; expected 2")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue