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"
|
2016-07-23 08:04:37 +08:00
|
|
|
"io/ioutil"
|
2014-05-15 06:21:44 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2015-05-15 17:37:58 +08:00
|
|
|
"strings"
|
2017-05-10 05:38:27 +08:00
|
|
|
"syscall" // only for Errno
|
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"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-07-23 08:04:37 +08:00
|
|
|
)
|
|
|
|
|
2016-10-19 18:15:55 +08:00
|
|
|
const (
|
|
|
|
cgroupKernelMemoryLimit = "memory.kmem.limit_in_bytes"
|
|
|
|
cgroupMemorySwapLimit = "memory.memsw.limit_in_bytes"
|
|
|
|
cgroupMemoryLimit = "memory.limit_in_bytes"
|
|
|
|
)
|
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-10-16 06:19:23 +08:00
|
|
|
|
|
|
|
func (s *MemoryGroup) Name() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2015-11-05 18:41:08 +08:00
|
|
|
func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
|
2015-06-18 15:44:30 +08:00
|
|
|
path, err := d.path("memory")
|
2015-10-20 14:01:48 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2015-03-24 03:14:03 +08:00
|
|
|
return err
|
2017-02-26 02:58:18 +08:00
|
|
|
} else if path == "" {
|
|
|
|
return nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2016-07-07 06:14:25 +08:00
|
|
|
if memoryAssigned(d.config) {
|
2017-02-26 02:58:18 +08:00
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
2016-07-21 19:05:40 +08:00
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-26 02:58:18 +08:00
|
|
|
// Only enable kernel memory accouting when this cgroup
|
|
|
|
// is created by libcontainer, otherwise we might get
|
|
|
|
// error when people use `cgroupsPath` to join an existed
|
|
|
|
// cgroup whose kernel memory is not initialized.
|
2016-10-08 01:02:19 +08:00
|
|
|
if err := EnableKernelMemoryAccounting(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-07 06:14:25 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 19:05:40 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-06-18 15:44:30 +08:00
|
|
|
// We need to join memory cgroup after set memory limits, because
|
|
|
|
// kmem.limit_in_bytes can only be set when the cgroup is empty.
|
2016-07-21 19:05:40 +08:00
|
|
|
_, err = d.join("memory")
|
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2015-06-18 15:44:30 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-01-13 22:05:08 +08:00
|
|
|
return nil
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2016-07-23 08:04:37 +08:00
|
|
|
func EnableKernelMemoryAccounting(path string) error {
|
|
|
|
// Check if kernel memory is enabled
|
|
|
|
// We have to limit the kernel memory here as it won't be accounted at all
|
|
|
|
// until a limit is set on the cgroup and limit cannot be set once the
|
|
|
|
// cgroup has children, or if there are already tasks in the cgroup.
|
2016-10-08 01:02:19 +08:00
|
|
|
for _, i := range []int64{1, -1} {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := setKernelMemory(path, uint64(i)); err != nil {
|
2016-10-08 01:02:19 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-07-23 08:04:37 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-21 19:05:40 +08:00
|
|
|
|
2017-03-20 18:51:39 +08:00
|
|
|
func setKernelMemory(path string, kernelMemoryLimit uint64) error {
|
2016-07-23 08:04:37 +08:00
|
|
|
if path == "" {
|
|
|
|
return fmt.Errorf("no such directory for %s", cgroupKernelMemoryLimit)
|
|
|
|
}
|
|
|
|
if !cgroups.PathExists(filepath.Join(path, cgroupKernelMemoryLimit)) {
|
|
|
|
// kernel memory is not enabled on the system so we should do nothing
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatUint(kernelMemoryLimit, 10)), 0700); err != nil {
|
2016-07-23 08:04:37 +08:00
|
|
|
// Check if the error number returned by the syscall is "EBUSY"
|
|
|
|
// The EBUSY signal is returned on attempts to write to the
|
|
|
|
// memory.kmem.limit_in_bytes file if the cgroup has children or
|
|
|
|
// once tasks have been attached to the cgroup
|
|
|
|
if pathErr, ok := err.(*os.PathError); ok {
|
|
|
|
if errNo, ok := pathErr.Err.(syscall.Errno); ok {
|
2017-05-10 05:38:27 +08:00
|
|
|
if errNo == unix.EBUSY {
|
2016-07-23 08:04:37 +08:00
|
|
|
return fmt.Errorf("failed to set %s, because either tasks have already joined this cgroup or it has children", cgroupKernelMemoryLimit)
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 22:05:08 +08:00
|
|
|
}
|
2016-07-23 08:04:37 +08:00
|
|
|
return fmt.Errorf("failed to write %v to %v: %v", kernelMemoryLimit, cgroupKernelMemoryLimit, err)
|
2016-01-13 22:05:08 +08:00
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-28 10:48:29 +08:00
|
|
|
func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
|
2017-03-20 18:51:39 +08:00
|
|
|
ulimited := -1
|
|
|
|
|
|
|
|
// If the memory update is set to uint64(-1) we should also
|
|
|
|
// set swap to uint64(-1), it means unlimited memory.
|
|
|
|
if cgroup.Resources.Memory == uint64(ulimited) {
|
2016-10-19 18:15:55 +08:00
|
|
|
// Only set swap if it's enbled in kernel
|
|
|
|
if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) {
|
2017-03-20 18:51:39 +08:00
|
|
|
cgroup.Resources.MemorySwap = uint64(ulimited)
|
2016-10-19 18:15:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 10:48:29 +08:00
|
|
|
// When memory and swap memory are both set, we need to handle the cases
|
|
|
|
// for updating container.
|
2016-10-19 18:15:55 +08:00
|
|
|
if cgroup.Resources.Memory != 0 && cgroup.Resources.MemorySwap != 0 {
|
2016-03-28 10:48:29 +08:00
|
|
|
memoryUsage, err := getMemoryData(path, "")
|
|
|
|
if err != nil {
|
2015-02-25 17:20:01 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-03-28 10:48:29 +08:00
|
|
|
|
|
|
|
// When update memory limit, we should adapt the write sequence
|
|
|
|
// for memory and swap memory, so it won't fail because the new
|
|
|
|
// value and the old value don't fit kernel's validation.
|
2017-03-20 18:51:39 +08:00
|
|
|
if cgroup.Resources.MemorySwap == uint64(ulimited) || memoryUsage.Limit < cgroup.Resources.MemorySwap {
|
|
|
|
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
|
2016-03-28 10:48:29 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
|
2016-03-28 10:48:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
|
2016-03-28 10:48:29 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
|
2016-03-28 10:48:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if cgroup.Resources.Memory != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
|
2016-03-28 10:48:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 18:15:55 +08:00
|
|
|
if cgroup.Resources.MemorySwap != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
|
2016-03-28 10:48:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 17:20:01 +08:00
|
|
|
}
|
2016-03-28 10:48:29 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
|
|
|
|
if err := setMemoryAndSwap(path, cgroup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-23 08:04:37 +08:00
|
|
|
if cgroup.Resources.KernelMemory != 0 {
|
|
|
|
if err := setKernelMemory(path, cgroup.Resources.KernelMemory); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-29 01:49:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 08:26:29 +08:00
|
|
|
if cgroup.Resources.MemoryReservation != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(cgroup.Resources.MemoryReservation, 10)); err != nil {
|
2015-02-25 17:20:01 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-07-23 08:04:37 +08:00
|
|
|
|
2016-03-20 18:45:52 +08:00
|
|
|
if cgroup.Resources.KernelMemoryTCP != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatUint(cgroup.Resources.KernelMemoryTCP, 10)); err != nil {
|
2016-03-20 18:45:52 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 08:26:29 +08:00
|
|
|
if cgroup.Resources.OomKillDisable {
|
2015-03-07 02:37:56 +08:00
|
|
|
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-02-21 09:29:53 +08:00
|
|
|
if cgroup.Resources.MemorySwappiness == nil || int64(*cgroup.Resources.MemorySwappiness) == -1 {
|
|
|
|
return nil
|
2017-03-20 18:51:39 +08:00
|
|
|
} else if *cgroup.Resources.MemorySwappiness <= 100 {
|
|
|
|
if err := writeFile(path, "memory.swappiness", strconv.FormatUint(*cgroup.Resources.MemorySwappiness, 10)); err != nil {
|
2015-06-11 19:26:03 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-07-03 20:49:45 +08:00
|
|
|
} else {
|
2017-03-20 18:51:39 +08:00
|
|
|
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", *cgroup.Resources.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
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:41:08 +08:00
|
|
|
func (s *MemoryGroup) Remove(d *cgroupData) 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
|
2016-03-20 19:04:02 +08:00
|
|
|
kernelTCPUsage, err := getMemoryData(path, "kmem.tcp")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stats.MemoryStats.KernelTCPUsage = kernelTCPUsage
|
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
|
|
|
|
2015-10-20 14:01:48 +08:00
|
|
|
func memoryAssigned(cgroup *configs.Cgroup) bool {
|
2015-12-15 08:26:29 +08:00
|
|
|
return cgroup.Resources.Memory != 0 ||
|
|
|
|
cgroup.Resources.MemoryReservation != 0 ||
|
|
|
|
cgroup.Resources.MemorySwap > 0 ||
|
|
|
|
cgroup.Resources.KernelMemory > 0 ||
|
2016-03-20 18:45:52 +08:00
|
|
|
cgroup.Resources.KernelMemoryTCP > 0 ||
|
2015-12-15 08:26:29 +08:00
|
|
|
cgroup.Resources.OomKillDisable ||
|
2017-03-20 18:51:39 +08:00
|
|
|
(cgroup.Resources.MemorySwappiness != nil && int64(*cgroup.Resources.MemorySwappiness) != -1)
|
2015-10-20 14:01:48 +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"}, ".")
|
2016-02-04 03:00:48 +08:00
|
|
|
limit := strings.Join([]string{moduleName, "limit_in_bytes"}, ".")
|
2015-05-15 17:37:58 +08:00
|
|
|
|
|
|
|
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
|
2016-02-04 03:00:48 +08:00
|
|
|
value, err = getCgroupParamUint(path, limit)
|
|
|
|
if err != nil {
|
|
|
|
if moduleName != "memory" && os.IsNotExist(err) {
|
|
|
|
return cgroups.MemoryData{}, nil
|
|
|
|
}
|
|
|
|
return cgroups.MemoryData{}, fmt.Errorf("failed to parse %s - %v", limit, err)
|
|
|
|
}
|
|
|
|
memoryData.Limit = value
|
2015-05-15 17:37:58 +08:00
|
|
|
|
|
|
|
return memoryData, nil
|
|
|
|
}
|