1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Add memory usage and max usage stats.

Docker-DCO-1.1-Signed-off-by: Victor Marmol <vmarmol@google.com> (github: vmarmol)
This commit is contained in:
Victor Marmol 2014-04-24 22:59:37 +00:00
parent a368c0dd50
commit ad924959a9
3 changed files with 98 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package fs
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
@ -56,13 +57,14 @@ func (s *memoryGroup) Stats(d *data) (map[string]float64, error) {
return nil, err
}
f, err := os.Open(filepath.Join(path, "memory.stat"))
// Set stats from memory.stat.
statsFile, err := os.Open(filepath.Join(path, "memory.stat"))
if err != nil {
return nil, err
}
defer f.Close()
defer statsFile.Close()
sc := bufio.NewScanner(f)
sc := bufio.NewScanner(statsFile)
for sc.Scan() {
t, v, err := getCgroupParamKeyValue(sc.Text())
if err != nil {
@ -70,5 +72,19 @@ func (s *memoryGroup) Stats(d *data) (map[string]float64, error) {
}
paramData[t] = v
}
// Set memory usage and max historical usage.
params := []string{
"usage_in_bytes",
"max_usage_in_bytes",
}
for _, param := range params {
value, err := getCgroupParamFloat64(path, fmt.Sprintf("memory.%s", param))
if err != nil {
return nil, err
}
paramData[param] = value
}
return paramData, nil
}

View file

@ -3,6 +3,8 @@ package fs
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
)
@ -27,3 +29,12 @@ func getCgroupParamKeyValue(t string) (string, float64, error) {
return "", 0.0, ErrNotValidFormat
}
}
// Gets a single float64 value from the specified cgroup file.
func getCgroupParamFloat64(cgroupPath, cgroupFile string) (float64, error) {
contents, err := ioutil.ReadFile(filepath.Join(cgroupPath, cgroupFile))
if err != nil {
return -1.0, err
}
return strconv.ParseFloat(strings.TrimSpace(string(contents)), 64)
}

View file

@ -0,0 +1,68 @@
package fs
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
const (
cgroupFile = "cgroup.file"
floatValue = 2048.0
floatString = "2048"
)
func TestGetCgroupParamsFloat64(t *testing.T) {
// Setup tempdir.
tempDir, err := ioutil.TempDir("", "cgroup_utils_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempFile := filepath.Join(tempDir, cgroupFile)
// Success.
err = ioutil.WriteFile(tempFile, []byte(floatString), 0755)
if err != nil {
t.Fatal(err)
}
value, err := getCgroupParamFloat64(tempDir, cgroupFile)
if err != nil {
t.Fatal(err)
} else if value != floatValue {
t.Fatalf("Expected %f to equal %f", value, floatValue)
}
// Success with new line.
err = ioutil.WriteFile(tempFile, []byte(floatString+"\n"), 0755)
if err != nil {
t.Fatal(err)
}
value, err = getCgroupParamFloat64(tempDir, cgroupFile)
if err != nil {
t.Fatal(err)
} else if value != floatValue {
t.Fatalf("Expected %f to equal %f", value, floatValue)
}
// Not a float.
err = ioutil.WriteFile(tempFile, []byte("not-a-float"), 0755)
if err != nil {
t.Fatal(err)
}
_, err = getCgroupParamFloat64(tempDir, cgroupFile)
if err == nil {
t.Fatal("Expecting error, got none")
}
// Unknown file.
err = os.Remove(tempFile)
if err != nil {
t.Fatal(err)
}
_, err = getCgroupParamFloat64(tempDir, cgroupFile)
if err == nil {
t.Fatal("Expecting error, got none")
}
}