|
| 1 | +use crate::{ |
| 2 | + AcpiTables, |
| 3 | + Handler, |
| 4 | + sdt::{ |
| 5 | + slit::{DistanceMatrix, Slit}, |
| 6 | + srat::{LocalApicAffinityFlags, MemoryAffinityFlags, Srat, SratEntry}, |
| 7 | + }, |
| 8 | +}; |
| 9 | +use alloc::{alloc::Global, vec::Vec}; |
| 10 | +use core::alloc::Allocator; |
| 11 | + |
| 12 | +/// Information about the setup of NUMA (Non-Uniform Memory Architecture) resources within the |
| 13 | +/// sytem. |
| 14 | +pub struct NumaInfo<A: Allocator = Global> { |
| 15 | + pub processor_affinity: Vec<ProcessorAffinity, A>, |
| 16 | + pub memory_affinity: Vec<MemoryAffinity, A>, |
| 17 | + pub num_proximity_domains: usize, |
| 18 | + pub distance_matrix: Vec<u8, A>, |
| 19 | +} |
| 20 | + |
| 21 | +impl NumaInfo<Global> { |
| 22 | + pub fn new(tables: AcpiTables<impl Handler>) -> NumaInfo<Global> { |
| 23 | + Self::new_in(tables, Global) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<A: Allocator + Clone> NumaInfo<A> { |
| 28 | + pub fn new_in(tables: AcpiTables<impl Handler>, allocator: A) -> NumaInfo<A> { |
| 29 | + let mut processor_affinity = Vec::new_in(allocator.clone()); |
| 30 | + let mut memory_affinity = Vec::new_in(allocator.clone()); |
| 31 | + |
| 32 | + if let Some(srat) = tables.find_table::<Srat>() { |
| 33 | + for entry in srat.get().entries() { |
| 34 | + match entry { |
| 35 | + SratEntry::LocalApicAffinity(entry) => processor_affinity.push(ProcessorAffinity { |
| 36 | + local_apic_id: entry.apic_id as u32, |
| 37 | + proximity_domain: entry.proximity_domain(), |
| 38 | + is_enabled: { entry.flags }.contains(LocalApicAffinityFlags::ENABLED), |
| 39 | + }), |
| 40 | + SratEntry::LocalApicX2Affinity(entry) => processor_affinity.push(ProcessorAffinity { |
| 41 | + local_apic_id: entry.x2apic_id, |
| 42 | + proximity_domain: entry.proximity_domain, |
| 43 | + is_enabled: { entry.flags }.contains(LocalApicAffinityFlags::ENABLED), |
| 44 | + }), |
| 45 | + SratEntry::MemoryAffinity(entry) => memory_affinity.push(MemoryAffinity { |
| 46 | + base_address: entry.base_address(), |
| 47 | + length: entry.length(), |
| 48 | + proximity_domain: entry.proximity_domain, |
| 49 | + is_enabled: { entry.flags }.contains(MemoryAffinityFlags::ENABLED), |
| 50 | + is_hot_pluggable: { entry.flags }.contains(MemoryAffinityFlags::HOT_PLUGGABLE), |
| 51 | + is_non_volatile: { entry.flags }.contains(MemoryAffinityFlags::NON_VOLATILE), |
| 52 | + }), |
| 53 | + _ => (), |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + let (num_proximity_domains, distance_matrix) = if let Some(slit) = tables.find_table::<Slit>() { |
| 59 | + (slit.get().num_proximity_domains as usize, slit.get().matrix_raw().to_vec_in(allocator.clone())) |
| 60 | + } else { |
| 61 | + (0, Vec::new_in(allocator.clone())) |
| 62 | + }; |
| 63 | + |
| 64 | + NumaInfo { processor_affinity, memory_affinity, num_proximity_domains, distance_matrix } |
| 65 | + } |
| 66 | + |
| 67 | + pub fn distance_matrix(&self) -> DistanceMatrix<'_> { |
| 68 | + DistanceMatrix { num_proximity_domains: self.num_proximity_domains as u64, matrix: &self.distance_matrix } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Clone, Debug)] |
| 73 | +pub struct ProcessorAffinity { |
| 74 | + pub local_apic_id: u32, |
| 75 | + pub proximity_domain: u32, |
| 76 | + pub is_enabled: bool, |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(Clone, Debug)] |
| 80 | +pub struct MemoryAffinity { |
| 81 | + pub base_address: u64, |
| 82 | + pub length: u64, |
| 83 | + pub proximity_domain: u32, |
| 84 | + pub is_enabled: bool, |
| 85 | + pub is_hot_pluggable: bool, |
| 86 | + pub is_non_volatile: bool, |
| 87 | +} |
0 commit comments