wip: process handler

This commit is contained in:
2026-03-09 20:57:21 +00:00
parent 570e8a172c
commit 8255974587
8 changed files with 563 additions and 8 deletions

View File

@@ -1,12 +1,35 @@
use std::option;
use serde::{Deserialize, Serialize};
use toml::Table;
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Config {
pub cpu: CpuConf,
pub process_management: ProcessManagementConf,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CpuConf {
pub comp: Option<Table>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ProcessManagementConf {
pub polling_rate: i32,
pub processes: Vec<ProcessConf>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ProcessConf {
pub process_name: String,
pub topo: ProcessTopo,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ProcessTopo {
// How much of what core would you might to dedicate to the process
pub p: Option<i32>,
pub e: Option<i32>,
pub l: Option<i32>,
}

View File

@@ -21,7 +21,7 @@ pub struct CpuCore {
pub core_type: CoreType,
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CoreType {
P, // Performence
E, // Efficent
@@ -108,4 +108,31 @@ impl Cpu {
let guard = cpu.lock().unwrap();
guard.clone()
}
pub fn get_e_cores(&self, amount: i32) -> Vec<CpuCore> {
let e_cores = self.cores.iter().filter(|x| x.core_type == CoreType::E);
let mut limited_e_cores = e_cores.take(amount as usize);
let mut e_cores_limited_final: Vec<CpuCore> = Vec::new();
while let Some(x) = limited_e_cores.next() {
e_cores_limited_final.push(x.clone());
}
e_cores_limited_final
}
pub fn get_p_cores(&self, amount: i32) -> Vec<CpuCore> {
let e_cores = self.cores.iter().filter(|x| x.core_type == CoreType::P);
let mut limited_e_cores = e_cores.take(amount as usize);
let mut e_cores_limited_final: Vec<CpuCore> = Vec::new();
while let Some(x) = limited_e_cores.next() {
e_cores_limited_final.push(x.clone());
}
e_cores_limited_final
}
pub fn get_l_cores(&self, amount: i32) -> Vec<CpuCore> {
let e_cores = self.cores.iter().filter(|x| x.core_type == CoreType::L);
let mut limited_e_cores = e_cores.take(amount as usize);
let mut e_cores_limited_final: Vec<CpuCore> = Vec::new();
while let Some(x) = limited_e_cores.next() {
e_cores_limited_final.push(x.clone());
}
e_cores_limited_final
}
}

View File

@@ -6,9 +6,12 @@ use tracing::{debug, error, info, warn};
use crate::{
config::Config,
cpu::{CoreType, Cpu, CpuCore},
processes::ProcessList,
};
mod config;
mod cpu;
mod process_worker;
mod processes;
/// Simple program to greet a person
#[derive(Parser, Debug)]
@@ -24,11 +27,16 @@ fn main() {
tracing_subscriber::fmt::init();
let args = Args::parse();
let mut custom_cpu: Option<Cpu> = None;
let mut process_list: ProcessList = ProcessList::empty();
let mut config: Option<Config> = None;
if let Some(config) = args.config {
let config_buf = std::fs::read(config).unwrap();
if let Some(config_arg) = args.config {
let config_buf = std::fs::read(config_arg).unwrap();
let config_par: Config = toml::from_slice(&config_buf).unwrap();
config = Some(config_par.clone());
if let Some(comp) = config_par.cpu.comp {
// TODO we should check if the custom cpu topo matches the amount of core actually on
// the system..
warn!("Using custom cpu topo loaded from config!");
let mut custom_topo: Vec<CpuCore> = Vec::new();
for x in comp {
@@ -54,12 +62,17 @@ fn main() {
custom_cpu = Some(Cpu { cores: custom_topo });
}
}
debug!("{config:?}");
let process_list = ProcessList::from_config(&config.unwrap());
debug!("{process_list:?}");
let mut cpu: Cpu;
let cpu: Cpu;
if custom_cpu.is_none() {
cpu = cpu::Cpu::new();
} else {
cpu = custom_cpu.unwrap();
};
debug!("{cpu:?}");
let test_take_core = cpu.get_p_cores(3);
debug!("{test_take_core:?}")
}

40
src/process_worker.rs Normal file
View File

@@ -0,0 +1,40 @@
use std::{
ffi::{OsStr, OsString},
string,
thread::sleep,
time::Duration,
};
use sysinfo::System;
use crate::{cpu::Cpu, processes::ProcessList};
pub struct ProcessAffinityWorker {
system: System,
cpu: Cpu,
process_list: ProcessList,
}
impl ProcessAffinityWorker {
fn new(cpu: Cpu, process_list: ProcessList) -> ProcessAffinityWorker {
let s = System::new();
ProcessAffinityWorker {
system: s,
cpu,
process_list,
}
}
fn run(&mut self) -> Result<(), ()> {
loop {
self.system
.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
for x in &self.process_list.processes {
let os_string = OsString::from(x.name.clone());
let processes = self.system.processes_by_name(&os_string);
}
sleep(Duration::from_secs(5));
}
Ok(())
}
}

43
src/processes.rs Normal file
View File

@@ -0,0 +1,43 @@
use crate::config::Config;
#[derive(Clone, Debug)]
pub struct ProcessList {
pub processes: Vec<Process>,
}
#[derive(Clone, Debug)]
pub struct Process {
pub name: String,
pub cpu_bounds: CpuAffinityList,
}
impl ProcessList {
pub fn empty() -> ProcessList {
ProcessList {
processes: Vec::new(),
}
}
pub fn from_config(config: &Config) -> ProcessList {
let mut process_list = ProcessList::empty();
for x in config.process_management.processes.iter().cloned() {
process_list.processes.push(Process {
name: x.process_name,
cpu_bounds: CpuAffinityList {
p: x.topo.p.unwrap_or(0),
e: x.topo.e.unwrap_or(0),
l: x.topo.l.unwrap_or(0),
},
});
}
process_list
}
}
#[derive(Clone, Debug)]
pub struct CpuAffinityList {
p: i32,
e: i32,
l: i32,
}