Skip to content

Commit cbfe6ae

Browse files
committed
Rename BpfPerfBuffer to PerfEventArray, add struct_parser to BpfObject as a shared_ptr
1 parent 05d5bba commit cbfe6ae

File tree

7 files changed

+80
-22
lines changed

7 files changed

+80
-22
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pybind11_add_module(
2121
src/core/bpf_object.cpp
2222

2323
# Maps
24-
src/maps/bpf_perf_buffer.h
25-
src/maps/bpf_perf_buffer.cpp
24+
src/maps/perf_event_array.h
25+
src/maps/perf_event_array.cpp
2626

2727
# Utils
2828
src/utils/struct_parser.h

src/bindings/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern "C" {
1010
#include "core/bpf_program.h"
1111
#include "core/bpf_exception.h"
1212
#include "core/bpf_map.h"
13-
#include "maps/bpf_perf_buffer.h"
13+
#include "maps/perf_event_array.h"
1414

1515
namespace py = pybind11;
1616

@@ -66,14 +66,14 @@ PYBIND11_MODULE(pylibbpf, m) {
6666
.def("get_value_size", &BpfMap::get_value_size)
6767
.def("get_max_entries", &BpfMap::get_max_entries);
6868

69-
py::class_<BpfPerfBuffer>(m, "BpfPerfBuffer")
69+
py::class_<PerfEventArray>(m, "PerfEventArray")
7070
.def(py::init<int, int, py::function, py::object>(),
7171
py::arg("map_fd"),
7272
py::arg("page_cnt") = 8,
7373
py::arg("callback"),
7474
py::arg("lost_callback") = py::none())
75-
.def("poll", &BpfPerfBuffer::poll, py::arg("timeout_ms") = -1)
76-
.def("consume", &BpfPerfBuffer::consume);
75+
.def("poll", &PerfEventArray::poll, py::arg("timeout_ms") = -1)
76+
.def("consume", &PerfEventArray::consume);
7777

7878

7979
#ifdef VERSION_INFO

src/core/bpf_object.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
#include "bpf_exception.h"
33
#include "bpf_map.h"
44
#include "bpf_program.h"
5+
#include "utils/struct_parser.h"
56
#include <cerrno>
67

78
BpfObject::BpfObject(std::string object_path, py::dict structs)
89
: obj_(nullptr), object_path_(std::move(object_path)), loaded_(false),
9-
struct_defs_(structs) {}
10+
struct_defs_(structs), struct_parser_(nullptr) {}
1011

1112
BpfObject::~BpfObject() {
1213
// Clear caches first (order matters!)
@@ -256,3 +257,11 @@ py::dict BpfObject::get_cached_maps() const {
256257
}
257258
return maps;
258259
}
260+
261+
std::shared_ptr<StructParser> BpfObject::get_struct_parser() const {
262+
if (!struct_parser_ && !struct_defs_.empty()) {
263+
// Create parser on first access
264+
struct_parser_ = std::make_shared<StructParser>(struct_defs_);
265+
}
266+
return struct_parser_;
267+
}

src/core/bpf_object.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace py = pybind11;
1212

1313
class BpfProgram;
1414
class BpfMap;
15+
class StructParser;
1516

1617
/**
1718
* BpfObject - Represents a loaded BPF object file.
@@ -29,6 +30,7 @@ class BpfObject : public std::enable_shared_from_this<BpfObject> {
2930
mutable std::unordered_map<std::string, std::shared_ptr<BpfProgram>>
3031
prog_cache_;
3132
py::dict struct_defs_;
33+
mutable std::shared_ptr<StructParser> struct_parser_;
3234

3335
std::shared_ptr<BpfProgram> _get_or_create_program(struct bpf_program *prog);
3436
std::shared_ptr<BpfMap> _get_or_create_map(struct bpf_map *map);
@@ -78,6 +80,10 @@ class BpfObject : public std::enable_shared_from_this<BpfObject> {
7880
[[nodiscard]] std::shared_ptr<BpfMap> get_map(const std::string &name);
7981
[[nodiscard]] struct bpf_map *find_map_by_name(const std::string &name) const;
8082
[[nodiscard]] py::dict get_cached_maps() const;
83+
84+
// Struct parsing
85+
[[nodiscard]] py::dict get_struct_defs() const { return struct_defs_; }
86+
[[nodiscard]] std::shared_ptr<StructParser> get_struct_parser() const;
8187
};
8288

8389
#endif // PYLIBBPF_BPF_OBJECT_H

src/maps/bpf_perf_buffer.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1-
#ifndef PYLIBBPF_BPF_PERF_BUFFER_H
2-
#define PYLIBBPF_BPF_PERF_BUFFER_H
1+
#ifndef PYLIBBPF_PERF_EVENT_ARRAY_H
2+
#define PYLIBBPF_PERF_EVENT_ARRAY_H
33

44
#include <libbpf.h>
55
#include <pybind11/functional.h>
66
#include <pybind11/pybind11.h>
7+
#include <string>
8+
9+
class StructParser;
710

811
namespace py = pybind11;
912

10-
class BpfPerfBuffer {
13+
class PerfEventArray {
1114
private:
1215
struct perf_buffer *pb_;
1316
py::function callback_;
1417
py::function lost_callback_;
1518

19+
std::shared_ptr<StructParser> parser_;
20+
std::string struct_name_;
21+
1622
// Static callback wrappers for C API
1723
static void sample_callback_wrapper(void *ctx, int cpu, void *data,
1824
unsigned int size);
1925
static void lost_callback_wrapper(void *ctx, int cpu, unsigned long long cnt);
2026

2127
public:
22-
BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
28+
PerfEventArray(int map_fd, int page_cnt, py::function callback,
2329
py::object lost_callback = py::none());
24-
~BpfPerfBuffer();
30+
~PerfEventArray();
2531

2632
int poll(int timeout_ms);
2733
int consume();
2834
[[nodiscard]] int fd() const;
2935
};
3036

31-
#endif // PYLIBBPF_BPF_PERF_BUFFER_H
37+
#endif // PYLIBBPF_PERF_EVENT_ARRAY_H

src/maps/bpf_perf_buffer.cpp renamed to src/maps/perf_event_array.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include "bpf_perf_buffer.h"
1+
#include "perf_event_array.h"
22
#include "core/bpf_exception.h"
33

4-
BpfPerfBuffer::BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
4+
PerfEventArray::PerfEventArray(int map_fd, int page_cnt, py::function callback,
55
py::object lost_callback)
66
: pb_(nullptr), callback_(std::move(callback)),
77
lost_callback_(lost_callback) {
@@ -27,15 +27,15 @@ BpfPerfBuffer::BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
2727
}
2828
}
2929

30-
BpfPerfBuffer::~BpfPerfBuffer() {
30+
PerfEventArray::~PerfEventArray() {
3131
if (pb_) {
3232
perf_buffer__free(pb_);
3333
}
3434
}
3535

36-
void BpfPerfBuffer::sample_callback_wrapper(void *ctx, int cpu, void *data,
36+
void PerfEventArray::sample_callback_wrapper(void *ctx, int cpu, void *data,
3737
unsigned int size) {
38-
auto *self = static_cast<BpfPerfBuffer *>(ctx);
38+
auto *self = static_cast<PerfEventArray *>(ctx);
3939

4040
// Acquire GIL for Python calls
4141
py::gil_scoped_acquire acquire;
@@ -51,9 +51,9 @@ void BpfPerfBuffer::sample_callback_wrapper(void *ctx, int cpu, void *data,
5151
}
5252
}
5353

54-
void BpfPerfBuffer::lost_callback_wrapper(void *ctx, int cpu,
54+
void PerfEventArray::lost_callback_wrapper(void *ctx, int cpu,
5555
unsigned long long cnt) {
56-
auto *self = static_cast<BpfPerfBuffer *>(ctx);
56+
auto *self = static_cast<PerfEventArray *>(ctx);
5757

5858
if (self->lost_callback_.is_none()) {
5959
return;
@@ -68,13 +68,13 @@ void BpfPerfBuffer::lost_callback_wrapper(void *ctx, int cpu,
6868
}
6969
}
7070

71-
int BpfPerfBuffer::poll(int timeout_ms) {
71+
int PerfEventArray::poll(int timeout_ms) {
7272
// Release GIL during blocking poll
7373
py::gil_scoped_release release;
7474
return perf_buffer__poll(pb_, timeout_ms);
7575
}
7676

77-
int BpfPerfBuffer::consume() {
77+
int PerfEventArray::consume() {
7878
py::gil_scoped_release release;
7979
return perf_buffer__consume(pb_);
8080
}

src/maps/perf_event_array.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef PYLIBBPF_BPF_PERF_BUFFER_H
2+
#define PYLIBBPF_BPF_PERF_BUFFER_H
3+
4+
#include <libbpf.h>
5+
#include <pybind11/functional.h>
6+
#include <pybind11/pybind11.h>
7+
#include <string>
8+
9+
class StructParser;
10+
11+
namespace py = pybind11;
12+
13+
class PerfEventArray {
14+
private:
15+
struct perf_buffer *pb_;
16+
py::function callback_;
17+
py::function lost_callback_;
18+
19+
std::shared_ptr<StructParser> parser_;
20+
std::string struct_name_;
21+
22+
// Static callback wrappers for C API
23+
static void sample_callback_wrapper(void *ctx, int cpu, void *data,
24+
unsigned int size);
25+
static void lost_callback_wrapper(void *ctx, int cpu, unsigned long long cnt);
26+
27+
public:
28+
PerfEventArray(int map_fd, int page_cnt, py::function callback,
29+
py::object lost_callback = py::none());
30+
~PerfEventArray();
31+
32+
int poll(int timeout_ms);
33+
int consume();
34+
[[nodiscard]] int fd() const;
35+
};
36+
37+
#endif // PYLIBBPF_BPF_PERF_BUFFER_H

0 commit comments

Comments
 (0)