wip: config
This commit is contained in:
13
src/config.rs
Normal file
13
src/config.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use toml::Table;
|
||||
use toml::map::Map;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub cpu: CpuConf,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CpuConf {
|
||||
pub comp: Option<Table>,
|
||||
}
|
||||
36
src/cpu.rs
36
src/cpu.rs
@@ -4,6 +4,7 @@ use std::{
|
||||
string,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use tracing::{error, info};
|
||||
|
||||
use core_affinity::*;
|
||||
use raw_cpuid::{CacheInfo, cpuid};
|
||||
@@ -15,9 +16,9 @@ pub struct Cpu {
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct CpuCore {
|
||||
id: i32,
|
||||
core_type: CoreType,
|
||||
pub struct CpuCore {
|
||||
pub id: i32,
|
||||
pub core_type: CoreType,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@@ -56,24 +57,34 @@ impl CpuCore {
|
||||
|
||||
impl Cpu {
|
||||
pub fn new() -> Cpu {
|
||||
let cpu = Arc::new(Mutex::new(Cpu {
|
||||
cores: HashMap::new(),
|
||||
}));
|
||||
let cpu = Arc::new(Mutex::new(Cpu { cores: Vec::new() }));
|
||||
let core_ids = get_core_ids().unwrap();
|
||||
let handles = core_ids
|
||||
.into_iter()
|
||||
.map(|id| {
|
||||
let cpu = cpu.clone();
|
||||
thread::spawn(move || {
|
||||
info!("Attempting to get core type for cpu {id:?}");
|
||||
// Pin this thread to a single CPU core.
|
||||
let res = core_affinity::set_for_current(id);
|
||||
if !res {
|
||||
return;
|
||||
}
|
||||
println!(
|
||||
"cpu: {:?}, result: {:#?}",
|
||||
id,
|
||||
CpuCore::determine_core_type().unwrap()
|
||||
)
|
||||
let coreType = CpuCore::determine_core_type();
|
||||
let mut cpu_thread = cpu.lock().unwrap();
|
||||
match coreType {
|
||||
Ok(x) => {
|
||||
info!("Got cpu type for {id:?}: {x:?}");
|
||||
cpu_thread.cores.push(CpuCore {
|
||||
id: id.id as i32,
|
||||
core_type: x,
|
||||
});
|
||||
}
|
||||
Err(_) => {
|
||||
error!("Can't get core type for {id:?}")
|
||||
}
|
||||
}
|
||||
return;
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@@ -81,6 +92,7 @@ impl Cpu {
|
||||
for handle in handles.into_iter() {
|
||||
handle.join().unwrap();
|
||||
}
|
||||
panic!();
|
||||
let guard = cpu.lock().unwrap();
|
||||
guard.clone()
|
||||
}
|
||||
}
|
||||
|
||||
57
src/main.rs
57
src/main.rs
@@ -1,7 +1,58 @@
|
||||
use clap::Parser;
|
||||
use raw_cpuid::*;
|
||||
use std::path::PathBuf;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
cpu::{Cpu, CpuCore},
|
||||
};
|
||||
mod config;
|
||||
mod cpu;
|
||||
|
||||
fn main() {
|
||||
cpu::Cpu::new();
|
||||
println!("Hello, world!");
|
||||
/// Simple program to greet a person
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(short, long, value_name = "FILE")]
|
||||
config: Option<PathBuf>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
if let Some(config) = args.config {
|
||||
let config_buf = std::fs::read(config).unwrap();
|
||||
let config_par: Config = toml::from_slice(&config_buf).unwrap();
|
||||
if let Some(comp) = config_par.cpu.comp {
|
||||
warn!("Using custom cpu topo loaded from config!");
|
||||
let custom_topo: Vec<CpuCore> = Vec::new();
|
||||
for x in comp {
|
||||
custom_topo.push(CpuCore {
|
||||
id: x.0.parse().unwrap(),
|
||||
core_type: {
|
||||
match x.1 {
|
||||
toml::Value::String(y) => match y.as_str() {
|
||||
"P" => {}
|
||||
"E" => {}
|
||||
"L" => {}
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
Cpu { cores: vec![] };
|
||||
}
|
||||
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let cpu = cpu::Cpu::new();
|
||||
debug!("{cpu:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user