1use std::sync::OnceLock;
7
8use crate::{Bytes, sys::TopologyFacts};
9
10#[derive(Clone, Debug, Default, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct Topology {
22 pub packages: Vec<Package>,
24 pub cores: Vec<Core>,
26}
27
28#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct Package {
32 pub id: u32,
34 pub cores: Vec<u32>,
36}
37
38#[derive(Clone, Debug, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct Core {
42 pub id: u32,
44 pub package: u32,
46 pub logical: Vec<u32>,
48 pub kind: CoreKind,
50 pub capacity: Option<u16>,
52 pub numa_node: Option<u32>,
54}
55
56#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
58#[non_exhaustive]
59pub enum CoreKind {
60 Performance,
62 Efficiency,
64 Uniform,
66 Unknown,
68}
69
70#[derive(Clone, Debug, PartialEq, Eq)]
72#[non_exhaustive]
73pub struct Cache {
74 pub level: u8,
76 pub kind: CacheKind,
78 pub size: Bytes,
80 pub line: u32,
82 pub shared_by: Vec<u32>,
84}
85
86#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
88#[non_exhaustive]
89pub enum CacheKind {
90 Data,
92 Instruction,
94 Unified,
96}
97
98#[derive(Clone, Debug, PartialEq, Eq)]
100#[non_exhaustive]
101pub struct NumaNode {
102 pub id: u32,
104 pub cpus: Vec<u32>,
106 pub memory: Option<Bytes>,
108}
109
110fn facts() -> &'static TopologyFacts {
111 static FACTS: OnceLock<TopologyFacts> = OnceLock::new();
112 FACTS.get_or_init(|| {
113 let mut f = crate::sys::cpu_topology();
114 f.topology.packages.sort_by_key(|p| p.id);
115 f.topology.cores.sort_by_key(|c| (c.package, c.id));
116 f.caches
117 .sort_by_key(|c| (c.level, c.shared_by.first().copied()));
118 f.numa.sort_by_key(|n| n.id);
119 f
120 })
121}
122
123#[must_use]
125pub fn topology() -> &'static Topology {
126 &facts().topology
127}
128
129#[must_use]
131pub fn caches() -> &'static [Cache] {
132 &facts().caches
133}
134
135#[must_use]
142pub fn cache_line() -> usize {
143 let line = caches()
144 .iter()
145 .find(|c| c.level == 1 && c.kind != CacheKind::Instruction)
146 .map(|c| c.line as usize)
147 .filter(|l| l.is_power_of_two() && (16..=1024).contains(l));
148 line.unwrap_or(
149 if cfg!(all(target_vendor = "apple", target_arch = "aarch64")) {
150 128
151 } else {
152 64
153 },
154 )
155}
156
157#[must_use]
159pub fn numa_nodes() -> &'static [NumaNode] {
160 &facts().numa
161}
162
163#[must_use]
165pub fn is_hybrid() -> bool {
166 let cores = &topology().cores;
167 cores.iter().any(|c| c.kind == CoreKind::Performance)
168 && cores.iter().any(|c| c.kind == CoreKind::Efficiency)
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174
175 #[test]
176 fn test_cpu_topology_live_is_consistent() {
177 assert!(cache_line().is_power_of_two());
178 let t = topology();
179 assert!(std::ptr::eq(t, topology()));
180 for c in &t.cores {
181 assert!(!c.logical.is_empty());
182 assert!(t.packages.iter().any(|p| p.id == c.package));
183 }
184 for pair in t.packages.windows(2) {
185 assert!(pair[0].id < pair[1].id);
186 }
187 for pair in numa_nodes().windows(2) {
188 assert!(pair[0].id < pair[1].id);
189 }
190 }
191}