Skip to content

Commit

Permalink
Add Remaining and Size to perf/ring Record
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Kahle <bryce.kahle@datadoghq.com>
  • Loading branch information
brycekahle committed Oct 16, 2023
1 parent ed9bf3b commit 3b522ca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions perf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type Record struct {
// The number of samples which could not be output, since
// the ring buffer was full.
LostSamples uint64

// The number of bytes remaining in the per-CPU perf buffer after this Record has been read.
Remaining int

// The total size of the per-CPU perf buffer in bytes.
Size int
}

// Read a record from a reader and tag it as being from the given CPU.
Expand Down Expand Up @@ -439,6 +445,8 @@ func (pr *Reader) readRecordFromRing(rec *Record, ring *perfEventRing) error {
if pr.overwritable && (errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF)) {
return errEOR
}
rec.Size = ring.size()
rec.Remaining = ring.remaining()
return err
}

Expand Down
9 changes: 9 additions & 0 deletions perf/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func createPerfEvent(cpu, watermark int, overwritable bool) (int, error) {
type ringReader interface {
loadHead()
size() int
remaining() int
writeTail()
Read(p []byte) (int, error)
}
Expand Down Expand Up @@ -157,6 +158,10 @@ func (rr *forwardReader) size() int {
return len(rr.ring)
}

func (rr *forwardReader) remaining() int {
return int((rr.head - rr.tail) & rr.mask)
}

func (rr *forwardReader) writeTail() {
// Commit the new tail. This lets the kernel know that
// the ring buffer has been consumed.
Expand Down Expand Up @@ -244,6 +249,10 @@ func (rr *reverseReader) size() int {
return len(rr.ring)
}

func (rr *reverseReader) remaining() int {
return int((rr.tail - rr.read) & rr.mask)
}

func (rr *reverseReader) writeTail() {
// We do not care about tail for over writable perf buffer.
// So, this function is noop.
Expand Down
8 changes: 8 additions & 0 deletions ringbuf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (rh *ringbufHeader) dataLen() int {

type Record struct {
RawSample []byte

// The number of bytes remaining in the ring buffer after this Record has been read.
Remaining int

// The total size of the ring buffer in bytes.
Size int
}

// Read a record from an event ring.
Expand Down Expand Up @@ -98,6 +104,8 @@ func readRecord(rd *ringbufEventRing, rec *Record, buf []byte) error {

rd.storeConsumer()
rec.RawSample = rec.RawSample[:header.dataLen()]
rec.Size = rd.size()
rec.Remaining = rd.remaining()
return nil
}

Expand Down
11 changes: 11 additions & 0 deletions ringbuf/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ func (rr *ringReader) isEmpty() bool {
return prod == cons
}

func (rr *ringReader) size() int {
return cap(rr.ring)
}

func (rr *ringReader) remaining() int {
cons := atomic.LoadUint64(rr.cons_pos)
prod := atomic.LoadUint64(rr.prod_pos)

return int((prod - cons) & rr.mask)
}

func (rr *ringReader) Read(p []byte) (int, error) {
prod := atomic.LoadUint64(rr.prod_pos)

Expand Down

0 comments on commit 3b522ca

Please sign in to comment.