Skip to content

Commit

Permalink
docs(bpf_engine): add some docs and rename some variables
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
  • Loading branch information
Andreagit97 committed Aug 1, 2024
1 parent 3ce0a2d commit ab24259
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions userspace/libscap/engine/bpf/scap_bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ static inline scap_evt *scap_bpf_evt_from_perf_sample(void *evt)

static inline void scap_bpf_get_buf_pointers(scap_device *dev, uint64_t *phead, uint64_t *ptail, uint64_t *pread_size)
{
struct perf_event_mmap_page *header;
uint64_t begin;
uint64_t end;

header = (struct perf_event_mmap_page *) dev->m_buffer;
struct perf_event_mmap_page * header = (struct perf_event_mmap_page *) dev->m_buffer;

Check warning on line 46 in userspace/libscap/engine/bpf/scap_bpf.h

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.h#L46

Added line #L46 was not covered by tests

*phead = header->data_head;
*ptail = header->data_tail;
Expand All @@ -56,16 +52,42 @@ static inline void scap_bpf_get_buf_pointers(scap_device *dev, uint64_t *phead,
asm volatile("" ::: "memory");
// clang-format on

begin = *ptail % header->data_size;
end = *phead % header->data_size;

if(begin > end)
uint64_t cons = *ptail % header->data_size; // consumer position
uint64_t prod = *phead % header->data_size; // producer position

Check warning on line 56 in userspace/libscap/engine/bpf/scap_bpf.h

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.h#L55-L56

Added lines #L55 - L56 were not covered by tests

/* `pread_size` is the number of bytes our consumer has to read to reach the producer.
* We want to obtain this information so we know how many bytes we can read.
*
* We have 2 possible cases:
* Where
* '*' = empty space
* '-' = data space
* 's' = total buffer size
* 'c' = consumer position
* 'p' = producer position
*
* 1. consumer > producer
*
* p c s
* |----|*********|--|
*
*
* We want to obtain the data space so we do `s - c + p`.
*
* 2. consumer <= producer
*
* c p s
* |****|---------|**|
*
* We want to obtain the data space so we do `p - c`.
*/
if(cons > prod)
{
*pread_size = header->data_size - begin + end;
*pread_size = header->data_size - cons + prod;

Check warning on line 86 in userspace/libscap/engine/bpf/scap_bpf.h

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.h#L86

Added line #L86 was not covered by tests
}
else
{
*pread_size = end - begin;
*pread_size = prod - cons;

Check warning on line 90 in userspace/libscap/engine/bpf/scap_bpf.h

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.h#L90

Added line #L90 was not covered by tests
}
}

Expand Down

0 comments on commit ab24259

Please sign in to comment.