2014-04-19 04:55:06 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-04-20 18:34:28 +00:00
|
|
|
"bufio"
|
2014-04-24 22:59:37 +00:00
|
|
|
"fmt"
|
2014-04-19 04:55:06 +00:00
|
|
|
"os"
|
2014-04-20 18:34:28 +00:00
|
|
|
"path/filepath"
|
2014-04-19 04:55:06 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type memoryGroup struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memoryGroup) Set(d *data) error {
|
|
|
|
dir, err := d.join("memory")
|
|
|
|
// only return an error for memory if it was not specified
|
2014-04-24 05:11:43 +00:00
|
|
|
if err != nil && (d.c.Memory != 0 || d.c.MemoryReservation != 0 || d.c.MemorySwap != 0) {
|
2014-04-19 04:55:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-04-24 05:11:43 +00:00
|
|
|
// Only set values if some config was specified.
|
|
|
|
if d.c.Memory != 0 || d.c.MemoryReservation != 0 || d.c.MemorySwap != 0 {
|
2014-04-19 04:55:06 +00:00
|
|
|
if d.c.Memory != 0 {
|
|
|
|
if err := writeFile(dir, "memory.limit_in_bytes", strconv.FormatInt(d.c.Memory, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-24 05:11:43 +00:00
|
|
|
}
|
|
|
|
if d.c.MemoryReservation != 0 {
|
|
|
|
if err := writeFile(dir, "memory.soft_limit_in_bytes", strconv.FormatInt(d.c.MemoryReservation, 10)); err != nil {
|
2014-04-19 04:55:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// By default, MemorySwap is set to twice the size of RAM.
|
|
|
|
// If you want to omit MemorySwap, set it to `-1'.
|
|
|
|
if d.c.MemorySwap != -1 {
|
|
|
|
if err := writeFile(dir, "memory.memsw.limit_in_bytes", strconv.FormatInt(d.c.Memory*2, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-19 05:17:31 +00:00
|
|
|
|
|
|
|
func (s *memoryGroup) Remove(d *data) error {
|
|
|
|
return removePath(d.path("memory"))
|
|
|
|
}
|
2014-04-20 18:34:28 +00:00
|
|
|
|
|
|
|
func (s *memoryGroup) Stats(d *data) (map[string]float64, error) {
|
|
|
|
paramData := make(map[string]float64)
|
|
|
|
path, err := d.path("memory")
|
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-04-24 22:59:37 +00:00
|
|
|
// Set stats from memory.stat.
|
|
|
|
statsFile, err := os.Open(filepath.Join(path, "memory.stat"))
|
2014-04-20 18:34:28 +00:00
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
2014-04-24 22:59:37 +00:00
|
|
|
defer statsFile.Close()
|
2014-04-20 19:02:39 +00:00
|
|
|
|
2014-04-24 22:59:37 +00:00
|
|
|
sc := bufio.NewScanner(statsFile)
|
2014-04-20 18:34:28 +00:00
|
|
|
for sc.Scan() {
|
|
|
|
t, v, err := getCgroupParamKeyValue(sc.Text())
|
|
|
|
if err != nil {
|
2014-04-20 19:02:39 +00:00
|
|
|
return nil, err
|
2014-04-20 18:34:28 +00:00
|
|
|
}
|
|
|
|
paramData[t] = v
|
|
|
|
}
|
2014-04-24 22:59:37 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-04-20 18:34:28 +00:00
|
|
|
return paramData, nil
|
|
|
|
}
|