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>
194 lines
5.4 KiB
Go
194 lines
5.4 KiB
Go
package core
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cilium/ebpf"
|
|
)
|
|
|
|
func TestEventQueueLossAccounting(t *testing.T) {
|
|
for _, capacity := range []int{0, 1, 64} {
|
|
var c EventCounters
|
|
q := make(chan []byte, capacity)
|
|
for i := 0; i <= capacity; i++ {
|
|
if !c.TryEnqueue(q, []byte{byte(i)}) {
|
|
c.Drain(q)
|
|
}
|
|
}
|
|
s := c.Snapshot()
|
|
if s.Received != uint64(capacity+1) || s.Enqueued != uint64(capacity) || s.DroppedCurrent != 1 || s.Drained != uint64(capacity) || len(q) != 0 {
|
|
t.Fatalf("capacity %d: %+v pending=%d", capacity, s, len(q))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEventQueueFIFOAndNil(t *testing.T) {
|
|
var c EventCounters
|
|
q := make(chan []byte, 2)
|
|
if !c.TryEnqueue(q, []byte("a")) || !c.TryEnqueue(q, []byte("b")) || c.TryEnqueue(q, []byte("c")) {
|
|
t.Fatal("unexpected enqueue outcome")
|
|
}
|
|
// TryEnqueue must not drain before the caller's original debug logging.
|
|
if string(<-q) != "a" || string(<-q) != "b" {
|
|
t.Fatal("FIFO changed")
|
|
}
|
|
if c.TryEnqueue(nil, nil) {
|
|
t.Fatal("nil queue accepted")
|
|
}
|
|
c.Drain(nil)
|
|
close(q)
|
|
c.Drain(q) // closed queues must not cause an unbounded diagnostic drain.
|
|
}
|
|
|
|
func TestEventQueueConcurrentAccounting(t *testing.T) {
|
|
var c EventCounters
|
|
q := make(chan []byte, 64)
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
for range q {
|
|
c.RecordDequeued()
|
|
c.RecordDecoded()
|
|
}
|
|
}()
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 4; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for j := 0; j < 1000; j++ {
|
|
if !c.TryEnqueue(q, nil) {
|
|
c.Drain(q)
|
|
}
|
|
_ = c.Snapshot()
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
close(q)
|
|
select {
|
|
case <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("consumer did not finish")
|
|
}
|
|
s := c.Snapshot()
|
|
if s.Received != 4000 || s.Received != s.Enqueued+s.DroppedCurrent || s.Enqueued != s.Dequeued+s.Drained || s.Dequeued != s.Decoded {
|
|
t.Fatalf("quiescent accounting: %+v", s)
|
|
}
|
|
}
|
|
|
|
func TestEventWorkerCounterMeanings(t *testing.T) {
|
|
var c EventCounters
|
|
for i := 0; i < 3; i++ {
|
|
c.RecordDequeued()
|
|
}
|
|
c.RecordDecoded()
|
|
c.RecordDecodeError()
|
|
c.RecordCanceled()
|
|
c.RecordReaderError()
|
|
s := c.Snapshot()
|
|
if s.Dequeued != 3 || s.Decoded != 1 || s.DecodeErrors != 1 || s.Canceled != 1 || s.ReaderErrors != 1 {
|
|
t.Fatalf("%+v", s)
|
|
}
|
|
if s.Dequeued != s.Decoded+s.DecodeErrors+s.Canceled {
|
|
t.Fatal("worker accounting")
|
|
}
|
|
}
|
|
|
|
type fakeEmissionSource struct {
|
|
totals eventEmissionCounts
|
|
err error
|
|
started chan struct{}
|
|
resume chan struct{}
|
|
}
|
|
|
|
func (s *fakeEmissionSource) read() (eventEmissionCounts, error) {
|
|
if s.started != nil {
|
|
close(s.started)
|
|
<-s.resume
|
|
}
|
|
return s.totals, s.err
|
|
}
|
|
|
|
func TestEventEmissionUnavailableAndLayout(t *testing.T) {
|
|
b := BindEventEmissionStats(EventModuleProcess, &ebpf.Collection{})
|
|
defer b.Close()
|
|
s := SnapshotEventDiagnostics().Process.Kernel
|
|
if s.Available || s.Status != "missing_map" || s.Attempted != 0 || s.Failed != 0 {
|
|
t.Fatalf("old object: %+v", s)
|
|
}
|
|
if validEventEmissionLayout(ebpf.Array, 4, 16, 1) || validEventEmissionLayout(ebpf.PerCPUArray, 8, 16, 1) || validEventEmissionLayout(ebpf.PerCPUArray, 4, 8, 1) || validEventEmissionLayout(ebpf.PerCPUArray, 4, 16, 2) || !validEventEmissionLayout(ebpf.PerCPUArray, 4, 16, 1) {
|
|
t.Fatal("counter ABI validation")
|
|
}
|
|
if b := BindEventEmissionStats(EventModule(255), nil); b != nil {
|
|
t.Fatal("invalid module accepted")
|
|
}
|
|
}
|
|
|
|
func TestEventEmissionReadFailureAndReplacement(t *testing.T) {
|
|
old := registerEventEmissionSource(EventModuleProcess, &fakeEmissionSource{totals: eventEmissionCounts{100, 3}}, "ok")
|
|
defer old.Close()
|
|
s := SnapshotEventDiagnostics().Process.Kernel
|
|
if !s.Available || s.Attempted != 100 || s.Failed != 3 || s.Status != "ok" {
|
|
t.Fatalf("%+v", s)
|
|
}
|
|
current := registerEventEmissionSource(EventModuleProcess, &fakeEmissionSource{err: errors.New("synthetic private detail")}, "ok")
|
|
defer current.Close()
|
|
old.Close() // an earlier load's cleanup must not retire its replacement.
|
|
now := SnapshotEventDiagnostics().Process.Kernel
|
|
if now.Available || now.Status != "read_error" || now.Generation <= s.Generation || now.Attempted != 0 || now.Failed != 0 {
|
|
t.Fatalf("%+v", now)
|
|
}
|
|
current.Close()
|
|
current.Close()
|
|
if SnapshotEventDiagnostics().Process.Kernel.Status != "unbound" {
|
|
t.Fatal("binding retained after close")
|
|
}
|
|
}
|
|
|
|
func TestEventEmissionSnapshotRetirement(t *testing.T) {
|
|
source := &fakeEmissionSource{totals: eventEmissionCounts{4, 1}, started: make(chan struct{}), resume: make(chan struct{})}
|
|
b := registerEventEmissionSource(EventModuleDNS, source, "ok")
|
|
readDone := make(chan struct{})
|
|
go func() { defer close(readDone); _ = SnapshotEventDiagnostics() }()
|
|
select {
|
|
case <-source.started:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("read not started")
|
|
}
|
|
closeDone := make(chan struct{})
|
|
go func() { b.Close(); close(closeDone) }()
|
|
select {
|
|
case <-closeDone:
|
|
t.Fatal("retired a source still being read")
|
|
case <-time.After(10 * time.Millisecond):
|
|
}
|
|
close(source.resume)
|
|
select {
|
|
case <-readDone:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("read did not finish")
|
|
}
|
|
select {
|
|
case <-closeDone:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("retirement did not finish")
|
|
}
|
|
if SnapshotEventDiagnostics().DNS.Kernel.Available {
|
|
t.Fatal("retired source still available")
|
|
}
|
|
}
|
|
|
|
func BenchmarkEventQueueDiagnostics(b *testing.B) {
|
|
var c EventCounters
|
|
q := make(chan []byte, 1)
|
|
for i := 0; i < b.N; i++ {
|
|
c.TryEnqueue(q, nil)
|
|
<-q
|
|
c.RecordDequeued()
|
|
c.RecordDecoded()
|
|
}
|
|
}
|