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 (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2015-01-27 20:54:19 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-06-12 16:15:24 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-06-12 16:15:24 +08:00
|
|
|
)
|
|
|
|
|
2020-05-07 09:29:59 +08:00
|
|
|
type notifyFunc func(path string) (<-chan struct{}, error)
|
2015-12-08 23:33:47 +08:00
|
|
|
|
|
|
|
func testMemoryNotification(t *testing.T, evName string, notify notifyFunc, targ string) {
|
|
|
|
memoryPath, err := ioutil.TempDir("", "testmemnotification-"+evName)
|
2015-01-27 20:54:19 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-12-08 23:33:47 +08:00
|
|
|
evFile := filepath.Join(memoryPath, evName)
|
2015-01-27 20:54:19 +08:00
|
|
|
eventPath := filepath.Join(memoryPath, "cgroup.event_control")
|
2015-12-08 23:33:47 +08:00
|
|
|
if err := ioutil.WriteFile(evFile, []byte{}, 0700); err != nil {
|
2015-01-27 20:54:19 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(eventPath, []byte{}, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-07 09:29:59 +08:00
|
|
|
ch, err := notify(memoryPath)
|
2014-06-12 16:15:24 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error, got:", err)
|
|
|
|
}
|
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
data, err := ioutil.ReadFile(eventPath)
|
2014-06-12 16:15:24 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("couldn't read event control file:", err)
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:33:47 +08:00
|
|
|
var eventFd, evFd int
|
|
|
|
var arg string
|
|
|
|
if targ != "" {
|
|
|
|
_, err = fmt.Sscanf(string(data), "%d %d %s", &eventFd, &evFd, &arg)
|
|
|
|
} else {
|
|
|
|
_, err = fmt.Sscanf(string(data), "%d %d", &eventFd, &evFd)
|
|
|
|
}
|
|
|
|
if err != nil || arg != targ {
|
2014-06-12 16:15:24 +08:00
|
|
|
t.Fatalf("invalid control data %q: %s", data, err)
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:28:03 +08:00
|
|
|
// dup the eventfd
|
2017-05-10 05:38:27 +08:00
|
|
|
efd, err := unix.Dup(eventFd)
|
2014-06-12 16:15:24 +08:00
|
|
|
if err != nil {
|
2017-08-14 15:28:03 +08:00
|
|
|
t.Fatal("unable to dup eventfd:", err)
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
defer unix.Close(efd)
|
2014-06-12 16:15:24 +08:00
|
|
|
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(buf, 1)
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, err := unix.Write(efd, buf); err != nil {
|
2014-06-12 16:15:24 +08:00
|
|
|
t.Fatal("unable to write to eventfd:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-12-08 23:33:47 +08:00
|
|
|
case <-ch:
|
2014-06-12 16:15:24 +08:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2015-12-08 23:33:47 +08:00
|
|
|
t.Fatal("no notification on channel after 100ms")
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// simulate what happens when a cgroup is destroyed by cleaning up and then
|
|
|
|
// writing to the eventfd.
|
2015-01-27 20:54:19 +08:00
|
|
|
if err := os.RemoveAll(memoryPath); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, err := unix.Write(efd, buf); err != nil {
|
2014-06-12 16:15:24 +08:00
|
|
|
t.Fatal("unable to write to eventfd:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// give things a moment to shut down
|
|
|
|
select {
|
2015-12-08 23:33:47 +08:00
|
|
|
case _, ok := <-ch:
|
2014-06-12 16:15:24 +08:00
|
|
|
if ok {
|
2015-12-08 23:33:47 +08:00
|
|
|
t.Fatal("expected no notification to be triggered")
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2017-08-14 15:28:03 +08:00
|
|
|
t.Fatal("channel not closed after 100ms")
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(evFd), unix.F_GETFD, 0); err != unix.EBADF {
|
|
|
|
t.Errorf("expected event control to be closed, but received error %s", err.Error())
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(eventFd), unix.F_GETFD, 0); err != unix.EBADF {
|
|
|
|
t.Errorf("expected event fd to be closed, but received error %s", err.Error())
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 23:33:47 +08:00
|
|
|
|
|
|
|
func TestNotifyOnOOM(t *testing.T) {
|
2020-05-07 09:29:59 +08:00
|
|
|
f := func(path string) (<-chan struct{}, error) {
|
|
|
|
return notifyOnOOM(path)
|
2015-12-08 23:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
testMemoryNotification(t, "memory.oom_control", f, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotifyMemoryPressure(t *testing.T) {
|
|
|
|
tests := map[PressureLevel]string{
|
|
|
|
LowPressure: "low",
|
|
|
|
MediumPressure: "medium",
|
|
|
|
CriticalPressure: "critical",
|
|
|
|
}
|
|
|
|
|
|
|
|
for level, arg := range tests {
|
2020-05-07 09:29:59 +08:00
|
|
|
f := func(path string) (<-chan struct{}, error) {
|
|
|
|
return notifyMemoryPressure(path, level)
|
2015-12-08 23:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
testMemoryNotification(t, "memory.pressure_level", f, arg)
|
|
|
|
}
|
|
|
|
}
|