Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory.events handling rework #145

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions v2/manager.go
Expand Up @@ -526,20 +526,20 @@ func (c *Manager) freeze(path string, state State) error {
}
}

func (c *Manager) MemoryEventFD() (uintptr, error) {
// MemoryEventFD returns inotify file descriptor and 'memory.events' inotify watch descriptor
func (c *Manager) MemoryEventFD() (int, uint32, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs godoc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(int, uint32

why different types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coz this is the base types for file descriptor and watch descriptor, at least syscall.InotifyRmWatch() expects it in that way.

fpath := filepath.Join(c.path, "memory.events")
fd, err := syscall.InotifyInit()
if err != nil {
return 0, errors.Errorf("Failed to create inotify fd")
return 0, 0, errors.Errorf("Failed to create inotify fd")
}
defer syscall.Close(fd)
wd, err := syscall.InotifyAddWatch(fd, fpath, unix.IN_MODIFY)
if wd < 0 {
return 0, errors.Errorf("Failed to add inotify watch for %q", fpath)
syscall.Close(fd)
return 0, 0, errors.Errorf("Failed to add inotify watch for %q", fpath)
}
defer syscall.InotifyRmWatch(fd, uint32(wd))

return uintptr(fd), nil
return fd, uint32(wd), nil
}

func (c *Manager) EventChan() (<-chan Event, <-chan error) {
Expand All @@ -551,15 +551,22 @@ func (c *Manager) EventChan() (<-chan Event, <-chan error) {
}

func (c *Manager) waitForEvents(ec chan<- Event, errCh chan<- error) {
fd, err := c.MemoryEventFD()
fd, wd, err := c.MemoryEventFD()

defer syscall.InotifyRmWatch(fd, wd)
defer syscall.Close(fd)

if err != nil {
errCh <- errors.Errorf("Failed to create memory event fd")
errCh <- err
return
}

for {
buffer := make([]byte, syscall.SizeofInotifyEvent*10)
bytesRead, err := syscall.Read(int(fd), buffer)
bytesRead, err := syscall.Read(fd, buffer)
if err != nil {
errCh <- err
return
}
var out map[string]interface{}
if bytesRead >= syscall.SizeofInotifyEvent {
Expand All @@ -572,6 +579,9 @@ func (c *Manager) waitForEvents(ec chan<- Event, errCh chan<- error) {
OOMKill: out["oom_kill"].(uint64),
}
ec <- e
} else {
errCh <- err
return
}
}
}
Expand Down