Skip to content

Commit 0228f12

Browse files
committed
chore: give breath to the code putting spaces
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.
1 parent 77c521f commit 0228f12

File tree

9 files changed

+108
-54
lines changed

9 files changed

+108
-54
lines changed

buf-perf.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,48 @@ func (pb *PerfBuffer) Start() {
4141
}
4242

4343
func (pb *PerfBuffer) Stop() {
44-
if pb.stop != nil {
45-
// Tell the poll goroutine that it's time to exit
46-
close(pb.stop)
47-
48-
// The event and lost channels should be drained here since the consumer
49-
// may have stopped at this point. Failure to drain it will
50-
// result in a deadlock: the channel will fill up and the poll
51-
// goroutine will block in the callback.
52-
go func() {
53-
// revive:disable:empty-block
54-
for range pb.eventsChan {
55-
}
44+
if pb.stop == nil {
45+
return
46+
}
5647

57-
if pb.lostChan != nil {
58-
for range pb.lostChan {
59-
}
60-
}
61-
// revive:enable:empty-block
62-
}()
48+
// Signal the poll goroutine to exit
49+
close(pb.stop)
6350

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

67-
// Close the channel -- this is useful for the consumer but
68-
// also to terminate the drain goroutine above.
69-
close(pb.eventsChan)
7060
if pb.lostChan != nil {
71-
close(pb.lostChan)
61+
for range pb.lostChan {
62+
}
7263
}
64+
// revive:enable:empty-block
65+
}()
66+
67+
// Wait for the poll goroutine to exit
68+
pb.wg.Wait()
7369

74-
// This allows Stop() to be called multiple times safely
75-
pb.stop = nil
70+
// Close the channel -- this is useful for the consumer but
71+
// also to terminate the drain goroutine above.
72+
close(pb.eventsChan)
73+
if pb.lostChan != nil {
74+
close(pb.lostChan)
7675
}
76+
77+
// Reset pb.stop to allow multiple safe calls to Stop()
78+
pb.stop = nil
7779
}
7880

7981
func (pb *PerfBuffer) Close() {
8082
if pb.closed {
8183
return
8284
}
85+
8386
pb.Stop()
8487
C.perf_buffer__free(pb.pb)
8588
eventChannels.remove(pb.slot)
@@ -101,6 +104,7 @@ func (pb *PerfBuffer) poll(timeout int) error {
101104
if errno == syscall.EINTR {
102105
continue
103106
}
107+
104108
return fmt.Errorf("error polling perf buffer: %w", errno)
105109
}
106110
}

buf-ring.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,41 @@ func (rb *RingBuffer) Start() {
3939
}
4040

4141
func (rb *RingBuffer) Stop() {
42-
if rb.stop != nil {
43-
// Tell the poll goroutine that it's time to exit
44-
close(rb.stop)
45-
46-
// The event channel should be drained here since the consumer
47-
// may have stopped at this point. Failure to drain it will
48-
// result in a deadlock: the channel will fill up and the poll
49-
// goroutine will block in the callback.
50-
eventChan := eventChannels.get(rb.slot).(chan []byte)
51-
go func() {
52-
// revive:disable:empty-block
53-
for range eventChan {
54-
}
55-
// revive:enable:empty-block
56-
}()
42+
if rb.stop == nil {
43+
return
44+
}
5745

58-
// Wait for the poll goroutine to exit
59-
rb.wg.Wait()
46+
// Signal the poll goroutine to exit
47+
close(rb.stop)
48+
49+
// The event channel should be drained here since the consumer
50+
// may have stopped at this point. Failure to drain it will
51+
// result in a deadlock: the channel will fill up and the poll
52+
// goroutine will block in the callback.
53+
eventChan := eventChannels.get(rb.slot).(chan []byte)
54+
go func() {
55+
// revive:disable:empty-block
56+
for range eventChan {
57+
}
58+
// revive:enable:empty-block
59+
}()
6060

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

65-
// This allows Stop() to be called multiple times safely
66-
rb.stop = nil
67-
}
64+
// Close the channel -- this is useful for the consumer but
65+
// also to terminate the drain goroutine above.
66+
close(eventChan)
67+
68+
// Reset pb.stop to allow multiple safe calls to Stop()
69+
rb.stop = nil
6870
}
6971

7072
func (rb *RingBuffer) Close() {
7173
if rb.closed {
7274
return
7375
}
76+
7477
rb.Stop()
7578
C.ring_buffer__free(rb.rb)
7679
eventChannels.remove(rb.slot)
@@ -100,8 +103,10 @@ func (rb *RingBuffer) poll(timeout int) error {
100103
if errno == syscall.EINTR {
101104
continue
102105
}
106+
103107
return fmt.Errorf("error polling ring buffer: %w", errno)
104108
}
105109
}
110+
106111
return nil
107112
}

elf.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ func isGlobalVariableSection(sectionName string) bool {
5757
strings.HasPrefix(sectionName, ".rodata.") {
5858
return true
5959
}
60+
6061
return false
6162
}

libbpf_cb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func perfLostCallback(ctx unsafe.Pointer, cpu C.int, cnt C.ulonglong) {
2828
func ringbufferCallback(ctx unsafe.Pointer, data unsafe.Pointer, size C.int) C.int {
2929
ch := eventChannels.get(uint(uintptr(ctx))).(chan []byte)
3030
ch <- C.GoBytes(data, size)
31+
3132
return C.int(0)
3233
}
3334

libbpfgo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (b LibbpfStrictMode) String() (str string) {
6565
if !ok {
6666
str = LibbpfStrictModeNone.String()
6767
}
68+
6869
return str
6970
}
7071

@@ -82,6 +83,7 @@ func BPFProgramTypeIsSupported(progType BPFProgType) (bool, error) {
8283
if supportedC < 1 {
8384
return false, syscall.Errno(-supportedC)
8485
}
86+
8587
return supportedC == 1, nil
8688
}
8789

@@ -90,6 +92,7 @@ func BPFMapTypeIsSupported(mapType MapType) (bool, error) {
9092
if supportedC < 1 {
9193
return false, syscall.Errno(-supportedC)
9294
}
95+
9396
return supportedC == 1, nil
9497
}
9598

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

link.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (l *BPFLink) DestroyLegacy(linkType LinkType) error {
6060
l.legacy.attachType,
6161
)
6262
}
63+
6364
return fmt.Errorf("unable to destroy legacy link")
6465
}
6566

@@ -70,7 +71,9 @@ func (l *BPFLink) Destroy() error {
7071
if retC := C.bpf_link__destroy(l.link); retC < 0 {
7172
return syscall.Errno(-retC)
7273
}
74+
7375
l.link = nil
76+
7477
return nil
7578
}
7679

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

@@ -99,6 +103,7 @@ func (l *BPFLink) Unpin() error {
99103
if retC < 0 {
100104
return fmt.Errorf("failed to unpin link %s: %w", l.eventName, syscall.Errno(-retC))
101105
}
106+
102107
return nil
103108
}
104109

@@ -111,6 +116,7 @@ func (l *BPFLink) Reader() (*BPFLinkReader, error) {
111116
if fdC < 0 {
112117
return nil, fmt.Errorf("failed to create reader: %w", syscall.Errno(-fdC))
113118
}
119+
114120
return &BPFLinkReader{
115121
l: l,
116122
fd: int(fdC),

module-iterator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ func (it *BPFObjectIterator) NextProgram() *BPFProg {
7272
prog: progC,
7373
module: it.m,
7474
}
75+
7576
it.prevProg = prog
77+
7678
return prog
7779
}

module.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ func (m *Module) InitPerfBuf(mapName string, eventsChan chan []byte, lostChan ch
349349
if err != nil {
350350
return nil, fmt.Errorf("failed to init perf buffer: %v", err)
351351
}
352+
352353
if eventsChan == nil {
353354
return nil, fmt.Errorf("failed to init perf buffer: events channel can not be nil")
354355
}

0 commit comments

Comments
 (0)