ringkernel-cpu 0.4.2

CPU backend for RingKernel - testing and fallback implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! CPU kernel implementation.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use parking_lot::{Mutex, RwLock};
use tokio::sync::{mpsc, Notify};

use ringkernel_core::control::ControlBlock;
use ringkernel_core::error::{Result, RingKernelError};
use ringkernel_core::hlc::HlcClock;
use ringkernel_core::k2k::{DeliveryReceipt, K2KEndpoint, K2KMessage};
use ringkernel_core::message::{CorrelationId, MessageEnvelope};
use ringkernel_core::queue::{BoundedQueue, MessageQueue};
use ringkernel_core::runtime::{
    KernelHandle, KernelHandleInner, KernelId, KernelState, KernelStatus, LaunchOptions,
};
use ringkernel_core::telemetry::{KernelMetrics, TelemetryBuffer};

/// CPU-based kernel implementation.
pub struct CpuKernel {
    /// Kernel identifier.
    id: KernelId,
    /// Numeric ID for message routing.
    id_num: u64,
    /// Current state.
    state: RwLock<KernelState>,
    /// Launch options.
    options: LaunchOptions,
    /// Control block.
    control: RwLock<ControlBlock>,
    /// Telemetry buffer.
    telemetry: RwLock<TelemetryBuffer>,
    /// Input queue (host -> kernel).
    input_queue: Arc<BoundedQueue>,
    /// Output queue (kernel -> host).
    output_queue: Arc<BoundedQueue>,
    /// HLC clock.
    clock: Arc<HlcClock>,
    /// Correlation waiters.
    correlation_waiters: Mutex<std::collections::HashMap<u64, mpsc::Sender<MessageEnvelope>>>,
    /// Termination notifier.
    terminate_notify: Notify,
    /// Launch time.
    launched_at: Instant,
    /// Message counter.
    message_counter: AtomicU64,
    /// K2K endpoint for kernel-to-kernel messaging.
    k2k_endpoint: Mutex<Option<K2KEndpoint>>,
}

impl CpuKernel {
    /// Create a new CPU kernel.
    pub fn new(id: KernelId, options: LaunchOptions, node_id: u64) -> Self {
        Self::new_with_k2k(id, options, node_id, None)
    }

    /// Create a new CPU kernel with optional K2K endpoint.
    pub fn new_with_k2k(
        id: KernelId,
        options: LaunchOptions,
        node_id: u64,
        k2k_endpoint: Option<K2KEndpoint>,
    ) -> Self {
        static KERNEL_COUNTER: AtomicU64 = AtomicU64::new(1);
        let id_num = KERNEL_COUNTER.fetch_add(1, Ordering::Relaxed);

        // Save capacity values before moving options
        let input_capacity = options.input_queue_capacity;
        let output_capacity = options.output_queue_capacity;

        let control = ControlBlock::with_capacities(input_capacity as u32, output_capacity as u32);

        Self {
            id,
            id_num,
            state: RwLock::new(KernelState::Created),
            options,
            control: RwLock::new(control),
            telemetry: RwLock::new(TelemetryBuffer::new()),
            input_queue: Arc::new(BoundedQueue::new(input_capacity)),
            output_queue: Arc::new(BoundedQueue::new(output_capacity)),
            clock: Arc::new(HlcClock::new(node_id)),
            correlation_waiters: Mutex::new(std::collections::HashMap::new()),
            terminate_notify: Notify::new(),
            launched_at: Instant::now(),
            message_counter: AtomicU64::new(0),
            k2k_endpoint: Mutex::new(k2k_endpoint),
        }
    }

    /// Launch the kernel (start processing).
    pub fn launch(&self) {
        let mut state = self.state.write();
        if *state == KernelState::Created {
            *state = KernelState::Launched;
        }
    }

    /// Get kernel ID.
    pub fn id(&self) -> &KernelId {
        &self.id
    }

    /// Get current state.
    pub fn state(&self) -> KernelState {
        *self.state.read()
    }

    /// Process one message (for testing).
    pub fn process_message(&self, envelope: MessageEnvelope) -> Result<()> {
        // Update telemetry
        let mut telemetry = self.telemetry.write();
        telemetry.messages_processed += 1;

        // For CPU backend, we just pass messages through
        // In a real implementation, this would call the registered handler
        self.output_queue.try_enqueue(envelope)?;

        Ok(())
    }

    /// Create a handle to this kernel.
    pub fn handle(self: &Arc<Self>) -> KernelHandle {
        KernelHandle::new(
            self.id.clone(),
            Arc::clone(self) as Arc<dyn KernelHandleInner>,
        )
    }

    /// Check if K2K messaging is enabled for this kernel.
    pub fn is_k2k_enabled(&self) -> bool {
        self.k2k_endpoint.lock().is_some()
    }

    /// Send a K2K message to another kernel.
    pub async fn k2k_send(
        &self,
        destination: KernelId,
        envelope: MessageEnvelope,
    ) -> Result<DeliveryReceipt> {
        let endpoint = {
            let mut endpoint_guard = self.k2k_endpoint.lock();
            endpoint_guard.take().ok_or_else(|| {
                RingKernelError::K2KError("K2K not enabled for this kernel".to_string())
            })?
        };
        let result = endpoint.send(destination, envelope).await;
        // Put it back
        *self.k2k_endpoint.lock() = Some(endpoint);
        result
    }

    /// Try to receive a K2K message (non-blocking).
    pub fn k2k_try_recv(&self) -> Option<K2KMessage> {
        let mut endpoint_guard = self.k2k_endpoint.lock();
        endpoint_guard.as_mut()?.try_receive()
    }

    /// Receive a K2K message (blocking).
    pub async fn k2k_recv(&self) -> Option<K2KMessage> {
        // We need to take the endpoint out temporarily to use the async receiver
        let mut endpoint = {
            let mut endpoint_guard = self.k2k_endpoint.lock();
            endpoint_guard.take()?
        };
        let result = endpoint.receive().await;
        // Put it back
        *self.k2k_endpoint.lock() = Some(endpoint);
        result
    }
}

#[async_trait]
impl KernelHandleInner for CpuKernel {
    fn kernel_id_num(&self) -> u64 {
        self.id_num
    }

    fn current_timestamp(&self) -> ringkernel_core::hlc::HlcTimestamp {
        self.clock.now()
    }

    async fn activate(&self) -> Result<()> {
        let mut state = self.state.write();
        if !state.can_activate() {
            return Err(RingKernelError::InvalidStateTransition {
                from: format!("{:?}", *state),
                to: "Active".to_string(),
            });
        }
        *state = KernelState::Active;

        // Update control block
        let mut control = self.control.write();
        control.is_active = 1;

        Ok(())
    }

    async fn deactivate(&self) -> Result<()> {
        let mut state = self.state.write();
        if !state.can_deactivate() {
            return Err(RingKernelError::InvalidStateTransition {
                from: format!("{:?}", *state),
                to: "Deactivated".to_string(),
            });
        }
        *state = KernelState::Deactivated;

        // Update control block
        let mut control = self.control.write();
        control.is_active = 0;

        Ok(())
    }

    async fn terminate(&self) -> Result<()> {
        let mut state = self.state.write();
        if !state.can_terminate() {
            return Err(RingKernelError::InvalidStateTransition {
                from: format!("{:?}", *state),
                to: "Terminated".to_string(),
            });
        }
        *state = KernelState::Terminating;

        // Update control block
        {
            let mut control = self.control.write();
            control.should_terminate = 1;
            control.is_active = 0;
        }

        // Notify waiting tasks
        self.terminate_notify.notify_waiters();

        // Mark as terminated
        *state = KernelState::Terminated;
        {
            let mut control = self.control.write();
            control.has_terminated = 1;
        }

        Ok(())
    }

    async fn send_envelope(&self, envelope: MessageEnvelope) -> Result<()> {
        let state = self.state();
        if !state.is_running() {
            return Err(RingKernelError::KernelNotActive(self.id.to_string()));
        }

        self.input_queue
            .enqueue_timeout(envelope, Duration::from_secs(5))?;
        self.message_counter.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    async fn receive(&self) -> Result<MessageEnvelope> {
        self.receive_timeout(Duration::from_secs(30)).await
    }

    async fn receive_timeout(&self, timeout: Duration) -> Result<MessageEnvelope> {
        let envelope = self.output_queue.dequeue_timeout(timeout)?;

        // Check if this matches any correlation waiter
        if envelope.header.correlation_id.is_some() {
            let waiters = self.correlation_waiters.lock();
            if let Some(sender) = waiters.get(&envelope.header.correlation_id.0) {
                let _ = sender.try_send(envelope.clone());
            }
        }

        Ok(envelope)
    }

    fn try_receive(&self) -> Result<MessageEnvelope> {
        self.output_queue.try_dequeue()
    }

    async fn receive_correlated(
        &self,
        correlation: CorrelationId,
        timeout: Duration,
    ) -> Result<MessageEnvelope> {
        let (tx, mut rx) = mpsc::channel(1);

        // Register waiter
        {
            let mut waiters = self.correlation_waiters.lock();
            waiters.insert(correlation.0, tx);
        }

        // Wait for response
        let result = tokio::time::timeout(timeout, rx.recv()).await;

        // Cleanup waiter
        {
            let mut waiters = self.correlation_waiters.lock();
            waiters.remove(&correlation.0);
        }

        match result {
            Ok(Some(envelope)) => Ok(envelope),
            Ok(None) => Err(RingKernelError::ChannelClosed),
            Err(_) => Err(RingKernelError::Timeout(timeout)),
        }
    }

    fn status(&self) -> KernelStatus {
        let state = *self.state.read();
        let control = self.control.read();

        KernelStatus {
            id: self.id.clone(),
            state,
            mode: self.options.mode,
            input_queue_depth: self.input_queue.len(),
            output_queue_depth: self.output_queue.len(),
            messages_processed: control.messages_processed,
            uptime: self.launched_at.elapsed(),
        }
    }

    fn metrics(&self) -> KernelMetrics {
        let telemetry = *self.telemetry.read();
        KernelMetrics {
            telemetry,
            kernel_id: self.id.to_string(),
            collected_at: Instant::now(),
            uptime: self.launched_at.elapsed(),
            invocations: 0,
            bytes_to_device: 0,
            bytes_from_device: 0,
            gpu_memory_used: 0,
            host_memory_used: 0,
        }
    }

    async fn wait(&self) -> Result<()> {
        loop {
            if self.state().is_finished() {
                return Ok(());
            }
            self.terminate_notify.notified().await;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ringkernel_core::hlc::HlcTimestamp;
    use ringkernel_core::message::MessageHeader;

    fn make_envelope() -> MessageEnvelope {
        MessageEnvelope {
            header: MessageHeader::new(1, 0, 1, 8, HlcTimestamp::now(1)),
            payload: vec![1, 2, 3, 4, 5, 6, 7, 8],
        }
    }

    #[tokio::test]
    async fn test_kernel_lifecycle() {
        let kernel = Arc::new(CpuKernel::new(
            KernelId::new("test"),
            LaunchOptions::default(),
            1,
        ));

        assert_eq!(kernel.state(), KernelState::Created);

        kernel.launch();
        assert_eq!(kernel.state(), KernelState::Launched);

        kernel.activate().await.unwrap();
        assert_eq!(kernel.state(), KernelState::Active);

        kernel.deactivate().await.unwrap();
        assert_eq!(kernel.state(), KernelState::Deactivated);

        kernel.activate().await.unwrap();
        assert_eq!(kernel.state(), KernelState::Active);

        kernel.terminate().await.unwrap();
        assert_eq!(kernel.state(), KernelState::Terminated);
    }

    #[tokio::test]
    async fn test_send_receive() {
        let kernel = Arc::new(CpuKernel::new(
            KernelId::new("test"),
            LaunchOptions::default(),
            1,
        ));

        kernel.launch();
        kernel.activate().await.unwrap();

        // Send message
        let env = make_envelope();
        kernel.send_envelope(env.clone()).await.unwrap();

        // Process it (simulates kernel processing)
        let recv = kernel.input_queue.try_dequeue().unwrap();
        kernel.output_queue.try_enqueue(recv).unwrap();

        // Receive
        let result = kernel.try_receive().unwrap();
        assert_eq!(result.header.message_type, env.header.message_type);
    }

    #[tokio::test]
    async fn test_status() {
        let kernel = Arc::new(CpuKernel::new(
            KernelId::new("test"),
            LaunchOptions::default(),
            1,
        ));

        kernel.launch();
        kernel.activate().await.unwrap();

        let status = kernel.status();
        assert_eq!(status.id.as_str(), "test");
        assert_eq!(status.state, KernelState::Active);
    }
}