ringkernel-core 0.4.2

Core traits and types for RingKernel GPU-native actor system
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Control block state helpers for GPU-compatible kernel state.
//!
//! This module provides utilities for storing kernel state either:
//! - **Embedded** in ControlBlock's 24-byte `_reserved` field (zero-copy)
//! - **External** in separate GPU memory with a pointer in the descriptor
//!
//! # Example
//!
//! ```ignore
//! use ringkernel_core::prelude::*;
//!
//! #[derive(Default, Clone, Copy)]
//! #[repr(C, align(8))]
//! struct OrderBookState {
//!     best_bid: u64,
//!     best_ask: u64,
//!     order_count: u32,
//!     _pad: u32,
//! }  // 24 bytes - fits in ControlBlock._reserved
//!
//! impl EmbeddedState for OrderBookState {}
//!
//! // Write state to control block
//! let mut block = ControlBlock::new();
//! let state = OrderBookState { best_bid: 100, best_ask: 101, order_count: 42, _pad: 0 };
//! ControlBlockStateHelper::write_embedded(&mut block, &state)?;
//!
//! // Read state from control block
//! let restored: OrderBookState = ControlBlockStateHelper::read_embedded(&block)?;
//! ```

use crate::control::ControlBlock;
use crate::error::{Result, RingKernelError};
use bytemuck::{Pod, Zeroable};

/// Size of the reserved field in ControlBlock available for state storage.
pub const CONTROL_BLOCK_STATE_SIZE: usize = 24;

/// Magic number for state descriptor ("STAT" in little-endian).
pub const STATE_DESCRIPTOR_MAGIC: u32 = 0x54415453; // "STAT"

// ============================================================================
// Embedded State Trait
// ============================================================================

/// Trait for state types that can be embedded directly in ControlBlock.
///
/// Types implementing this trait must:
/// - Be `Pod` + `Zeroable` (plain old data, safe to reinterpret)
/// - Be `Default` for initialization
/// - Be `Copy` for efficient transfer
/// - Fit within 24 bytes (checked at compile time via `EmbeddedStateSize`)
///
/// # Example
///
/// ```ignore
/// #[derive(Default, Clone, Copy, Pod, Zeroable)]
/// #[repr(C, align(8))]
/// struct MyState {
///     value_a: u64,
///     value_b: u64,
///     counter: u32,
///     _pad: u32,
/// }
///
/// impl EmbeddedState for MyState {}
/// ```
pub trait EmbeddedState: Pod + Zeroable + Default + Copy + Send + Sync + 'static {
    /// State version for forward compatibility.
    /// Override to support migrations.
    const VERSION: u32 = 1;

    /// Whether this state is embedded (true) or external (false).
    fn is_embedded() -> bool {
        true
    }
}

/// Marker trait to verify state fits in 24 bytes at compile time.
///
/// Use `assert_embedded_size!()` macro or manual assertion.
pub trait EmbeddedStateSize: EmbeddedState {
    /// Compile-time assertion that size fits.
    const SIZE_CHECK: () = assert!(
        std::mem::size_of::<Self>() <= CONTROL_BLOCK_STATE_SIZE,
        "EmbeddedState must fit in 24 bytes"
    );
}

// Automatically implement EmbeddedStateSize for all EmbeddedState types
impl<T: EmbeddedState> EmbeddedStateSize for T {}

// ============================================================================
// State Descriptor
// ============================================================================

/// Descriptor stored in `_reserved` when using external state.
///
/// When state is too large to embed, this 24-byte descriptor points
/// to the external GPU memory location.
#[derive(Debug, Clone, Copy, Default)]
#[repr(C, align(8))]
pub struct StateDescriptor {
    /// Magic number for validation (STATE_DESCRIPTOR_MAGIC).
    pub magic: u32,
    /// State version number.
    pub version: u32,
    /// Total size of external state in bytes.
    pub total_size: u64,
    /// Pointer to external state buffer (GPU address).
    pub external_ptr: u64,
}

// SAFETY: StateDescriptor is #[repr(C)] with only primitive types
unsafe impl Zeroable for StateDescriptor {}
unsafe impl Pod for StateDescriptor {}

impl EmbeddedState for StateDescriptor {}

const _: () = assert!(std::mem::size_of::<StateDescriptor>() == 24);

impl StateDescriptor {
    /// Create a new state descriptor.
    pub const fn new(version: u32, total_size: u64, external_ptr: u64) -> Self {
        Self {
            magic: STATE_DESCRIPTOR_MAGIC,
            version,
            total_size,
            external_ptr,
        }
    }

    /// Check if this descriptor is valid.
    pub fn is_valid(&self) -> bool {
        self.magic == STATE_DESCRIPTOR_MAGIC
    }

    /// Check if state is external (has external pointer).
    pub fn is_external(&self) -> bool {
        self.is_valid() && self.external_ptr != 0
    }

    /// Check if state is embedded (no external pointer).
    pub fn is_embedded(&self) -> bool {
        !self.is_valid() || self.external_ptr == 0
    }
}

// ============================================================================
// GPU State Trait (for external state)
// ============================================================================

/// Trait for GPU-compatible state types that may be stored externally.
///
/// Unlike `EmbeddedState`, types implementing `GpuState` can be larger
/// than 24 bytes and are stored in separate GPU memory.
pub trait GpuState: Send + Sync + 'static {
    /// Serialize state to bytes for GPU transfer.
    fn to_control_block_bytes(&self) -> Vec<u8>;

    /// Deserialize state from bytes read from GPU.
    fn from_control_block_bytes(bytes: &[u8]) -> Result<Self>
    where
        Self: Sized;

    /// State version for compatibility checking.
    fn state_version() -> u32 {
        1
    }

    /// Whether this state should be embedded (if small enough).
    fn prefer_embedded() -> bool
    where
        Self: Sized,
    {
        std::mem::size_of::<Self>() <= CONTROL_BLOCK_STATE_SIZE
    }
}

// Blanket implementation for EmbeddedState types
impl<T: EmbeddedState> GpuState for T {
    fn to_control_block_bytes(&self) -> Vec<u8> {
        bytemuck::bytes_of(self).to_vec()
    }

    fn from_control_block_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < std::mem::size_of::<Self>() {
            return Err(RingKernelError::InvalidState {
                expected: format!("{} bytes", std::mem::size_of::<Self>()),
                actual: format!("{} bytes", bytes.len()),
            });
        }
        Ok(*bytemuck::from_bytes(&bytes[..std::mem::size_of::<Self>()]))
    }

    fn state_version() -> u32 {
        Self::VERSION
    }

    fn prefer_embedded() -> bool {
        true
    }
}

// ============================================================================
// ControlBlock State Helper
// ============================================================================

/// Helper for reading/writing state to/from ControlBlock.
pub struct ControlBlockStateHelper;

impl ControlBlockStateHelper {
    /// Write embedded state to ControlBlock's reserved field.
    ///
    /// # Errors
    ///
    /// Returns error if state doesn't fit in 24 bytes.
    pub fn write_embedded<S: EmbeddedState>(block: &mut ControlBlock, state: &S) -> Result<()> {
        let bytes = bytemuck::bytes_of(state);
        if bytes.len() > CONTROL_BLOCK_STATE_SIZE {
            return Err(RingKernelError::InvalidState {
                expected: format!("<= {} bytes", CONTROL_BLOCK_STATE_SIZE),
                actual: format!("{} bytes", bytes.len()),
            });
        }

        // Clear reserved field first
        block._reserved = [0u8; 24];

        // Copy state bytes
        block._reserved[..bytes.len()].copy_from_slice(bytes);

        Ok(())
    }

    /// Read embedded state from ControlBlock's reserved field.
    ///
    /// # Errors
    ///
    /// Returns error if state type size exceeds 24 bytes.
    pub fn read_embedded<S: EmbeddedState>(block: &ControlBlock) -> Result<S> {
        let size = std::mem::size_of::<S>();
        if size > CONTROL_BLOCK_STATE_SIZE {
            return Err(RingKernelError::InvalidState {
                expected: format!("<= {} bytes", CONTROL_BLOCK_STATE_SIZE),
                actual: format!("{} bytes", size),
            });
        }

        Ok(*bytemuck::from_bytes(&block._reserved[..size]))
    }

    /// Write state descriptor for external state.
    pub fn write_descriptor(block: &mut ControlBlock, descriptor: &StateDescriptor) -> Result<()> {
        Self::write_embedded(block, descriptor)
    }

    /// Read state descriptor from ControlBlock.
    ///
    /// Returns `None` if no valid descriptor is present.
    pub fn read_descriptor(block: &ControlBlock) -> Option<StateDescriptor> {
        let desc: StateDescriptor =
            *bytemuck::from_bytes::<StateDescriptor>(&block._reserved[..24]);
        if desc.is_valid() {
            Some(desc)
        } else {
            None
        }
    }

    /// Check if ControlBlock has embedded state (no external pointer).
    pub fn has_embedded_state(block: &ControlBlock) -> bool {
        match Self::read_descriptor(block) {
            Some(desc) => desc.is_embedded(),
            None => true, // No descriptor means raw embedded bytes
        }
    }

    /// Check if ControlBlock references external state.
    pub fn has_external_state(block: &ControlBlock) -> bool {
        match Self::read_descriptor(block) {
            Some(desc) => desc.is_external(),
            None => false,
        }
    }

    /// Clear all state from ControlBlock.
    pub fn clear_state(block: &mut ControlBlock) {
        block._reserved = [0u8; 24];
    }

    /// Get raw bytes from reserved field.
    pub fn raw_bytes(block: &ControlBlock) -> &[u8; 24] {
        &block._reserved
    }

    /// Get mutable raw bytes from reserved field.
    pub fn raw_bytes_mut(block: &mut ControlBlock) -> &mut [u8; 24] {
        &mut block._reserved
    }
}

// ============================================================================
// State Snapshot
// ============================================================================

/// Snapshot of kernel state for checkpointing.
#[derive(Debug, Clone)]
pub struct StateSnapshot {
    /// State data bytes.
    pub data: Vec<u8>,
    /// State version.
    pub version: u32,
    /// Whether state was embedded or external.
    pub was_embedded: bool,
    /// Kernel ID this state belongs to.
    pub kernel_id: u64,
    /// Timestamp when snapshot was taken (HLC counter).
    pub timestamp: u64,
}

impl StateSnapshot {
    /// Create a new state snapshot.
    pub fn new(data: Vec<u8>, version: u32, was_embedded: bool, kernel_id: u64) -> Self {
        Self {
            data,
            version,
            was_embedded,
            kernel_id,
            timestamp: 0,
        }
    }

    /// Create snapshot with timestamp.
    pub fn with_timestamp(mut self, timestamp: u64) -> Self {
        self.timestamp = timestamp;
        self
    }

    /// Deserialize state from snapshot.
    pub fn restore<S: GpuState>(&self) -> Result<S> {
        S::from_control_block_bytes(&self.data)
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // Test embedded state type (exactly 24 bytes)
    #[derive(Default, Clone, Copy, Debug, PartialEq)]
    #[repr(C, align(8))]
    struct TestState {
        value_a: u64,
        value_b: u64,
        counter: u32,
        flags: u32,
    }

    // SAFETY: TestState is #[repr(C)] with only primitive types
    unsafe impl Zeroable for TestState {}
    unsafe impl Pod for TestState {}

    impl EmbeddedState for TestState {}

    // Small state (8 bytes)
    #[derive(Default, Clone, Copy, Debug, PartialEq)]
    #[repr(C)]
    struct SmallState {
        value: u64,
    }

    unsafe impl Zeroable for SmallState {}
    unsafe impl Pod for SmallState {}

    impl EmbeddedState for SmallState {}

    #[test]
    fn test_state_size_constant() {
        assert_eq!(CONTROL_BLOCK_STATE_SIZE, 24);
    }

    #[test]
    fn test_state_descriptor_size() {
        assert_eq!(std::mem::size_of::<StateDescriptor>(), 24);
    }

    #[test]
    fn test_state_descriptor_validation() {
        let desc = StateDescriptor::new(1, 256, 0x1000);
        assert!(desc.is_valid());
        assert!(desc.is_external());
        assert!(!desc.is_embedded());

        let embedded_desc = StateDescriptor::new(1, 24, 0);
        assert!(embedded_desc.is_valid());
        assert!(!embedded_desc.is_external());
        assert!(embedded_desc.is_embedded());

        let invalid_desc = StateDescriptor::default();
        assert!(!invalid_desc.is_valid());
    }

    #[test]
    fn test_write_read_embedded_state() {
        let mut block = ControlBlock::new();
        let state = TestState {
            value_a: 0x1234567890ABCDEF,
            value_b: 0xFEDCBA0987654321,
            counter: 42,
            flags: 0xFF,
        };

        ControlBlockStateHelper::write_embedded(&mut block, &state).unwrap();
        let restored: TestState = ControlBlockStateHelper::read_embedded(&block).unwrap();

        assert_eq!(state, restored);
    }

    #[test]
    fn test_write_read_small_state() {
        let mut block = ControlBlock::new();
        let state = SmallState { value: 42 };

        ControlBlockStateHelper::write_embedded(&mut block, &state).unwrap();
        let restored: SmallState = ControlBlockStateHelper::read_embedded(&block).unwrap();

        assert_eq!(state, restored);
    }

    #[test]
    fn test_write_read_descriptor() {
        let mut block = ControlBlock::new();
        let desc = StateDescriptor::new(2, 1024, 0xDEADBEEF);

        ControlBlockStateHelper::write_descriptor(&mut block, &desc).unwrap();

        let restored = ControlBlockStateHelper::read_descriptor(&block).unwrap();
        assert_eq!(restored.magic, STATE_DESCRIPTOR_MAGIC);
        assert_eq!(restored.version, 2);
        assert_eq!(restored.total_size, 1024);
        assert_eq!(restored.external_ptr, 0xDEADBEEF);
    }

    #[test]
    fn test_has_embedded_external_state() {
        let mut block = ControlBlock::new();

        // Fresh block has embedded state (no descriptor)
        assert!(ControlBlockStateHelper::has_embedded_state(&block));
        assert!(!ControlBlockStateHelper::has_external_state(&block));

        // Write external descriptor
        let desc = StateDescriptor::new(1, 256, 0x1000);
        ControlBlockStateHelper::write_descriptor(&mut block, &desc).unwrap();

        assert!(!ControlBlockStateHelper::has_embedded_state(&block));
        assert!(ControlBlockStateHelper::has_external_state(&block));

        // Write embedded descriptor (external_ptr = 0)
        let desc = StateDescriptor::new(1, 24, 0);
        ControlBlockStateHelper::write_descriptor(&mut block, &desc).unwrap();

        assert!(ControlBlockStateHelper::has_embedded_state(&block));
        assert!(!ControlBlockStateHelper::has_external_state(&block));
    }

    #[test]
    fn test_clear_state() {
        let mut block = ControlBlock::new();
        let state = TestState {
            value_a: 123,
            value_b: 456,
            counter: 789,
            flags: 0xABC,
        };

        ControlBlockStateHelper::write_embedded(&mut block, &state).unwrap();
        assert!(block._reserved.iter().any(|&b| b != 0));

        ControlBlockStateHelper::clear_state(&mut block);
        assert!(block._reserved.iter().all(|&b| b == 0));
    }

    #[test]
    fn test_raw_bytes_access() {
        let mut block = ControlBlock::new();
        block._reserved[0] = 0x42;
        block._reserved[23] = 0xFF;

        let bytes = ControlBlockStateHelper::raw_bytes(&block);
        assert_eq!(bytes[0], 0x42);
        assert_eq!(bytes[23], 0xFF);

        let bytes_mut = ControlBlockStateHelper::raw_bytes_mut(&mut block);
        bytes_mut[1] = 0x99;
        assert_eq!(block._reserved[1], 0x99);
    }

    #[test]
    fn test_gpu_state_trait() {
        let state = TestState {
            value_a: 100,
            value_b: 200,
            counter: 300,
            flags: 400,
        };

        let bytes = state.to_control_block_bytes();
        assert_eq!(bytes.len(), 24);

        let restored = TestState::from_control_block_bytes(&bytes).unwrap();
        assert_eq!(state, restored);

        assert!(TestState::prefer_embedded());
        assert_eq!(TestState::state_version(), 1);
    }

    #[test]
    fn test_state_snapshot() {
        let state = TestState {
            value_a: 1,
            value_b: 2,
            counter: 3,
            flags: 4,
        };

        let snapshot =
            StateSnapshot::new(state.to_control_block_bytes(), 1, true, 42).with_timestamp(1000);

        assert_eq!(snapshot.version, 1);
        assert!(snapshot.was_embedded);
        assert_eq!(snapshot.kernel_id, 42);
        assert_eq!(snapshot.timestamp, 1000);

        let restored: TestState = snapshot.restore().unwrap();
        assert_eq!(state, restored);
    }

    #[test]
    fn test_embedded_state_size_check() {
        // This should compile - TestState is exactly 24 bytes
        assert_eq!(std::mem::size_of::<TestState>(), 24);
        // Force compile-time size check evaluation
        assert_eq!(<TestState as EmbeddedStateSize>::SIZE_CHECK, ());

        // SmallState is smaller - also OK
        assert!(std::mem::size_of::<SmallState>() <= CONTROL_BLOCK_STATE_SIZE);
        // Force compile-time size check evaluation
        assert_eq!(<SmallState as EmbeddedStateSize>::SIZE_CHECK, ());
    }
}