Skip to content

Commit

Permalink
perf: opportunistically check all rings for data
Browse files Browse the repository at this point in the history
Waking up user space from the kernel is expensive and so the perf reader
allows adjusting the rate at which wakeups happen. This saves CPU at the
cost of latency: some data will remain in the buffer for longer.

The reader is an abstraction over multiple ring buffers. It only reads
from a ring buffer if it has received a wakeup from the kernel. This is
wasteful because wakeups are expensive (due to context switching
and so on) but checking a ring for contents is cheap (just an atomic load).

Change the behaviour so that we read data from any ready ring buffer
regardless of why we were woken up.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb committed May 10, 2024
1 parent 1803440 commit 75bce38
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions perf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ type perfEventHeader struct {
Size uint16
}

func cpuForEvent(event *unix.EpollEvent) int {
return int(event.Pad)
}

// Record contains either a sample or a counter of the
// number of lost samples.
type Record struct {
Expand Down Expand Up @@ -241,7 +237,7 @@ func NewReaderWithOptions(array *ebpf.Map, perCPUBuffer int, opts ReaderOptions)
rings = append(rings, ring)
eventFds = append(eventFds, event)

if err := poller.Add(event.Int(), i); err != nil {
if err := poller.Add(event.Int(), 0); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -361,7 +357,7 @@ func (pr *Reader) ReadInto(rec *Record) error {
// NB: The deferred pauseMu.Unlock will panic if Wait panics, which
// might obscure the original panic.
pr.pauseMu.Unlock()
nEvents, err := pr.poller.Wait(pr.epollEvents, pr.deadline)
_, err := pr.poller.Wait(pr.epollEvents, pr.deadline)
pr.pauseMu.Lock()
if err != nil {
return err
Expand All @@ -372,14 +368,11 @@ func (pr *Reader) ReadInto(rec *Record) error {
return errMustBePaused
}

for _, event := range pr.epollEvents[:nEvents] {
ring := pr.rings[cpuForEvent(&event)]
pr.epollRings = append(pr.epollRings, ring)

// Read the current head pointer now, not every time
// we read a record. This prevents a single fast producer
// from keeping the reader busy.
// Waking up userspace is expensive, make the most of it by checking
// all rings.
for _, ring := range pr.rings {
ring.loadHead()
pr.epollRings = append(pr.epollRings, ring)
}
}

Expand Down

0 comments on commit 75bce38

Please sign in to comment.