ruvector-nervous-system 2.0.6

Bio-inspired neural system with spiking networks, BTSP learning, and EWC plasticity
Documentation
# EventBus Implementation - DVS Event Streams

## Overview

High-performance event bus implementation for Dynamic Vision Sensor (DVS) event streams with lock-free queues, region-based sharding, and adaptive backpressure management.

## Architecture

### Components

1. **Event Types** (`event.rs`)
   - `Event` trait - Core abstraction for timestamped events
   - `DVSEvent` - DVS sensor event with polarity and confidence
   - `EventSurface` - Sparse 2D event tracking with atomic updates

2. **Lock-Free Queue** (`queue.rs`)
   - `EventRingBuffer<E>` - SPSC ring buffer
   - Power-of-2 capacity for efficient modulo
   - Atomic head/tail pointers
   - Zero-copy event storage

3. **Sharded Bus** (`shard.rs`)
   - `ShardedEventBus<E>` - Parallel event processing
   - Spatial sharding (by source_id)
   - Temporal sharding (by timestamp)
   - Hybrid sharding (spatial + temporal)
   - Custom shard functions

4. **Backpressure Control** (`backpressure.rs`)
   - `BackpressureController` - Adaptive flow control
   - High/low watermark state transitions
   - Three states: Normal, Throttle, Drop
   - <1μs decision time

## Performance Characteristics

### Ring Buffer
- **Push/Pop**: <100ns per operation
- **Throughput**: 10,000+ events/millisecond
- **Capacity**: Power-of-2, typically 256-4096
- **Overhead**: ~8 bytes per slot + event size

### Sharded Bus
- **Distribution**: Balanced across shards (±50% of mean)
- **Scalability**: Linear with number of shards
- **Typical Config**: 4-16 shards × 1024 capacity

### Backpressure
- **Decision**: <1μs
- **Update**: <100ns
- **State Transition**: Atomic, wait-free

## Implementation Details

### Lock-Free Queue Algorithm

```rust
// Push (Producer)
1. Load tail (relaxed)
2. Calculate next_tail
3. Check if full (acquire head)
4. Write event to buffer[tail]
5. Store next_tail (release)

// Pop (Consumer)
1. Load head (relaxed)
2. Check if empty (acquire tail)
3. Read event from buffer[head]
4. Store next_head (release)
```

**Memory Ordering**:
- Producer uses Release on tail
- Consumer uses Acquire on tail
- Ensures event visibility across threads

### Event Surface

Sparse tracking of last event per source:
- Atomic timestamp per pixel/source
- Lock-free concurrent updates
- Query active events by time range

### Sharding Strategies

**Spatial** (by source):
```rust
shard_id = source_id % num_shards
```

**Temporal** (by time window):
```rust
shard_id = (timestamp / window_size) % num_shards
```

**Hybrid** (spatial ⊕ temporal):
```rust
shard_id = (source_id ^ (timestamp / window)) % num_shards
```

### Backpressure States

```
Normal (0-20% full):
  ↓ Accept all events

Throttle (20-80% full):
  ↓ Reduce incoming rate

Drop (80-100% full):
  ↓ Reject new events

  ↑ Return to Normal when < 20%
```

## Usage Examples

### Basic Ring Buffer

```rust
use ruvector_nervous_system::eventbus::{DVSEvent, EventRingBuffer};

// Create buffer (capacity must be power of 2)
let buffer = EventRingBuffer::new(1024);

// Push events
let event = DVSEvent::new(1000, 42, 123, true);
buffer.push(event)?;

// Pop events
while let Some(event) = buffer.pop() {
    println!("Event: {:?}", event);
}
```

### Sharded Bus with Backpressure

```rust
use ruvector_nervous_system::eventbus::{
    DVSEvent, ShardedEventBus, BackpressureController
};

// Create sharded bus (4 shards, spatial partitioning)
let bus = ShardedEventBus::new_spatial(4, 1024);

// Create backpressure controller
let controller = BackpressureController::new(0.8, 0.2);

// Process events with backpressure
for event in events {
    // Update backpressure based on fill ratio
    let fill = bus.avg_fill_ratio();
    controller.update(fill);

    // Check if should accept
    if controller.should_accept() {
        bus.push(event)?;
    } else {
        // Drop or throttle
        println!("Backpressure: {:?}", controller.get_state());
    }
}

// Parallel shard processing
use std::thread;
let mut handles = vec![];

for shard_id in 0..bus.num_shards() {
    handles.push(thread::spawn(move || {
        while let Some(event) = bus.pop_shard(shard_id) {
            // Process event
        }
    }));
}
```

### Event Surface Tracking

```rust
use ruvector_nervous_system::eventbus::{DVSEvent, EventSurface};

// Create surface for 640×480 DVS camera
let surface = EventSurface::new(640, 480);

// Update with events
for event in events {
    surface.update(&event);
}

// Query active events since timestamp
let active = surface.get_active_events(since_timestamp);
for (x, y, timestamp) in active {
    println!("Event at ({}, {}) @ {}", x, y, timestamp);
}
```

## Test Coverage

**38 tests** covering:
- Ring buffer FIFO ordering
- Concurrent SPSC/MPSC access
- Shard distribution balance
- Backpressure state transitions
- Event surface sparse updates
- Performance benchmarks

### Test Results

```
test eventbus::backpressure::tests::test_concurrent_access ... ok
test eventbus::backpressure::tests::test_decision_performance ... ok
test eventbus::queue::tests::test_spsc_threaded ... ok (10,000 events)
test eventbus::queue::tests::test_concurrent_push_pop ... ok (1,000 events)
test eventbus::shard::tests::test_parallel_shard_processing ... ok (1,000 events, 4 shards)
test eventbus::shard::tests::test_shard_distribution ... ok (1,000 events, 8 shards)

test result: ok. 38 passed; 0 failed
```

## Integration with Nervous System

The EventBus integrates with other nervous system components:

1. **Dendritic Processing**: Events trigger synaptic inputs
2. **HDC Encoding**: Events bind to hypervectors
3. **Plasticity**: Event timing drives STDP/e-prop
4. **Routing**: Event streams route through cognitive pathways

## Future Enhancements

### Planned Features
- [ ] MPMC ring buffer variant
- [ ] Event filtering/transformation pipelines
- [ ] Hardware accelerated event encoding
- [ ] Integration with neuromorphic chips (Loihi, TrueNorth)
- [ ] Event replay and simulation tools

### Performance Optimizations
- [ ] SIMD-optimized event processing
- [ ] Cache-line aligned buffer slots
- [ ] Adaptive shard count based on load
- [ ] Predictive backpressure adjustment

## References

1. **DVS Cameras**: Gallego et al., "Event-based Vision: A Survey" (2020)
2. **Lock-Free Queues**: Lamport, "Proving the Correctness of Multiprocess Programs" (1977)
3. **Backpressure**: Little's Law and queueing theory
4. **Neuromorphic**: Davies et al., "Loihi: A Neuromorphic Manycore Processor" (2018)

## License

Part of RuVector Nervous System - See main LICENSE file.