Skip to content

Commit

Permalink
use ErrFlushed
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Jun 21, 2024
1 parent 1d16d24 commit e5f3960
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 45 deletions.
15 changes: 0 additions & 15 deletions internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,3 @@ func (le *VerifierError) Format(f fmt.State, verb rune) {
fmt.Fprintf(f, "%%!%c(BADVERB)", verb)
}
}

type FlushCompleteError struct {
Err error
}

func (fe *FlushCompleteError) Error() string {
if fe.Err == nil {
return "flush complete"
}
return fmt.Sprintf("flush complete: %s", fe.Err.Error())
}

func (fe *FlushCompleteError) Unwrap() error {
return fe.Err
}
13 changes: 6 additions & 7 deletions perf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import (
)

var (
ErrClosed = os.ErrClosed
errEOR = errors.New("end of ring")
ErrClosed = os.ErrClosed
ErrFlushed = errors.New("end of pending data")
errEOR = errors.New("end of ring")
)

type FlushCompleteError = internal.FlushCompleteError

var perfEventHeaderSize = binary.Size(perfEventHeader{})

// perfEventHeader must match 'struct perf_event_header` in <linux/perf_event.h>.
Expand Down Expand Up @@ -378,7 +377,7 @@ func (pr *Reader) ReadInto(rec *Record) error {
if errors.Is(err, os.ErrDeadlineExceeded) {
// We've hit the deadline, check whether there is any data in
// the rings that we've not been woken up for.
pr.pendingErr = &FlushCompleteError{Err: os.ErrDeadlineExceeded}
pr.pendingErr = err
} else if err != nil {
return err
}
Expand Down Expand Up @@ -468,9 +467,9 @@ func (pr *Reader) BufferSize() int {
}

// Flush unblocks Read/ReadInto and successive Read/ReadInto calls will return pending samples at this point,
// until you receive a FlushCompleteError error.
// until you receive a ErrFlushed error.
func (pr *Reader) Flush() error {
pr.pendingErr = &FlushCompleteError{}
pr.pendingErr = ErrFlushed
return pr.poller.Flush()
}

Expand Down
14 changes: 6 additions & 8 deletions perf/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ func TestReaderSetDeadlinePendingEvents(t *testing.T) {

outputSamples(t, events, 5)

// another sample should not be returned before we get FlushCompleteError to indicate initial set of samples read
var fe *FlushCompleteError
// another sample should not be returned before we get ErrFlushed to indicate initial set of samples read
_, err = rd.Read()
if !errors.As(err, &fe) {
t.Error("Expected FlushCompleteError from second Read, got:", err)
if !errors.Is(err, ErrFlushed) {
t.Error("Expected ErrFlushed from second Read, got:", err)
}
if !errors.Is(err, os.ErrDeadlineExceeded) {
t.Error("Expected os.ErrDeadlineExceeded from second Read, got:", err)
Expand Down Expand Up @@ -135,11 +134,10 @@ func TestReaderFlushPendingEvents(t *testing.T) {

outputSamples(t, events, 5)

// another sample should not be returned before we get FlushCompleteError to indicate initial set of samples read
var fe *FlushCompleteError
// another sample should not be returned before we get ErrFlushed to indicate initial set of samples read
_, err = rd.Read()
if !errors.As(err, &fe) {
t.Error("Expected FlushCompleteError from second Read, got:", err)
if !errors.Is(err, ErrFlushed) {
t.Error("Expected ErrFlushed from second Read, got:", err)
}

// the second sample should now be read
Expand Down
16 changes: 7 additions & 9 deletions ringbuf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import (
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/epoll"
"github.com/cilium/ebpf/internal/unix"
)

var (
ErrClosed = os.ErrClosed
errEOR = errors.New("end of ring")
errBusy = errors.New("sample not committed yet")
ErrClosed = os.ErrClosed
ErrFlushed = errors.New("end of pending data")
errEOR = errors.New("end of ring")
errBusy = errors.New("sample not committed yet")
)

type FlushCompleteError = internal.FlushCompleteError

// ringbufHeader from 'struct bpf_ringbuf_hdr' in kernel/bpf/ringbuf.c
type ringbufHeader struct {
Len uint32
Expand Down Expand Up @@ -160,7 +158,7 @@ func (r *Reader) ReadInto(rec *Record) error {
if errors.Is(err, os.ErrDeadlineExceeded) {
// Ignoring this for reading a valid entry after timeout
// This can occur if the producer submitted to the ring buffer with BPF_RB_NO_WAKEUP
r.pendingErr = &FlushCompleteError{Err: os.ErrDeadlineExceeded}
r.pendingErr = err
} else if err != nil {
return err
}
Expand Down Expand Up @@ -189,8 +187,8 @@ func (r *Reader) BufferSize() int {
}

// Flush unblocks Read/ReadInto and successive Read/ReadInto calls will return pending samples at this point,
// until you receive a FlushCompleteError error.
// until you receive a ErrFlushed error.
func (r *Reader) Flush() error {
r.pendingErr = &FlushCompleteError{}
r.pendingErr = ErrFlushed
return r.poller.Flush()
}
10 changes: 4 additions & 6 deletions ringbuf/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,9 @@ func TestReaderNoWakeup(t *testing.T) {
t.Errorf("Expected to read 7 bytes but got %d", len(record.RawSample))
}

var fe *FlushCompleteError
_, err = rd.Read()
if !errors.As(err, &fe) {
t.Errorf("Expected FlushCompleteError from third Read but got %v", err)
if !errors.Is(err, ErrFlushed) {
t.Errorf("Expected ErrFlushed from third Read but got %v", err)
}
if !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("Expected os.ErrDeadlineExceeded from third Read but got %v", err)
Expand Down Expand Up @@ -362,10 +361,9 @@ func TestReaderFlushPendingEvents(t *testing.T) {
t.Errorf("Expected to read 7 bytes but got %d", len(record.RawSample))
}

var fe *FlushCompleteError
_, err = rd.Read()
if !errors.As(err, &fe) {
t.Errorf("Expected FlushCompleteError from third Read but got %v", err)
if !errors.Is(err, ErrFlushed) {
t.Errorf("Expected ErrFlushed from third Read but got %v", err)
}
}

Expand Down

0 comments on commit e5f3960

Please sign in to comment.