diff --git a/.github/workflows/build-scheds.yml b/.github/workflows/build-scheds.yml index 235a4f2e5..5dbdc9ea9 100644 --- a/.github/workflows/build-scheds.yml +++ b/.github/workflows/build-scheds.yml @@ -2,6 +2,21 @@ name: build-scheds run-name: ${{ github.actor }} PR run on: [pull_request] jobs: + lint: + runs-on: ubuntu-22.04 + steps: + - name: Install deps + run: | + curl https://sh.rustup.rs -sSf | RUSTUP_INIT_SKIP_PATH_CHECK=yes sh -s -- -y + rustup install nightly-2024-09-10 + export PATH="~/.cargo/bin:$PATH" + + - uses: actions/checkout@v4 + + # Lint code + - run: cargo +nightly-2024-09-10 fmt + - run: git diff --exit-code + build-schedulers: runs-on: ubuntu-22.04 steps: diff --git a/rust/scx_loader/src/main.rs b/rust/scx_loader/src/main.rs index 627177911..837e8338d 100644 --- a/rust/scx_loader/src/main.rs +++ b/rust/scx_loader/src/main.rs @@ -180,8 +180,7 @@ async fn worker_loop(mut receiver: UnboundedReceiver) -> Result<()> // setup channel for scheduler runner let (runner_tx, runner_rx) = tokio::sync::mpsc::channel::(1); - let run_sched_future = - tokio::spawn(async move { handle_child_process(runner_rx).await }); + let run_sched_future = tokio::spawn(async move { handle_child_process(runner_rx).await }); // prepare future for tokio tokio::pin!(run_sched_future); diff --git a/rust/scx_rustland_core/src/alloc.rs b/rust/scx_rustland_core/src/alloc.rs index 24271f964..443b1ff4d 100644 --- a/rust/scx_rustland_core/src/alloc.rs +++ b/rust/scx_rustland_core/src/alloc.rs @@ -26,11 +26,11 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -use std::sync::Mutex; use std::alloc::{GlobalAlloc, Layout}; use std::fs::File; use std::io::{BufRead, BufReader, Write}; use std::num::ParseIntError; +use std::sync::Mutex; /// Buddy allocator /// diff --git a/rust/scx_rustland_core/src/rustland_builder.rs b/rust/scx_rustland_core/src/rustland_builder.rs index 4057bb43e..7d7a489e4 100644 --- a/rust/scx_rustland_core/src/rustland_builder.rs +++ b/rust/scx_rustland_core/src/rustland_builder.rs @@ -31,7 +31,10 @@ impl RustLandBuilder { pub fn build(&mut self) -> Result<()> { // Embed the BPF source files. let intf = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/bpf/intf.h")); - let skel = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/bpf/main.bpf.c")); + let skel = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/assets/bpf/main.bpf.c" + )); let bpf = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/bpf.rs")); // Generate BPF backend code (C). diff --git a/rust/scx_utils/src/bpf_builder.rs b/rust/scx_utils/src/bpf_builder.rs index ee25212dc..6830ce236 100644 --- a/rust/scx_utils/src/bpf_builder.rs +++ b/rust/scx_utils/src/bpf_builder.rs @@ -590,11 +590,9 @@ mod tests { println!("vmlinux.h: ver={:?} sha1={:?}", &ver, &sha1,); - assert!( - regex::Regex::new(r"^([1-9][0-9]*\.[1-9][0-9][a-z0-9-]*)$") - .unwrap() - .is_match(&ver) - ); + assert!(regex::Regex::new(r"^([1-9][0-9]*\.[1-9][0-9][a-z0-9-]*)$") + .unwrap() + .is_match(&ver)); assert!(regex::Regex::new(r"^[0-9a-z]{12}$") .unwrap() .is_match(&sha1)); diff --git a/rust/scx_utils/src/cpumask.rs b/rust/scx_utils/src/cpumask.rs index f24c40258..7d7e65ffe 100644 --- a/rust/scx_utils/src/cpumask.rs +++ b/rust/scx_utils/src/cpumask.rs @@ -95,12 +95,12 @@ impl Cpumask { "none" => { let mask = bitvec![u64, Lsb0; 0; *NR_CPU_IDS]; return Ok(Self { mask }); - }, + } "all" => { let mask = bitvec![u64, Lsb0; 1; *NR_CPU_IDS]; return Ok(Self { mask }); - }, - _ => {}, + } + _ => {} } let hex_str = { let mut tmp_str = cpumask diff --git a/rust/scx_utils/src/infeasible.rs b/rust/scx_utils/src/infeasible.rs index 24b9a4256..2c0e1e22c 100644 --- a/rust/scx_utils/src/infeasible.rs +++ b/rust/scx_utils/src/infeasible.rs @@ -112,7 +112,7 @@ //!```rust //! use scx_utils::LoadAggregator; //! use log::info; -//! +//! //! let mut aggregator = LoadAggregator::new(32, false); //! // Create a LoadAggregator object, specifying the number of CPUs on the //! // system, and whether it should only aggregate duty cycle. diff --git a/rust/scx_utils/src/lib.rs b/rust/scx_utils/src/lib.rs index e7a44ab7d..e5272d3e6 100644 --- a/rust/scx_utils/src/lib.rs +++ b/rust/scx_utils/src/lib.rs @@ -82,4 +82,3 @@ pub use log_recorder::LogRecorderBuilder; mod misc; pub use misc::monitor_stats; pub use misc::set_rlimit_infinity; - diff --git a/rust/scx_utils/src/misc.rs b/rust/scx_utils/src/misc.rs index 21918c01a..316b2512c 100644 --- a/rust/scx_utils/src/misc.rs +++ b/rust/scx_utils/src/misc.rs @@ -1,10 +1,10 @@ use anyhow::Result; +use libc; use log::info; use scx_stats::prelude::*; use serde::Deserialize; use std::thread::sleep; use std::time::Duration; -use libc; pub fn monitor_stats( stats_args: &Vec<(String, String)>, @@ -59,7 +59,7 @@ where Ok(()) } -pub fn set_rlimit_infinity(){ +pub fn set_rlimit_infinity() { unsafe { // Call setrlimit to set the locked-in-memory limit to unlimited. let new_rlimit = libc::rlimit { @@ -71,4 +71,4 @@ pub fn set_rlimit_infinity(){ panic!("setrlimit failed with error code: {}", res); } }; -} \ No newline at end of file +} diff --git a/rust/scx_utils/src/topology.rs b/rust/scx_utils/src/topology.rs index ffd264466..deab5ca56 100644 --- a/rust/scx_utils/src/topology.rs +++ b/rust/scx_utils/src/topology.rs @@ -610,7 +610,9 @@ fn create_gpus() -> BTreeMap> { let Ok(nvidia_gpu) = nvml.device_by_index(i) else { continue; }; - let graphics_boost_clock = nvidia_gpu.max_customer_boost_clock(Clock::Graphics).unwrap_or(0); + let graphics_boost_clock = nvidia_gpu + .max_customer_boost_clock(Clock::Graphics) + .unwrap_or(0); let sm_boost_clock = nvidia_gpu.max_customer_boost_clock(Clock::SM).unwrap_or(0); let Ok(memory_info) = nvidia_gpu.memory_info() else { continue; @@ -629,8 +631,8 @@ fn create_gpus() -> BTreeMap> { let numa_path = format!("/sys/bus/pci/devices/{}/numa_node", fixed_bus_id); let numa_node = read_file_usize(&Path::new(&numa_path)).unwrap_or(0); - let gpu = Gpu{ - index: GpuIndex::Nvidia{nvml_id: index}, + let gpu = Gpu { + index: GpuIndex::Nvidia { nvml_id: index }, node_id: numa_node as usize, max_graphics_clock: graphics_boost_clock as usize, max_sm_clock: sm_boost_clock as usize, @@ -661,7 +663,7 @@ fn create_default_node(online_mask: &Cpumask) -> Result> { node_gpus.insert(gpu.index, gpu.clone()); } } - _ => {}, + _ => {} }; let mut node = Node { @@ -708,7 +710,7 @@ fn create_numa_nodes(online_mask: &Cpumask) -> Result> { node_gpus.insert(gpu.index, gpu.clone()); } } - _ => {}, + _ => {} }; let mut node = Node { diff --git a/scheds/rust/scx_bpfland/src/main.rs b/scheds/rust/scx_bpfland/src/main.rs index da95d2dc0..fcec8119d 100644 --- a/scheds/rust/scx_bpfland/src/main.rs +++ b/scheds/rust/scx_bpfland/src/main.rs @@ -11,8 +11,6 @@ pub mod bpf_intf; pub use bpf_intf::*; mod stats; -use stats::Metrics; - use std::collections::HashMap; use std::ffi::c_int; use std::fs::File; @@ -28,22 +26,19 @@ use anyhow::Context; use anyhow::Result; use clap::Parser; use crossbeam::channel::RecvTimeoutError; -use log::info; - -use log::warn; - use libbpf_rs::skel::OpenSkel; use libbpf_rs::skel::Skel; use libbpf_rs::skel::SkelBuilder; use libbpf_rs::OpenObject; use libbpf_rs::ProgramInput; - +use log::info; +use log::warn; use scx_stats::prelude::*; - use scx_utils::build_id; use scx_utils::scx_ops_attach; use scx_utils::scx_ops_load; use scx_utils::scx_ops_open; +use scx_utils::set_rlimit_infinity; use scx_utils::uei_exited; use scx_utils::uei_report; use scx_utils::CoreType; @@ -51,7 +46,7 @@ use scx_utils::Cpumask; use scx_utils::Topology; use scx_utils::UserExitInfo; use scx_utils::NR_CPU_IDS; -use scx_utils::set_rlimit_infinity; +use stats::Metrics; const SCHEDULER_NAME: &'static str = "scx_bpfland"; @@ -88,7 +83,7 @@ fn get_primary_cpus(mode: Powermode) -> std::io::Result> { // Convert an array of CPUs to the corresponding cpumask of any arbitrary size. fn cpus_to_cpumask(cpus: &Vec) -> String { if cpus.is_empty() { - return String::from("none"); + return String::from("none"); } // Determine the maximum CPU ID to create a sufficiently large byte vector. @@ -293,8 +288,7 @@ impl<'a> Scheduler<'a> { // Initialize the primary scheduling domain and the preferred domain. let energy_profile = Self::read_energy_profile(); - if let Err(err) = Self::init_preferred_domain(&mut skel, &opts.preferred_domain) - { + if let Err(err) = Self::init_preferred_domain(&mut skel, &opts.preferred_domain) { warn!("failed to initialize preferred domain: error {}", err); } if let Err(err) = Self::init_energy_domain(&mut skel, &opts.primary_domain, &energy_profile) @@ -408,17 +402,10 @@ impl<'a> Scheduler<'a> { Cpumask::from_str(&cpus_to_cpumask(&cpus)) } - fn init_preferred_domain( - skel: &mut BpfSkel<'_>, - preferred_domain: &String, - ) -> Result<()> { + fn init_preferred_domain(skel: &mut BpfSkel<'_>, preferred_domain: &String) -> Result<()> { let domain = match preferred_domain.as_str() { - "auto" => { - Self::epp_to_cpumask(Powermode::Turbo)? - } - &_ => { - Cpumask::from_str(&preferred_domain)? - } + "auto" => Self::epp_to_cpumask(Powermode::Turbo)?, + &_ => Cpumask::from_str(&preferred_domain)?, }; info!("preferred CPU domain = 0x{:x}", domain); @@ -430,7 +417,10 @@ impl<'a> Scheduler<'a> { for cpu in 0..*NR_CPU_IDS { if domain.test_cpu(cpu) { if let Err(err) = Self::enable_preferred_cpu(skel, cpu as i32) { - warn!("failed to add CPU {} to preferred domain: error {}", cpu, err); + warn!( + "failed to add CPU {} to preferred domain: error {}", + cpu, err + ); } } } @@ -444,31 +434,19 @@ impl<'a> Scheduler<'a> { energy_profile: &String, ) -> Result<()> { let domain = match primary_domain.as_str() { - "powersave" => { - Self::epp_to_cpumask(Powermode::Powersave)? - } - "performance" => { - Self::epp_to_cpumask(Powermode::Performance)? - } - "auto" => { - match energy_profile.as_str() { - "power" | "balance_power" | "powersave" => { - Self::epp_to_cpumask(Powermode::Powersave)? - } - "balance_performance" | "performance" => { - Self::epp_to_cpumask(Powermode::Performance)? - } - &_ => { - Self::epp_to_cpumask(Powermode::Any)? - } + "powersave" => Self::epp_to_cpumask(Powermode::Powersave)?, + "performance" => Self::epp_to_cpumask(Powermode::Performance)?, + "auto" => match energy_profile.as_str() { + "power" | "balance_power" | "powersave" => { + Self::epp_to_cpumask(Powermode::Powersave)? } - } - "all" => { - Self::epp_to_cpumask(Powermode::Any)? - } - &_ => { - Cpumask::from_str(&primary_domain)? - } + "balance_performance" | "performance" => { + Self::epp_to_cpumask(Powermode::Performance)? + } + &_ => Self::epp_to_cpumask(Powermode::Any)?, + }, + "all" => Self::epp_to_cpumask(Powermode::Any)?, + &_ => Cpumask::from_str(&primary_domain)?, }; info!("primary CPU domain = 0x{:x}", domain); @@ -526,10 +504,9 @@ impl<'a> Scheduler<'a> { self.energy_profile = energy_profile.clone(); if self.opts.primary_domain == "auto" { - if let Err(err) = Self::init_preferred_domain( - &mut self.skel, - &self.opts.preferred_domain, - ) { + if let Err(err) = + Self::init_preferred_domain(&mut self.skel, &self.opts.preferred_domain) + { warn!("failed to refresh preferred domain: error {}", err); } if let Err(err) = Self::init_energy_domain( diff --git a/scheds/rust/scx_bpfland/src/stats.rs b/scheds/rust/scx_bpfland/src/stats.rs index 71c5d8650..d10885151 100644 --- a/scheds/rust/scx_bpfland/src/stats.rs +++ b/scheds/rust/scx_bpfland/src/stats.rs @@ -1,14 +1,15 @@ -use anyhow::Result; -use scx_stats::prelude::*; -use scx_stats_derive::Stats; -use serde::Deserialize; -use serde::Serialize; use std::io::Write; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use std::sync::Arc; use std::time::Duration; +use anyhow::Result; +use scx_stats::prelude::*; +use scx_stats_derive::Stats; +use serde::Deserialize; +use serde::Serialize; + #[derive(Clone, Debug, Default, Serialize, Deserialize, Stats)] #[stat(top)] pub struct Metrics { diff --git a/scheds/rust/scx_lavd/src/main.rs b/scheds/rust/scx_lavd/src/main.rs index d6ec7ac0a..8e17fe06a 100644 --- a/scheds/rust/scx_lavd/src/main.rs +++ b/scheds/rust/scx_lavd/src/main.rs @@ -12,21 +12,14 @@ pub mod bpf_intf; pub use bpf_intf::*; mod stats; -use stats::SysStats; -use stats::SchedSample; -use stats::SchedSamples; -use stats::StatsReq; -use stats::StatsRes; - -use libc::c_char; use std::cell::Cell; use std::cell::RefCell; use std::collections::BTreeMap; -use std::fs::File; -use std::io::Read; use std::ffi::c_int; use std::ffi::CStr; use std::fmt; +use std::fs::File; +use std::io::Read; use std::mem; use std::mem::MaybeUninit; use std::str; @@ -44,27 +37,32 @@ use crossbeam::channel::Receiver; use crossbeam::channel::RecvTimeoutError; use crossbeam::channel::Sender; use crossbeam::channel::TrySendError; +use itertools::iproduct; use libbpf_rs::skel::OpenSkel; use libbpf_rs::skel::Skel; use libbpf_rs::skel::SkelBuilder; use libbpf_rs::OpenObject; use libbpf_rs::ProgramInput; +use libc::c_char; use log::debug; use log::info; use log::warn; +use plain::Plain; use scx_stats::prelude::*; use scx_utils::build_id; use scx_utils::scx_ops_attach; use scx_utils::scx_ops_load; use scx_utils::scx_ops_open; +use scx_utils::set_rlimit_infinity; use scx_utils::uei_exited; use scx_utils::uei_report; use scx_utils::Topology; use scx_utils::UserExitInfo; -use scx_utils::set_rlimit_infinity; - -use itertools::iproduct; -use plain::Plain; +use stats::SchedSample; +use stats::SchedSamples; +use stats::StatsReq; +use stats::StatsRes; +use stats::SysStats; /// scx_lavd: Latency-criticality Aware Virtual Deadline (LAVD) scheduler /// @@ -152,18 +150,18 @@ struct Opts { impl Opts { fn nothing_specified(&self) -> bool { - return self.autopilot == false && - self.autopower == false && - self.performance == false && - self.powersave == false && - self.balanced == false && - self.no_core_compaction == false && - self.prefer_smt_core == false && - self.prefer_little_core == false && - self.no_prefer_turbo_core == false && - self.no_freq_scaling == false && - self.monitor == None && - self.monitor_sched_samples == None; + return self.autopilot == false + && self.autopower == false + && self.performance == false + && self.powersave == false + && self.balanced == false + && self.no_core_compaction == false + && self.prefer_smt_core == false + && self.prefer_little_core == false + && self.no_prefer_turbo_core == false + && self.no_freq_scaling == false + && self.monitor == None + && self.monitor_sched_samples == None; } fn proc(&mut self) -> Option<&mut Self> { @@ -266,8 +264,7 @@ impl FlatTopology { pub fn new() -> Result { let (cpu_fids_performance, avg_freq, nr_cpus_online) = Self::build_cpu_fids(false, false).unwrap(); - let (cpu_fids_powersave, _, _) = - Self::build_cpu_fids(true, true).unwrap(); + let (cpu_fids_powersave, _, _) = Self::build_cpu_fids(true, true).unwrap(); // Note that building compute domain is not dependent to CPU orer // so it is okay to use any cpu_fids_*. @@ -559,7 +556,7 @@ impl<'a> Scheduler<'a> { const LAVD_CPDOM_MAX_NR: u8 = 32; const LAVD_CPDOM_MAX_DIST: usize = 4; if v.neighbor_map.borrow().iter().len() > LAVD_CPDOM_MAX_DIST { - panic!("The processor topology is too complex to handle in BPF."); + panic!("The processor topology is too complex to handle in BPF."); } for (k, (_d, neighbors)) in v.neighbor_map.borrow().iter().enumerate() { @@ -576,7 +573,7 @@ impl<'a> Scheduler<'a> { } fn is_powersave_mode(opts: &Opts) -> bool { - opts.prefer_smt_core && opts.prefer_little_core + opts.prefer_smt_core && opts.prefer_little_core } fn init_globals(skel: &mut OpenBpfSkel, opts: &Opts, nr_cpus_onln: u64) { @@ -587,7 +584,7 @@ impl<'a> Scheduler<'a> { skel.maps.bss_data.is_powersave_mode = Self::is_powersave_mode(&opts); skel.maps.rodata_data.is_smt_active = match FlatTopology::is_smt_active() { Ok(ret) => (ret == 1) as u32, - Err(_) => 0, + Err(_) => 0, }; skel.maps.rodata_data.is_autopilot_on = opts.autopilot; skel.maps.rodata_data.verbose = opts.verbose; @@ -691,9 +688,7 @@ impl<'a> Scheduler<'a> { self.monitor_tid = Some(*tid); StatsRes::Ack } - StatsReq::SysStatsReq { - tid, - } => { + StatsReq::SysStatsReq { tid } => { if Some(*tid) != self.monitor_tid { return Ok(StatsRes::Bye); } @@ -717,9 +712,9 @@ impl<'a> Scheduler<'a> { let pc_pc_on_big = Self::get_pc(st.nr_pc_on_big, nr_big); let pc_lc_on_big = Self::get_pc(st.nr_lc_on_big, nr_big); let power_mode = Self::get_power_mode(bss_data.power_mode); - let total_time = bss_data.performance_mode_ns + - bss_data.balanced_mode_ns + - bss_data.powersave_mode_ns; + let total_time = bss_data.performance_mode_ns + + bss_data.balanced_mode_ns + + bss_data.powersave_mode_ns; let pc_performance = Self::get_pc(bss_data.performance_mode_ns, total_time); let pc_balanced = Self::get_pc(bss_data.balanced_mode_ns, total_time); let pc_powersave = Self::get_pc(bss_data.powersave_mode_ns, total_time); @@ -924,7 +919,9 @@ fn main() -> Result<()> { if let Some(nr_samples) = opts.monitor_sched_samples { let shutdown_copy = shutdown.clone(); - let jh = std::thread::spawn(move || stats::monitor_sched_samples(nr_samples, shutdown_copy).unwrap()); + let jh = std::thread::spawn(move || { + stats::monitor_sched_samples(nr_samples, shutdown_copy).unwrap() + }); let _ = jh.join(); return Ok(()); } diff --git a/scheds/rust/scx_lavd/src/stats.rs b/scheds/rust/scx_lavd/src/stats.rs index de54c18a4..ec7bdf02d 100644 --- a/scheds/rust/scx_lavd/src/stats.rs +++ b/scheds/rust/scx_lavd/src/stats.rs @@ -1,9 +1,3 @@ -use anyhow::bail; -use anyhow::Result; -use scx_stats::prelude::*; -use scx_stats_derive::Stats; -use serde::Deserialize; -use serde::Serialize; use std::collections::BTreeMap; use std::io::Write; use std::sync::atomic::AtomicBool; @@ -11,7 +5,14 @@ use std::sync::atomic::Ordering; use std::sync::Arc; use std::thread::ThreadId; use std::time::Duration; + +use anyhow::bail; +use anyhow::Result; use gpoint::GPoint; +use scx_stats::prelude::*; +use scx_stats_derive::Stats; +use serde::Deserialize; +use serde::Serialize; #[derive(Clone, Debug, Default, Serialize, Deserialize, Stats)] #[stat(top)] @@ -122,7 +123,6 @@ impl SysStats { )?; Ok(()) } - } #[derive(Clone, Debug, Default, Serialize, Deserialize, Stats)] @@ -133,7 +133,9 @@ pub struct SchedSample { pub pid: i32, #[stat(desc = "Task name")] pub comm: String, - #[stat(desc = "LR: 'L'atency-critical or 'R'egular, HI: performance-'H'ungry or performance-'I'nsensitive, BT: 'B'ig or li'T'tle, EG: 'E'ligigle or 'G'reedy, PN: 'P'reempting or 'N'ot")] + #[stat( + desc = "LR: 'L'atency-critical or 'R'egular, HI: performance-'H'ungry or performance-'I'nsensitive, BT: 'B'ig or li'T'tle, EG: 'E'ligigle or 'G'reedy, PN: 'P'reempting or 'N'ot" + )] pub stat: String, #[stat(desc = "CPU id where this task is scheduled on")] pub cpu_id: u32, @@ -272,12 +274,8 @@ pub enum StatsReq { } impl StatsReq { - fn from_args_stats( - tid: ThreadId, - ) -> Result { - Ok(Self::SysStatsReq { - tid, - }) + fn from_args_stats(tid: ThreadId) -> Result { + Ok(Self::SysStatsReq { tid }) } fn from_args_samples( @@ -316,30 +314,31 @@ pub enum StatsRes { pub fn server_data(nr_cpus_onln: u64) -> StatsServerData { let open: Box> = Box::new(move |(req_ch, res_ch)| { - let tid = std::thread::current().id(); - req_ch.send(StatsReq::NewSampler(tid))?; - match res_ch.recv()? { - StatsRes::Ack => {} - res => bail!("invalid response: {:?}", &res), - } + let tid = std::thread::current().id(); + req_ch.send(StatsReq::NewSampler(tid))?; + match res_ch.recv()? { + StatsRes::Ack => {} + res => bail!("invalid response: {:?}", &res), + } - let read: Box> = - Box::new(move |_args, (req_ch, res_ch)| { - let req = StatsReq::from_args_stats(tid)?; - req_ch.send(req)?; + let read: Box> = + Box::new(move |_args, (req_ch, res_ch)| { + let req = StatsReq::from_args_stats(tid)?; + req_ch.send(req)?; - let stats = match res_ch.recv()? { - StatsRes::SysStats(v) => v, - StatsRes::Bye => bail!("preempted by another sampler"), - res => bail!("invalid response: {:?}", &res), - }; + let stats = match res_ch.recv()? { + StatsRes::SysStats(v) => v, + StatsRes::Bye => bail!("preempted by another sampler"), + res => bail!("invalid response: {:?}", &res), + }; - stats.to_json() - }); - Ok(read) - }); + stats.to_json() + }); + Ok(read) + }); - let samples_open: Box> = Box::new(move |(req_ch, res_ch)| { + let samples_open: Box> = + Box::new(move |(req_ch, res_ch)| { let tid = std::thread::current().id(); req_ch.send(StatsReq::NewSampler(tid))?; match res_ch.recv()? { @@ -365,13 +364,7 @@ pub fn server_data(nr_cpus_onln: u64) -> StatsServerData { StatsServerData::new() .add_meta(SysStats::meta()) - .add_ops( - "top", - StatsOps { - open: open, - close: None, - }, - ) + .add_ops("top", StatsOps { open, close: None }) .add_meta(SchedSample::meta()) .add_ops( "sched_samples", diff --git a/scheds/rust/scx_layered/src/main.rs b/scheds/rust/scx_layered/src/main.rs index c48d85a86..0fe447df4 100644 --- a/scheds/rust/scx_layered/src/main.rs +++ b/scheds/rust/scx_layered/src/main.rs @@ -6,10 +6,6 @@ mod bpf_skel; mod stats; pub use bpf_skel::*; pub mod bpf_intf; -use stats::LayerStats; -use stats::StatsReq; -use stats::StatsRes; -use stats::SysStats; use std::collections::BTreeMap; use std::collections::BTreeSet; use std::collections::HashMap; @@ -57,6 +53,10 @@ use scx_utils::Topology; use scx_utils::UserExitInfo; use serde::Deserialize; use serde::Serialize; +use stats::LayerStats; +use stats::StatsReq; +use stats::StatsRes; +use stats::SysStats; const RAVG_FRAC_BITS: u32 = bpf_intf::ravg_consts_RAVG_FRAC_BITS; const MAX_CPUS: usize = bpf_intf::consts_MAX_CPUS as usize; @@ -455,7 +455,9 @@ enum LayerGrowthAlgo { } impl Default for LayerGrowthAlgo { - fn default() -> Self { LayerGrowthAlgo::Sticky } + fn default() -> Self { + LayerGrowthAlgo::Sticky + } } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -1093,8 +1095,8 @@ fn layer_core_order( spec: &LayerSpec, growth_algo: LayerGrowthAlgo, layer_idx: usize, - topo: &Topology - ) -> Vec { + topo: &Topology, +) -> Vec { let mut core_order = vec![]; match growth_algo { LayerGrowthAlgo::Sticky => { @@ -1144,12 +1146,7 @@ struct Layer { } impl Layer { - fn new( - spec: &LayerSpec, - idx: usize, - cpu_pool: &CpuPool, - topo: &Topology, - ) -> Result { + fn new(spec: &LayerSpec, idx: usize, cpu_pool: &CpuPool, topo: &Topology) -> Result { let name = &spec.name; let kind = spec.kind.clone(); let mut cpus = bitvec![0; cpu_pool.nr_cpus]; @@ -1276,8 +1273,7 @@ impl Layer { { trace!( "layer-{} needs more CPUs (util={:.3}) but is over the load fraction", - &self.name, - layer_util + &self.name, layer_util ); return Ok(false); } @@ -1665,12 +1661,7 @@ impl<'a, 'b> Scheduler<'a, 'b> { let mut layers = vec![]; for (idx, spec) in layer_specs.iter().enumerate() { - layers.push(Layer::new( - &spec, - idx, - &cpu_pool, - &topo, - )?); + layers.push(Layer::new(&spec, idx, &cpu_pool, &topo)?); } // Other stuff. diff --git a/scheds/rust/scx_layered/src/stats.rs b/scheds/rust/scx_layered/src/stats.rs index 987fcc80f..5a70fcfbb 100644 --- a/scheds/rust/scx_layered/src/stats.rs +++ b/scheds/rust/scx_layered/src/stats.rs @@ -1,17 +1,3 @@ -use crate::bpf_intf; -use crate::BpfStats; -use crate::Layer; -use crate::LayerKind; -use crate::Stats; -use anyhow::{bail, Result}; -use bitvec::prelude::*; -use chrono::DateTime; -use chrono::Local; -use log::warn; -use scx_stats::prelude::*; -use scx_stats_derive::Stats; -use serde::Deserialize; -use serde::Serialize; use std::collections::BTreeMap; use std::io::Write; use std::sync::atomic::AtomicBool; @@ -23,6 +9,23 @@ use std::time::Duration; use std::time::SystemTime; use std::time::UNIX_EPOCH; +use anyhow::bail; +use anyhow::Result; +use bitvec::prelude::*; +use chrono::DateTime; +use chrono::Local; +use log::warn; +use scx_stats::prelude::*; +use scx_stats_derive::Stats; +use serde::Deserialize; +use serde::Serialize; + +use crate::bpf_intf; +use crate::BpfStats; +use crate::Layer; +use crate::LayerKind; +use crate::Stats; + fn fmt_pct(v: f64) -> String { if v >= 99.995 { format!("{:5.1}", v) @@ -147,11 +150,7 @@ impl LayerStats { } }; let calc_frac = |a, b| { - if b != 0.0 { - a / b * 100.0 - } else { - 0.0 - } + if b != 0.0 { a / b * 100.0 } else { 0.0 } }; let is_excl = match &layer.kind { diff --git a/scheds/rust/scx_rlfifo/src/main.rs b/scheds/rust/scx_rlfifo/src/main.rs index a6c9407a2..db3a505e0 100644 --- a/scheds/rust/scx_rlfifo/src/main.rs +++ b/scheds/rust/scx_rlfifo/src/main.rs @@ -66,17 +66,15 @@ mod bpf_skel; pub use bpf_skel::*; pub mod bpf_intf; +#[rustfmt::skip] mod bpf; -use bpf::*; - -use scx_utils::UserExitInfo; - -use libbpf_rs::OpenObject; - use std::mem::MaybeUninit; use std::time::SystemTime; use anyhow::Result; +use bpf::*; +use libbpf_rs::OpenObject; +use scx_utils::UserExitInfo; // Maximum time slice (in nanoseconds) that a task can use before it is re-enqueued. const SLICE_NS: u64 = 5_000_000; diff --git a/scheds/rust/scx_rustland/src/main.rs b/scheds/rust/scx_rustland/src/main.rs index 86421caac..53abda2cb 100644 --- a/scheds/rust/scx_rustland/src/main.rs +++ b/scheds/rust/scx_rustland/src/main.rs @@ -6,29 +6,28 @@ mod bpf_skel; pub use bpf_skel::*; pub mod bpf_intf; +#[rustfmt::skip] mod bpf; use bpf::*; mod stats; -use scx_stats::prelude::*; -use stats::Metrics; - -use scx_utils::UserExitInfo; - use std::collections::BTreeSet; use std::collections::HashMap; +use std::fs::File; +use std::io::Read; +use std::io::{self}; use std::mem::MaybeUninit; use std::time::Duration; use std::time::SystemTime; -use std::fs::File; -use std::io::{self, Read}; - use anyhow::Result; use clap::Parser; use libbpf_rs::OpenObject; use log::info; use log::warn; +use scx_stats::prelude::*; +use scx_utils::UserExitInfo; +use stats::Metrics; const SCHEDULER_NAME: &'static str = "RustLand"; diff --git a/scheds/rust/scx_rustland/src/stats.rs b/scheds/rust/scx_rustland/src/stats.rs index 4e6b318e4..4a83f8de7 100644 --- a/scheds/rust/scx_rustland/src/stats.rs +++ b/scheds/rust/scx_rustland/src/stats.rs @@ -1,10 +1,11 @@ +use std::io::Write; +use std::time::Duration; + use anyhow::Result; use scx_stats::prelude::*; use scx_stats_derive::Stats; use serde::Deserialize; use serde::Serialize; -use std::io::Write; -use std::time::Duration; #[derive(Clone, Debug, Default, Serialize, Deserialize, Stats)] #[stat(top)] diff --git a/scheds/rust/scx_rusty/src/domain.rs b/scheds/rust/scx_rusty/src/domain.rs index 000173ae7..f5cbe4041 100644 --- a/scheds/rust/scx_rusty/src/domain.rs +++ b/scheds/rust/scx_rusty/src/domain.rs @@ -5,7 +5,6 @@ use std::collections::BTreeMap; use anyhow::Result; - use scx_utils::Cpumask; use scx_utils::Topology; diff --git a/scheds/rust/scx_rusty/src/load_balance.rs b/scheds/rust/scx_rusty/src/load_balance.rs index ac21fd1b7..1fd856c24 100644 --- a/scheds/rust/scx_rusty/src/load_balance.rs +++ b/scheds/rust/scx_rusty/src/load_balance.rs @@ -131,14 +131,9 @@ //! - We're not accounting for cgroups when performing load balancing. use core::cmp::Ordering; - -use crate::bpf_intf; -use crate::bpf_skel::*; -use crate::stats::DomainStats; -use crate::stats::NodeStats; -use crate::DomainGroup; - use std::cell::Cell; +use std::collections::BTreeMap; +use std::collections::VecDeque; use std::fmt; use std::sync::Arc; @@ -153,8 +148,12 @@ use scx_utils::ravg::ravg_read; use scx_utils::LoadAggregator; use scx_utils::LoadLedger; use sorted_vec::SortedVec; -use std::collections::BTreeMap; -use std::collections::VecDeque; + +use crate::bpf_intf; +use crate::bpf_skel::*; +use crate::stats::DomainStats; +use crate::stats::NodeStats; +use crate::DomainGroup; const RAVG_FRAC_BITS: u32 = bpf_intf::ravg_consts_RAVG_FRAC_BITS; diff --git a/scheds/rust/scx_rusty/src/main.rs b/scheds/rust/scx_rusty/src/main.rs index c9a9f986d..d9fd0915f 100644 --- a/scheds/rust/scx_rusty/src/main.rs +++ b/scheds/rust/scx_rusty/src/main.rs @@ -16,9 +16,6 @@ pub mod load_balance; use load_balance::LoadBalancer; mod stats; -use stats::ClusterStats; -use stats::NodeStats; - use std::collections::BTreeMap; use std::mem::MaybeUninit; use std::sync::atomic::AtomicBool; @@ -29,6 +26,9 @@ use std::time::Instant; use std::time::SystemTime; use std::time::UNIX_EPOCH; +use stats::ClusterStats; +use stats::NodeStats; + #[macro_use] extern crate static_assertions; diff --git a/scheds/rust/scx_rusty/src/stats.rs b/scheds/rust/scx_rusty/src/stats.rs index 1a2849208..98b6ebd1d 100644 --- a/scheds/rust/scx_rusty/src/stats.rs +++ b/scheds/rust/scx_rusty/src/stats.rs @@ -1,4 +1,11 @@ -use crate::StatsCtx; +use std::collections::BTreeMap; +use std::io::Write; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::sync::Arc; +use std::time::Duration; +use std::time::UNIX_EPOCH; + use anyhow::Result; use chrono::DateTime; use chrono::Local; @@ -7,13 +14,8 @@ use scx_stats_derive::Stats; use scx_utils::Cpumask; use serde::Deserialize; use serde::Serialize; -use std::collections::BTreeMap; -use std::io::Write; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering; -use std::sync::Arc; -use std::time::Duration; -use std::time::UNIX_EPOCH; + +use crate::StatsCtx; fn signed(x: f64) -> String { if x >= 0.0f64 { diff --git a/scheds/rust/scx_rusty/src/tuner.rs b/scheds/rust/scx_rusty/src/tuner.rs index 1d1a28ae4..eb8efad6f 100644 --- a/scheds/rust/scx_rusty/src/tuner.rs +++ b/scheds/rust/scx_rusty/src/tuner.rs @@ -5,17 +5,16 @@ use std::collections::BTreeMap; use std::sync::Arc; -use crate::sub_or_zero; -use crate::BpfSkel; -use crate::DomainGroup; - use ::fb_procfs as procfs; use anyhow::anyhow; use anyhow::bail; use anyhow::Result; - use scx_utils::Cpumask; +use crate::sub_or_zero; +use crate::BpfSkel; +use crate::DomainGroup; + fn calc_util(curr: &procfs::CpuStat, prev: &procfs::CpuStat) -> Result { match (curr, prev) { (