opensnitch/daemon/core/event_stats.go
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

210 lines
6.7 KiB
Go

package core
import (
"sync"
"sync/atomic"
"github.com/cilium/ebpf"
)
// EventCounters holds fixed-size, process-lifetime totals, never event payloads.
// Snapshots are individually atomic, not a transaction across counters. Accounting
// identities apply at quiescent checkpoints; uint64 counters wrap modulo 2^64.
type EventCounters struct {
received, enqueued, droppedCurrent, drained atomic.Uint64
dequeued, decoded, decodeErrors, readerErrors, canceled atomic.Uint64
}
type EventQueueSnapshot struct {
Received uint64
Enqueued uint64
DroppedCurrent uint64
Drained uint64
Dequeued uint64
Decoded uint64
DecodeErrors uint64
ReaderErrors uint64
Canceled uint64
}
// These two stores have constant cardinality and are not reset on module reload.
var ProcessEventCounters, DNSEventCounters EventCounters
// TryEnqueue preserves the existing nonblocking send and current-record drop.
// The caller retains its original logging followed by Drain on failure.
func (c *EventCounters) TryEnqueue(q chan []byte, record []byte) bool {
c.received.Add(1)
select {
case q <- record:
c.enqueued.Add(1)
return true
default:
c.droppedCurrent.Add(1)
return false
}
}
// Drain retains discard-all behavior for the application's sole queue producer.
// There can be at most cap(q) buffered records. Bound the loop even if a future
// caller accidentally introduces competing producers; never wait for a record.
func (c *EventCounters) Drain(q <-chan []byte) {
for i := 0; i < cap(q); i++ {
select {
case _, ok := <-q:
if !ok {
return
}
c.drained.Add(1)
default:
return
}
}
}
func (c *EventCounters) RecordDequeued() { c.dequeued.Add(1) }
func (c *EventCounters) RecordDecoded() { c.decoded.Add(1) }
func (c *EventCounters) RecordDecodeError() { c.decodeErrors.Add(1) }
func (c *EventCounters) RecordReaderError() { c.readerErrors.Add(1) }
func (c *EventCounters) RecordCanceled() { c.canceled.Add(1) }
func (c *EventCounters) Snapshot() EventQueueSnapshot {
return EventQueueSnapshot{c.received.Load(), c.enqueued.Load(), c.droppedCurrent.Load(), c.drained.Load(), c.dequeued.Load(), c.decoded.Load(), c.decodeErrors.Load(), c.readerErrors.Load(), c.canceled.Load()}
}
type EventModule uint8
const (
EventModuleProcess EventModule = iota
EventModuleDNS
eventModuleCount
)
// eventEmissionCounts must match event_stats.h: one 16-byte value per CPU.
type eventEmissionCounts struct{ Attempted, Failed uint64 }
type eventEmissionSource interface {
read() (eventEmissionCounts, error)
}
type kernelEmissionSource struct {
m *ebpf.Map
cpus int
}
func (s *kernelEmissionSource) read() (eventEmissionCounts, error) {
values := make([]eventEmissionCounts, s.cpus)
if err := s.m.Lookup(uint32(0), &values); err != nil {
return eventEmissionCounts{}, err
}
var total eventEmissionCounts
for _, v := range values {
total.Attempted += v.Attempted
total.Failed += v.Failed
}
return total, nil
}
type EventEmissionSnapshot struct {
Available bool
Status string
Generation uint64
Attempted uint64
Failed uint64
}
type EventDiagnostics struct {
Userspace EventQueueSnapshot
Kernel EventEmissionSnapshot
}
type EventDiagnosticsSnapshot struct{ Process, DNS EventDiagnostics }
var emissionRegistry struct {
sync.RWMutex
bindings [eventModuleCount]*EventEmissionBinding
generations [eventModuleCount]uint64
}
// EventEmissionBinding borrows a collection-owned map: Close MUST precede the
// collection's Close. It retires the binding and waits for any in-flight snapshot;
// it never closes the map, clones a descriptor, or changes detection behavior.
type EventEmissionBinding struct {
module EventModule
source eventEmissionSource
status string
generation uint64
}
func (b *EventEmissionBinding) Close() {
if b == nil {
return
}
emissionRegistry.Lock()
defer emissionRegistry.Unlock()
if emissionRegistry.bindings[b.module] == b {
emissionRegistry.bindings[b.module] = nil
}
}
func registerEventEmissionSource(module EventModule, source eventEmissionSource, status string) *EventEmissionBinding {
if module >= eventModuleCount {
return nil
}
emissionRegistry.Lock()
defer emissionRegistry.Unlock()
emissionRegistry.generations[module]++
b := &EventEmissionBinding{module, source, status, emissionRegistry.generations[module]}
emissionRegistry.bindings[module] = b
return b
}
func validEventEmissionLayout(kind ebpf.MapType, key, value, entries uint32) bool {
return kind == ebpf.PerCPUArray && key == 4 && value == 16 && entries == 1
}
// BindEventEmissionStats deliberately does not add a required Collection.Assign
// field: older objects still load, with explicitly unavailable diagnostics. Each
// read's explicit value buffer is at most 8192*16 bytes (library overhead is
// additional); unsupported layouts/CPU counts degrade
// diagnostics only. Call after collection initialization, before attachment.
func BindEventEmissionStats(module EventModule, collection *ebpf.Collection) *EventEmissionBinding {
if module >= eventModuleCount {
return nil
}
if collection == nil || collection.Maps["event_stats"] == nil {
return registerEventEmissionSource(module, nil, "missing_map")
}
m := collection.Maps["event_stats"]
if !validEventEmissionLayout(m.Type(), m.KeySize(), m.ValueSize(), m.MaxEntries()) {
return registerEventEmissionSource(module, nil, "unsupported_map")
}
cpus, err := ebpf.PossibleCPU()
if err != nil || cpus < 1 || cpus > 8192 {
return registerEventEmissionSource(module, nil, "unsupported_cpus")
}
return registerEventEmissionSource(module, &kernelEmissionSource{m, cpus}, "ok")
}
func snapshotEventEmission(module EventModule) EventEmissionSnapshot {
emissionRegistry.RLock()
defer emissionRegistry.RUnlock()
b := emissionRegistry.bindings[module]
if b == nil {
return EventEmissionSnapshot{Status: "unbound", Generation: emissionRegistry.generations[module]}
}
s := EventEmissionSnapshot{Status: b.status, Generation: b.generation}
if b.source == nil {
return s
}
totals, err := b.source.read()
if err != nil {
s.Status = "read_error"
return s
}
s.Available = true
s.Attempted = totals.Attempted
s.Failed = totals.Failed
return s
}
// SnapshotEventDiagnostics is an on-demand, read-only in-process API. No new
// endpoint, ticker or collector is installed. Kernel totals describe the current
// module generation, userspace totals the daemon lifetime. Per-CPU reads are not
// cross-counter/CPU atomic; these counters cover known paths, not all event loss.
func SnapshotEventDiagnostics() EventDiagnosticsSnapshot {
return EventDiagnosticsSnapshot{
Process: EventDiagnostics{ProcessEventCounters.Snapshot(), snapshotEventEmission(EventModuleProcess)},
DNS: EventDiagnostics{DNSEventCounters.Snapshot(), snapshotEventEmission(EventModuleDNS)},
}
}