opensnitch/ebpf_prog/tests/event_stats_test.c
trvon 0be4f7c4c4
daemon: event-loss diagnostics via per-module event_stats counters
Add fixed-size, process-lifetime counters (received/enqueued/dropped/
drained/dequeued/decoded/decode-errors/reader-errors/canceled) for the
process and DNS eBPF event paths, exposed as individually-atomic
snapshots. Wire DNSEventCounters and ProcessEventCounters through the
perf reader/enqueue/decode loop in daemon/dns and daemon/procmon/ebpf,
with a matching ebpf_prog/event_stats.h and a structural eBPF test.
Counters hold totals only, never event payloads.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-05 16:58:11 -07:00

42 lines
1.5 KiB
C

/* Unprivileged unit test of the actual shared emission helper, with fake BPF helpers. */
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
typedef uint32_t u32;
typedef uint64_t u64;
#define BPF_MAP_TYPE_PERCPU_ARRAY 6
#define __uint(name, value) int (*name)[value]
#define __type(name, value) value *name
#define SEC(name)
#undef __always_inline
#define __always_inline inline __attribute__((always_inline))
static int events;
static long output_result;
static int output_calls;
static int lookup_missing;
static long bpf_ringbuf_output(void *, void *, u64, u64);
static void *bpf_map_lookup_elem(void *, const void *);
#include "../event_stats.h"
static struct event_emission_counts counts;
static long bpf_ringbuf_output(void *map, void *data, u64 size, u64 flags) {
assert(map == &events && data != NULL && size == 1 && flags == 0);
output_calls++;
return output_result;
}
static void *bpf_map_lookup_elem(void *map, const void *key) {
assert(map == &event_stats && *(const u32 *)key == 0);
return lookup_missing ? NULL : &counts;
}
int main(void) {
char data = 0;
assert(sizeof(counts) == 16);
assert(emit_counted_event(&data, 1) == 0);
assert(counts.attempted == 1 && counts.failed == 0 && output_calls == 1);
output_result = -11;
assert(emit_counted_event(&data, 1) == -11);
assert(counts.attempted == 2 && counts.failed == 1 && output_calls == 2);
lookup_missing = 1;
assert(emit_counted_event(&data, 1) == -11);
assert(counts.attempted == 2 && counts.failed == 1 && output_calls == 3);
return 0;
}