2014-06-12 16:15:24 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
package libcontainer
|
2014-06-12 16:15:24 +08:00
|
|
|
|
|
|
|
import (
|
2020-05-07 09:29:59 +08:00
|
|
|
"errors"
|
2014-06-12 16:15:24 +08:00
|
|
|
"fmt"
|
2015-01-27 20:54:19 +08:00
|
|
|
"io/ioutil"
|
2014-06-12 16:15:24 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-06-12 16:15:24 +08:00
|
|
|
)
|
|
|
|
|
2015-12-08 23:33:47 +08:00
|
|
|
type PressureLevel uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
LowPressure PressureLevel = iota
|
|
|
|
MediumPressure
|
|
|
|
CriticalPressure
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerMemoryEvent(cgDir string, evName string, arg string) (<-chan struct{}, error) {
|
|
|
|
evFile, err := os.Open(filepath.Join(cgDir, evName))
|
2014-06-12 16:15:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-21 15:42:13 +08:00
|
|
|
fd, err := unix.Eventfd(0, unix.EFD_CLOEXEC)
|
|
|
|
if err != nil {
|
2015-12-08 23:33:47 +08:00
|
|
|
evFile.Close()
|
2017-06-21 15:42:13 +08:00
|
|
|
return nil, err
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
|
2017-06-21 15:42:13 +08:00
|
|
|
eventfd := os.NewFile(uintptr(fd), "eventfd")
|
2014-06-12 16:15:24 +08:00
|
|
|
|
2015-12-08 23:33:47 +08:00
|
|
|
eventControlPath := filepath.Join(cgDir, "cgroup.event_control")
|
|
|
|
data := fmt.Sprintf("%d %d %s", eventfd.Fd(), evFile.Fd(), arg)
|
2015-01-27 20:54:19 +08:00
|
|
|
if err := ioutil.WriteFile(eventControlPath, []byte(data), 0700); err != nil {
|
2014-06-12 16:15:24 +08:00
|
|
|
eventfd.Close()
|
2015-12-08 23:33:47 +08:00
|
|
|
evFile.Close()
|
2014-06-12 16:15:24 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2014-06-25 03:08:03 +08:00
|
|
|
defer func() {
|
|
|
|
eventfd.Close()
|
2015-12-08 23:33:47 +08:00
|
|
|
evFile.Close()
|
2017-08-14 15:18:59 +08:00
|
|
|
close(ch)
|
2014-06-25 03:08:03 +08:00
|
|
|
}()
|
|
|
|
buf := make([]byte, 8)
|
2014-06-12 16:15:24 +08:00
|
|
|
for {
|
|
|
|
if _, err := eventfd.Read(buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// When a cgroup is destroyed, an event is sent to eventfd.
|
|
|
|
// So if the control path is gone, return instead of notifying.
|
|
|
|
if _, err := os.Lstat(eventControlPath); os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch, nil
|
|
|
|
}
|
2015-12-08 23:33:47 +08:00
|
|
|
|
|
|
|
// notifyOnOOM returns channel on which you can expect event about OOM,
|
|
|
|
// if process died without OOM this channel will be closed.
|
2020-05-07 09:29:59 +08:00
|
|
|
func notifyOnOOM(dir string) (<-chan struct{}, error) {
|
2015-12-08 23:33:47 +08:00
|
|
|
if dir == "" {
|
2020-05-07 09:29:59 +08:00
|
|
|
return nil, errors.New("memory controller missing")
|
2015-12-08 23:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return registerMemoryEvent(dir, "memory.oom_control", "")
|
|
|
|
}
|
|
|
|
|
2020-05-07 09:29:59 +08:00
|
|
|
func notifyMemoryPressure(dir string, level PressureLevel) (<-chan struct{}, error) {
|
2015-12-08 23:33:47 +08:00
|
|
|
if dir == "" {
|
2020-05-07 09:29:59 +08:00
|
|
|
return nil, errors.New("memory controller missing")
|
2015-12-08 23:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if level > CriticalPressure {
|
|
|
|
return nil, fmt.Errorf("invalid pressure level %d", level)
|
|
|
|
}
|
|
|
|
|
|
|
|
levelStr := []string{"low", "medium", "critical"}[level]
|
|
|
|
return registerMemoryEvent(dir, "memory.pressure_level", levelStr)
|
|
|
|
}
|