diff --git a/src/cpu.rs b/src/cpu.rs index f59b207..21bbb56 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -1,30 +1,63 @@ use std::{ collections::HashMap, + error::Error, string, sync::{Arc, Mutex}, }; use core_affinity::*; +use raw_cpuid::{CacheInfo, cpuid}; use std::thread; +#[derive(Clone, Debug)] pub struct Cpu { pub cores: HashMap, } +#[derive(Clone, Copy, Debug)] struct CpuCore {} +#[derive(Debug)] enum CoreType { P, // Performence E, // Efficent L, // Low power } +impl CpuCore { + fn determine_core_type() -> Result { + if !(CpuCore::is_atomic()?) { + return Ok(CoreType::P); + } + if CpuCore::is_atomic()? && CpuCore::is_in_l3()? { + return Ok(CoreType::E); + } + if CpuCore::is_atomic()? && !(CpuCore::is_in_l3()?) { + return Ok(CoreType::L); + } + unreachable!("Couldn't resolve cpu core type") + } + fn is_in_l3() -> Result { + let cpuid = cpuid!(0x04, 3); + Ok(cpuid.eax == 0xfc0fc163) + } + fn is_atomic() -> Result { + let cpuid = cpuid!(0x1a); + if cpuid.all_zero() { + // If its not an intel cpu it can fail me thinks + return Err(()); + } + Ok(cpuid.eax == 536870914) + } +} + impl Cpu { pub fn new() -> Cpu { let cpu = Arc::new(Mutex::new(Cpu { cores: HashMap::new(), })); - let _ = core_ids + let core_ids = get_core_ids().unwrap(); + let handles = core_ids .into_iter() .map(|id| { thread::spawn(move || { @@ -33,6 +66,11 @@ impl Cpu { if !res { return; } + println!( + "cpu: {:?}, result: {:#?}", + id, + CpuCore::determine_core_type().unwrap() + ) }) }) .collect::>(); @@ -40,5 +78,6 @@ impl Cpu { for handle in handles.into_iter() { handle.join().unwrap(); } + panic!(); } } diff --git a/src/main.rs b/src/main.rs index 03e1203..66e9698 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ use raw_cpuid::*; mod cpu; fn main() { - let a = cpuid!(26, 0); - println!("{:?}", a); + cpu::Cpu::new(); println!("Hello, world!"); }