From 9ba2bbe8cd8dbedd3653c1a30775ef2f5660bf2a Mon Sep 17 00:00:00 2001 From: David Venhoff Date: Fri, 12 Sep 2025 10:52:11 +0200 Subject: [PATCH] Fix compilation of embench The separation did not work before, somehow. The new approach of redefining the function names, while hacky, definitely works though. --- benchmark_all.sh | 4 +- client/build.rs | 83 +++++++++++--------------- client/src/bin/client_combined.rs | 4 +- client/src/bin/client_crc_32.rs | 4 +- client/src/bin/client_edn.rs | 4 +- client/src/bin/client_huffbench.rs | 4 +- client/src/bin/client_matmult-int.rs | 4 +- client/src/bin/client_md5.rs | 4 +- client/src/bin/client_minver.rs | 4 +- client/src/bin/client_mont64.rs | 4 +- client/src/bin/client_nbody.rs | 4 +- client/src/bin/client_nettle-aes.rs | 4 +- client/src/bin/client_nettle-sha256.rs | 4 +- client/src/bin/client_nsichneu.rs | 4 +- client/src/bin/client_picojpeg.rs | 4 +- client/src/bin/client_primecount.rs | 4 +- client/src/bin/client_slre.rs | 4 +- client/src/bin/client_st.rs | 4 +- client/src/bin/client_statemate.rs | 4 +- client/src/bin/client_tarfind.rs | 4 +- client/src/bin/client_ud.rs | 4 +- client/src/lib.rs | 25 +++++--- 22 files changed, 92 insertions(+), 96 deletions(-) diff --git a/benchmark_all.sh b/benchmark_all.sh index bceedf8..180abc5 100755 --- a/benchmark_all.sh +++ b/benchmark_all.sh @@ -1,3 +1,5 @@ +set -e + cargo build --workspace --release echo "Disabling turbo..." @@ -13,4 +15,4 @@ done echo 0 | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo -echo "Done!" \ No newline at end of file +echo "Done!" diff --git a/client/build.rs b/client/build.rs index e10ea9c..a516561 100644 --- a/client/build.rs +++ b/client/build.rs @@ -44,70 +44,57 @@ fn main() { let embench_dir = PathBuf::from_str(&format!("{out_dir}/embench-iot")).unwrap(); let mut generated_code = String::new(); - let mut benchmark_functions = vec![]; for &benchmark_file in BENCHMARKS { let path = embench_dir.join(benchmark_file); let dir = path.parent().unwrap(); - let benchmark_name = dir.file_name().unwrap().to_string_lossy(); - let (wrapper_path, function_name) = make_wrapper(&benchmark_name, &out_dir); - cc::Build::new() + let benchmark_name = dir.file_name().unwrap().to_string_lossy().replace("-", "_"); + println!("cargo:warning={path:?}"); + + let mut build = cc::Build::new(); + build .include(embench_dir.join("support")) - .include(dir) + .file(embench_dir.join("support/beebsc.c")) .file(&path) - .file(&wrapper_path) - .define("CPU_MHZ", "2700") // FIXME: Use correct CPU frequency - .compile(&format!("libembench_{benchmark_name}.a")); + // Just use some roughly accurate value, it just influences how long each benchmark runs + .define("CPU_MHZ", "2700"); + + // Rename all functions that are defined multiple times to avoid collisions + let colliding_functions = [ + "benchmark", + "initialise_benchmark", + "warm_caches", + "verify_benchmark", + ]; + for name in colliding_functions { + build.define(name, format!("{benchmark_name}_{name}").as_str()); + } + + build.compile(&format!("libembench_{benchmark_name}.a")); write!( generated_code, r#" + #[allow(non_camel_case_types)] pub struct {benchmark_name}; + unsafe extern "C" {{ - fn {function_name}(); - }}"# - ) - .unwrap(); - benchmark_functions.push(function_name); - } + fn {benchmark_name}_benchmark(); + fn {benchmark_name}_initialise_benchmark(); + }} - writeln!( - generated_code, - "#[allow(non_camel_case_types)] #[derive(Clone, Copy)] pub enum Benchmark {{" - ) - .unwrap(); - for benchmark_name in &benchmark_functions { - writeln!(generated_code, " {benchmark_name},").unwrap(); - } - writeln!(generated_code, "}}").unwrap(); + impl Benchmark for {benchmark_name} {{ + unsafe fn init() {{ + unsafe {{ {benchmark_name}_initialise_benchmark(); }} + }} - writeln!(generated_code, "pub unsafe fn run(benchmark: Benchmark) {{").unwrap(); - writeln!(generated_code, "match benchmark {{").unwrap(); - for benchmark_name in benchmark_functions { - writeln!( - generated_code, - " Benchmark::{benchmark_name} => unsafe {{ {benchmark_name}() }}," + unsafe fn benchmark() {{ + unsafe {{ {benchmark_name}_benchmark(); }} + }} + }} + "# ) .unwrap(); } - writeln!(generated_code, "}}").unwrap(); - writeln!(generated_code, "}}").unwrap(); fs::write(format!("{out_dir}/libembench-sys.rs"), generated_code).unwrap(); } - -fn make_wrapper(benchmark_name: &str, out_dir: &str) -> (PathBuf, String) { - let benchmark_name = benchmark_name.replace("-", "_"); - let function_name = format!("benchmark_{benchmark_name}"); - let code = format!( - r#" - extern int benchmark(void); - - void {function_name}(void) {{ - benchmark(); - }} - "# - ); - let path = PathBuf::from(out_dir).join(format!("{benchmark_name}_wrapper.c")); - fs::write(&path, code).unwrap(); - (path, function_name) -} diff --git a/client/src/bin/client_combined.rs b/client/src/bin/client_combined.rs index ab40386..c125de3 100644 --- a/client/src/bin/client_combined.rs +++ b/client/src/bin/client_combined.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_sglib_combined); + program::(); } diff --git a/client/src/bin/client_crc_32.rs b/client/src/bin/client_crc_32.rs index 3ba68c8..3b26e1c 100644 --- a/client/src/bin/client_crc_32.rs +++ b/client/src/bin/client_crc_32.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_crc32); + program::(); } diff --git a/client/src/bin/client_edn.rs b/client/src/bin/client_edn.rs index f8b3098..c380a26 100644 --- a/client/src/bin/client_edn.rs +++ b/client/src/bin/client_edn.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_edn); + program::(); } diff --git a/client/src/bin/client_huffbench.rs b/client/src/bin/client_huffbench.rs index b1dc108..2bfa901 100644 --- a/client/src/bin/client_huffbench.rs +++ b/client/src/bin/client_huffbench.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_huffbench); + program::(); } diff --git a/client/src/bin/client_matmult-int.rs b/client/src/bin/client_matmult-int.rs index eb95f73..2e063d7 100644 --- a/client/src/bin/client_matmult-int.rs +++ b/client/src/bin/client_matmult-int.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_matmult_int); + program::(); } diff --git a/client/src/bin/client_md5.rs b/client/src/bin/client_md5.rs index 7771587..34bae8c 100644 --- a/client/src/bin/client_md5.rs +++ b/client/src/bin/client_md5.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_md5sum); + program::(); } diff --git a/client/src/bin/client_minver.rs b/client/src/bin/client_minver.rs index 7a78a2d..9ab2245 100644 --- a/client/src/bin/client_minver.rs +++ b/client/src/bin/client_minver.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_minver); + program::(); } diff --git a/client/src/bin/client_mont64.rs b/client/src/bin/client_mont64.rs index e3a28ce..047b573 100644 --- a/client/src/bin/client_mont64.rs +++ b/client/src/bin/client_mont64.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_aha_mont64); + program::(); } diff --git a/client/src/bin/client_nbody.rs b/client/src/bin/client_nbody.rs index ce18c13..319cc7c 100644 --- a/client/src/bin/client_nbody.rs +++ b/client/src/bin/client_nbody.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_nbody); + program::(); } diff --git a/client/src/bin/client_nettle-aes.rs b/client/src/bin/client_nettle-aes.rs index 65c6580..61d27df 100644 --- a/client/src/bin/client_nettle-aes.rs +++ b/client/src/bin/client_nettle-aes.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_nettle_aes); + program::(); } diff --git a/client/src/bin/client_nettle-sha256.rs b/client/src/bin/client_nettle-sha256.rs index af7aa28..8655c85 100644 --- a/client/src/bin/client_nettle-sha256.rs +++ b/client/src/bin/client_nettle-sha256.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_nettle_sha256); + program::(); } diff --git a/client/src/bin/client_nsichneu.rs b/client/src/bin/client_nsichneu.rs index 9bd67eb..99baec4 100644 --- a/client/src/bin/client_nsichneu.rs +++ b/client/src/bin/client_nsichneu.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_nsichneu); + program::(); } diff --git a/client/src/bin/client_picojpeg.rs b/client/src/bin/client_picojpeg.rs index 0617cea..0825504 100644 --- a/client/src/bin/client_picojpeg.rs +++ b/client/src/bin/client_picojpeg.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_picojpeg); + program::(); } diff --git a/client/src/bin/client_primecount.rs b/client/src/bin/client_primecount.rs index dd642c7..5375929 100644 --- a/client/src/bin/client_primecount.rs +++ b/client/src/bin/client_primecount.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_primecount); + program::(); } diff --git a/client/src/bin/client_slre.rs b/client/src/bin/client_slre.rs index 870344d..6b8dc86 100644 --- a/client/src/bin/client_slre.rs +++ b/client/src/bin/client_slre.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_slre); + program::(); } diff --git a/client/src/bin/client_st.rs b/client/src/bin/client_st.rs index 858de2f..c100977 100644 --- a/client/src/bin/client_st.rs +++ b/client/src/bin/client_st.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_st); + program::(); } diff --git a/client/src/bin/client_statemate.rs b/client/src/bin/client_statemate.rs index 31cbc7c..3ba4a46 100644 --- a/client/src/bin/client_statemate.rs +++ b/client/src/bin/client_statemate.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_statemate); + program::(); } diff --git a/client/src/bin/client_tarfind.rs b/client/src/bin/client_tarfind.rs index deded3c..01b4a67 100644 --- a/client/src/bin/client_tarfind.rs +++ b/client/src/bin/client_tarfind.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_tarfind); + program::(); } diff --git a/client/src/bin/client_ud.rs b/client/src/bin/client_ud.rs index aeb7c73..5acd0c7 100644 --- a/client/src/bin/client_ud.rs +++ b/client/src/bin/client_ud.rs @@ -1,5 +1,5 @@ -use client::{Benchmark, program}; +use client::program; fn main() { - program(Benchmark::benchmark_ud); + program::(); } diff --git a/client/src/lib.rs b/client/src/lib.rs index 6e809c1..a8e51a8 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1,6 +1,12 @@ use std::arch::asm; -mod embench_sys { +pub mod embench_sys { + pub trait Benchmark { + unsafe fn init(); + + unsafe fn benchmark(); + } + include!(concat!(env!("OUT_DIR"), "/libembench-sys.rs")); } @@ -12,14 +18,15 @@ fn ptwrite(val: u32) { } } -pub fn program(benchmark: embench_sys::Benchmark) { - let start = std::time::Instant::now(); - for _ in std::hint::black_box(0..100) { - ptwrite(42); - unsafe { - embench_sys::run(benchmark); - } - ptwrite(43); +pub fn program() { + unsafe { + B::init(); } + let start = std::time::Instant::now(); + ptwrite(42); + unsafe { + B::benchmark(); + } + ptwrite(43); println!("{}", start.elapsed().as_secs_f64()); }