From 9a108810c2fcfd96c9253e36664d9aa1ffe90190 Mon Sep 17 00:00:00 2001 From: Lorenz Bauer Date: Mon, 24 Jun 2024 16:52:47 +0100 Subject: [PATCH] perf: rearrange fields to make locking of Reader clearer Reader has two separate locks which guard separate data. Re-organise the fields so that it's clearer which lock protects which fields. Signed-off-by: Lorenz Bauer --- perf/reader.go | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/perf/reader.go b/perf/reader.go index 4d2257a1a..ee557e449 100644 --- a/perf/reader.go +++ b/perf/reader.go @@ -136,33 +136,27 @@ func readRawSample(rd io.Reader, buf, sampleBuf []byte) ([]byte, error) { // Reader allows reading bpf_perf_event_output // from user space. type Reader struct { - poller *epoll.Poller - deadline time.Time + poller *epoll.Poller // mu protects read/write access to the Reader structure with the - // exception of 'pauseFds', which is protected by 'pauseMu'. + // exception fields protected by 'pauseMu'. // If locking both 'mu' and 'pauseMu', 'mu' must be locked first. - mu sync.Mutex - - // Closing a PERF_EVENT_ARRAY removes all event fds - // stored in it, so we keep a reference alive. - array *ebpf.Map - rings []*perfEventRing - epollEvents []unix.EpollEvent - epollRings []*perfEventRing - eventHeader []byte + mu sync.Mutex + array *ebpf.Map + rings []*perfEventRing + epollEvents []unix.EpollEvent + epollRings []*perfEventRing + eventHeader []byte + deadline time.Time + overwritable bool + bufferSize int + pendingErr error // pauseMu protects eventFds so that Pause / Resume can be invoked while // Read is blocked. pauseMu sync.Mutex eventFds []*sys.FD - - paused bool - overwritable bool - - bufferSize int - - pendingErr error + paused bool } // ReaderOptions control the behaviour of the user @@ -245,6 +239,8 @@ func NewReaderWithOptions(array *ebpf.Map, perCPUBuffer int, opts ReaderOptions) } } + // Closing a PERF_EVENT_ARRAY removes all event fds + // stored in it, so we keep a reference alive. array, err = array.Clone() if err != nil { return nil, err