From bc7c2afcbfb45836ba58edfbbf7497b884c395d7 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Mon, 27 Nov 2023 22:00:40 -1000 Subject: [PATCH 1/9] scx_{rusty|layered}: Generate skel file in $OUT_DIR Currently, skel files are put in src/bpf/.output. Place it inside $OUT_DIR where build artifacts belong. --- tools/sched_ext/scx_layered/build.rs | 24 ++++++---------------- tools/sched_ext/scx_layered/src/layered.rs | 12 +++++++++++ tools/sched_ext/scx_layered/src/main.rs | 1 - tools/sched_ext/scx_rusty/build.rs | 24 ++++++---------------- tools/sched_ext/scx_rusty/src/main.rs | 1 - tools/sched_ext/scx_rusty/src/rusty.rs | 12 +++++++++++ 6 files changed, 36 insertions(+), 38 deletions(-) create mode 100644 tools/sched_ext/scx_layered/src/layered.rs create mode 100644 tools/sched_ext/scx_rusty/src/rusty.rs diff --git a/tools/sched_ext/scx_layered/build.rs b/tools/sched_ext/scx_layered/build.rs index ea0bbd48af8251..0f113716db14b4 100644 --- a/tools/sched_ext/scx_layered/build.rs +++ b/tools/sched_ext/scx_layered/build.rs @@ -5,8 +5,6 @@ extern crate bindgen; use std::env; -use std::fs::create_dir_all; -use std::path::Path; use std::path::PathBuf; use glob::glob; @@ -43,17 +41,16 @@ fn bindgen_layered() { fn gen_bpf_sched(name: &str) { let bpf_cflags = env::var("SCX_RUST_BPF_CFLAGS").unwrap(); let clang = env::var("SCX_RUST_CLANG").unwrap(); - eprintln!("{}", clang); - let outpath = format!("./src/bpf/.output/{}.skel.rs", name); - let skel = Path::new(&outpath); let src = format!("./src/bpf/{}.bpf.c", name); - let obj = format!("./src/bpf/.output/{}.bpf.o", name); + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + let skel_path = out_path.join(format!("{}.bpf.skel.rs", name)); + let obj = out_path.join(format!("{}.bpf.o", name)); SkeletonBuilder::new() - .source(src.clone()) - .obj(obj) + .source(&src) + .obj(&obj) .clang(clang) .clang_args(bpf_cflags) - .build_and_generate(skel) + .build_and_generate(&skel_path) .unwrap(); // Trigger rebuild if any .[hc] files are changed in the directory. @@ -64,14 +61,5 @@ fn gen_bpf_sched(name: &str) { fn main() { bindgen_layered(); - // It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton. - // Reasons are because the generated skeleton contains compiler attributes - // that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]` - // trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside - // the path attribute either (see https://github.com/rust-lang/rust/pull/83366). - // - // However, there is hope! When the above feature stabilizes we can clean this - // all up. - create_dir_all("./src/bpf/.output").unwrap(); gen_bpf_sched("layered"); } diff --git a/tools/sched_ext/scx_layered/src/layered.rs b/tools/sched_ext/scx_layered/src/layered.rs new file mode 100644 index 00000000000000..660499863dabaf --- /dev/null +++ b/tools/sched_ext/scx_layered/src/layered.rs @@ -0,0 +1,12 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2. + +// We can't directly include the generated skeleton in main.rs as it may +// contain compiler attributes that can't be `include!()`ed via macro and we +// can't use the `#[path = "..."]` because `concat!(env!("OUT_DIR"), +// "/bpf.skel.rs")` does not work inside the path attribute yet (see +// https://github.com/rust-lang/rust/pull/83366). + +include!(concat!(env!("OUT_DIR"), "/layered.bpf.skel.rs")); diff --git a/tools/sched_ext/scx_layered/src/main.rs b/tools/sched_ext/scx_layered/src/main.rs index 7eb2edf53661fd..8f9e1a7ba69648 100644 --- a/tools/sched_ext/scx_layered/src/main.rs +++ b/tools/sched_ext/scx_layered/src/main.rs @@ -2,7 +2,6 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -#[path = "bpf/.output/layered.skel.rs"] mod layered; pub use layered::*; pub mod layered_sys; diff --git a/tools/sched_ext/scx_rusty/build.rs b/tools/sched_ext/scx_rusty/build.rs index c54b8f33c57785..6397a1ed0045ab 100644 --- a/tools/sched_ext/scx_rusty/build.rs +++ b/tools/sched_ext/scx_rusty/build.rs @@ -5,8 +5,6 @@ extern crate bindgen; use std::env; -use std::fs::create_dir_all; -use std::path::Path; use std::path::PathBuf; use libbpf_cargo::SkeletonBuilder; @@ -42,31 +40,21 @@ fn bindgen_rusty() { fn gen_bpf_sched(name: &str) { let bpf_cflags = env::var("SCX_RUST_BPF_CFLAGS").unwrap(); let clang = env::var("SCX_RUST_CLANG").unwrap(); - eprintln!("{}", clang); - let outpath = format!("./src/bpf/.output/{}.skel.rs", name); - let skel = Path::new(&outpath); let src = format!("./src/bpf/{}.bpf.c", name); - let obj = format!("./src/bpf/.output/{}.bpf.o", name); + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + let skel_path = out_path.join(format!("{}.bpf.skel.rs", name)); + let obj = out_path.join(format!("{}.bpf.o", name)); SkeletonBuilder::new() - .source(src.clone()) - .obj(obj) + .source(&src) + .obj(&obj) .clang(clang) .clang_args(bpf_cflags) - .build_and_generate(skel) + .build_and_generate(&skel_path) .unwrap(); println!("cargo:rerun-if-changed={}", src); } fn main() { bindgen_rusty(); - // It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton. - // Reasons are because the generated skeleton contains compiler attributes - // that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]` - // trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside - // the path attribute either (see https://github.com/rust-lang/rust/pull/83366). - // - // However, there is hope! When the above feature stabilizes we can clean this - // all up. - create_dir_all("./src/bpf/.output").unwrap(); gen_bpf_sched("rusty"); } diff --git a/tools/sched_ext/scx_rusty/src/main.rs b/tools/sched_ext/scx_rusty/src/main.rs index 3b0bcd742e0541..841f9a28a788df 100644 --- a/tools/sched_ext/scx_rusty/src/main.rs +++ b/tools/sched_ext/scx_rusty/src/main.rs @@ -2,7 +2,6 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -#[path = "bpf/.output/rusty.skel.rs"] mod rusty; pub use rusty::*; pub mod rusty_sys; diff --git a/tools/sched_ext/scx_rusty/src/rusty.rs b/tools/sched_ext/scx_rusty/src/rusty.rs new file mode 100644 index 00000000000000..485ad5150dd0e9 --- /dev/null +++ b/tools/sched_ext/scx_rusty/src/rusty.rs @@ -0,0 +1,12 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2. + +// We can't directly include the generated skeleton in main.rs as it may +// contain compiler attributes that can't be `include!()`ed via macro and we +// can't use the `#[path = "..."]` because `concat!(env!("OUT_DIR"), +// "/bpf.skel.rs")` does not work inside the path attribute yet (see +// https://github.com/rust-lang/rust/pull/83366). + +include!(concat!(env!("OUT_DIR"), "/rusty.bpf.skel.rs")); From 1d9acf6d6985364c413caf2e4c7a0ab776b1eee8 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Tue, 28 Nov 2023 08:52:46 -1000 Subject: [PATCH 2/9] scx_{rusty|layered}: ravg_read is now provided by scx_utils crate, remove .rs.h file --- tools/sched_ext/ravg_read.rs.h | 82 ------------------------- tools/sched_ext/scx_layered/Cargo.toml | 1 + tools/sched_ext/scx_layered/src/main.rs | 3 +- tools/sched_ext/scx_rusty/Cargo.toml | 1 + tools/sched_ext/scx_rusty/src/main.rs | 3 +- 5 files changed, 4 insertions(+), 86 deletions(-) delete mode 100644 tools/sched_ext/ravg_read.rs.h diff --git a/tools/sched_ext/ravg_read.rs.h b/tools/sched_ext/ravg_read.rs.h deleted file mode 100644 index 4efaa2390aa610..00000000000000 --- a/tools/sched_ext/ravg_read.rs.h +++ /dev/null @@ -1,82 +0,0 @@ -/// ravg_read() implementation for rust userland. See ravg_read() in -/// ravg_impl.bpf.h. We don't yet have a good mechanism to share BPF and -/// matching rust code across multiple schedulers. For now, include both BPF -/// and rust code from scheduler implementations. -fn ravg_read( - val: u64, - val_at: u64, - old: u64, - cur: u64, - now: u64, - half_life: u32, - frac_bits: u32, -) -> f64 { - let ravg_1: f64 = (1 << frac_bits) as f64; - let half_life = half_life as u64; - let val = val as f64; - let mut old = old as f64 / ravg_1; - let mut cur = cur as f64 / ravg_1; - - let now = now.max(val_at); - let normalized_dur = |dur| dur as f64 / half_life as f64; - - // - // The following is f64 implementation of BPF ravg_accumulate(). - // - let cur_seq = (now / half_life) as i64; - let val_seq = (val_at / half_life) as i64; - let seq_delta = (cur_seq - val_seq) as i32; - - if seq_delta > 0 { - let full_decay = 2f64.powi(seq_delta); - - // Decay $old and fold $cur into it. - old /= full_decay; - old += cur / full_decay; - cur = 0.0; - - // Fold the oldest period whicy may be partial. - old += val * normalized_dur(half_life - val_at % half_life) / full_decay; - - // Pre-computed decayed full-period values. - const FULL_SUMS: [f64; 20] = [ - 0.5, - 0.75, - 0.875, - 0.9375, - 0.96875, - 0.984375, - 0.9921875, - 0.99609375, - 0.998046875, - 0.9990234375, - 0.99951171875, - 0.999755859375, - 0.9998779296875, - 0.99993896484375, - 0.999969482421875, - 0.9999847412109375, - 0.9999923706054688, - 0.9999961853027344, - 0.9999980926513672, - 0.9999990463256836, - // Use the same value beyond this point. - ]; - - // Fold the full periods in the middle. - if seq_delta >= 2 { - let idx = ((seq_delta - 2) as usize).min(FULL_SUMS.len() - 1); - old += val * FULL_SUMS[idx]; - } - - // Accumulate the current period duration into @cur. - cur += val * normalized_dur(now % half_life); - } else { - cur += val * normalized_dur(now - val_at); - } - - // - // The following is the blending part of BPF ravg_read(). - // - old * (1.0 - normalized_dur(now % half_life) / 2.0) + cur / 2.0 -} diff --git a/tools/sched_ext/scx_layered/Cargo.toml b/tools/sched_ext/scx_layered/Cargo.toml index 6ba1b98d25cd9d..6567ec748be4ce 100644 --- a/tools/sched_ext/scx_layered/Cargo.toml +++ b/tools/sched_ext/scx_layered/Cargo.toml @@ -7,6 +7,7 @@ description = "Userspace scheduling with BPF for Ads" license = "GPL-2.0-only" [dependencies] +scx_utils = "0.1" anyhow = "1.0" bitvec = "1.0" clap = { version = "4.1", features = ["derive", "env", "unicode", "wrap_help"] } diff --git a/tools/sched_ext/scx_layered/src/main.rs b/tools/sched_ext/scx_layered/src/main.rs index 8f9e1a7ba69648..3562fb8bb8f0e4 100644 --- a/tools/sched_ext/scx_layered/src/main.rs +++ b/tools/sched_ext/scx_layered/src/main.rs @@ -33,6 +33,7 @@ use libbpf_rs::skel::SkelBuilder as _; use log::debug; use log::info; use log::trace; +use scx_utils::ravg::ravg_read; use serde::Deserialize; use serde::Serialize; @@ -49,8 +50,6 @@ const NR_LSTATS: usize = layered_sys::layer_stat_idx_NR_LSTATS as usize; const NR_LAYER_MATCH_KINDS: usize = layered_sys::layer_match_kind_NR_LAYER_MATCH_KINDS as usize; const CORE_CACHE_LEVEL: u32 = 2; -include!("../../ravg_read.rs.h"); - lazy_static::lazy_static! { static ref NR_POSSIBLE_CPUS: usize = libbpf_rs::num_possible_cpus().unwrap(); static ref USAGE_DECAY: f64 = 0.5f64.powf(1.0 / USAGE_HALF_LIFE_F64); diff --git a/tools/sched_ext/scx_rusty/Cargo.toml b/tools/sched_ext/scx_rusty/Cargo.toml index b0edd3b937d411..77c0205c23e297 100644 --- a/tools/sched_ext/scx_rusty/Cargo.toml +++ b/tools/sched_ext/scx_rusty/Cargo.toml @@ -7,6 +7,7 @@ description = "Userspace scheduling with BPF" license = "GPL-2.0-only" [dependencies] +scx_utils = "0.1" anyhow = "1.0.65" bitvec = { version = "1.0", features = ["serde"] } clap = { version = "4.1", features = ["derive", "env", "unicode", "wrap_help"] } diff --git a/tools/sched_ext/scx_rusty/src/main.rs b/tools/sched_ext/scx_rusty/src/main.rs index 841f9a28a788df..57c568b3e9c98a 100644 --- a/tools/sched_ext/scx_rusty/src/main.rs +++ b/tools/sched_ext/scx_rusty/src/main.rs @@ -33,13 +33,12 @@ use log::info; use log::trace; use log::warn; use ordered_float::OrderedFloat; +use scx_utils::ravg::ravg_read; const RAVG_FRAC_BITS: u32 = rusty_sys::ravg_consts_RAVG_FRAC_BITS; const MAX_DOMS: usize = rusty_sys::consts_MAX_DOMS as usize; const MAX_CPUS: usize = rusty_sys::consts_MAX_CPUS as usize; -include!("../../ravg_read.rs.h"); - /// scx_rusty: A multi-domain BPF / userspace hybrid scheduler /// /// The BPF part does simple vtime or round robin scheduling in each domain From 2e2daa7e5331a3b576eee3b73dea709c41407276 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Tue, 28 Nov 2023 20:45:42 -1000 Subject: [PATCH 3/9] scx_{rusty|layered}: Make naming and build consistent between the two rust userland schedulers - NAME_sys and NAME was used to refer to rust wrapper of the bindgen-generated header file and the bpf skeleton, respectively. The NAME part is self-referential and thus doesn't really signify anything and _sys suffix is arbitrary too. Let's use bpf_intf and bpf_skel instead. - The env vars that are used during build are a bit unusual and the SCX_RUST_CLANG name is a bit confusing as it doesn't indicate it's for compiling BPF. Let's use the names BPF_CLANG and BPF_CFLAGS instead. - build.rs is now identical between the two schedulers. --- tools/sched_ext/Makefile | 4 +- tools/sched_ext/scx_layered/build.rs | 23 ++--- .../scx_layered/src/bpf/{layered.h => intf.h} | 6 +- .../src/bpf/{layered.bpf.c => main.bpf.c} | 2 +- .../src/bpf_intf.rs} | 2 +- .../rusty.rs => scx_layered/src/bpf_skel.rs} | 2 +- tools/sched_ext/scx_layered/src/main.rs | 84 +++++++++---------- tools/sched_ext/scx_rusty/Cargo.toml | 1 + tools/sched_ext/scx_rusty/build.rs | 32 ++++--- .../scx_rusty/src/bpf/{rusty.h => intf.h} | 6 +- .../src/bpf/{rusty.bpf.c => main.bpf.c} | 2 +- .../src/bpf_intf.rs} | 2 +- .../layered.rs => scx_rusty/src/bpf_skel.rs} | 2 +- tools/sched_ext/scx_rusty/src/main.rs | 77 +++++++++-------- 14 files changed, 126 insertions(+), 119 deletions(-) rename tools/sched_ext/scx_layered/src/bpf/{layered.h => intf.h} (96%) rename tools/sched_ext/scx_layered/src/bpf/{layered.bpf.c => main.bpf.c} (99%) rename tools/sched_ext/{scx_rusty/src/rusty_sys.rs => scx_layered/src/bpf_intf.rs} (84%) rename tools/sched_ext/{scx_rusty/src/rusty.rs => scx_layered/src/bpf_skel.rs} (89%) rename tools/sched_ext/scx_rusty/src/bpf/{rusty.h => intf.h} (97%) rename tools/sched_ext/scx_rusty/src/bpf/{rusty.bpf.c => main.bpf.c} (99%) rename tools/sched_ext/{scx_layered/src/layered_sys.rs => scx_rusty/src/bpf_intf.rs} (83%) rename tools/sched_ext/{scx_layered/src/layered.rs => scx_rusty/src/bpf_skel.rs} (89%) diff --git a/tools/sched_ext/Makefile b/tools/sched_ext/Makefile index 2380cbe5845cb6..43926befe86a4c 100644 --- a/tools/sched_ext/Makefile +++ b/tools/sched_ext/Makefile @@ -216,8 +216,8 @@ $(addsuffix _deps,$(rust-sched-targets)): $(rust-sched-targets): %: $(INCLUDE_DIR)/vmlinux.h $(SCX_COMMON_DEPS) $(eval export RUSTFLAGS = -C link-args=-lzstd -C link-args=-lz -C link-args=-lelf -L $(BPFOBJ_DIR)) - $(eval export SCX_RUST_CLANG = $(CLANG)) - $(eval export SCX_RUST_BPF_CFLAGS= $(BPF_CFLAGS)) + $(eval export BPF_CLANG = $(CLANG)) + $(eval export BPF_CFLAGS = $(BPF_CFLAGS)) $(eval sched=$(notdir $@)) $(Q)cargo build --manifest-path=$(sched)/Cargo.toml $(CARGOFLAGS) $(Q)cp $(OUTPUT_DIR)/release/$(sched) $(BINDIR)/$@ diff --git a/tools/sched_ext/scx_layered/build.rs b/tools/sched_ext/scx_layered/build.rs index 0f113716db14b4..4f240bfbc43be5 100644 --- a/tools/sched_ext/scx_layered/build.rs +++ b/tools/sched_ext/scx_layered/build.rs @@ -10,9 +10,10 @@ use std::path::PathBuf; use glob::glob; use libbpf_cargo::SkeletonBuilder; -const HEADER_PATH: &str = "src/bpf/layered.h"; +const HEADER_PATH: &str = "src/bpf/intf.h"; +const SKEL_NAME: &str = "bpf"; -fn bindgen_layered() { +fn bindgen_bpf_intf() { // Tell cargo to invalidate the built crate whenever the wrapper changes println!("cargo:rerun-if-changed={}", HEADER_PATH); @@ -34,17 +35,17 @@ fn bindgen_layered() { // Write the bindings to the $OUT_DIR/bindings.rs file. let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings - .write_to_file(out_path.join("layered_sys.rs")) + .write_to_file(out_path.join("bpf_intf.rs")) .expect("Couldn't write bindings!"); } -fn gen_bpf_sched(name: &str) { - let bpf_cflags = env::var("SCX_RUST_BPF_CFLAGS").unwrap(); - let clang = env::var("SCX_RUST_CLANG").unwrap(); - let src = format!("./src/bpf/{}.bpf.c", name); +fn gen_bpf_skel() { + let bpf_cflags = env::var("BPF_CFLAGS").unwrap(); + let clang = env::var("BPF_CLANG").unwrap(); + let src = format!("./src/bpf/main.bpf.c"); let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let skel_path = out_path.join(format!("{}.bpf.skel.rs", name)); - let obj = out_path.join(format!("{}.bpf.o", name)); + let skel_path = out_path.join(format!("{}_skel.rs", SKEL_NAME)); + let obj = out_path.join(format!("{}.bpf.o", SKEL_NAME)); SkeletonBuilder::new() .source(&src) .obj(&obj) @@ -60,6 +61,6 @@ fn gen_bpf_sched(name: &str) { } fn main() { - bindgen_layered(); - gen_bpf_sched("layered"); + bindgen_bpf_intf(); + gen_bpf_skel(); } diff --git a/tools/sched_ext/scx_layered/src/bpf/layered.h b/tools/sched_ext/scx_layered/src/bpf/intf.h similarity index 96% rename from tools/sched_ext/scx_layered/src/bpf/layered.h rename to tools/sched_ext/scx_layered/src/bpf/intf.h index bedfa0650c0052..9b9e6cb909a0ea 100644 --- a/tools/sched_ext/scx_layered/src/bpf/layered.h +++ b/tools/sched_ext/scx_layered/src/bpf/intf.h @@ -2,8 +2,8 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -#ifndef __LAYERED_H -#define __LAYERED_H +#ifndef __INTF_H +#define __INTF_H #include #ifndef __kptr @@ -97,4 +97,4 @@ struct layer { unsigned int nr_cpus; // managed from BPF side }; -#endif /* __LAYERED_H */ +#endif /* __INTF_H */ diff --git a/tools/sched_ext/scx_layered/src/bpf/layered.bpf.c b/tools/sched_ext/scx_layered/src/bpf/main.bpf.c similarity index 99% rename from tools/sched_ext/scx_layered/src/bpf/layered.bpf.c rename to tools/sched_ext/scx_layered/src/bpf/main.bpf.c index b0a27f3c713703..4b3330785c5d94 100644 --- a/tools/sched_ext/scx_layered/src/bpf/layered.bpf.c +++ b/tools/sched_ext/scx_layered/src/bpf/main.bpf.c @@ -1,6 +1,6 @@ /* Copyright (c) Meta Platforms, Inc. and affiliates. */ #include "../../../scx_common.bpf.h" -#include "layered.h" +#include "intf.h" #include #include diff --git a/tools/sched_ext/scx_rusty/src/rusty_sys.rs b/tools/sched_ext/scx_layered/src/bpf_intf.rs similarity index 84% rename from tools/sched_ext/scx_rusty/src/rusty_sys.rs rename to tools/sched_ext/scx_layered/src/bpf_intf.rs index e948d81e7356ec..0ed31f8e087380 100644 --- a/tools/sched_ext/scx_rusty/src/rusty_sys.rs +++ b/tools/sched_ext/scx_layered/src/bpf_intf.rs @@ -7,4 +7,4 @@ #![allow(non_snake_case)] #![allow(dead_code)] -include!(concat!(env!("OUT_DIR"), "/rusty_sys.rs")); +include!(concat!(env!("OUT_DIR"), "/bpf_intf.rs")); diff --git a/tools/sched_ext/scx_rusty/src/rusty.rs b/tools/sched_ext/scx_layered/src/bpf_skel.rs similarity index 89% rename from tools/sched_ext/scx_rusty/src/rusty.rs rename to tools/sched_ext/scx_layered/src/bpf_skel.rs index 485ad5150dd0e9..063ccf896d61e1 100644 --- a/tools/sched_ext/scx_rusty/src/rusty.rs +++ b/tools/sched_ext/scx_layered/src/bpf_skel.rs @@ -9,4 +9,4 @@ // "/bpf.skel.rs")` does not work inside the path attribute yet (see // https://github.com/rust-lang/rust/pull/83366). -include!(concat!(env!("OUT_DIR"), "/rusty.bpf.skel.rs")); +include!(concat!(env!("OUT_DIR"), "/bpf_skel.rs")); diff --git a/tools/sched_ext/scx_layered/src/main.rs b/tools/sched_ext/scx_layered/src/main.rs index 3562fb8bb8f0e4..8f4d77db04ea9e 100644 --- a/tools/sched_ext/scx_layered/src/main.rs +++ b/tools/sched_ext/scx_layered/src/main.rs @@ -2,9 +2,9 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -mod layered; -pub use layered::*; -pub mod layered_sys; +mod bpf_skel; +pub use bpf_skel::*; +pub mod bpf_intf; use std::collections::BTreeMap; use std::collections::BTreeSet; @@ -37,17 +37,17 @@ use scx_utils::ravg::ravg_read; use serde::Deserialize; use serde::Serialize; -const RAVG_FRAC_BITS: u32 = layered_sys::ravg_consts_RAVG_FRAC_BITS; -const MAX_CPUS: usize = layered_sys::consts_MAX_CPUS as usize; -const MAX_PATH: usize = layered_sys::consts_MAX_PATH as usize; -const MAX_COMM: usize = layered_sys::consts_MAX_COMM as usize; -const MAX_LAYER_MATCH_ORS: usize = layered_sys::consts_MAX_LAYER_MATCH_ORS as usize; -const MAX_LAYERS: usize = layered_sys::consts_MAX_LAYERS as usize; -const USAGE_HALF_LIFE: u32 = layered_sys::consts_USAGE_HALF_LIFE; +const RAVG_FRAC_BITS: u32 = bpf_intf::ravg_consts_RAVG_FRAC_BITS; +const MAX_CPUS: usize = bpf_intf::consts_MAX_CPUS as usize; +const MAX_PATH: usize = bpf_intf::consts_MAX_PATH as usize; +const MAX_COMM: usize = bpf_intf::consts_MAX_COMM as usize; +const MAX_LAYER_MATCH_ORS: usize = bpf_intf::consts_MAX_LAYER_MATCH_ORS as usize; +const MAX_LAYERS: usize = bpf_intf::consts_MAX_LAYERS as usize; +const USAGE_HALF_LIFE: u32 = bpf_intf::consts_USAGE_HALF_LIFE; const USAGE_HALF_LIFE_F64: f64 = USAGE_HALF_LIFE as f64 / 1_000_000_000.0; -const NR_GSTATS: usize = layered_sys::global_stat_idx_NR_GSTATS as usize; -const NR_LSTATS: usize = layered_sys::layer_stat_idx_NR_LSTATS as usize; -const NR_LAYER_MATCH_KINDS: usize = layered_sys::layer_match_kind_NR_LAYER_MATCH_KINDS as usize; +const NR_GSTATS: usize = bpf_intf::global_stat_idx_NR_GSTATS as usize; +const NR_LSTATS: usize = bpf_intf::layer_stat_idx_NR_LSTATS as usize; +const NR_LAYER_MATCH_KINDS: usize = bpf_intf::layer_match_kind_NR_LAYER_MATCH_KINDS as usize; const CORE_CACHE_LEVEL: u32 = 2; lazy_static::lazy_static! { @@ -408,7 +408,7 @@ fn format_bitvec(bitvec: &BitVec) -> String { output } -fn read_cpu_ctxs(skel: &LayeredSkel) -> Result> { +fn read_cpu_ctxs(skel: &BpfSkel) -> Result> { let mut cpu_ctxs = vec![]; let cpu_ctxs_vec = skel .maps() @@ -418,7 +418,7 @@ fn read_cpu_ctxs(skel: &LayeredSkel) -> Result> { .unwrap(); for cpu in 0..*NR_POSSIBLE_CPUS { cpu_ctxs.push(*unsafe { - &*(cpu_ctxs_vec[cpu].as_slice().as_ptr() as *const layered_sys::cpu_ctx) + &*(cpu_ctxs_vec[cpu].as_slice().as_ptr() as *const bpf_intf::cpu_ctx) }); } Ok(cpu_ctxs) @@ -432,7 +432,7 @@ struct BpfStats { } impl BpfStats { - fn read(cpu_ctxs: &[layered_sys::cpu_ctx], nr_layers: usize) -> Self { + fn read(cpu_ctxs: &[bpf_intf::cpu_ctx], nr_layers: usize) -> Self { let mut gstats = vec![0u64; NR_GSTATS]; let mut lstats = vec![vec![0u64; NR_LSTATS]; nr_layers]; @@ -501,7 +501,7 @@ struct Stats { } impl Stats { - fn read_layer_loads(skel: &mut LayeredSkel, nr_layers: usize) -> (f64, Vec) { + fn read_layer_loads(skel: &mut BpfSkel, nr_layers: usize) -> (f64, Vec) { let now_mono = now_monotonic(); let layer_loads: Vec = skel .bss() @@ -524,7 +524,7 @@ impl Stats { (layer_loads.iter().sum(), layer_loads) } - fn read_layer_cycles(cpu_ctxs: &[layered_sys::cpu_ctx], nr_layers: usize) -> Vec { + fn read_layer_cycles(cpu_ctxs: &[bpf_intf::cpu_ctx], nr_layers: usize) -> Vec { let mut layer_cycles = vec![0u64; nr_layers]; for cpu in 0..*NR_POSSIBLE_CPUS { @@ -536,7 +536,7 @@ impl Stats { layer_cycles } - fn new(skel: &mut LayeredSkel, proc_reader: &procfs::ProcReader) -> Result { + fn new(skel: &mut BpfSkel, proc_reader: &procfs::ProcReader) -> Result { let nr_layers = skel.rodata().nr_layers as usize; let bpf_stats = BpfStats::read(&read_cpu_ctxs(skel)?, nr_layers); @@ -563,7 +563,7 @@ impl Stats { fn refresh( &mut self, - skel: &mut LayeredSkel, + skel: &mut BpfSkel, proc_reader: &procfs::ProcReader, now: Instant, ) -> Result<()> { @@ -632,7 +632,7 @@ struct UserExitInfo { } impl UserExitInfo { - fn read(bpf_uei: &layered_bss_types::user_exit_info) -> Result { + fn read(bpf_uei: &bpf_bss_types::user_exit_info) -> Result { let kind = unsafe { std::ptr::read_volatile(&bpf_uei.kind as *const _) }; let (reason, msg) = if kind != 0 { @@ -659,7 +659,7 @@ impl UserExitInfo { Ok(Self { kind, reason, msg }) } - fn exited(bpf_uei: &layered_bss_types::user_exit_info) -> Result { + fn exited(bpf_uei: &bpf_bss_types::user_exit_info) -> Result { Ok(Self::read(bpf_uei)?.kind != 0) } @@ -1100,7 +1100,7 @@ impl Layer { } struct Scheduler<'a> { - skel: LayeredSkel<'a>, + skel: BpfSkel<'a>, struct_ops: Option, layer_specs: Vec, @@ -1121,7 +1121,7 @@ struct Scheduler<'a> { } impl<'a> Scheduler<'a> { - fn init_layers(skel: &mut OpenLayeredSkel, specs: &Vec) -> Result<()> { + fn init_layers(skel: &mut OpenBpfSkel, specs: &Vec) -> Result<()> { skel.rodata().nr_layers = specs.len() as u32; for (spec_i, spec) in specs.iter().enumerate() { @@ -1132,19 +1132,19 @@ impl<'a> Scheduler<'a> { let mt = &mut layer.matches[or_i].matches[and_i]; match and { LayerMatch::CgroupPrefix(prefix) => { - mt.kind = layered_sys::layer_match_kind_MATCH_CGROUP_PREFIX as i32; + mt.kind = bpf_intf::layer_match_kind_MATCH_CGROUP_PREFIX as i32; copy_into_cstr(&mut mt.cgroup_prefix, prefix.as_str()); } LayerMatch::CommPrefix(prefix) => { - mt.kind = layered_sys::layer_match_kind_MATCH_COMM_PREFIX as i32; + mt.kind = bpf_intf::layer_match_kind_MATCH_COMM_PREFIX as i32; copy_into_cstr(&mut mt.comm_prefix, prefix.as_str()); } LayerMatch::NiceAbove(nice) => { - mt.kind = layered_sys::layer_match_kind_MATCH_NICE_ABOVE as i32; + mt.kind = bpf_intf::layer_match_kind_MATCH_NICE_ABOVE as i32; mt.nice_above_or_below = *nice; } LayerMatch::NiceBelow(nice) => { - mt.kind = layered_sys::layer_match_kind_MATCH_NICE_BELOW as i32; + mt.kind = bpf_intf::layer_match_kind_MATCH_NICE_BELOW as i32; mt.nice_above_or_below = *nice; } } @@ -1171,7 +1171,7 @@ impl<'a> Scheduler<'a> { let mut cpu_pool = CpuPool::new()?; // Open the BPF prog first for verification. - let mut skel_builder = LayeredSkelBuilder::default(); + let mut skel_builder = BpfSkelBuilder::default(); skel_builder.obj_builder.debug(opts.verbose > 1); let mut skel = skel_builder.open().context("Failed to open BPF program")?; @@ -1227,7 +1227,7 @@ impl<'a> Scheduler<'a> { }) } - fn update_bpf_layer_cpumask(layer: &Layer, bpf_layer: &mut layered_bss_types::layer) { + fn update_bpf_layer_cpumask(layer: &Layer, bpf_layer: &mut bpf_bss_types::layer) { for bit in 0..layer.cpus.len() { if layer.cpus[bit] { bpf_layer.cpus[bit / 8] |= 1 << (bit % 8); @@ -1323,8 +1323,8 @@ impl<'a> Scheduler<'a> { self.prev_processing_dur = self.processing_dur; let lsum = |idx| stats.bpf_stats.lstats_sums[idx as usize]; - let total = lsum(layered_sys::layer_stat_idx_LSTAT_LOCAL) - + lsum(layered_sys::layer_stat_idx_LSTAT_GLOBAL); + let total = lsum(bpf_intf::layer_stat_idx_LSTAT_LOCAL) + + lsum(bpf_intf::layer_stat_idx_LSTAT_GLOBAL); let lsum_pct = |idx| { if total != 0 { lsum(idx) as f64 / total as f64 * 100.0 @@ -1336,11 +1336,11 @@ impl<'a> Scheduler<'a> { info!( "tot={:7} local={:5.2} open_idle={:5.2} affn_viol={:5.2} tctx_err={} proc={:?}ms", total, - lsum_pct(layered_sys::layer_stat_idx_LSTAT_LOCAL), - lsum_pct(layered_sys::layer_stat_idx_LSTAT_OPEN_IDLE), - lsum_pct(layered_sys::layer_stat_idx_LSTAT_AFFN_VIOL), + lsum_pct(bpf_intf::layer_stat_idx_LSTAT_LOCAL), + lsum_pct(bpf_intf::layer_stat_idx_LSTAT_OPEN_IDLE), + lsum_pct(bpf_intf::layer_stat_idx_LSTAT_AFFN_VIOL), stats.prev_bpf_stats.gstats - [layered_sys::global_stat_idx_GSTAT_TASK_CTX_FREE_FAILED as usize], + [bpf_intf::global_stat_idx_GSTAT_TASK_CTX_FREE_FAILED as usize], processing_dur.as_millis(), ); @@ -1366,8 +1366,8 @@ impl<'a> Scheduler<'a> { for (lidx, (spec, layer)) in self.layer_specs.iter().zip(self.layers.iter()).enumerate() { let lstat = |sidx| stats.bpf_stats.lstats[lidx][sidx as usize]; - let ltotal = lstat(layered_sys::layer_stat_idx_LSTAT_LOCAL) - + lstat(layered_sys::layer_stat_idx_LSTAT_GLOBAL); + let ltotal = lstat(bpf_intf::layer_stat_idx_LSTAT_LOCAL) + + lstat(bpf_intf::layer_stat_idx_LSTAT_GLOBAL); let lstat_pct = |sidx| { if ltotal != 0 { lstat(sidx) as f64 / ltotal as f64 * 100.0 @@ -1390,10 +1390,10 @@ impl<'a> Scheduler<'a> { " {: #ifndef __kptr @@ -94,4 +94,4 @@ struct dom_ctx { u64 dbg_load_printed_at; }; -#endif /* __RUSTY_H */ +#endif /* __INTF_H */ diff --git a/tools/sched_ext/scx_rusty/src/bpf/rusty.bpf.c b/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c similarity index 99% rename from tools/sched_ext/scx_rusty/src/bpf/rusty.bpf.c rename to tools/sched_ext/scx_rusty/src/bpf/main.bpf.c index 7a8b27ceae054e..befd8d4c6c6ea1 100644 --- a/tools/sched_ext/scx_rusty/src/bpf/rusty.bpf.c +++ b/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c @@ -37,7 +37,7 @@ */ #include "../../../scx_common.bpf.h" #include "../../../ravg_impl.bpf.h" -#include "rusty.h" +#include "intf.h" #include #include diff --git a/tools/sched_ext/scx_layered/src/layered_sys.rs b/tools/sched_ext/scx_rusty/src/bpf_intf.rs similarity index 83% rename from tools/sched_ext/scx_layered/src/layered_sys.rs rename to tools/sched_ext/scx_rusty/src/bpf_intf.rs index afc821d388d2cb..0ed31f8e087380 100644 --- a/tools/sched_ext/scx_layered/src/layered_sys.rs +++ b/tools/sched_ext/scx_rusty/src/bpf_intf.rs @@ -7,4 +7,4 @@ #![allow(non_snake_case)] #![allow(dead_code)] -include!(concat!(env!("OUT_DIR"), "/layered_sys.rs")); +include!(concat!(env!("OUT_DIR"), "/bpf_intf.rs")); diff --git a/tools/sched_ext/scx_layered/src/layered.rs b/tools/sched_ext/scx_rusty/src/bpf_skel.rs similarity index 89% rename from tools/sched_ext/scx_layered/src/layered.rs rename to tools/sched_ext/scx_rusty/src/bpf_skel.rs index 660499863dabaf..063ccf896d61e1 100644 --- a/tools/sched_ext/scx_layered/src/layered.rs +++ b/tools/sched_ext/scx_rusty/src/bpf_skel.rs @@ -9,4 +9,4 @@ // "/bpf.skel.rs")` does not work inside the path attribute yet (see // https://github.com/rust-lang/rust/pull/83366). -include!(concat!(env!("OUT_DIR"), "/layered.bpf.skel.rs")); +include!(concat!(env!("OUT_DIR"), "/bpf_skel.rs")); diff --git a/tools/sched_ext/scx_rusty/src/main.rs b/tools/sched_ext/scx_rusty/src/main.rs index 57c568b3e9c98a..3d802e27d9ea23 100644 --- a/tools/sched_ext/scx_rusty/src/main.rs +++ b/tools/sched_ext/scx_rusty/src/main.rs @@ -2,9 +2,9 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -mod rusty; -pub use rusty::*; -pub mod rusty_sys; +mod bpf_skel; +pub use bpf_skel::*; +pub mod bpf_intf; use std::cell::Cell; use std::collections::BTreeMap; @@ -35,9 +35,9 @@ use log::warn; use ordered_float::OrderedFloat; use scx_utils::ravg::ravg_read; -const RAVG_FRAC_BITS: u32 = rusty_sys::ravg_consts_RAVG_FRAC_BITS; -const MAX_DOMS: usize = rusty_sys::consts_MAX_DOMS as usize; -const MAX_CPUS: usize = rusty_sys::consts_MAX_CPUS as usize; +const RAVG_FRAC_BITS: u32 = bpf_intf::ravg_consts_RAVG_FRAC_BITS; +const MAX_DOMS: usize = bpf_intf::consts_MAX_DOMS as usize; +const MAX_CPUS: usize = bpf_intf::consts_MAX_CPUS as usize; /// scx_rusty: A multi-domain BPF / userspace hybrid scheduler /// @@ -420,7 +420,7 @@ impl Tuner { }) } - fn step(&mut self, skel: &mut RustySkel) -> Result<()> { + fn step(&mut self, skel: &mut BpfSkel) -> Result<()> { let curr_cpu_stats = self .proc_reader .read_stat()? @@ -496,7 +496,7 @@ struct TaskInfo { } struct LoadBalancer<'a, 'b, 'c> { - skel: &'a mut RustySkel<'b>, + skel: &'a mut BpfSkel<'b>, top: Arc, skip_kworkers: bool, @@ -531,7 +531,7 @@ impl<'a, 'b, 'c> LoadBalancer<'a, 'b, 'c> { const LOAD_IMBAL_PUSH_MAX_RATIO: f64 = 0.50; fn new( - skel: &'a mut RustySkel<'b>, + skel: &'a mut BpfSkel<'b>, top: Arc, skip_kworkers: bool, nr_lb_data_errors: &'c mut u64, @@ -568,9 +568,8 @@ impl<'a, 'b, 'c> LoadBalancer<'a, 'b, 'c> { .lookup(&key, libbpf_rs::MapFlags::ANY) .context("Failed to lookup dom_ctx")? { - let dom_ctx = unsafe { - &*(dom_ctx_map_elem.as_slice().as_ptr() as *const rusty_sys::dom_ctx) - }; + let dom_ctx = + unsafe { &*(dom_ctx_map_elem.as_slice().as_ptr() as *const bpf_intf::dom_ctx) }; let rd = &dom_ctx.load_rd; self.dom_loads[i] = ravg_read( @@ -620,7 +619,7 @@ impl<'a, 'b, 'c> LoadBalancer<'a, 'b, 'c> { // // XXX - We can't read task_ctx inline because self.skel.bss() // borrows mutably and thus conflicts with self.skel.maps(). - const MAX_PIDS: u64 = rusty_sys::consts_MAX_DOM_ACTIVE_PIDS as u64; + const MAX_PIDS: u64 = bpf_intf::consts_MAX_DOM_ACTIVE_PIDS as u64; let active_pids = &mut self.skel.bss().dom_active_pids[dom as usize]; let mut pids = vec![]; @@ -649,7 +648,7 @@ impl<'a, 'b, 'c> LoadBalancer<'a, 'b, 'c> { if let Some(task_data_elem) = task_data.lookup(&key, libbpf_rs::MapFlags::ANY)? { let task_ctx = - unsafe { &*(task_data_elem.as_slice().as_ptr() as *const rusty_sys::task_ctx) }; + unsafe { &*(task_data_elem.as_slice().as_ptr() as *const bpf_intf::task_ctx) }; if task_ctx.dom_id != dom { continue; @@ -860,7 +859,7 @@ impl<'a, 'b, 'c> LoadBalancer<'a, 'b, 'c> { } struct Scheduler<'a> { - skel: RustySkel<'a>, + skel: BpfSkel<'a>, struct_ops: Option, sched_interval: Duration, @@ -882,7 +881,7 @@ struct Scheduler<'a> { impl<'a> Scheduler<'a> { fn init(opts: &Opts) -> Result { // Open the BPF prog first for verification. - let mut skel_builder = RustySkelBuilder::default(); + let mut skel_builder = BpfSkelBuilder::default(); skel_builder.obj_builder.debug(opts.verbose > 0); let mut skel = skel_builder.open().context("Failed to open BPF program")?; @@ -1024,7 +1023,7 @@ impl<'a> Scheduler<'a> { let mut stats: Vec = Vec::new(); let zero_vec = vec![vec![0u8; stats_map.value_size() as usize]; self.top.nr_cpus]; - for stat in 0..rusty_sys::stat_idx_RUSTY_NR_STATS { + for stat in 0..bpf_intf::stat_idx_RUSTY_NR_STATS { let cpu_stat_vec = stats_map .lookup_percpu(&stat.to_ne_bytes(), libbpf_rs::MapFlags::ANY) .with_context(|| format!("Failed to lookup stat {}", stat))? @@ -1057,22 +1056,22 @@ impl<'a> Scheduler<'a> { imbal: &[f64], ) { let stat = |idx| stats[idx as usize]; - let total = stat(rusty_sys::stat_idx_RUSTY_STAT_WAKE_SYNC) - + stat(rusty_sys::stat_idx_RUSTY_STAT_PREV_IDLE) - + stat(rusty_sys::stat_idx_RUSTY_STAT_GREEDY_IDLE) - + stat(rusty_sys::stat_idx_RUSTY_STAT_PINNED) - + stat(rusty_sys::stat_idx_RUSTY_STAT_DIRECT_DISPATCH) - + stat(rusty_sys::stat_idx_RUSTY_STAT_DIRECT_GREEDY) - + stat(rusty_sys::stat_idx_RUSTY_STAT_DIRECT_GREEDY_FAR) - + stat(rusty_sys::stat_idx_RUSTY_STAT_DSQ_DISPATCH) - + stat(rusty_sys::stat_idx_RUSTY_STAT_GREEDY); + let total = stat(bpf_intf::stat_idx_RUSTY_STAT_WAKE_SYNC) + + stat(bpf_intf::stat_idx_RUSTY_STAT_PREV_IDLE) + + stat(bpf_intf::stat_idx_RUSTY_STAT_GREEDY_IDLE) + + stat(bpf_intf::stat_idx_RUSTY_STAT_PINNED) + + stat(bpf_intf::stat_idx_RUSTY_STAT_DIRECT_DISPATCH) + + stat(bpf_intf::stat_idx_RUSTY_STAT_DIRECT_GREEDY) + + stat(bpf_intf::stat_idx_RUSTY_STAT_DIRECT_GREEDY_FAR) + + stat(bpf_intf::stat_idx_RUSTY_STAT_DSQ_DISPATCH) + + stat(bpf_intf::stat_idx_RUSTY_STAT_GREEDY); info!( "cpu={:7.2} bal={} load_avg={:8.2} task_err={} lb_data_err={} proc={:?}ms", cpu_busy * 100.0, - stats[rusty_sys::stat_idx_RUSTY_STAT_LOAD_BALANCE as usize], + stats[bpf_intf::stat_idx_RUSTY_STAT_LOAD_BALANCE as usize], load_avg, - stats[rusty_sys::stat_idx_RUSTY_STAT_TASK_GET_ERR as usize], + stats[bpf_intf::stat_idx_RUSTY_STAT_TASK_GET_ERR as usize], self.nr_lb_data_errors, processing_dur.as_millis(), ); @@ -1082,25 +1081,25 @@ impl<'a> Scheduler<'a> { info!( "tot={:7} wsync={:5.2} prev_idle={:5.2} greedy_idle={:5.2} pin={:5.2}", total, - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_WAKE_SYNC), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_PREV_IDLE), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_GREEDY_IDLE), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_PINNED), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_WAKE_SYNC), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_PREV_IDLE), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_GREEDY_IDLE), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_PINNED), ); info!( "dir={:5.2} dir_greedy={:5.2} dir_greedy_far={:5.2}", - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_DIRECT_DISPATCH), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_DIRECT_GREEDY), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_DIRECT_GREEDY_FAR), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_DIRECT_DISPATCH), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_DIRECT_GREEDY), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_DIRECT_GREEDY_FAR), ); info!( "dsq={:5.2} greedy={:5.2} kick_greedy={:5.2} rep={:5.2}", - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_DSQ_DISPATCH), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_GREEDY), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_KICK_GREEDY), - stat_pct(rusty_sys::stat_idx_RUSTY_STAT_REPATRIATE), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_DSQ_DISPATCH), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_GREEDY), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_KICK_GREEDY), + stat_pct(bpf_intf::stat_idx_RUSTY_STAT_REPATRIATE), ); let ti = &self.skel.bss().tune_input; From 2d46bf9089ddc22d154c38978e7ced2e53648f07 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Tue, 28 Nov 2023 21:25:12 -1000 Subject: [PATCH 4/9] scx_{rusty|layered}: Run bindgen's clang with CLANG_CFLAGS and remove explicit paths from includes So that build env can decide where to put these headers. --- tools/sched_ext/scx_layered/build.rs | 12 ++++++++---- tools/sched_ext/scx_layered/src/bpf/intf.h | 2 +- tools/sched_ext/scx_layered/src/bpf/main.bpf.c | 4 ++-- tools/sched_ext/scx_rusty/build.rs | 12 ++++++++---- tools/sched_ext/scx_rusty/src/bpf/intf.h | 2 +- tools/sched_ext/scx_rusty/src/bpf/main.bpf.c | 4 ++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/tools/sched_ext/scx_layered/build.rs b/tools/sched_ext/scx_layered/build.rs index 4f240bfbc43be5..ff3bb0b76e794d 100644 --- a/tools/sched_ext/scx_layered/build.rs +++ b/tools/sched_ext/scx_layered/build.rs @@ -21,6 +21,8 @@ fn bindgen_bpf_intf() { // to bindgen, and lets you build up options for // the resulting bindings. let bindings = bindgen::Builder::default() + // Should run clang with the same -I options as BPF compilation. + .clang_args(env::var("BPF_CFLAGS").unwrap().split_whitespace()) // The input header we would like to generate // bindings for. .header(HEADER_PATH) @@ -41,21 +43,23 @@ fn bindgen_bpf_intf() { fn gen_bpf_skel() { let bpf_cflags = env::var("BPF_CFLAGS").unwrap(); - let clang = env::var("BPF_CLANG").unwrap(); + let bpf_clang = env::var("BPF_CLANG").unwrap(); + let src = format!("./src/bpf/main.bpf.c"); let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let skel_path = out_path.join(format!("{}_skel.rs", SKEL_NAME)); let obj = out_path.join(format!("{}.bpf.o", SKEL_NAME)); + let skel_path = out_path.join(format!("{}_skel.rs", SKEL_NAME)); + SkeletonBuilder::new() .source(&src) .obj(&obj) - .clang(clang) + .clang(bpf_clang) .clang_args(bpf_cflags) .build_and_generate(&skel_path) .unwrap(); // Trigger rebuild if any .[hc] files are changed in the directory. - for path in glob("./src/bpf/*.[hc]").unwrap().filter_map(Result::ok) { + for path in glob("src/bpf/*.[hc]").unwrap().filter_map(Result::ok) { println!("cargo:rerun-if-changed={}", path.to_str().unwrap()); } } diff --git a/tools/sched_ext/scx_layered/src/bpf/intf.h b/tools/sched_ext/scx_layered/src/bpf/intf.h index 9b9e6cb909a0ea..8513779d5f5474 100644 --- a/tools/sched_ext/scx_layered/src/bpf/intf.h +++ b/tools/sched_ext/scx_layered/src/bpf/intf.h @@ -18,7 +18,7 @@ typedef unsigned long long u64; typedef long long s64; #endif -#include "../../../ravg.bpf.h" +#include "ravg.bpf.h" enum consts { MAX_CPUS_SHIFT = 9, diff --git a/tools/sched_ext/scx_layered/src/bpf/main.bpf.c b/tools/sched_ext/scx_layered/src/bpf/main.bpf.c index 4b3330785c5d94..d4714f89ee69f8 100644 --- a/tools/sched_ext/scx_layered/src/bpf/main.bpf.c +++ b/tools/sched_ext/scx_layered/src/bpf/main.bpf.c @@ -1,5 +1,5 @@ /* Copyright (c) Meta Platforms, Inc. and affiliates. */ -#include "../../../scx_common.bpf.h" +#include "scx_common.bpf.h" #include "intf.h" #include @@ -27,7 +27,7 @@ static u32 preempt_cursor; #define trace(fmt, args...) do { if (debug > 1) bpf_printk(fmt, ##args); } while (0) #include "util.bpf.c" -#include "../../../ravg_impl.bpf.h" +#include "ravg_impl.bpf.h" struct user_exit_info uei; diff --git a/tools/sched_ext/scx_rusty/build.rs b/tools/sched_ext/scx_rusty/build.rs index 4f240bfbc43be5..ff3bb0b76e794d 100644 --- a/tools/sched_ext/scx_rusty/build.rs +++ b/tools/sched_ext/scx_rusty/build.rs @@ -21,6 +21,8 @@ fn bindgen_bpf_intf() { // to bindgen, and lets you build up options for // the resulting bindings. let bindings = bindgen::Builder::default() + // Should run clang with the same -I options as BPF compilation. + .clang_args(env::var("BPF_CFLAGS").unwrap().split_whitespace()) // The input header we would like to generate // bindings for. .header(HEADER_PATH) @@ -41,21 +43,23 @@ fn bindgen_bpf_intf() { fn gen_bpf_skel() { let bpf_cflags = env::var("BPF_CFLAGS").unwrap(); - let clang = env::var("BPF_CLANG").unwrap(); + let bpf_clang = env::var("BPF_CLANG").unwrap(); + let src = format!("./src/bpf/main.bpf.c"); let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let skel_path = out_path.join(format!("{}_skel.rs", SKEL_NAME)); let obj = out_path.join(format!("{}.bpf.o", SKEL_NAME)); + let skel_path = out_path.join(format!("{}_skel.rs", SKEL_NAME)); + SkeletonBuilder::new() .source(&src) .obj(&obj) - .clang(clang) + .clang(bpf_clang) .clang_args(bpf_cflags) .build_and_generate(&skel_path) .unwrap(); // Trigger rebuild if any .[hc] files are changed in the directory. - for path in glob("./src/bpf/*.[hc]").unwrap().filter_map(Result::ok) { + for path in glob("src/bpf/*.[hc]").unwrap().filter_map(Result::ok) { println!("cargo:rerun-if-changed={}", path.to_str().unwrap()); } } diff --git a/tools/sched_ext/scx_rusty/src/bpf/intf.h b/tools/sched_ext/scx_rusty/src/bpf/intf.h index 34e2e5af76a31e..54d28696ac5a72 100644 --- a/tools/sched_ext/scx_rusty/src/bpf/intf.h +++ b/tools/sched_ext/scx_rusty/src/bpf/intf.h @@ -19,7 +19,7 @@ typedef unsigned int u32; typedef unsigned long long u64; #endif -#include "../../../ravg.bpf.h" +#include "ravg.bpf.h" enum consts { MAX_CPUS = 512, diff --git a/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c b/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c index befd8d4c6c6ea1..c82ad8973d96aa 100644 --- a/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c +++ b/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c @@ -35,8 +35,8 @@ * task weight, dom mask and current dom in the task_data map and executes the * load balance based on userspace populating the lb_data map. */ -#include "../../../scx_common.bpf.h" -#include "../../../ravg_impl.bpf.h" +#include "scx_common.bpf.h" +#include "ravg_impl.bpf.h" #include "intf.h" #include From 65d1b96d784980849399dbf66ece0a8903d04103 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Wed, 29 Nov 2023 08:23:05 -1000 Subject: [PATCH 5/9] scx_{rusty|layered}: Factor out build.rs's into scx_utils::build_helpers This greatly simplifies build.rs and allows building more common logic into build_helpers such as discovering BPF_CFLAGS on its own without depending on upper level Makefile. Some caveats: - Dropped static libbpf-sys dep. scx_utils is out of kernel tree and pulls in libbpf-sys through libbpf-cargo which conflicts with the explicit libbpf-sys dependency. This means that we use packaged version of libbpf-cargo for skel generation. Should be fine. - Path dependency for scx_utils is temporary during development. Should be dropped later. --- tools/sched_ext/scx_layered/Cargo.toml | 7 +-- tools/sched_ext/scx_layered/build.rs | 65 +------------------------- tools/sched_ext/scx_rusty/Cargo.toml | 7 +-- tools/sched_ext/scx_rusty/build.rs | 65 +------------------------- 4 files changed, 8 insertions(+), 136 deletions(-) diff --git a/tools/sched_ext/scx_layered/Cargo.toml b/tools/sched_ext/scx_layered/Cargo.toml index 6567ec748be4ce..53726a2233082f 100644 --- a/tools/sched_ext/scx_layered/Cargo.toml +++ b/tools/sched_ext/scx_layered/Cargo.toml @@ -7,7 +7,6 @@ description = "Userspace scheduling with BPF for Ads" license = "GPL-2.0-only" [dependencies] -scx_utils = "0.1" anyhow = "1.0" bitvec = "1.0" clap = { version = "4.1", features = ["derive", "env", "unicode", "wrap_help"] } @@ -15,17 +14,15 @@ ctrlc = { version = "3.1", features = ["termination"] } fb_procfs = "0.7" lazy_static = "1.4" libbpf-rs = "0.21" -libbpf-sys = { version = "1.2.0", features = ["novendor", "static"] } libc = "0.2" log = "0.4" +scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" simplelog = "0.12" [build-dependencies] -bindgen = { version = "0.61" } -libbpf-cargo = "0.21" -glob = "0.3" +scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } [features] enable_backtrace = [] diff --git a/tools/sched_ext/scx_layered/build.rs b/tools/sched_ext/scx_layered/build.rs index ff3bb0b76e794d..bbaa1ea71c0cb6 100644 --- a/tools/sched_ext/scx_layered/build.rs +++ b/tools/sched_ext/scx_layered/build.rs @@ -2,69 +2,8 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -extern crate bindgen; - -use std::env; -use std::path::PathBuf; - -use glob::glob; -use libbpf_cargo::SkeletonBuilder; - -const HEADER_PATH: &str = "src/bpf/intf.h"; -const SKEL_NAME: &str = "bpf"; - -fn bindgen_bpf_intf() { - // Tell cargo to invalidate the built crate whenever the wrapper changes - println!("cargo:rerun-if-changed={}", HEADER_PATH); - - // The bindgen::Builder is the main entry point - // to bindgen, and lets you build up options for - // the resulting bindings. - let bindings = bindgen::Builder::default() - // Should run clang with the same -I options as BPF compilation. - .clang_args(env::var("BPF_CFLAGS").unwrap().split_whitespace()) - // The input header we would like to generate - // bindings for. - .header(HEADER_PATH) - // Tell cargo to invalidate the built crate whenever any of the - // included header files changed. - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - // Finish the builder and generate the bindings. - .generate() - // Unwrap the Result and panic on failure. - .expect("Unable to generate bindings"); - - // Write the bindings to the $OUT_DIR/bindings.rs file. - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - bindings - .write_to_file(out_path.join("bpf_intf.rs")) - .expect("Couldn't write bindings!"); -} - -fn gen_bpf_skel() { - let bpf_cflags = env::var("BPF_CFLAGS").unwrap(); - let bpf_clang = env::var("BPF_CLANG").unwrap(); - - let src = format!("./src/bpf/main.bpf.c"); - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let obj = out_path.join(format!("{}.bpf.o", SKEL_NAME)); - let skel_path = out_path.join(format!("{}_skel.rs", SKEL_NAME)); - - SkeletonBuilder::new() - .source(&src) - .obj(&obj) - .clang(bpf_clang) - .clang_args(bpf_cflags) - .build_and_generate(&skel_path) - .unwrap(); - - // Trigger rebuild if any .[hc] files are changed in the directory. - for path in glob("src/bpf/*.[hc]").unwrap().filter_map(Result::ok) { - println!("cargo:rerun-if-changed={}", path.to_str().unwrap()); - } -} fn main() { - bindgen_bpf_intf(); - gen_bpf_skel(); + scx_utils::build_helpers::bindgen_bpf_intf(None, None); + scx_utils::build_helpers::gen_bpf_skel(None, None, None); } diff --git a/tools/sched_ext/scx_rusty/Cargo.toml b/tools/sched_ext/scx_rusty/Cargo.toml index 68db432c5d24af..a25eb1099f4832 100644 --- a/tools/sched_ext/scx_rusty/Cargo.toml +++ b/tools/sched_ext/scx_rusty/Cargo.toml @@ -7,7 +7,6 @@ description = "Userspace scheduling with BPF" license = "GPL-2.0-only" [dependencies] -scx_utils = "0.1" anyhow = "1.0.65" bitvec = { version = "1.0", features = ["serde"] } clap = { version = "4.1", features = ["derive", "env", "unicode", "wrap_help"] } @@ -15,16 +14,14 @@ ctrlc = { version = "3.1", features = ["termination"] } fb_procfs = "0.7.0" hex = "0.4.3" libbpf-rs = "0.21.0" -libbpf-sys = { version = "1.2.0", features = ["novendor", "static"] } libc = "0.2.137" log = "0.4.17" ordered-float = "3.4.0" +scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } simplelog = "0.12.0" [build-dependencies] -bindgen = { version = "0.61.0" } -libbpf-cargo = "0.21.0" -glob = "0.3" +scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } [features] enable_backtrace = [] diff --git a/tools/sched_ext/scx_rusty/build.rs b/tools/sched_ext/scx_rusty/build.rs index ff3bb0b76e794d..bbaa1ea71c0cb6 100644 --- a/tools/sched_ext/scx_rusty/build.rs +++ b/tools/sched_ext/scx_rusty/build.rs @@ -2,69 +2,8 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2. -extern crate bindgen; - -use std::env; -use std::path::PathBuf; - -use glob::glob; -use libbpf_cargo::SkeletonBuilder; - -const HEADER_PATH: &str = "src/bpf/intf.h"; -const SKEL_NAME: &str = "bpf"; - -fn bindgen_bpf_intf() { - // Tell cargo to invalidate the built crate whenever the wrapper changes - println!("cargo:rerun-if-changed={}", HEADER_PATH); - - // The bindgen::Builder is the main entry point - // to bindgen, and lets you build up options for - // the resulting bindings. - let bindings = bindgen::Builder::default() - // Should run clang with the same -I options as BPF compilation. - .clang_args(env::var("BPF_CFLAGS").unwrap().split_whitespace()) - // The input header we would like to generate - // bindings for. - .header(HEADER_PATH) - // Tell cargo to invalidate the built crate whenever any of the - // included header files changed. - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - // Finish the builder and generate the bindings. - .generate() - // Unwrap the Result and panic on failure. - .expect("Unable to generate bindings"); - - // Write the bindings to the $OUT_DIR/bindings.rs file. - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - bindings - .write_to_file(out_path.join("bpf_intf.rs")) - .expect("Couldn't write bindings!"); -} - -fn gen_bpf_skel() { - let bpf_cflags = env::var("BPF_CFLAGS").unwrap(); - let bpf_clang = env::var("BPF_CLANG").unwrap(); - - let src = format!("./src/bpf/main.bpf.c"); - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let obj = out_path.join(format!("{}.bpf.o", SKEL_NAME)); - let skel_path = out_path.join(format!("{}_skel.rs", SKEL_NAME)); - - SkeletonBuilder::new() - .source(&src) - .obj(&obj) - .clang(bpf_clang) - .clang_args(bpf_cflags) - .build_and_generate(&skel_path) - .unwrap(); - - // Trigger rebuild if any .[hc] files are changed in the directory. - for path in glob("src/bpf/*.[hc]").unwrap().filter_map(Result::ok) { - println!("cargo:rerun-if-changed={}", path.to_str().unwrap()); - } -} fn main() { - bindgen_bpf_intf(); - gen_bpf_skel(); + scx_utils::build_helpers::bindgen_bpf_intf(None, None); + scx_utils::build_helpers::gen_bpf_skel(None, None, None); } From df7ea88b9774882c45aec43233523d14ef465657 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Wed, 29 Nov 2023 21:13:34 -1000 Subject: [PATCH 6/9] scx_{rusty|layered}: Follow scx_utils::BpfBuilder API updates --- tools/sched_ext/scx_layered/build.rs | 8 ++++++-- tools/sched_ext/scx_rusty/build.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/sched_ext/scx_layered/build.rs b/tools/sched_ext/scx_layered/build.rs index bbaa1ea71c0cb6..1c972ae8d82e22 100644 --- a/tools/sched_ext/scx_layered/build.rs +++ b/tools/sched_ext/scx_layered/build.rs @@ -4,6 +4,10 @@ // GNU General Public License version 2. fn main() { - scx_utils::build_helpers::bindgen_bpf_intf(None, None); - scx_utils::build_helpers::gen_bpf_skel(None, None, None); + scx_utils::BpfBuilder::new() + .unwrap() + .enable_intf("src/bpf/intf.h", "bpf_intf.rs") + .enable_skel("src/bpf/main.bpf.c", "bpf") + .build() + .unwrap(); } diff --git a/tools/sched_ext/scx_rusty/build.rs b/tools/sched_ext/scx_rusty/build.rs index bbaa1ea71c0cb6..1c972ae8d82e22 100644 --- a/tools/sched_ext/scx_rusty/build.rs +++ b/tools/sched_ext/scx_rusty/build.rs @@ -4,6 +4,10 @@ // GNU General Public License version 2. fn main() { - scx_utils::build_helpers::bindgen_bpf_intf(None, None); - scx_utils::build_helpers::gen_bpf_skel(None, None, None); + scx_utils::BpfBuilder::new() + .unwrap() + .enable_intf("src/bpf/intf.h", "bpf_intf.rs") + .enable_skel("src/bpf/main.bpf.c", "bpf") + .build() + .unwrap(); } From 5f200bb4bc14af1857ba39a71fb387709592c6f1 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Sun, 3 Dec 2023 12:16:08 -1000 Subject: [PATCH 7/9] scx_{layered, rusty}: Minor build updates --- tools/sched_ext/scx_layered/Cargo.toml | 4 ++-- tools/sched_ext/scx_layered/build.rs | 2 +- tools/sched_ext/scx_rusty/Cargo.toml | 4 ++-- tools/sched_ext/scx_rusty/build.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/sched_ext/scx_layered/Cargo.toml b/tools/sched_ext/scx_layered/Cargo.toml index 53726a2233082f..1de594354ed0ca 100644 --- a/tools/sched_ext/scx_layered/Cargo.toml +++ b/tools/sched_ext/scx_layered/Cargo.toml @@ -16,13 +16,13 @@ lazy_static = "1.4" libbpf-rs = "0.21" libc = "0.2" log = "0.4" -scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } +scx_utils = "0.2" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" simplelog = "0.12" [build-dependencies] -scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } +scx_utils = "0.2" [features] enable_backtrace = [] diff --git a/tools/sched_ext/scx_layered/build.rs b/tools/sched_ext/scx_layered/build.rs index 1c972ae8d82e22..d26db839cd9e1c 100644 --- a/tools/sched_ext/scx_layered/build.rs +++ b/tools/sched_ext/scx_layered/build.rs @@ -1,5 +1,5 @@ // Copyright (c) Meta Platforms, Inc. and affiliates. - +// // This software may be used and distributed according to the terms of the // GNU General Public License version 2. diff --git a/tools/sched_ext/scx_rusty/Cargo.toml b/tools/sched_ext/scx_rusty/Cargo.toml index a25eb1099f4832..23d37b70e4eb1a 100644 --- a/tools/sched_ext/scx_rusty/Cargo.toml +++ b/tools/sched_ext/scx_rusty/Cargo.toml @@ -17,11 +17,11 @@ libbpf-rs = "0.21.0" libc = "0.2.137" log = "0.4.17" ordered-float = "3.4.0" -scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } +scx_utils = "0.2" simplelog = "0.12.0" [build-dependencies] -scx_utils = { path = "/home/htejun/os/scx/rust/scx_utils", version = "0.1" } +scx_utils = "0.2" [features] enable_backtrace = [] diff --git a/tools/sched_ext/scx_rusty/build.rs b/tools/sched_ext/scx_rusty/build.rs index 1c972ae8d82e22..d26db839cd9e1c 100644 --- a/tools/sched_ext/scx_rusty/build.rs +++ b/tools/sched_ext/scx_rusty/build.rs @@ -1,5 +1,5 @@ // Copyright (c) Meta Platforms, Inc. and affiliates. - +// // This software may be used and distributed according to the terms of the // GNU General Public License version 2. From 47c9356e203f713389e746c89fa3850ac82d529f Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Sun, 3 Dec 2023 12:32:44 -1000 Subject: [PATCH 8/9] scx: Move common headers under include/scx --- tools/sched_ext/Makefile | 8 ++++---- .../{scx_common.bpf.h => include/scx/common.bpf.h} | 0 tools/sched_ext/{scx_common.h => include/scx/common.h} | 0 tools/sched_ext/{ => include/scx}/ravg.bpf.h | 0 tools/sched_ext/{ => include/scx}/ravg_impl.bpf.h | 0 tools/sched_ext/{ => include/scx}/user_exit_info.h | 0 tools/sched_ext/scx_central.bpf.c | 2 +- tools/sched_ext/scx_central.c | 2 +- tools/sched_ext/scx_flatcg.bpf.c | 2 +- tools/sched_ext/scx_flatcg.c | 2 +- tools/sched_ext/scx_layered/src/bpf/intf.h | 2 +- tools/sched_ext/scx_layered/src/bpf/main.bpf.c | 4 ++-- tools/sched_ext/scx_pair.bpf.c | 2 +- tools/sched_ext/scx_pair.c | 2 +- tools/sched_ext/scx_qmap.bpf.c | 2 +- tools/sched_ext/scx_qmap.c | 2 +- tools/sched_ext/scx_rusty/src/bpf/intf.h | 2 +- tools/sched_ext/scx_rusty/src/bpf/main.bpf.c | 4 ++-- tools/sched_ext/scx_simple.bpf.c | 2 +- tools/sched_ext/scx_simple.c | 2 +- tools/sched_ext/scx_userland.bpf.c | 2 +- tools/sched_ext/scx_userland.c | 2 +- 22 files changed, 22 insertions(+), 22 deletions(-) rename tools/sched_ext/{scx_common.bpf.h => include/scx/common.bpf.h} (100%) rename tools/sched_ext/{scx_common.h => include/scx/common.h} (100%) rename tools/sched_ext/{ => include/scx}/ravg.bpf.h (100%) rename tools/sched_ext/{ => include/scx}/ravg_impl.bpf.h (100%) rename tools/sched_ext/{ => include/scx}/user_exit_info.h (100%) diff --git a/tools/sched_ext/Makefile b/tools/sched_ext/Makefile index 43926befe86a4c..4cfdf4ece2b13f 100644 --- a/tools/sched_ext/Makefile +++ b/tools/sched_ext/Makefile @@ -91,7 +91,7 @@ endif CFLAGS += -g -O2 -rdynamic -pthread -Wall -Werror $(GENFLAGS) \ -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \ - -I$(TOOLSINCDIR) -I$(APIDIR) + -I$(TOOLSINCDIR) -I$(APIDIR) -I$(CURDIR)/include CARGOFLAGS := --release --target-dir $(OUTPUT_DIR) ifneq ($(CARGO_OFFLINE),) @@ -122,7 +122,7 @@ endef BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) \ $(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) \ - -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ + -I$(INCLUDE_DIR) -I$(CURDIR)/include -I$(APIDIR) \ -I../../include \ $(call get_sys_includes,$(CLANG)) \ -Wall -Wno-compare-distinct-pointer-types \ @@ -163,7 +163,7 @@ else $(Q)cp "$(VMLINUX_H)" $@ endif -$(SCXOBJ_DIR)/%.bpf.o: %.bpf.c $(INCLUDE_DIR)/vmlinux.h scx_common.bpf.h \ +$(SCXOBJ_DIR)/%.bpf.o: %.bpf.c $(INCLUDE_DIR)/vmlinux.h include/scx/common.bpf.h \ user_exit_info.h ravg.bpf.h ravg_impl.bpf.h \ | $(BPFOBJ) $(SCXOBJ_DIR) $(call msg,CLNG-BPF,,$(notdir $@)) @@ -179,7 +179,7 @@ $(INCLUDE_DIR)/%.bpf.skel.h: $(SCXOBJ_DIR)/%.bpf.o $(INCLUDE_DIR)/vmlinux.h $(BP $(Q)$(BPFTOOL) gen skeleton $(<:.o=.linked3.o) name $(subst .bpf.skel.h,,$(sched)) > $@ $(Q)$(BPFTOOL) gen subskeleton $(<:.o=.linked3.o) name $(subst .bpf.skel.h,,$(sched)) > $(@:.skel.h=.subskel.h) -SCX_COMMON_DEPS := scx_common.h user_exit_info.h | $(BINDIR) +SCX_COMMON_DEPS := include/scx/common.h include/scx/user_exit_info.h | $(BINDIR) ################ # C schedulers # diff --git a/tools/sched_ext/scx_common.bpf.h b/tools/sched_ext/include/scx/common.bpf.h similarity index 100% rename from tools/sched_ext/scx_common.bpf.h rename to tools/sched_ext/include/scx/common.bpf.h diff --git a/tools/sched_ext/scx_common.h b/tools/sched_ext/include/scx/common.h similarity index 100% rename from tools/sched_ext/scx_common.h rename to tools/sched_ext/include/scx/common.h diff --git a/tools/sched_ext/ravg.bpf.h b/tools/sched_ext/include/scx/ravg.bpf.h similarity index 100% rename from tools/sched_ext/ravg.bpf.h rename to tools/sched_ext/include/scx/ravg.bpf.h diff --git a/tools/sched_ext/ravg_impl.bpf.h b/tools/sched_ext/include/scx/ravg_impl.bpf.h similarity index 100% rename from tools/sched_ext/ravg_impl.bpf.h rename to tools/sched_ext/include/scx/ravg_impl.bpf.h diff --git a/tools/sched_ext/user_exit_info.h b/tools/sched_ext/include/scx/user_exit_info.h similarity index 100% rename from tools/sched_ext/user_exit_info.h rename to tools/sched_ext/include/scx/user_exit_info.h diff --git a/tools/sched_ext/scx_central.bpf.c b/tools/sched_ext/scx_central.bpf.c index de05779619878b..5faf0d22d32b6f 100644 --- a/tools/sched_ext/scx_central.bpf.c +++ b/tools/sched_ext/scx_central.bpf.c @@ -45,7 +45,7 @@ * Copyright (c) 2022 Tejun Heo * Copyright (c) 2022 David Vernet */ -#include "scx_common.bpf.h" +#include char _license[] SEC("license") = "GPL"; diff --git a/tools/sched_ext/scx_central.c b/tools/sched_ext/scx_central.c index 914993d31fa837..1092443230743b 100644 --- a/tools/sched_ext/scx_central.c +++ b/tools/sched_ext/scx_central.c @@ -11,7 +11,7 @@ #include #include #include -#include "scx_common.h" +#include #include "scx_central.bpf.skel.h" const char help_fmt[] = diff --git a/tools/sched_ext/scx_flatcg.bpf.c b/tools/sched_ext/scx_flatcg.bpf.c index 2db3d8d45e6837..79d625b3850454 100644 --- a/tools/sched_ext/scx_flatcg.bpf.c +++ b/tools/sched_ext/scx_flatcg.bpf.c @@ -43,7 +43,7 @@ * within by using nested weighted vtime scheduling by default. The * cgroup-internal scheduling can be switched to FIFO with the -f option. */ -#include "scx_common.bpf.h" +#include #include "user_exit_info.h" #include "scx_flatcg.h" diff --git a/tools/sched_ext/scx_flatcg.c b/tools/sched_ext/scx_flatcg.c index 886891540cbd8f..6a6e47c83ede7f 100644 --- a/tools/sched_ext/scx_flatcg.c +++ b/tools/sched_ext/scx_flatcg.c @@ -12,7 +12,7 @@ #include #include #include -#include "scx_common.h" +#include #include "scx_flatcg.h" #include "scx_flatcg.bpf.skel.h" diff --git a/tools/sched_ext/scx_layered/src/bpf/intf.h b/tools/sched_ext/scx_layered/src/bpf/intf.h index 8513779d5f5474..000f48b4d75022 100644 --- a/tools/sched_ext/scx_layered/src/bpf/intf.h +++ b/tools/sched_ext/scx_layered/src/bpf/intf.h @@ -18,7 +18,7 @@ typedef unsigned long long u64; typedef long long s64; #endif -#include "ravg.bpf.h" +#include enum consts { MAX_CPUS_SHIFT = 9, diff --git a/tools/sched_ext/scx_layered/src/bpf/main.bpf.c b/tools/sched_ext/scx_layered/src/bpf/main.bpf.c index d4714f89ee69f8..cd74769e952b92 100644 --- a/tools/sched_ext/scx_layered/src/bpf/main.bpf.c +++ b/tools/sched_ext/scx_layered/src/bpf/main.bpf.c @@ -1,5 +1,6 @@ /* Copyright (c) Meta Platforms, Inc. and affiliates. */ -#include "scx_common.bpf.h" +#include +#include #include "intf.h" #include @@ -27,7 +28,6 @@ static u32 preempt_cursor; #define trace(fmt, args...) do { if (debug > 1) bpf_printk(fmt, ##args); } while (0) #include "util.bpf.c" -#include "ravg_impl.bpf.h" struct user_exit_info uei; diff --git a/tools/sched_ext/scx_pair.bpf.c b/tools/sched_ext/scx_pair.bpf.c index 9c9cf97f4feeba..9da53c4b3e634a 100644 --- a/tools/sched_ext/scx_pair.bpf.c +++ b/tools/sched_ext/scx_pair.bpf.c @@ -115,7 +115,7 @@ * Copyright (c) 2022 Tejun Heo * Copyright (c) 2022 David Vernet */ -#include "scx_common.bpf.h" +#include #include "scx_pair.h" char _license[] SEC("license") = "GPL"; diff --git a/tools/sched_ext/scx_pair.c b/tools/sched_ext/scx_pair.c index 7c377f180724a6..693f095b8c6602 100644 --- a/tools/sched_ext/scx_pair.c +++ b/tools/sched_ext/scx_pair.c @@ -9,7 +9,7 @@ #include #include #include -#include "scx_common.h" +#include #include "scx_pair.h" #include "scx_pair.bpf.skel.h" diff --git a/tools/sched_ext/scx_qmap.bpf.c b/tools/sched_ext/scx_qmap.bpf.c index 090548ddd28980..831df3f644d5a8 100644 --- a/tools/sched_ext/scx_qmap.bpf.c +++ b/tools/sched_ext/scx_qmap.bpf.c @@ -22,7 +22,7 @@ * Copyright (c) 2022 Tejun Heo * Copyright (c) 2022 David Vernet */ -#include "scx_common.bpf.h" +#include char _license[] SEC("license") = "GPL"; diff --git a/tools/sched_ext/scx_qmap.c b/tools/sched_ext/scx_qmap.c index 0b85067703fb82..d817115c0b0a84 100644 --- a/tools/sched_ext/scx_qmap.c +++ b/tools/sched_ext/scx_qmap.c @@ -10,7 +10,7 @@ #include #include #include -#include "scx_common.h" +#include #include "scx_qmap.bpf.skel.h" const char help_fmt[] = diff --git a/tools/sched_ext/scx_rusty/src/bpf/intf.h b/tools/sched_ext/scx_rusty/src/bpf/intf.h index 54d28696ac5a72..f295695102051c 100644 --- a/tools/sched_ext/scx_rusty/src/bpf/intf.h +++ b/tools/sched_ext/scx_rusty/src/bpf/intf.h @@ -19,7 +19,7 @@ typedef unsigned int u32; typedef unsigned long long u64; #endif -#include "ravg.bpf.h" +#include enum consts { MAX_CPUS = 512, diff --git a/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c b/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c index c82ad8973d96aa..befaba957105eb 100644 --- a/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c +++ b/tools/sched_ext/scx_rusty/src/bpf/main.bpf.c @@ -35,8 +35,8 @@ * task weight, dom mask and current dom in the task_data map and executes the * load balance based on userspace populating the lb_data map. */ -#include "scx_common.bpf.h" -#include "ravg_impl.bpf.h" +#include +#include #include "intf.h" #include diff --git a/tools/sched_ext/scx_simple.bpf.c b/tools/sched_ext/scx_simple.bpf.c index 56b589d7f6630e..eeb7414883a677 100644 --- a/tools/sched_ext/scx_simple.bpf.c +++ b/tools/sched_ext/scx_simple.bpf.c @@ -20,7 +20,7 @@ * Copyright (c) 2022 Tejun Heo * Copyright (c) 2022 David Vernet */ -#include "scx_common.bpf.h" +#include char _license[] SEC("license") = "GPL"; diff --git a/tools/sched_ext/scx_simple.c b/tools/sched_ext/scx_simple.c index b09b76be0a2670..5c5589770a2fcf 100644 --- a/tools/sched_ext/scx_simple.c +++ b/tools/sched_ext/scx_simple.c @@ -9,7 +9,7 @@ #include #include #include -#include "scx_common.h" +#include #include "scx_simple.bpf.skel.h" const char help_fmt[] = diff --git a/tools/sched_ext/scx_userland.bpf.c b/tools/sched_ext/scx_userland.bpf.c index 9e107a874a92d4..f2791a6aecc8b8 100644 --- a/tools/sched_ext/scx_userland.bpf.c +++ b/tools/sched_ext/scx_userland.bpf.c @@ -21,7 +21,7 @@ * Copyright (c) 2022 David Vernet */ #include -#include "scx_common.bpf.h" +#include #include "scx_userland.h" char _license[] SEC("license") = "GPL"; diff --git a/tools/sched_ext/scx_userland.c b/tools/sched_ext/scx_userland.c index 7b5322b3f1c85a..fef028a1756e0b 100644 --- a/tools/sched_ext/scx_userland.c +++ b/tools/sched_ext/scx_userland.c @@ -27,7 +27,7 @@ #include #include -#include "scx_common.h" +#include #include "scx_userland.h" #include "scx_userland.bpf.skel.h" From d6bd20a939a930a650a3041e155088ddf5636b40 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Sun, 3 Dec 2023 14:32:53 -1000 Subject: [PATCH 9/9] scx: More include path and build updates --- tools/sched_ext/Makefile | 6 +++--- tools/sched_ext/gnu/stubs.h | 1 - tools/sched_ext/include/bpf-compat/gnu/stubs.h | 11 +++++++++++ tools/sched_ext/scx_flatcg.bpf.c | 1 - tools/sched_ext/scx_layered/Cargo.toml | 4 ++-- tools/sched_ext/scx_rusty/Cargo.toml | 4 ++-- 6 files changed, 18 insertions(+), 9 deletions(-) delete mode 100644 tools/sched_ext/gnu/stubs.h create mode 100644 tools/sched_ext/include/bpf-compat/gnu/stubs.h diff --git a/tools/sched_ext/Makefile b/tools/sched_ext/Makefile index 4cfdf4ece2b13f..b9e42771a4c502 100644 --- a/tools/sched_ext/Makefile +++ b/tools/sched_ext/Makefile @@ -122,7 +122,8 @@ endef BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) \ $(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) \ - -I$(INCLUDE_DIR) -I$(CURDIR)/include -I$(APIDIR) \ + -I$(CURDIR)/include -I$(CURDIR)/include/bpf-compat \ + -I$(INCLUDE_DIR) -I$(APIDIR) \ -I../../include \ $(call get_sys_includes,$(CLANG)) \ -Wall -Wno-compare-distinct-pointer-types \ @@ -163,8 +164,7 @@ else $(Q)cp "$(VMLINUX_H)" $@ endif -$(SCXOBJ_DIR)/%.bpf.o: %.bpf.c $(INCLUDE_DIR)/vmlinux.h include/scx/common.bpf.h \ - user_exit_info.h ravg.bpf.h ravg_impl.bpf.h \ +$(SCXOBJ_DIR)/%.bpf.o: %.bpf.c $(INCLUDE_DIR)/vmlinux.h include/scx/*.h \ | $(BPFOBJ) $(SCXOBJ_DIR) $(call msg,CLNG-BPF,,$(notdir $@)) $(Q)$(CLANG) $(BPF_CFLAGS) -target bpf -c $< -o $@ diff --git a/tools/sched_ext/gnu/stubs.h b/tools/sched_ext/gnu/stubs.h deleted file mode 100644 index 719225b1662697..00000000000000 --- a/tools/sched_ext/gnu/stubs.h +++ /dev/null @@ -1 +0,0 @@ -/* dummy .h to trick /usr/include/features.h to work with 'clang -target bpf' */ diff --git a/tools/sched_ext/include/bpf-compat/gnu/stubs.h b/tools/sched_ext/include/bpf-compat/gnu/stubs.h new file mode 100644 index 00000000000000..ad7d139ce907b2 --- /dev/null +++ b/tools/sched_ext/include/bpf-compat/gnu/stubs.h @@ -0,0 +1,11 @@ +/* + * Dummy gnu/stubs.h. clang can end up including /usr/include/gnu/stubs.h when + * compiling BPF files although its content doesn't play any role. The file in + * turn includes stubs-64.h or stubs-32.h depending on whether __x86_64__ is + * defined. When compiling a BPF source, __x86_64__ isn't set and thus + * stubs-32.h is selected. However, the file is not there if the system doesn't + * have 32bit glibc devel package installed leading to a build failure. + * + * The problem is worked around by making this file available in the include + * search paths before the system one when building BPF. + */ diff --git a/tools/sched_ext/scx_flatcg.bpf.c b/tools/sched_ext/scx_flatcg.bpf.c index 79d625b3850454..84a60d7e4024ba 100644 --- a/tools/sched_ext/scx_flatcg.bpf.c +++ b/tools/sched_ext/scx_flatcg.bpf.c @@ -44,7 +44,6 @@ * cgroup-internal scheduling can be switched to FIFO with the -f option. */ #include -#include "user_exit_info.h" #include "scx_flatcg.h" char _license[] SEC("license") = "GPL"; diff --git a/tools/sched_ext/scx_layered/Cargo.toml b/tools/sched_ext/scx_layered/Cargo.toml index 1de594354ed0ca..19dd0243a9f2a6 100644 --- a/tools/sched_ext/scx_layered/Cargo.toml +++ b/tools/sched_ext/scx_layered/Cargo.toml @@ -16,13 +16,13 @@ lazy_static = "1.4" libbpf-rs = "0.21" libc = "0.2" log = "0.4" -scx_utils = "0.2" +scx_utils = "0.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" simplelog = "0.12" [build-dependencies] -scx_utils = "0.2" +scx_utils = "0.3" [features] enable_backtrace = [] diff --git a/tools/sched_ext/scx_rusty/Cargo.toml b/tools/sched_ext/scx_rusty/Cargo.toml index 23d37b70e4eb1a..309643687d0c6f 100644 --- a/tools/sched_ext/scx_rusty/Cargo.toml +++ b/tools/sched_ext/scx_rusty/Cargo.toml @@ -17,11 +17,11 @@ libbpf-rs = "0.21.0" libc = "0.2.137" log = "0.4.17" ordered-float = "3.4.0" -scx_utils = "0.2" +scx_utils = "0.3" simplelog = "0.12.0" [build-dependencies] -scx_utils = "0.2" +scx_utils = "0.3" [features] enable_backtrace = []