Skip to content

Commit

Permalink
Add ebpf lost event counter. Issue #38
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
haesbaert committed Jul 3, 2024
1 parent 68af27c commit eb703d6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
31 changes: 25 additions & 6 deletions bpf_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
#include "elastic-ebpf/GPL/Events/EbpfEventProto.h"

struct bpf_queue {
struct bpf_prog *prog;
struct ring_buffer *ringbuf;
struct bpf_prog *prog;
struct ring_buffer *ringbuf;
};

static int bpf_queue_populate(struct quark_queue *);
static int bpf_queue_update_stats(struct quark_queue *);
static void bpf_queue_close(struct quark_queue *);

struct quark_queue_ops queue_ops_bpf = {
.open = bpf_queue_open,
.populate = bpf_queue_populate,
.close = bpf_queue_close,
.open = bpf_queue_open,
.populate = bpf_queue_populate,
.update_stats = bpf_queue_update_stats,
.close = bpf_queue_close,
};

static int
Expand Down Expand Up @@ -262,7 +264,6 @@ bpf_queue_populate(struct quark_queue *qq)
struct bpf_queue *bqq = qq->queue_be;
int npop, space_left;

npop = 0;
space_left = qq->length >= qq->max_length ?
0 : qq->max_length - qq->length;
if (space_left == 0)
Expand All @@ -273,6 +274,24 @@ bpf_queue_populate(struct quark_queue *qq)
return (npop < 0 ? -1 : npop);
}

static int
bpf_queue_update_stats(struct quark_queue *qq)
{
struct bpf_queue *bqq = qq->queue_be;
struct ebpf_event_stats pcpu_ees[libbpf_num_possible_cpus()];
u32 zero = 0;
int i;

if (bpf_map__lookup_elem(bqq->prog->maps.ringbuf_stats, &zero,
sizeof(zero), pcpu_ees, sizeof(pcpu_ees), 0) != 0)
return (-1);

for (i = 0; i < libbpf_num_possible_cpus(); i++)
qq->stats.lost = pcpu_ees[i].lost;

return (0);
}

static void
bpf_queue_close(struct quark_queue *qq)
{
Expand Down
15 changes: 12 additions & 3 deletions kprobe_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,14 @@ struct kprobe_queue {
};

static int kprobe_queue_populate(struct quark_queue *);
static int kprobe_queue_update_stats(struct quark_queue *);
static void kprobe_queue_close(struct quark_queue *);

struct quark_queue_ops queue_ops_kprobe = {
.open = kprobe_queue_open,
.populate = kprobe_queue_populate,
.close = kprobe_queue_close,
.open = kprobe_queue_open,
.populate = kprobe_queue_populate,
.update_stats = kprobe_queue_update_stats,
.close = kprobe_queue_close,
};

static char *
Expand Down Expand Up @@ -1298,6 +1300,13 @@ kprobe_queue_populate(struct quark_queue *qq)
return (npop);
}

static int
kprobe_queue_update_stats(struct quark_queue *qq)
{
/* NADA */
return (0);
}

static void
kprobe_queue_close(struct quark_queue *qq)
{
Expand Down
1 change: 1 addition & 0 deletions quark.c
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,7 @@ quark_queue_get_epollfd(struct quark_queue *qq)
void
quark_queue_get_stats(struct quark_queue *qq, struct quark_queue_stats *qs)
{
qq->queue_ops->update_stats(qq);
*qs = qq->stats;
}

Expand Down
1 change: 1 addition & 0 deletions quark.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ struct quark_queue_stats {
struct quark_queue_ops {
int (*open)(struct quark_queue *);
int (*populate)(struct quark_queue *);
int (*update_stats)(struct quark_queue *);
void (*close)(struct quark_queue *);
};

Expand Down

0 comments on commit eb703d6

Please sign in to comment.