Rename BpfPerfBuffer to PerfEventArray, add struct_parser to BpfObject as a shared_ptr

This commit is contained in:
Pragyansh Chaturvedi
2025-10-19 22:34:10 +05:30
parent 05d5bba4f7
commit cbfe6ae95e
7 changed files with 80 additions and 22 deletions

View File

@ -1,31 +1,37 @@
#ifndef PYLIBBPF_BPF_PERF_BUFFER_H
#define PYLIBBPF_BPF_PERF_BUFFER_H
#ifndef PYLIBBPF_PERF_EVENT_ARRAY_H
#define PYLIBBPF_PERF_EVENT_ARRAY_H
#include <libbpf.h>
#include <pybind11/functional.h>
#include <pybind11/pybind11.h>
#include <string>
class StructParser;
namespace py = pybind11;
class BpfPerfBuffer {
class PerfEventArray {
private:
struct perf_buffer *pb_;
py::function callback_;
py::function lost_callback_;
std::shared_ptr<StructParser> parser_;
std::string struct_name_;
// Static callback wrappers for C API
static void sample_callback_wrapper(void *ctx, int cpu, void *data,
unsigned int size);
static void lost_callback_wrapper(void *ctx, int cpu, unsigned long long cnt);
public:
BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
PerfEventArray(int map_fd, int page_cnt, py::function callback,
py::object lost_callback = py::none());
~BpfPerfBuffer();
~PerfEventArray();
int poll(int timeout_ms);
int consume();
[[nodiscard]] int fd() const;
};
#endif // PYLIBBPF_BPF_PERF_BUFFER_H
#endif // PYLIBBPF_PERF_EVENT_ARRAY_H

View File

@ -1,7 +1,7 @@
#include "bpf_perf_buffer.h"
#include "perf_event_array.h"
#include "core/bpf_exception.h"
BpfPerfBuffer::BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
PerfEventArray::PerfEventArray(int map_fd, int page_cnt, py::function callback,
py::object lost_callback)
: pb_(nullptr), callback_(std::move(callback)),
lost_callback_(lost_callback) {
@ -27,15 +27,15 @@ BpfPerfBuffer::BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
}
}
BpfPerfBuffer::~BpfPerfBuffer() {
PerfEventArray::~PerfEventArray() {
if (pb_) {
perf_buffer__free(pb_);
}
}
void BpfPerfBuffer::sample_callback_wrapper(void *ctx, int cpu, void *data,
void PerfEventArray::sample_callback_wrapper(void *ctx, int cpu, void *data,
unsigned int size) {
auto *self = static_cast<BpfPerfBuffer *>(ctx);
auto *self = static_cast<PerfEventArray *>(ctx);
// Acquire GIL for Python calls
py::gil_scoped_acquire acquire;
@ -51,9 +51,9 @@ void BpfPerfBuffer::sample_callback_wrapper(void *ctx, int cpu, void *data,
}
}
void BpfPerfBuffer::lost_callback_wrapper(void *ctx, int cpu,
void PerfEventArray::lost_callback_wrapper(void *ctx, int cpu,
unsigned long long cnt) {
auto *self = static_cast<BpfPerfBuffer *>(ctx);
auto *self = static_cast<PerfEventArray *>(ctx);
if (self->lost_callback_.is_none()) {
return;
@ -68,13 +68,13 @@ void BpfPerfBuffer::lost_callback_wrapper(void *ctx, int cpu,
}
}
int BpfPerfBuffer::poll(int timeout_ms) {
int PerfEventArray::poll(int timeout_ms) {
// Release GIL during blocking poll
py::gil_scoped_release release;
return perf_buffer__poll(pb_, timeout_ms);
}
int BpfPerfBuffer::consume() {
int PerfEventArray::consume() {
py::gil_scoped_release release;
return perf_buffer__consume(pb_);
}

View File

@ -0,0 +1,37 @@
#ifndef PYLIBBPF_BPF_PERF_BUFFER_H
#define PYLIBBPF_BPF_PERF_BUFFER_H
#include <libbpf.h>
#include <pybind11/functional.h>
#include <pybind11/pybind11.h>
#include <string>
class StructParser;
namespace py = pybind11;
class PerfEventArray {
private:
struct perf_buffer *pb_;
py::function callback_;
py::function lost_callback_;
std::shared_ptr<StructParser> parser_;
std::string struct_name_;
// Static callback wrappers for C API
static void sample_callback_wrapper(void *ctx, int cpu, void *data,
unsigned int size);
static void lost_callback_wrapper(void *ctx, int cpu, unsigned long long cnt);
public:
PerfEventArray(int map_fd, int page_cnt, py::function callback,
py::object lost_callback = py::none());
~PerfEventArray();
int poll(int timeout_ms);
int consume();
[[nodiscard]] int fd() const;
};
#endif // PYLIBBPF_BPF_PERF_BUFFER_H