Skip to content

Commit

Permalink
kqueue.go: Remove timeout from reading kevents.
Browse files Browse the repository at this point in the history
Instead of having a timeout that would trigger the readEvents to check it's
done channel, run a seaprate go-routine that watches w.done, and directly
closes the KQueue FD.

This change allows a go-process to fully sleep and go into CPU idle mode,
instead of waking up every 100ms on macOS.
  • Loading branch information
paulquerna-okta committed Mar 20, 2019
1 parent 1485a34 commit 47b309e
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions kqueue.go
Expand Up @@ -24,7 +24,7 @@ type Watcher struct {
Errors chan error
done chan struct{} // Channel for sending a "quit message" to the reader goroutine

kq int // File descriptor (as returned by the kqueue() syscall).
_kq int // File descriptor (as returned by the kqueue() syscall).

mu sync.Mutex // Protects access to watcher data
watches map[string]int // Map of watched file descriptors (key: path).
Expand All @@ -48,7 +48,7 @@ func NewWatcher() (*Watcher, error) {
}

w := &Watcher{
kq: kq,
_kq: kq,
watches: make(map[string]int),
dirFlags: make(map[string]uint32),
paths: make(map[int]pathInfo),
Expand All @@ -59,6 +59,7 @@ func NewWatcher() (*Watcher, error) {
done: make(chan struct{}),
}

go w.closeKq()
go w.readEvents()
return w, nil
}
Expand Down Expand Up @@ -110,7 +111,7 @@ func (w *Watcher) Remove(name string) error {
}

const registerRemove = unix.EV_DELETE
if err := register(w.kq, []int{watchfd}, registerRemove, 0); err != nil {
if err := register(w.kq(), []int{watchfd}, registerRemove, 0); err != nil {
return err
}

Expand Down Expand Up @@ -150,9 +151,6 @@ func (w *Watcher) Remove(name string) error {
// Watch all events (except NOTE_EXTEND, NOTE_LINK, NOTE_REVOKE)
const noteAllEvents = unix.NOTE_DELETE | unix.NOTE_WRITE | unix.NOTE_ATTRIB | unix.NOTE_RENAME

// keventWaitTime to block on each read from kevent
var keventWaitTime = durationToTimespec(100 * time.Millisecond)

// addWatch adds name to the watched file set.
// The flags are interpreted as described in kevent(2).
// Returns the real path to the file which was added, if any, which may be different from the one passed in the case of symlinks.
Expand Down Expand Up @@ -224,7 +222,7 @@ func (w *Watcher) addWatch(name string, flags uint32) (string, error) {
}

const registerAdd = unix.EV_ADD | unix.EV_CLEAR | unix.EV_ENABLE
if err := register(w.kq, []int{watchfd}, registerAdd, flags); err != nil {
if err := register(w.kq(), []int{watchfd}, registerAdd, flags); err != nil {
unix.Close(watchfd)
return "", err
}
Expand Down Expand Up @@ -256,6 +254,23 @@ func (w *Watcher) addWatch(name string, flags uint32) (string, error) {
return name, nil
}

func (w *Watcher) kq() int {
w.mu.Lock()
defer w.mu.Unlock()
return w._kq
}

func (w *Watcher) closeKq() {
select {
case <-w.done:
w.mu.Lock()
unix.Close(w._kq)
w._kq = -1
w.mu.Unlock()
return
}
}

// readEvents reads from kqueue and converts the received kevents into
// Event values that it sends down the Events channel.
func (w *Watcher) readEvents() {
Expand All @@ -271,9 +286,9 @@ loop:
}

// Get new events
kevents, err := read(w.kq, eventBuffer, &keventWaitTime)
kevents, err := read(w.kq(), eventBuffer, nil)
// EINTR is okay, the syscall was interrupted before timeout expired.
if err != nil && err != unix.EINTR {
if err != nil && err != unix.EINTR && err != unix.EBADF {
select {
case w.Errors <- err:
case <-w.done:
Expand Down Expand Up @@ -352,8 +367,8 @@ loop:
}

// cleanup
err := unix.Close(w.kq)
if err != nil {
err := unix.Close(w.kq())
if err != nil && err != unix.EBADF {
// only way the previous loop breaks is if w.done was closed so we need to async send to w.Errors.
select {
case w.Errors <- err:
Expand Down

0 comments on commit 47b309e

Please sign in to comment.