Skip to content

Commit

Permalink
add RecordHandler and RecordGetter to ringbuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Nov 9, 2023
1 parent 26ef88f commit b3dfa75
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions ringbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ type RingBufferOptions struct {
// DataHandler - Callback function called when a new sample was retrieved from the perf
// ring buffer.
DataHandler func(CPU int, data []byte, ringBuffer *RingBuffer, manager *Manager)

// RecordHandler - Callback function called when a new record was retrieved from the perf
// ring buffer.
RecordHandler func(record *ringbuf.Record, ringBuffer *RingBuffer, manager *Manager)

// RecordGetter - if specified this getter will be used to get a new record
RecordGetter func() *ringbuf.Record
}

type RingBuffer struct {
Expand Down Expand Up @@ -59,8 +66,8 @@ func loadNewRingBuffer(spec *ebpf.MapSpec, options MapOptions, ringBufferOptions
func (rb *RingBuffer) init(manager *Manager) error {
rb.manager = manager

if rb.DataHandler == nil {
return fmt.Errorf("no DataHandler set for %s", rb.Name)
if rb.DataHandler == nil && rb.RecordHandler == nil {
return fmt.Errorf("no DataHandler/RecordHandler set for %s", rb.Name)
}

// Set default values if not already set
Expand Down Expand Up @@ -95,11 +102,17 @@ func (rb *RingBuffer) Start() error {
rb.wgReader.Add(1)

go func() {
var record ringbuf.Record
var record *ringbuf.Record
var err error

for {
if err = rb.ringReader.ReadInto(&record); err != nil {
if rb.RingBufferOptions.RecordGetter != nil {
record = rb.RingBufferOptions.RecordGetter()
} else if rb.DataHandler != nil {
record = new(ringbuf.Record)
}

if err = rb.ringReader.ReadInto(record); err != nil {
if isRingBufferClosed(err) {
rb.wgReader.Done()
return
Expand All @@ -110,6 +123,12 @@ func (rb *RingBuffer) Start() error {
continue
}
rb.DataHandler(0, record.RawSample, rb, rb.manager)

if rb.RecordHandler != nil {
rb.RecordHandler(record, rb, rb.manager)
} else if rb.DataHandler != nil {
rb.DataHandler(0, record.RawSample, rb, rb.manager)
}
}
}()

Expand Down

0 comments on commit b3dfa75

Please sign in to comment.