Skip to content

Commit

Permalink
chore: give breath to the code putting spaces
Browse files Browse the repository at this point in the history
This adds spaces to the code to make it more readable.

This also puts RingBuffer.Stop() and PerfBuffer.Stop() happy path
out of the branching logic.
  • Loading branch information
geyslan committed Aug 28, 2023
1 parent 7aea3ec commit 10d3c2e
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 54 deletions.
56 changes: 30 additions & 26 deletions buf-perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,48 @@ func (pb *PerfBuffer) Start() {
}

func (pb *PerfBuffer) Stop() {
if pb.stop != nil {
// Tell the poll goroutine that it's time to exit
close(pb.stop)

// The event and lost channels should be drained here since the consumer
// may have stopped at this point. Failure to drain it will
// result in a deadlock: the channel will fill up and the poll
// goroutine will block in the callback.
go func() {
// revive:disable:empty-block
for range pb.eventsChan {
}
if pb.stop == nil {
return
}

if pb.lostChan != nil {
for range pb.lostChan {
}
}
// revive:enable:empty-block
}()
// Signal the poll goroutine to exit
close(pb.stop)

// Wait for the poll goroutine to exit
pb.wg.Wait()
// The event and lost channels should be drained here since the consumer
// may have stopped at this point. Failure to drain it will
// result in a deadlock: the channel will fill up and the poll
// goroutine will block in the callback.
go func() {
// revive:disable:empty-block
for range pb.eventsChan {
}

// Close the channel -- this is useful for the consumer but
// also to terminate the drain goroutine above.
close(pb.eventsChan)
if pb.lostChan != nil {
close(pb.lostChan)
for range pb.lostChan {
}
}
// revive:enable:empty-block
}()

// Wait for the poll goroutine to exit
pb.wg.Wait()

// This allows Stop() to be called multiple times safely
pb.stop = nil
// Close the channel -- this is useful for the consumer but
// also to terminate the drain goroutine above.
close(pb.eventsChan)
if pb.lostChan != nil {
close(pb.lostChan)
}

// Reset pb.stop to allow multiple safe calls to Stop()
pb.stop = nil
}

func (pb *PerfBuffer) Close() {
if pb.closed {
return
}

pb.Stop()
C.perf_buffer__free(pb.pb)
eventChannels.remove(pb.slot)
Expand All @@ -101,6 +104,7 @@ func (pb *PerfBuffer) poll(timeout int) error {
if errno == syscall.EINTR {
continue
}

return fmt.Errorf("error polling perf buffer: %w", errno)
}
}
Expand Down
51 changes: 28 additions & 23 deletions buf-ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,41 @@ func (rb *RingBuffer) Start() {
}

func (rb *RingBuffer) Stop() {
if rb.stop != nil {
// Tell the poll goroutine that it's time to exit
close(rb.stop)

// The event channel should be drained here since the consumer
// may have stopped at this point. Failure to drain it will
// result in a deadlock: the channel will fill up and the poll
// goroutine will block in the callback.
eventChan := eventChannels.get(rb.slot).(chan []byte)
go func() {
// revive:disable:empty-block
for range eventChan {
}
// revive:enable:empty-block
}()
if rb.stop == nil {
return
}

// Wait for the poll goroutine to exit
rb.wg.Wait()
// Signal the poll goroutine to exit
close(rb.stop)

// The event channel should be drained here since the consumer
// may have stopped at this point. Failure to drain it will
// result in a deadlock: the channel will fill up and the poll
// goroutine will block in the callback.
eventChan := eventChannels.get(rb.slot).(chan []byte)
go func() {
// revive:disable:empty-block
for range eventChan {
}
// revive:enable:empty-block
}()

// Close the channel -- this is useful for the consumer but
// also to terminate the drain goroutine above.
close(eventChan)
// Wait for the poll goroutine to exit
rb.wg.Wait()

// This allows Stop() to be called multiple times safely
rb.stop = nil
}
// Close the channel -- this is useful for the consumer but
// also to terminate the drain goroutine above.
close(eventChan)

// Reset pb.stop to allow multiple safe calls to Stop()
rb.stop = nil
}

func (rb *RingBuffer) Close() {
if rb.closed {
return
}

rb.Stop()
C.ring_buffer__free(rb.rb)
eventChannels.remove(rb.slot)
Expand Down Expand Up @@ -100,8 +103,10 @@ func (rb *RingBuffer) poll(timeout int) error {
if errno == syscall.EINTR {
continue
}

return fmt.Errorf("error polling ring buffer: %w", errno)
}
}

return nil
}
1 change: 1 addition & 0 deletions elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ func isGlobalVariableSection(sectionName string) bool {
strings.HasPrefix(sectionName, ".rodata.") {
return true
}

return false
}
1 change: 1 addition & 0 deletions libbpf_cb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func perfLostCallback(ctx unsafe.Pointer, cpu C.int, cnt C.ulonglong) {
func ringbufferCallback(ctx unsafe.Pointer, data unsafe.Pointer, size C.int) C.int {
ch := eventChannels.get(uint(uintptr(ctx))).(chan []byte)
ch <- C.GoBytes(data, size)

return C.int(0)
}

Expand Down
4 changes: 4 additions & 0 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (b LibbpfStrictMode) String() (str string) {
if !ok {
str = LibbpfStrictModeNone.String()
}

return str
}

Expand All @@ -82,6 +83,7 @@ func BPFProgramTypeIsSupported(progType BPFProgType) (bool, error) {
if supportedC < 1 {
return false, syscall.Errno(-supportedC)
}

return supportedC == 1, nil
}

Expand All @@ -90,6 +92,7 @@ func BPFMapTypeIsSupported(mapType MapType) (bool, error) {
if supportedC < 1 {
return false, syscall.Errno(-supportedC)
}

return supportedC == 1, nil
}

Expand All @@ -102,5 +105,6 @@ func NumPossibleCPUs() (int, error) {
if nCPUsC < 0 {
return 0, fmt.Errorf("failed to retrieve the number of CPUs: %w", syscall.Errno(-nCPUsC))
}

return int(nCPUsC), nil
}
6 changes: 6 additions & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (l *BPFLink) DestroyLegacy(linkType LinkType) error {
l.legacy.attachType,
)
}

return fmt.Errorf("unable to destroy legacy link")
}

Expand All @@ -70,7 +71,9 @@ func (l *BPFLink) Destroy() error {
if retC := C.bpf_link__destroy(l.link); retC < 0 {
return syscall.Errno(-retC)
}

l.link = nil

return nil
}

Expand All @@ -91,6 +94,7 @@ func (l *BPFLink) Pin(pinPath string) error {
if retC < 0 {
return fmt.Errorf("failed to pin link %s to path %s: %w", l.eventName, pinPath, syscall.Errno(-retC))
}

return nil
}

Expand All @@ -99,6 +103,7 @@ func (l *BPFLink) Unpin() error {
if retC < 0 {
return fmt.Errorf("failed to unpin link %s: %w", l.eventName, syscall.Errno(-retC))
}

return nil
}

Expand All @@ -111,6 +116,7 @@ func (l *BPFLink) Reader() (*BPFLinkReader, error) {
if fdC < 0 {
return nil, fmt.Errorf("failed to create reader: %w", syscall.Errno(-fdC))
}

return &BPFLinkReader{
l: l,
fd: int(fdC),
Expand Down
2 changes: 2 additions & 0 deletions module-iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func (it *BPFObjectIterator) NextProgram() *BPFProg {
prog: progC,
module: it.m,
}

it.prevProg = prog

return prog
}

0 comments on commit 10d3c2e

Please sign in to comment.