sochdb-vector 2.0.4

Streaming elimination vector search engine for SochDB - CPU-first ANN with RDF + BPS
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
// Copyright 2025 SochDB Authors
//
// Licensed under the Apache License, Version 2.0

//! NUMA-Aware Memory Allocation
//!
//! This module provides NUMA-aware memory allocation for multi-socket systems.
//! Proper NUMA placement can provide 2-4× improvement on multi-socket systems.
//!
//! # Problem
//!
//! Default allocators ignore NUMA topology:
//! - Memory may be allocated on remote NUMA node
//! - Cross-socket access: ~100ns latency vs ~70ns local
//! - Bandwidth: ~40GB/s local vs ~20GB/s remote
//!
//! # Solution
//!
//! 1. Detect NUMA topology at startup
//! 2. Allocate memory on specific NUMA nodes
//! 3. Pre-fault pages to materialize allocations
//! 4. Pin threads to match memory placement
//!
//! # Usage
//!
//! ```ignore
//! let alloc = NumaAllocator::new()?;
//! let buffer = alloc.allocate_on_node(1024 * 1024, 0)?; // 1MB on node 0
//! alloc.prefault(&buffer); // Touch all pages
//! ```

use std::alloc::{Layout, alloc, dealloc};
use std::ptr::NonNull;
use std::sync::atomic::{AtomicUsize, Ordering};

/// NUMA node identifier.
pub type NumaNode = u32;

/// Result type for NUMA operations.
pub type NumaResult<T> = Result<T, NumaError>;

/// NUMA allocation errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NumaError {
    /// NUMA not available on this system.
    NotAvailable,
    /// Invalid NUMA node.
    InvalidNode(NumaNode),
    /// Allocation failed.
    AllocationFailed,
    /// Thread pinning failed.
    PinningFailed,
    /// System error.
    SystemError(String),
}

impl std::fmt::Display for NumaError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NumaError::NotAvailable => write!(f, "NUMA not available"),
            NumaError::InvalidNode(n) => write!(f, "invalid NUMA node: {}", n),
            NumaError::AllocationFailed => write!(f, "NUMA allocation failed"),
            NumaError::PinningFailed => write!(f, "thread pinning failed"),
            NumaError::SystemError(e) => write!(f, "system error: {}", e),
        }
    }
}

impl std::error::Error for NumaError {}

/// NUMA topology information.
#[derive(Debug, Clone)]
pub struct NumaTopology {
    /// Number of NUMA nodes.
    pub num_nodes: usize,
    /// CPUs per node.
    pub cpus_per_node: Vec<Vec<usize>>,
    /// Memory per node in bytes.
    pub memory_per_node: Vec<usize>,
    /// Distance matrix (node × node).
    pub distances: Vec<Vec<u32>>,
}

impl NumaTopology {
    /// Detect NUMA topology from the system.
    pub fn detect() -> Self {
        // On most systems, we can read from /sys/devices/system/node/
        // For cross-platform compatibility, we provide a reasonable default

        #[cfg(target_os = "linux")]
        {
            Self::detect_linux().unwrap_or_else(Self::single_node)
        }

        #[cfg(not(target_os = "linux"))]
        {
            Self::single_node()
        }
    }

    /// Create single-node topology (fallback).
    pub fn single_node() -> Self {
        let num_cpus = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);

        Self {
            num_nodes: 1,
            cpus_per_node: vec![(0..num_cpus).collect()],
            memory_per_node: vec![0],  // Unknown
            distances: vec![vec![10]], // Local distance
        }
    }

    #[cfg(target_os = "linux")]
    fn detect_linux() -> Option<Self> {
        use std::fs;

        // Read number of NUMA nodes
        let node_path = "/sys/devices/system/node";
        let entries = fs::read_dir(node_path).ok()?;

        let mut nodes = Vec::new();
        for entry in entries.flatten() {
            let name = entry.file_name();
            let name_str = name.to_string_lossy();
            if name_str.starts_with("node") {
                if let Ok(num) = name_str[4..].parse::<usize>() {
                    nodes.push(num);
                }
            }
        }

        if nodes.is_empty() {
            return None;
        }

        nodes.sort();
        let num_nodes = nodes.len();

        // Get CPUs per node
        let mut cpus_per_node = Vec::new();
        for node in &nodes {
            let cpu_path = format!("{}/node{}/cpulist", node_path, node);
            let cpulist = fs::read_to_string(cpu_path).ok()?;
            let cpus = Self::parse_cpulist(&cpulist);
            cpus_per_node.push(cpus);
        }

        // Get memory per node (approximate from meminfo)
        let mut memory_per_node = Vec::new();
        for node in &nodes {
            let mem_path = format!("{}/node{}/meminfo", node_path, node);
            let meminfo = fs::read_to_string(mem_path).unwrap_or_default();
            let mem = Self::parse_meminfo(&meminfo);
            memory_per_node.push(mem);
        }

        // Simple distance matrix (assume uniform for now)
        let mut distances = vec![vec![20u32; num_nodes]; num_nodes];
        for i in 0..num_nodes {
            distances[i][i] = 10; // Local distance
        }

        Some(Self {
            num_nodes,
            cpus_per_node,
            memory_per_node,
            distances,
        })
    }

    #[cfg(target_os = "linux")]
    fn parse_cpulist(cpulist: &str) -> Vec<usize> {
        let mut cpus = Vec::new();
        for part in cpulist.trim().split(',') {
            if part.contains('-') {
                let range: Vec<&str> = part.split('-').collect();
                if range.len() == 2 {
                    if let (Ok(start), Ok(end)) =
                        (range[0].parse::<usize>(), range[1].parse::<usize>())
                    {
                        cpus.extend(start..=end);
                    }
                }
            } else if let Ok(cpu) = part.parse::<usize>() {
                cpus.push(cpu);
            }
        }
        cpus
    }

    #[cfg(target_os = "linux")]
    fn parse_meminfo(meminfo: &str) -> usize {
        for line in meminfo.lines() {
            if line.starts_with("Node") && line.contains("MemTotal:") {
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.len() >= 4 {
                    if let Ok(kb) = parts[3].parse::<usize>() {
                        return kb * 1024; // Convert to bytes
                    }
                }
            }
        }
        0
    }

    /// Get local CPUs for a NUMA node.
    pub fn local_cpus(&self, node: NumaNode) -> &[usize] {
        self.cpus_per_node
            .get(node as usize)
            .map(|v| v.as_slice())
            .unwrap_or(&[])
    }

    /// Get distance between two nodes.
    pub fn distance(&self, from: NumaNode, to: NumaNode) -> u32 {
        self.distances
            .get(from as usize)
            .and_then(|row| row.get(to as usize))
            .copied()
            .unwrap_or(u32::MAX)
    }

    /// Find nearest nodes to a given node.
    pub fn nearest_nodes(&self, node: NumaNode) -> Vec<NumaNode> {
        let mut nodes: Vec<(NumaNode, u32)> = (0..self.num_nodes as NumaNode)
            .map(|n| (n, self.distance(node, n)))
            .collect();
        nodes.sort_by_key(|&(_, d)| d);
        nodes.into_iter().map(|(n, _)| n).collect()
    }
}

/// NUMA-aware memory buffer.
pub struct NumaBuffer {
    /// Pointer to allocated memory.
    ptr: NonNull<u8>,
    /// Size in bytes.
    size: usize,
    /// Layout used for allocation.
    layout: Layout,
    /// NUMA node (if known).
    node: Option<NumaNode>,
    /// Whether pages are faulted.
    faulted: bool,
}

// SAFETY: NumaBuffer owns its memory and can be sent across threads
unsafe impl Send for NumaBuffer {}
unsafe impl Sync for NumaBuffer {}

impl NumaBuffer {
    /// Get pointer to buffer.
    #[inline]
    pub fn as_ptr(&self) -> *const u8 {
        self.ptr.as_ptr()
    }

    /// Get mutable pointer to buffer.
    #[inline]
    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.ptr.as_ptr()
    }

    /// Get buffer size.
    #[inline]
    pub fn len(&self) -> usize {
        self.size
    }

    /// Check if empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.size == 0
    }

    /// Get NUMA node.
    #[inline]
    pub fn node(&self) -> Option<NumaNode> {
        self.node
    }

    /// Check if pages are faulted.
    #[inline]
    pub fn is_faulted(&self) -> bool {
        self.faulted
    }

    /// Get as slice.
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        unsafe { std::slice::from_raw_parts(self.ptr.as_ptr(), self.size) }
    }

    /// Get as mutable slice.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.size) }
    }
}

impl Drop for NumaBuffer {
    fn drop(&mut self) {
        unsafe {
            dealloc(self.ptr.as_ptr(), self.layout);
        }
    }
}

/// NUMA-aware allocator.
pub struct NumaAllocator {
    /// NUMA topology.
    topology: NumaTopology,
    /// Page size.
    page_size: usize,
    /// Total bytes allocated.
    allocated: AtomicUsize,
    /// Allocation count per node.
    allocations_per_node: Vec<AtomicUsize>,
}

impl NumaAllocator {
    /// Create a new NUMA allocator.
    pub fn new() -> NumaResult<Self> {
        let topology = NumaTopology::detect();
        Self::with_topology(topology)
    }

    /// Create with specific topology.
    pub fn with_topology(topology: NumaTopology) -> NumaResult<Self> {
        let page_size = Self::get_page_size();
        let allocations_per_node = (0..topology.num_nodes)
            .map(|_| AtomicUsize::new(0))
            .collect();

        Ok(Self {
            topology,
            page_size,
            allocated: AtomicUsize::new(0),
            allocations_per_node,
        })
    }

    /// Get system page size.
    fn get_page_size() -> usize {
        #[cfg(unix)]
        {
            // SAFETY: sysconf is safe to call
            let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
            if page_size > 0 {
                return page_size as usize;
            }
        }
        // Fallback for non-Unix or if sysconf fails
        4096
    }

    /// Allocate memory on a specific NUMA node.
    pub fn allocate_on_node(&self, size: usize, node: NumaNode) -> NumaResult<NumaBuffer> {
        if node as usize >= self.topology.num_nodes {
            return Err(NumaError::InvalidNode(node));
        }

        // Round up to page size for NUMA-friendly alignment
        let aligned_size = (size + self.page_size - 1) & !(self.page_size - 1);
        let layout = Layout::from_size_align(aligned_size, self.page_size)
            .map_err(|_| NumaError::AllocationFailed)?;

        // On Linux with libnuma, we would use numa_alloc_onnode
        // For cross-platform, use standard allocation with touch
        let ptr = unsafe { alloc(layout) };
        let ptr = NonNull::new(ptr).ok_or(NumaError::AllocationFailed)?;

        self.allocated.fetch_add(aligned_size, Ordering::Relaxed);
        self.allocations_per_node[node as usize].fetch_add(1, Ordering::Relaxed);

        Ok(NumaBuffer {
            ptr,
            size: aligned_size,
            layout,
            node: Some(node),
            faulted: false,
        })
    }

    /// Allocate without node preference.
    pub fn allocate(&self, size: usize) -> NumaResult<NumaBuffer> {
        let aligned_size = (size + self.page_size - 1) & !(self.page_size - 1);
        let layout = Layout::from_size_align(aligned_size, self.page_size)
            .map_err(|_| NumaError::AllocationFailed)?;

        let ptr = unsafe { alloc(layout) };
        let ptr = NonNull::new(ptr).ok_or(NumaError::AllocationFailed)?;

        self.allocated.fetch_add(aligned_size, Ordering::Relaxed);

        Ok(NumaBuffer {
            ptr,
            size: aligned_size,
            layout,
            node: None,
            faulted: false,
        })
    }

    /// Pre-fault pages to materialize allocation.
    ///
    /// This ensures pages are actually allocated in physical memory
    /// on the intended NUMA node before use.
    pub fn prefault(&self, buffer: &mut NumaBuffer) {
        if buffer.faulted {
            return;
        }

        // Touch every page to trigger page fault
        let page_size = self.page_size;
        let ptr = buffer.as_mut_ptr();
        let size = buffer.len();

        for offset in (0..size).step_by(page_size) {
            unsafe {
                std::ptr::write_volatile(ptr.add(offset), 0);
            }
        }

        // Memory barrier to ensure writes are visible
        std::sync::atomic::fence(Ordering::SeqCst);
        buffer.faulted = true;
    }

    /// Get NUMA topology.
    pub fn topology(&self) -> &NumaTopology {
        &self.topology
    }

    /// Get total bytes allocated.
    pub fn total_allocated(&self) -> usize {
        self.allocated.load(Ordering::Relaxed)
    }

    /// Get page size.
    pub fn page_size(&self) -> usize {
        self.page_size
    }
}

impl Default for NumaAllocator {
    fn default() -> Self {
        Self::new().unwrap_or_else(|_| Self::with_topology(NumaTopology::single_node()).unwrap())
    }
}

/// Thread pinning utilities.
pub struct ThreadPinner {
    topology: NumaTopology,
}

impl ThreadPinner {
    /// Create a new thread pinner.
    pub fn new(topology: NumaTopology) -> Self {
        Self { topology }
    }

    /// Pin current thread to a specific CPU.
    #[cfg(target_os = "linux")]
    pub fn pin_to_cpu(&self, cpu: usize) -> NumaResult<()> {
        use std::mem::size_of;

        unsafe {
            let mut cpuset: libc::cpu_set_t = std::mem::zeroed();
            libc::CPU_ZERO(&mut cpuset);
            libc::CPU_SET(cpu, &mut cpuset);

            let result = libc::sched_setaffinity(
                0, // Current thread
                size_of::<libc::cpu_set_t>(),
                &cpuset,
            );

            if result == 0 {
                Ok(())
            } else {
                Err(NumaError::PinningFailed)
            }
        }
    }

    /// Pin current thread to a specific CPU (non-Linux fallback).
    #[cfg(not(target_os = "linux"))]
    pub fn pin_to_cpu(&self, _cpu: usize) -> NumaResult<()> {
        // Thread pinning not available on non-Linux
        Err(NumaError::NotAvailable)
    }

    /// Pin current thread to a NUMA node (any CPU on that node).
    pub fn pin_to_node(&self, node: NumaNode) -> NumaResult<()> {
        let cpus = self.topology.local_cpus(node);
        if cpus.is_empty() {
            return Err(NumaError::InvalidNode(node));
        }

        // Pin to first CPU on node
        self.pin_to_cpu(cpus[0])
    }

    /// Get current CPU.
    #[cfg(target_os = "linux")]
    pub fn current_cpu(&self) -> Option<usize> {
        unsafe {
            let cpu = libc::sched_getcpu();
            if cpu >= 0 { Some(cpu as usize) } else { None }
        }
    }

    /// Get current CPU (non-Linux fallback).
    #[cfg(not(target_os = "linux"))]
    pub fn current_cpu(&self) -> Option<usize> {
        None
    }

    /// Find which NUMA node the current thread is on.
    pub fn current_node(&self) -> Option<NumaNode> {
        let cpu = self.current_cpu()?;

        for (node, cpus) in self.topology.cpus_per_node.iter().enumerate() {
            if cpus.contains(&cpu) {
                return Some(node as NumaNode);
            }
        }

        None
    }
}

/// NUMA-local allocation strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllocationStrategy {
    /// Allocate on specific node.
    Fixed(NumaNode),
    /// Allocate on current node.
    Local,
    /// Round-robin across nodes.
    RoundRobin,
    /// Interleave pages across nodes.
    Interleave,
}

/// NUMA-aware vector storage.
pub struct NumaVectorStorage<T> {
    buffers: Vec<NumaBuffer>,
    len: usize,
    capacity: usize,
    element_size: usize,
    #[allow(dead_code)]
    allocator: NumaAllocator,
    _phantom: std::marker::PhantomData<T>,
}

impl<T: Copy> NumaVectorStorage<T> {
    /// Create new storage with capacity on specific node.
    pub fn with_capacity_on_node(capacity: usize, node: NumaNode) -> NumaResult<Self> {
        let allocator = NumaAllocator::new()?;
        let element_size = std::mem::size_of::<T>();
        let byte_size = capacity * element_size;

        let mut buffer = allocator.allocate_on_node(byte_size, node)?;
        allocator.prefault(&mut buffer);

        Ok(Self {
            buffers: vec![buffer],
            len: 0,
            capacity,
            element_size,
            allocator,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Get element at index.
    pub fn get(&self, index: usize) -> Option<&T> {
        if index >= self.len {
            return None;
        }

        // Simple: assume single buffer for now
        let buffer = &self.buffers[0];
        let offset = index * self.element_size;

        if offset + self.element_size > buffer.len() {
            return None;
        }

        unsafe { Some(&*(buffer.as_ptr().add(offset) as *const T)) }
    }

    /// Push element.
    pub fn push(&mut self, value: T) -> NumaResult<()> {
        if self.len >= self.capacity {
            return Err(NumaError::AllocationFailed);
        }

        let buffer = &mut self.buffers[0];
        let offset = self.len * self.element_size;

        unsafe {
            std::ptr::write(buffer.as_mut_ptr().add(offset) as *mut T, value);
        }

        self.len += 1;
        Ok(())
    }

    /// Get length.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get capacity.
    pub fn capacity(&self) -> usize {
        self.capacity
    }
}

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

    #[test]
    fn test_topology_single_node() {
        let topo = NumaTopology::single_node();
        assert_eq!(topo.num_nodes, 1);
        assert!(!topo.cpus_per_node[0].is_empty());
        assert_eq!(topo.distance(0, 0), 10);
    }

    #[test]
    fn test_topology_detect() {
        let topo = NumaTopology::detect();
        assert!(topo.num_nodes >= 1);
        assert!(!topo.cpus_per_node.is_empty());
    }

    #[test]
    fn test_allocator_basic() {
        let allocator = NumaAllocator::default();

        let buffer = allocator.allocate(4096).unwrap();
        assert!(buffer.len() >= 4096);
        assert!(!buffer.is_faulted());
    }

    #[test]
    fn test_allocator_on_node() {
        let allocator = NumaAllocator::default();

        // Allocate on node 0 (always exists)
        let buffer = allocator.allocate_on_node(8192, 0).unwrap();
        assert!(buffer.len() >= 8192);
        assert_eq!(buffer.node(), Some(0));
    }

    #[test]
    fn test_prefault() {
        let allocator = NumaAllocator::default();
        let mut buffer = allocator.allocate(65536).unwrap();

        assert!(!buffer.is_faulted());
        allocator.prefault(&mut buffer);
        assert!(buffer.is_faulted());
    }

    #[test]
    fn test_buffer_read_write() {
        let allocator = NumaAllocator::default();
        let mut buffer = allocator.allocate(4096).unwrap();
        allocator.prefault(&mut buffer);

        // Write data
        let slice = buffer.as_mut_slice();
        for (i, byte) in slice.iter_mut().enumerate() {
            *byte = (i % 256) as u8;
        }

        // Read back
        let slice = buffer.as_slice();
        for (i, &byte) in slice.iter().enumerate() {
            assert_eq!(byte, (i % 256) as u8);
        }
    }

    #[test]
    fn test_invalid_node() {
        let allocator = NumaAllocator::default();
        let result = allocator.allocate_on_node(4096, 999);
        assert!(matches!(result, Err(NumaError::InvalidNode(999))));
    }

    #[test]
    fn test_total_allocated() {
        let allocator = NumaAllocator::default();

        let initial = allocator.total_allocated();
        let _b1 = allocator.allocate(4096).unwrap();
        let _b2 = allocator.allocate(8192).unwrap();

        let total = allocator.total_allocated();
        // Total should be at least the sum (may be more due to page alignment)
        assert!(total >= initial + 4096 + 8192);
    }

    #[test]
    fn test_nearest_nodes() {
        let topo = NumaTopology {
            num_nodes: 3,
            cpus_per_node: vec![vec![0, 1], vec![2, 3], vec![4, 5]],
            memory_per_node: vec![0, 0, 0],
            distances: vec![vec![10, 20, 30], vec![20, 10, 20], vec![30, 20, 10]],
        };

        let nearest = topo.nearest_nodes(0);
        assert_eq!(nearest[0], 0); // Self is nearest
        assert_eq!(nearest[1], 1); // Distance 20
        assert_eq!(nearest[2], 2); // Distance 30
    }

    #[test]
    fn test_vector_storage() {
        let storage: NumaVectorStorage<f32> =
            NumaVectorStorage::with_capacity_on_node(100, 0).unwrap();

        assert_eq!(storage.len(), 0);
        assert_eq!(storage.capacity(), 100);
    }

    #[test]
    fn test_vector_storage_push_get() {
        let mut storage: NumaVectorStorage<u64> =
            NumaVectorStorage::with_capacity_on_node(10, 0).unwrap();

        storage.push(42).unwrap();
        storage.push(123).unwrap();

        assert_eq!(storage.len(), 2);
        assert_eq!(storage.get(0), Some(&42));
        assert_eq!(storage.get(1), Some(&123));
        assert_eq!(storage.get(2), None);
    }

    #[test]
    fn test_thread_pinner() {
        let topo = NumaTopology::detect();
        let pinner = ThreadPinner::new(topo);

        // current_cpu may or may not work depending on platform
        let _ = pinner.current_cpu();
        let _ = pinner.current_node();
    }

    #[test]
    fn test_thread_pinner_pin_to_cpu() {
        let topo = NumaTopology::detect();
        let pinner = ThreadPinner::new(topo.clone());

        // Get first available CPU
        if let Some(cpus) = topo.cpus_per_node.first() {
            if let Some(&cpu) = cpus.first() {
                // pin_to_cpu uses libc::sched_setaffinity on Linux
                let result = pinner.pin_to_cpu(cpu);
                // May succeed on Linux, will return NotAvailable on other platforms
                #[cfg(target_os = "linux")]
                assert!(result.is_ok(), "Pin should succeed on Linux");
                #[cfg(not(target_os = "linux"))]
                assert!(matches!(result, Err(NumaError::NotAvailable)));
            }
        }
    }

    #[test]
    fn test_thread_pinner_pin_to_node() {
        let topo = NumaTopology::detect();
        let pinner = ThreadPinner::new(topo.clone());

        // Attempt to pin to node 0
        let result = pinner.pin_to_node(0);
        #[cfg(target_os = "linux")]
        {
            // On Linux, should succeed if node 0 has CPUs
            if !topo.cpus_per_node.is_empty() && !topo.cpus_per_node[0].is_empty() {
                assert!(result.is_ok());
            }
        }
        #[cfg(not(target_os = "linux"))]
        assert!(matches!(result, Err(NumaError::NotAvailable)));
    }

    #[test]
    fn test_thread_pinner_current_cpu_libc() {
        let topo = NumaTopology::detect();
        let pinner = ThreadPinner::new(topo);

        // On Linux, sched_getcpu should return a valid CPU
        #[cfg(target_os = "linux")]
        {
            let cpu = pinner.current_cpu();
            assert!(cpu.is_some(), "sched_getcpu should work on Linux");
        }

        // On non-Linux, returns None
        #[cfg(not(target_os = "linux"))]
        {
            let cpu = pinner.current_cpu();
            assert!(cpu.is_none());
        }
    }

    #[test]
    fn test_page_size_libc() {
        // Use NumaAllocator's page_size which internally calls get_page_size() with libc
        let allocator = NumaAllocator::default();
        let page_size = allocator.page_size;

        // Should be a power of 2 and at least 4KB
        assert!(page_size >= 4096);
        assert!(page_size.is_power_of_two());

        // On Unix, verify it matches libc::sysconf directly
        #[cfg(unix)]
        {
            let libc_page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
            if libc_page_size > 0 {
                assert_eq!(page_size, libc_page_size as usize);
            }
        }
    }
}