Skip to content

Commit eb703d6

Browse files
committed
Add ebpf lost event counter. Issue #38
Pretty straighforward, contrary to kprobes which we get the counter on the data path, with ebpf we have to actually read it, so add a new ops for updating the counter, we should caution users to not hammer the reading, as it's real syscall. Tested by hacking quark-mon away.
1 parent 68af27c commit eb703d6

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

bpf_queue.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111
#include "elastic-ebpf/GPL/Events/EbpfEventProto.h"
1212

1313
struct bpf_queue {
14-
struct bpf_prog *prog;
15-
struct ring_buffer *ringbuf;
14+
struct bpf_prog *prog;
15+
struct ring_buffer *ringbuf;
1616
};
1717

1818
static int bpf_queue_populate(struct quark_queue *);
19+
static int bpf_queue_update_stats(struct quark_queue *);
1920
static void bpf_queue_close(struct quark_queue *);
2021

2122
struct quark_queue_ops queue_ops_bpf = {
22-
.open = bpf_queue_open,
23-
.populate = bpf_queue_populate,
24-
.close = bpf_queue_close,
23+
.open = bpf_queue_open,
24+
.populate = bpf_queue_populate,
25+
.update_stats = bpf_queue_update_stats,
26+
.close = bpf_queue_close,
2527
};
2628

2729
static int
@@ -262,7 +264,6 @@ bpf_queue_populate(struct quark_queue *qq)
262264
struct bpf_queue *bqq = qq->queue_be;
263265
int npop, space_left;
264266

265-
npop = 0;
266267
space_left = qq->length >= qq->max_length ?
267268
0 : qq->max_length - qq->length;
268269
if (space_left == 0)
@@ -273,6 +274,24 @@ bpf_queue_populate(struct quark_queue *qq)
273274
return (npop < 0 ? -1 : npop);
274275
}
275276

277+
static int
278+
bpf_queue_update_stats(struct quark_queue *qq)
279+
{
280+
struct bpf_queue *bqq = qq->queue_be;
281+
struct ebpf_event_stats pcpu_ees[libbpf_num_possible_cpus()];
282+
u32 zero = 0;
283+
int i;
284+
285+
if (bpf_map__lookup_elem(bqq->prog->maps.ringbuf_stats, &zero,
286+
sizeof(zero), pcpu_ees, sizeof(pcpu_ees), 0) != 0)
287+
return (-1);
288+
289+
for (i = 0; i < libbpf_num_possible_cpus(); i++)
290+
qq->stats.lost = pcpu_ees[i].lost;
291+
292+
return (0);
293+
}
294+
276295
static void
277296
bpf_queue_close(struct quark_queue *qq)
278297
{

kprobe_queue.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,14 @@ struct kprobe_queue {
266266
};
267267

268268
static int kprobe_queue_populate(struct quark_queue *);
269+
static int kprobe_queue_update_stats(struct quark_queue *);
269270
static void kprobe_queue_close(struct quark_queue *);
270271

271272
struct quark_queue_ops queue_ops_kprobe = {
272-
.open = kprobe_queue_open,
273-
.populate = kprobe_queue_populate,
274-
.close = kprobe_queue_close,
273+
.open = kprobe_queue_open,
274+
.populate = kprobe_queue_populate,
275+
.update_stats = kprobe_queue_update_stats,
276+
.close = kprobe_queue_close,
275277
};
276278

277279
static char *
@@ -1298,6 +1300,13 @@ kprobe_queue_populate(struct quark_queue *qq)
12981300
return (npop);
12991301
}
13001302

1303+
static int
1304+
kprobe_queue_update_stats(struct quark_queue *qq)
1305+
{
1306+
/* NADA */
1307+
return (0);
1308+
}
1309+
13011310
static void
13021311
kprobe_queue_close(struct quark_queue *qq)
13031312
{

quark.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,7 @@ quark_queue_get_epollfd(struct quark_queue *qq)
15971597
void
15981598
quark_queue_get_stats(struct quark_queue *qq, struct quark_queue_stats *qs)
15991599
{
1600+
qq->queue_ops->update_stats(qq);
16001601
*qs = qq->stats;
16011602
}
16021603

quark.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ struct quark_queue_stats {
320320
struct quark_queue_ops {
321321
int (*open)(struct quark_queue *);
322322
int (*populate)(struct quark_queue *);
323+
int (*update_stats)(struct quark_queue *);
323324
void (*close)(struct quark_queue *);
324325
};
325326

0 commit comments

Comments
 (0)