diff --git a/internal/errors.go b/internal/errors.go index ef337b096..bda01e2fd 100644 --- a/internal/errors.go +++ b/internal/errors.go @@ -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 -} diff --git a/perf/reader.go b/perf/reader.go index 3346dc9ba..4ef07f4bd 100644 --- a/perf/reader.go +++ b/perf/reader.go @@ -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 . @@ -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 } @@ -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() } diff --git a/perf/reader_test.go b/perf/reader_test.go index fd5bf4cd2..c04c8eec1 100644 --- a/perf/reader_test.go +++ b/perf/reader_test.go @@ -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) @@ -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 diff --git a/ringbuf/reader.go b/ringbuf/reader.go index cee90acf9..95b1360ad 100644 --- a/ringbuf/reader.go +++ b/ringbuf/reader.go @@ -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 @@ -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 } @@ -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() } diff --git a/ringbuf/reader_test.go b/ringbuf/reader_test.go index e64e9434e..dc1779d8d 100644 --- a/ringbuf/reader_test.go +++ b/ringbuf/reader_test.go @@ -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) @@ -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) } }