zernel_ebpf/consumers/
dist_sync.rs

1// Copyright (C) 2026 Dyber, Inc. — GPL-2.0
2
3use serde::Serialize;
4
5/// Distributed training synchronization event.
6/// Layout must match struct zernel_dist_sync_event in common.h.
7#[derive(Debug, Clone, Serialize)]
8#[repr(C)]
9pub struct DistSyncEvent {
10    pub pid: u32,
11    pub tid: u32,
12    pub wait_ns: u64,
13    pub timestamp_ns: u64,
14}
15
16pub struct DistSyncConsumer;
17
18impl DistSyncConsumer {
19    pub fn new() -> Self {
20        Self
21    }
22
23    pub fn process_event(&self, raw: &[u8]) -> Option<DistSyncEvent> {
24        if raw.len() < std::mem::size_of::<DistSyncEvent>() {
25            return None;
26        }
27        let event = unsafe { &*(raw.as_ptr() as *const DistSyncEvent) };
28        Some(event.clone())
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn deserialize_dist_sync_event() {
38        let event = DistSyncEvent {
39            pid: 42,
40            tid: 100,
41            wait_ns: 500_000,
42            timestamp_ns: 1_000_000_000,
43        };
44
45        let raw = unsafe {
46            std::slice::from_raw_parts(
47                &event as *const DistSyncEvent as *const u8,
48                std::mem::size_of::<DistSyncEvent>(),
49            )
50        };
51
52        let consumer = DistSyncConsumer::new();
53        let result = consumer.process_event(raw).unwrap();
54        assert_eq!(result.pid, 42);
55        assert_eq!(result.wait_ns, 500_000);
56    }
57}