1use std::sync::OnceLock;
15
16use crate::{os::keep, parse::cpu_name};
17
18#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
20#[non_exhaustive]
21pub enum Arch {
22 X86_64,
24 X86,
26 Aarch64,
28 Arm,
30 Riscv64,
32 Powerpc64,
34 S390x,
36 Loongarch64,
38 Wasm32,
40 Other,
42}
43
44impl Arch {
45 #[must_use]
51 pub const fn name(self) -> &'static str {
52 match self {
53 Self::X86_64 => "x86_64",
54 Self::X86 => "x86",
55 Self::Aarch64 => "aarch64",
56 Self::Arm => "arm",
57 Self::Riscv64 => "riscv64",
58 Self::Powerpc64 => "powerpc64",
59 Self::S390x => "s390x",
60 Self::Loongarch64 => "loongarch64",
61 Self::Wasm32 => "wasm32",
62 Self::Other => "other",
63 }
64 }
65
66 #[must_use]
74 pub const fn is_64bit(self) -> bool {
75 matches!(
76 self,
77 Self::X86_64
78 | Self::Aarch64
79 | Self::Riscv64
80 | Self::Powerpc64
81 | Self::S390x
82 | Self::Loongarch64
83 )
84 }
85}
86
87#[must_use]
89pub const fn arch() -> Arch {
90 if cfg!(target_arch = "x86_64") {
91 Arch::X86_64
92 } else if cfg!(target_arch = "x86") {
93 Arch::X86
94 } else if cfg!(target_arch = "aarch64") {
95 Arch::Aarch64
96 } else if cfg!(target_arch = "arm") {
97 Arch::Arm
98 } else if cfg!(target_arch = "riscv64") {
99 Arch::Riscv64
100 } else if cfg!(target_arch = "powerpc64") {
101 Arch::Powerpc64
102 } else if cfg!(target_arch = "s390x") {
103 Arch::S390x
104 } else if cfg!(target_arch = "loongarch64") {
105 Arch::Loongarch64
106 } else if cfg!(target_arch = "wasm32") {
107 Arch::Wasm32
108 } else {
109 Arch::Other
110 }
111}
112
113#[must_use]
121pub fn is_64bit() -> bool {
122 static IS_64: OnceLock<bool> = OnceLock::new();
123 *IS_64.get_or_init(crate::sys::processor_is_64bit)
124}
125
126#[must_use]
128pub fn is_32bit() -> bool {
129 !is_64bit()
130}
131
132#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
134#[non_exhaustive]
135pub enum Vendor {
136 Intel,
138 Amd,
140 Apple,
142 Arm,
144 Qualcomm,
146 Ampere,
148 Nvidia,
150 Samsung,
152 Mediatek,
154 Hygon,
156 Zhaoxin,
158 Broadcom,
160 Marvell,
162 Fujitsu,
164 HiSilicon,
166 Microsoft,
168 #[default]
170 Unknown,
171}
172
173impl Vendor {
174 #[must_use]
176 pub const fn name(self) -> &'static str {
177 match self {
178 Self::Intel => "Intel",
179 Self::Amd => "AMD",
180 Self::Apple => "Apple",
181 Self::Arm => "ARM",
182 Self::Qualcomm => "Qualcomm",
183 Self::Ampere => "Ampere",
184 Self::Nvidia => "NVIDIA",
185 Self::Samsung => "Samsung",
186 Self::Mediatek => "MediaTek",
187 Self::Hygon => "Hygon",
188 Self::Zhaoxin => "Zhaoxin",
189 Self::Broadcom => "Broadcom",
190 Self::Marvell => "Marvell",
191 Self::Fujitsu => "Fujitsu",
192 Self::HiSilicon => "HiSilicon",
193 Self::Microsoft => "Microsoft",
194 Self::Unknown => "unknown",
195 }
196 }
197}
198
199struct Static {
200 logical: usize,
201 physical: usize,
202 name: &'static str,
203 raw_name: Option<&'static str>,
204 vendor: Vendor,
205}
206
207fn facts() -> &'static Static {
208 static FACTS: OnceLock<Static> = OnceLock::new();
209 FACTS.get_or_init(|| build(crate::sys::cpu()))
210}
211
212fn build(f: crate::sys::CpuFacts) -> Static {
213 let logical = f.logical.max(1);
214 let physical = f.physical.unwrap_or(logical).clamp(1, logical);
215 let name = f
216 .name
217 .or_else(|| f.raw_name.as_deref().map(cpu_name::clean))
218 .filter(|n| !n.is_empty())
219 .unwrap_or_else(|| match f.vendor {
220 Vendor::Unknown => "unknown".to_owned(),
221 v => format!("{} {}", v.name(), arch().name()),
222 });
223 Static {
224 logical,
225 physical,
226 name: keep(name),
227 raw_name: f.raw_name.filter(|n| !n.trim().is_empty()).map(keep),
228 vendor: f.vendor,
229 }
230}
231
232#[must_use]
236pub fn logical() -> usize {
237 facts().logical
238}
239
240#[must_use]
243pub fn physical() -> usize {
244 facts().physical
245}
246
247#[must_use]
253pub fn available() -> usize {
254 crate::sys::cpu_available(logical()).clamp(1, logical())
255}
256
257#[must_use]
261pub fn name() -> &'static str {
262 facts().name
263}
264
265#[must_use]
268pub fn raw_name() -> Option<&'static str> {
269 facts().raw_name
270}
271
272#[must_use]
274pub fn vendor() -> Vendor {
275 facts().vendor
276}
277
278#[cfg(test)]
279mod tests {
280 use super::*;
281
282 fn facts_with(
283 logical: usize,
284 physical: Option<usize>,
285 raw: Option<&str>,
286 name: Option<&str>,
287 vendor: Vendor,
288 ) -> Static {
289 build(crate::sys::CpuFacts {
290 logical,
291 physical,
292 raw_name: raw.map(str::to_owned),
293 name: name.map(str::to_owned),
294 vendor,
295 })
296 }
297
298 #[test]
299 fn test_cpu_counts_hold_their_invariants() {
300 assert!(physical() >= 1 && physical() <= logical());
301 let a = available();
302 assert!(a >= 1 && a <= logical());
303 assert_ne!(is_64bit(), is_32bit());
304 if arch().is_64bit() {
305 assert!(is_64bit());
306 }
307 assert!(!name().is_empty());
308 }
309
310 #[test]
311 fn test_cpu_build_cleans_and_falls_back() {
312 let s = facts_with(
313 8,
314 Some(4),
315 Some("Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz"),
316 None,
317 Vendor::Intel,
318 );
319 assert_eq!(
320 (s.logical, s.physical, s.name),
321 (8, 4, "Intel Core i7-8700")
322 );
323 assert_eq!(s.raw_name, Some("Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz"));
324 let s = facts_with(0, Some(99), None, None, Vendor::Arm);
325 assert_eq!((s.logical, s.physical), (1, 1));
326 assert_eq!(s.name, format!("ARM {}", arch().name()));
327 assert_eq!(
328 facts_with(4, None, Some(" "), None, Vendor::Unknown).name,
329 "unknown"
330 );
331 assert_eq!(
332 facts_with(4, None, None, Some("Cortex-A76"), Vendor::Arm).name,
333 "Cortex-A76"
334 );
335 }
336
337 #[test]
338 fn test_cpu_arch_names_and_bitness() {
339 assert_eq!(Arch::X86_64.name(), "x86_64");
340 assert!(Arch::X86_64.is_64bit() && !Arch::X86.is_64bit() && !Arch::Arm.is_64bit());
341 assert_eq!(arch().name(), std::env::consts::ARCH);
342 }
343}