From 6299489d03d024ad8f973f121a7c183840904c33 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Tue, 5 Apr 2016 23:17:30 -0400 Subject: [PATCH] move epoll syscall to arch specific to enable containerd run on arm64 Signed-off-by: Lei Jitang --- archutils/epoll_aarch64.go | 70 +++++++++++++++++++++++++++++++++++++ archutils/epoll_amd64.go | 18 ++++++++++ supervisor/monitor_linux.go | 9 ++--- 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 archutils/epoll_aarch64.go create mode 100644 archutils/epoll_amd64.go diff --git a/archutils/epoll_aarch64.go b/archutils/epoll_aarch64.go new file mode 100644 index 0000000..b0ea48c --- /dev/null +++ b/archutils/epoll_aarch64.go @@ -0,0 +1,70 @@ +// +build arm64,linux + +package archutils + +// #include +/* +int EpollCreate1(int flag) { + return epoll_create1(0); +} + +int EpollCtl(int efd, int op,int sfd, int Events, int Fd) { + struct epoll_event event; + event.events = Events; + event.data.fd = Fd; + + return epoll_ctl(efd,op,sfd,&event); +} + +typedef struct Event{ + uint32_t events; + int fd; +}; + +struct epoll_event events[128]; +int run_epoll_wait(int fd, struct Event *event) { + int n, i; + n = epoll_wait(fd, events, 128, -1); + for (i = 0; i < n; i++) { + event[i].events = events[i].events; + event[i].fd = events[i].data.fd; + } + return n; +} +*/ +import "C" + +import ( + "fmt" + "syscall" + "unsafe" +) + +func EpollCreate1(flag int) (int, error) { + fd := int(C.EpollCreate1(0)) + if fd < 0 { + return fd, fmt.Errorf("failed to create epoll, errno is %d", fd) + } + return fd, nil +} + +func EpollCtl(epfd int, op int, fd int, event *syscall.EpollEvent) error { + errno := C.EpollCtl(C.int(epfd), C.int(syscall.EPOLL_CTL_ADD), C.int(fd), C.int(event.Events), C.int(event.Fd)) + if errno < 0 { + return fmt.Errorf("Failed to ctl epoll") + } + return nil +} + +func EpollWait(epfd int, events []syscall.EpollEvent, msec int) (int, error) { + var c_events [128]C.struct_Event + n := int(C.run_epoll_wait(C.int(epfd), (*C.struct_Event)(unsafe.Pointer(&c_events)))) + if n < 0 { + return int(n), fmt.Errorf("Failed to wait epoll") + } + for i := 0; i < n; i++ { + events[i].Fd = int32(c_events[i].fd) + events[i].Events = uint32(c_events[i].events) + } + return int(n), nil +} diff --git a/archutils/epoll_amd64.go b/archutils/epoll_amd64.go new file mode 100644 index 0000000..6922b52 --- /dev/null +++ b/archutils/epoll_amd64.go @@ -0,0 +1,18 @@ +//+build !arm64,linux +package archutils + +import ( + "syscall" +) + +func EpollCreate1(flag int) (int, error) { + return syscall.EpollCreate1(flag) +} + +func EpollCtl(epfd int, op int, fd int, event *syscall.EpollEvent) error { + return syscall.EpollCtl(epfd, op, fd, event) +} + +func EpollWait(epfd int, events []syscall.EpollEvent, msec int) (int, error) { + return syscall.EpollWait(epfd, events, msec) +} diff --git a/supervisor/monitor_linux.go b/supervisor/monitor_linux.go index adf4ffd..b176585 100644 --- a/supervisor/monitor_linux.go +++ b/supervisor/monitor_linux.go @@ -5,6 +5,7 @@ import ( "syscall" "github.com/Sirupsen/logrus" + "github.com/docker/containerd/archutils" "github.com/docker/containerd/runtime" ) @@ -14,7 +15,7 @@ func NewMonitor() (*Monitor, error) { exits: make(chan runtime.Process, 1024), ooms: make(chan string, 1024), } - fd, err := syscall.EpollCreate1(0) + fd, err := archutils.EpollCreate1(0) if err != nil { return nil, err } @@ -47,7 +48,7 @@ func (m *Monitor) Monitor(p runtime.Process) error { Fd: int32(fd), Events: syscall.EPOLLHUP, } - if err := syscall.EpollCtl(m.epollFd, syscall.EPOLL_CTL_ADD, fd, &event); err != nil { + if err := archutils.EpollCtl(m.epollFd, syscall.EPOLL_CTL_ADD, fd, &event); err != nil { return err } EpollFdCounter.Inc(1) @@ -67,7 +68,7 @@ func (m *Monitor) MonitorOOM(c runtime.Container) error { Fd: int32(fd), Events: syscall.EPOLLHUP | syscall.EPOLLIN, } - if err := syscall.EpollCtl(m.epollFd, syscall.EPOLL_CTL_ADD, fd, &event); err != nil { + if err := archutils.EpollCtl(m.epollFd, syscall.EPOLL_CTL_ADD, fd, &event); err != nil { return err } EpollFdCounter.Inc(1) @@ -82,7 +83,7 @@ func (m *Monitor) Close() error { func (m *Monitor) start() { var events [128]syscall.EpollEvent for { - n, err := syscall.EpollWait(m.epollFd, events[:], -1) + n, err := archutils.EpollWait(m.epollFd, events[:], -1) if err != nil { if err == syscall.EINTR { continue