Skip to content

Commit

Permalink
tango_init() is now exported by client crate only
Browse files Browse the repository at this point in the history
It allows to get rid of dummy entrypoints in nontango executables
  • Loading branch information
bazhenov committed Dec 19, 2023
1 parent d6d854a commit 72bc08f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
6 changes: 0 additions & 6 deletions examples/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ use test_funcs::{factorial, str_take, sum, RandomSubstring};

mod test_funcs;

/// Because benchmarks are builded with linker flag -rdynamic there should be dummy library entrypoint defined
/// in all benchmarks. This is only needed when two benchmarks harnesses are used in a single crate.
mod dummy_entrypoint {
tango_bench::tango_benchmarks!([]);
}

fn sum_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("arithmetic");

Expand Down
30 changes: 7 additions & 23 deletions tango-bench/src/dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ impl<'l> Spi<'l> {

/// State which holds the information about list of benchmarks and which one is selected.
/// Used in FFI API (`tango_*` functions).
struct State {
benchmarks: Vec<Box<dyn MeasureTarget>>,
selected_function: usize,
pub struct State {
pub benchmarks: Vec<Box<dyn MeasureTarget>>,
pub selected_function: usize,
}

impl State {
Expand All @@ -107,12 +107,12 @@ impl State {
/// Tango execution model implies simultaneous exectution of the code from two binaries. To achive that
/// Tango benchmark is compiled in a way that executable is also a shared library (.dll, .so, .dylib). This
/// way two executables can coexist in the single process at the same time.
mod ffi {
pub mod ffi {
use super::*;
use std::{os::raw::c_char, ptr::null, usize};

/// Signature types of all FFI API functions
type InitFn = unsafe extern "C" fn();
pub type InitFn = unsafe extern "C" fn();
type CountFn = unsafe extern "C" fn() -> usize;
type GetTestNameFn = unsafe extern "C" fn(*mut *const c_char, *mut usize);
type SelectFn = unsafe extern "C" fn(usize);
Expand All @@ -128,7 +128,6 @@ mod ffi {
mod type_check {
use super::*;

const TANGO_INIT: InitFn = tango_init;
const TANGO_COUNT: CountFn = tango_count;
const TANGO_SELECT: SelectFn = tango_select;
const TANGO_GET_TEST_NAME: GetTestNameFn = tango_get_test_name;
Expand All @@ -138,23 +137,8 @@ mod ffi {
const TANGO_FREE: FreeFn = tango_free;
}

extern "Rust" {
/// Each benchmark executable should define this function for the harness to load all benchmarks
fn __tango_create_benchmarks() -> Vec<Box<dyn MeasureTarget>>;
}

/// Global state of the benchmarking library
static mut STATE: Option<State> = None;

#[no_mangle]
unsafe extern "C" fn tango_init() {
if STATE.is_none() {
STATE = Some(State {
benchmarks: __tango_create_benchmarks(),
selected_function: 0,
});
}
}
pub static mut STATE: Option<State> = None;

#[no_mangle]
unsafe extern "C" fn tango_count() -> usize {
Expand Down Expand Up @@ -241,7 +225,7 @@ mod ffi {

impl VTable for SelfVTable {
fn init(&self) {
unsafe { tango_init() }
// In executable mode `tango_init` is already called by the main function
}

fn count(&self) -> usize {
Expand Down
21 changes: 17 additions & 4 deletions tango-bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,22 @@ pub enum Error {
#[macro_export]
macro_rules! tango_benchmarks {
($($func_expr:expr),+) => {

/// Type checking tango_init() function
const TANGO_INIT: $crate::dylib::ffi::InitFn = tango_init;

/// Exported function for initializing the benchmark harness
#[no_mangle]
pub fn __tango_create_benchmarks() -> Vec<Box<dyn $crate::MeasureTarget>> {
let mut benchmarks = vec![];
$(benchmarks.extend($crate::IntoBenchmarks::into_benchmarks($func_expr));)*
benchmarks
unsafe extern "C" fn tango_init() {
use $crate::dylib::{ffi::STATE, State};
if STATE.is_none() {
let mut benchmarks = vec![];
$(benchmarks.extend($crate::IntoBenchmarks::into_benchmarks($func_expr));)*
STATE = Some(State {
benchmarks,
selected_function: 0,
});
}
}
};
}
Expand All @@ -88,6 +99,8 @@ macro_rules! tango_benchmarks {
macro_rules! tango_main {
($settings:expr) => {
fn main() -> $crate::cli::Result<std::process::ExitCode> {
// Initialize Tango for SelfVTable usage
unsafe { tango_init() };
$crate::cli::run($settings)
}
};
Expand Down

0 comments on commit 72bc08f

Please sign in to comment.