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

406
Cargo.lock generated
View File

@@ -2,6 +2,21 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.21"
@@ -52,18 +67,57 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
[[package]]
name = "bumpalo"
version = "3.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb"
[[package]]
name = "cc"
version = "1.2.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chrono"
version = "0.4.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
dependencies = [
"iana-time-zone",
"num-traits",
"windows-link",
]
[[package]]
name = "clap"
version = "4.5.60"
@@ -110,6 +164,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "core_affinity"
version = "0.8.3"
@@ -121,6 +181,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
dependencies = [
"cfg-if",
]
[[package]]
name = "equivalent"
version = "1.0.2"
@@ -137,6 +206,22 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "flate2"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "hashbrown"
version = "0.16.1"
@@ -155,6 +240,36 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "iana-time-zone"
version = "0.1.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "indexmap"
version = "2.13.0"
@@ -171,6 +286,16 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "js-sys"
version = "0.3.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b49715b7073f385ba4bc528e5747d02e66cb39c6146efb66b781f131f0fb399c"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
@@ -183,6 +308,12 @@ version = "0.2.182"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
[[package]]
name = "log"
version = "0.4.29"
@@ -195,14 +326,41 @@ version = "0.1.0"
dependencies = [
"clap",
"core_affinity",
"procfs",
"raw-cpuid",
"scheduler",
"serde",
"sysinfo",
"toml",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "memchr"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
"simd-adler32",
]
[[package]]
name = "ntapi"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae"
dependencies = [
"winapi",
]
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
@@ -212,6 +370,15 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.17.0"
@@ -222,6 +389,25 @@ dependencies = [
"libc",
]
[[package]]
name = "objc2-core-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags 2.11.0",
]
[[package]]
name = "objc2-io-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]]
name = "once_cell"
version = "1.21.3"
@@ -249,6 +435,30 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "procfs"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25485360a54d6861439d60facef26de713b1e126bf015ec8f98239467a2b82f7"
dependencies = [
"bitflags 2.11.0",
"chrono",
"flate2",
"procfs-core",
"rustix",
]
[[package]]
name = "procfs-core"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6401bf7b6af22f78b563665d15a22e9aef27775b79b149a66ca022468a4e405"
dependencies = [
"bitflags 2.11.0",
"chrono",
"hex",
]
[[package]]
name = "quote"
version = "1.0.45"
@@ -263,9 +473,28 @@ name = "raw-cpuid"
version = "10.6.1"
source = "git+https://github.com/oxidecomputer/rust-cpuid.git#e86d96ba1e63a4c4ecfc521e9fc7a312db0e0c17"
dependencies = [
"bitflags",
"bitflags 1.3.2",
]
[[package]]
name = "rustix"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
dependencies = [
"bitflags 2.11.0",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "rustversion"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "scheduler"
version = "0.1.3"
@@ -324,6 +553,18 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "simd-adler32"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2"
[[package]]
name = "smallvec"
version = "1.15.1"
@@ -347,6 +588,20 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sysinfo"
version = "0.38.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92ab6a2f8bfe508deb3c6406578252e491d299cbbf3bc0529ecc3313aee4a52f"
dependencies = [
"libc",
"memchr",
"ntapi",
"objc2-core-foundation",
"objc2-io-kit",
"windows",
]
[[package]]
name = "thread_local"
version = "1.1.9"
@@ -470,6 +725,51 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]]
name = "wasm-bindgen"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6532f9a5c1ece3798cb1c2cfdba640b9b3ba884f5db45973a6f442510a87d38e"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18a2d50fcf105fb33bb15f00e7a77b772945a2ee45dcf454961fd843e74c18e6"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03ce4caeaac547cdf713d280eda22a730824dd11e6b8c3ca9e42247b25c631e3"
dependencies = [
"bumpalo",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75a326b8c223ee17883a4251907455a2431acc2791c98c26279376490c378c16"
dependencies = [
"unicode-ident",
]
[[package]]
name = "winapi"
version = "0.3.9"
@@ -492,12 +792,107 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
dependencies = [
"windows-collections",
"windows-core",
"windows-future",
"windows-numerics",
]
[[package]]
name = "windows-collections"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610"
dependencies = [
"windows-core",
]
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-future"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
dependencies = [
"windows-core",
"windows-link",
"windows-threading",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-numerics"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
dependencies = [
"windows-core",
"windows-link",
]
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
@@ -507,6 +902,15 @@ dependencies = [
"windows-link",
]
[[package]]
name = "windows-threading"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
dependencies = [
"windows-link",
]
[[package]]
name = "winnow"
version = "0.7.14"

View File

@@ -7,9 +7,11 @@ edition = "2024"
[dependencies]
clap = { version = "4.5.60", features = ["derive"] }
core_affinity = "0.8.3"
procfs = "0.18.0"
raw-cpuid = { git = "https://github.com/oxidecomputer/rust-cpuid.git", version = "10.6.1" }
scheduler = "0.1.3"
serde = { version = "1.0.228", features = ["serde_derive"] }
sysinfo = "0.38.4"
toml = "1.0.4"
tracing = "0.1.44"
tracing-subscriber = "0.3.22"

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,
}

View File

@@ -1,4 +1,7 @@
[cpu]
comp = {
"0" = "E"
"0" = "E", "1" = "P", "2" = "P", "3" = "P", "4" = "P", "5" = "P"
}
[process_management]
polling_rate = 5000
processes = [{process_name = "", topo = {p = 2, l = 2}}]