2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-09-24 08:13:57 +08:00
|
|
|
"fmt"
|
2014-05-15 06:21:44 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-05-28 08:01:08 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
type MemoryGroup struct {
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-09 13:46:30 +08:00
|
|
|
func (s *MemoryGroup) Apply(d *data) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
dir, err := d.join("memory")
|
2014-08-06 06:12:28 +08:00
|
|
|
// only return an error for memory if it was specified
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil && (d.c.Memory != 0 || d.c.MemoryReservation != 0 || d.c.MemorySwap != 0) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Only set values if some config was specified.
|
|
|
|
if d.c.Memory != 0 || d.c.MemoryReservation != 0 || d.c.MemorySwap != 0 {
|
|
|
|
if d.c.Memory != 0 {
|
|
|
|
if err := writeFile(dir, "memory.limit_in_bytes", strconv.FormatInt(d.c.Memory, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.c.MemoryReservation != 0 {
|
|
|
|
if err := writeFile(dir, "memory.soft_limit_in_bytes", strconv.FormatInt(d.c.MemoryReservation, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// By default, MemorySwap is set to twice the size of RAM.
|
2014-12-12 01:40:55 +08:00
|
|
|
// If you want to omit MemorySwap, set it to '-1'.
|
|
|
|
if d.c.MemorySwap == 0 {
|
2014-05-15 06:21:44 +08:00
|
|
|
if err := writeFile(dir, "memory.memsw.limit_in_bytes", strconv.FormatInt(d.c.Memory*2, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 01:40:55 +08:00
|
|
|
if d.c.MemorySwap > 0 {
|
|
|
|
if err := writeFile(dir, "memory.memsw.limit_in_bytes", strconv.FormatInt(d.c.MemorySwap, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *MemoryGroup) Remove(d *data) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
return removePath(d.path("memory"))
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *MemoryGroup) GetStats(path string, stats *cgroups.Stats) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
// Set stats from memory.stat.
|
|
|
|
statsFile, err := os.Open(filepath.Join(path, "memory.stat"))
|
|
|
|
if err != nil {
|
2014-07-10 00:39:38 +08:00
|
|
|
if os.IsNotExist(err) {
|
2014-07-09 05:25:55 +08:00
|
|
|
return nil
|
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
defer statsFile.Close()
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(statsFile)
|
|
|
|
for sc.Scan() {
|
|
|
|
t, v, err := getCgroupParamKeyValue(sc.Text())
|
|
|
|
if err != nil {
|
2014-09-24 08:13:57 +08:00
|
|
|
return fmt.Errorf("failed to parse memory.stat (%q) - %v", sc.Text(), err)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
stats.MemoryStats.Stats[t] = v
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set memory usage and max historical usage.
|
2014-09-24 08:13:57 +08:00
|
|
|
value, err := getCgroupParamUint(path, "memory.usage_in_bytes")
|
2014-05-28 08:01:08 +08:00
|
|
|
if err != nil {
|
2014-09-24 08:13:57 +08:00
|
|
|
return fmt.Errorf("failed to parse memory.usage_in_bytes - %v", err)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
stats.MemoryStats.Usage = value
|
2014-09-24 08:13:57 +08:00
|
|
|
value, err = getCgroupParamUint(path, "memory.max_usage_in_bytes")
|
2014-05-28 08:01:08 +08:00
|
|
|
if err != nil {
|
2014-09-24 08:13:57 +08:00
|
|
|
return fmt.Errorf("failed to parse memory.max_usage_in_bytes - %v", err)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-28 08:01:08 +08:00
|
|
|
stats.MemoryStats.MaxUsage = value
|
2014-09-24 08:13:57 +08:00
|
|
|
value, err = getCgroupParamUint(path, "memory.failcnt")
|
2014-06-04 14:41:03 +08:00
|
|
|
if err != nil {
|
2014-09-24 08:13:57 +08:00
|
|
|
return fmt.Errorf("failed to parse memory.failcnt - %v", err)
|
2014-06-04 14:41:03 +08:00
|
|
|
}
|
|
|
|
stats.MemoryStats.Failcnt = value
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2014-05-28 08:01:08 +08:00
|
|
|
return nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|