it works i think
This commit is contained in:
41
src/cpu.rs
41
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<i32, CpuCore>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct CpuCore {}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum CoreType {
|
||||
P, // Performence
|
||||
E, // Efficent
|
||||
L, // Low power
|
||||
}
|
||||
|
||||
impl CpuCore {
|
||||
fn determine_core_type() -> Result<CoreType, ()> {
|
||||
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<bool, ()> {
|
||||
let cpuid = cpuid!(0x04, 3);
|
||||
Ok(cpuid.eax == 0xfc0fc163)
|
||||
}
|
||||
fn is_atomic() -> Result<bool, ()> {
|
||||
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::<Vec<_>>();
|
||||
@@ -40,5 +78,6 @@ impl Cpu {
|
||||
for handle in handles.into_iter() {
|
||||
handle.join().unwrap();
|
||||
}
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ use raw_cpuid::*;
|
||||
mod cpu;
|
||||
|
||||
fn main() {
|
||||
let a = cpuid!(26, 0);
|
||||
println!("{:?}", a);
|
||||
cpu::Cpu::new();
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user