it works i think
This commit is contained in:
41
src/cpu.rs
41
src/cpu.rs
@@ -1,30 +1,63 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
|
error::Error,
|
||||||
string,
|
string,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
use core_affinity::*;
|
use core_affinity::*;
|
||||||
|
use raw_cpuid::{CacheInfo, cpuid};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct Cpu {
|
pub struct Cpu {
|
||||||
pub cores: HashMap<i32, CpuCore>,
|
pub cores: HashMap<i32, CpuCore>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct CpuCore {}
|
struct CpuCore {}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
enum CoreType {
|
enum CoreType {
|
||||||
P, // Performence
|
P, // Performence
|
||||||
E, // Efficent
|
E, // Efficent
|
||||||
L, // Low power
|
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 {
|
impl Cpu {
|
||||||
pub fn new() -> Cpu {
|
pub fn new() -> Cpu {
|
||||||
let cpu = Arc::new(Mutex::new(Cpu {
|
let cpu = Arc::new(Mutex::new(Cpu {
|
||||||
cores: HashMap::new(),
|
cores: HashMap::new(),
|
||||||
}));
|
}));
|
||||||
let _ = core_ids
|
let core_ids = get_core_ids().unwrap();
|
||||||
|
let handles = core_ids
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|id| {
|
.map(|id| {
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
@@ -33,6 +66,11 @@ impl Cpu {
|
|||||||
if !res {
|
if !res {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
println!(
|
||||||
|
"cpu: {:?}, result: {:#?}",
|
||||||
|
id,
|
||||||
|
CpuCore::determine_core_type().unwrap()
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
@@ -40,5 +78,6 @@ impl Cpu {
|
|||||||
for handle in handles.into_iter() {
|
for handle in handles.into_iter() {
|
||||||
handle.join().unwrap();
|
handle.join().unwrap();
|
||||||
}
|
}
|
||||||
|
panic!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ use raw_cpuid::*;
|
|||||||
mod cpu;
|
mod cpu;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = cpuid!(26, 0);
|
cpu::Cpu::new();
|
||||||
println!("{:?}", a);
|
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user