2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
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"
|
2015-05-15 17:37:58 +08:00
|
|
|
"strings"
|
2014-05-28 08:01:08 +08:00
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
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-08-16 12:19:44 +08:00
|
|
|
func (s *MemoryGroup) Apply(d *data) (err error) {
|
2015-06-18 15:44:30 +08:00
|
|
|
path, err := d.path("memory")
|
2015-07-13 18:22:35 +08:00
|
|
|
if err != nil {
|
|
|
|
if cgroups.IsNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-24 03:14:03 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
2015-06-18 15:44:30 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-08-16 12:19:44 +08:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-06-18 15:44:30 +08:00
|
|
|
if err := s.Set(path, d.c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to join memory cgroup after set memory limits, because
|
|
|
|
// kmem.limit_in_bytes can only be set when the cgroup is empty.
|
2015-08-16 12:19:44 +08:00
|
|
|
if _, err = d.join("memory"); err != nil {
|
2015-06-18 15:44:30 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
|
|
|
|
if cgroup.Memory != 0 {
|
|
|
|
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Memory, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.MemoryReservation != 0 {
|
|
|
|
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.MemoryReservation, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cgroup.MemorySwap > 0 {
|
|
|
|
if err := writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatInt(cgroup.MemorySwap, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 14:24:56 +08:00
|
|
|
if cgroup.KernelMemory > 0 {
|
|
|
|
if err := writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatInt(cgroup.KernelMemory, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 17:20:01 +08:00
|
|
|
|
2015-03-07 02:37:56 +08:00
|
|
|
if cgroup.OomKillDisable {
|
|
|
|
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 19:26:03 +08:00
|
|
|
if cgroup.MemorySwappiness >= 0 && cgroup.MemorySwappiness <= 100 {
|
|
|
|
if err := writeFile(path, "memory.swappiness", strconv.FormatInt(cgroup.MemorySwappiness, 10)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-03 20:49:45 +08:00
|
|
|
} else if cgroup.MemorySwappiness == -1 {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", cgroup.MemorySwappiness)
|
2015-06-11 19:26:03 +08:00
|
|
|
}
|
2015-03-07 02:37:56 +08:00
|
|
|
|
2015-02-25 17:20:01 +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
|
|
|
}
|
2015-05-15 17:37:58 +08:00
|
|
|
stats.MemoryStats.Cache = stats.MemoryStats.Stats["cache"]
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2015-05-15 17:37:58 +08:00
|
|
|
memoryUsage, err := getMemoryData(path, "")
|
2014-05-28 08:01:08 +08:00
|
|
|
if err != nil {
|
2015-05-15 17:37:58 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2015-05-15 17:37:58 +08:00
|
|
|
stats.MemoryStats.Usage = memoryUsage
|
|
|
|
swapUsage, err := getMemoryData(path, "memsw")
|
2014-05-28 08:01:08 +08:00
|
|
|
if err != nil {
|
2015-05-15 17:37:58 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2015-05-15 17:37:58 +08:00
|
|
|
stats.MemoryStats.SwapUsage = swapUsage
|
|
|
|
kernelUsage, err := getMemoryData(path, "kmem")
|
2014-06-04 14:41:03 +08:00
|
|
|
if err != nil {
|
2015-05-15 17:37:58 +08:00
|
|
|
return err
|
2014-06-04 14:41:03 +08:00
|
|
|
}
|
2015-05-15 17:37:58 +08:00
|
|
|
stats.MemoryStats.KernelUsage = kernelUsage
|
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
|
|
|
}
|
2015-05-15 17:37:58 +08:00
|
|
|
|
|
|
|
func getMemoryData(path, name string) (cgroups.MemoryData, error) {
|
|
|
|
memoryData := cgroups.MemoryData{}
|
|
|
|
|
|
|
|
moduleName := "memory"
|
|
|
|
if name != "" {
|
|
|
|
moduleName = strings.Join([]string{"memory", name}, ".")
|
|
|
|
}
|
|
|
|
usage := strings.Join([]string{moduleName, "usage_in_bytes"}, ".")
|
|
|
|
maxUsage := strings.Join([]string{moduleName, "max_usage_in_bytes"}, ".")
|
|
|
|
failcnt := strings.Join([]string{moduleName, "failcnt"}, ".")
|
|
|
|
|
|
|
|
value, err := getCgroupParamUint(path, usage)
|
|
|
|
if err != nil {
|
|
|
|
if moduleName != "memory" && os.IsNotExist(err) {
|
|
|
|
return cgroups.MemoryData{}, nil
|
|
|
|
}
|
|
|
|
return cgroups.MemoryData{}, fmt.Errorf("failed to parse %s - %v", usage, err)
|
|
|
|
}
|
|
|
|
memoryData.Usage = value
|
|
|
|
value, err = getCgroupParamUint(path, maxUsage)
|
|
|
|
if err != nil {
|
|
|
|
if moduleName != "memory" && os.IsNotExist(err) {
|
|
|
|
return cgroups.MemoryData{}, nil
|
|
|
|
}
|
|
|
|
return cgroups.MemoryData{}, fmt.Errorf("failed to parse %s - %v", maxUsage, err)
|
|
|
|
}
|
|
|
|
memoryData.MaxUsage = value
|
|
|
|
value, err = getCgroupParamUint(path, failcnt)
|
|
|
|
if err != nil {
|
|
|
|
if moduleName != "memory" && os.IsNotExist(err) {
|
|
|
|
return cgroups.MemoryData{}, nil
|
|
|
|
}
|
|
|
|
return cgroups.MemoryData{}, fmt.Errorf("failed to parse %s - %v", failcnt, err)
|
|
|
|
}
|
|
|
|
memoryData.Failcnt = value
|
|
|
|
|
|
|
|
return memoryData, nil
|
|
|
|
}
|