zernel_ebpf/consumers/
dataload.rs

1// Copyright (C) 2026 Dyber, Inc. — GPL-2.0
2
3use serde::Serialize;
4
5/// Dataset I/O event from DataLoader worker threads.
6/// Layout must match struct zernel_dataload_event in common.h.
7#[derive(Debug, Clone, Serialize)]
8#[repr(C)]
9pub struct DataLoadEvent {
10    pub pid: u32,
11    pub tid: u32,
12    pub read_bytes: u64,
13    pub latency_ns: u64,
14    pub io_type: u8,
15    pub _pad: [u8; 7],
16}
17
18impl DataLoadEvent {
19    pub fn io_type_name(&self) -> &'static str {
20        match self.io_type {
21            0 => "read",
22            1 => "io_uring",
23            _ => "unknown",
24        }
25    }
26}
27
28pub struct DataLoadConsumer;
29
30impl DataLoadConsumer {
31    pub fn new() -> Self {
32        Self
33    }
34
35    pub fn process_event(&self, raw: &[u8]) -> Option<DataLoadEvent> {
36        if raw.len() < std::mem::size_of::<DataLoadEvent>() {
37            return None;
38        }
39        let event = unsafe { &*(raw.as_ptr() as *const DataLoadEvent) };
40        Some(event.clone())
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn io_type_names() {
50        let e = DataLoadEvent {
51            pid: 1,
52            tid: 100,
53            read_bytes: 4096,
54            latency_ns: 8_000_000,
55            io_type: 0,
56            _pad: [0; 7],
57        };
58        assert_eq!(e.io_type_name(), "read");
59    }
60}