Skip to content

Commit

Permalink
get the C code building
Browse files Browse the repository at this point in the history
  • Loading branch information
lquenti committed Feb 22, 2024
1 parent 8b5650c commit e9dca70
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
3 changes: 1 addition & 2 deletions blackheap-benchmarker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ name = "blackheap-benchmarker"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = "0.2"

[build-dependencies]
cc = "1.0"
2 changes: 1 addition & 1 deletion blackheap-benchmarker/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
cc::Build::new()
.file("src/c_code/benchmarker.c")
.file("src/c_code/benchmarker_internal.c")
.compile("c_benchmarker");
}
48 changes: 48 additions & 0 deletions blackheap-benchmarker/src/c_code/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
// needed for autogenerated code by bindgen
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]

mod benchmarker;

use std::ffi::CString;

use benchmarker::{access_pattern_ACCESS_PATTERN_SEQUENTIAL, benchmark_config, error_codes_ERROR_CODES_SUCCESS};

fn run_benchmark() {
let filepath = CString::new("/tmp/test_file.bin").expect("CString::new failed");
let config = benchmark_config {
filepath: filepath.as_ptr(),
memory_buffer_in_bytes: 1024,
file_size_in_bytes: 1024 * 10,
access_size_in_bytes: 128,
number_of_io_op_tests: 10,
access_pattern_in_memory: access_pattern_ACCESS_PATTERN_SEQUENTIAL,
access_pattern_in_file: access_pattern_ACCESS_PATTERN_SEQUENTIAL,
is_read_operation: true,
prepare_file_size: true,
drop_cache_first: false,
do_reread: false,
restrict_free_ram_to: 0,
};

unsafe {
let results = benchmarker::benchmark_file(&config);

if results.res == error_codes_ERROR_CODES_SUCCESS {
println!("Benchmark completed successfully.");
println!("Results length: {}", results.length);

// Access the durations array
let durations_slice = std::slice::from_raw_parts(results.durations, results.length);
for (i, &duration) in durations_slice.iter().enumerate() {
println!("Duration for operation {}: {} seconds", i, duration);
}

// Free the durations array allocated by the C code
libc::free(results.durations as *mut libc::c_void);
} else {
println!("Benchmark failed with error code: {:?}", results.res);
}
}
}

0 comments on commit e9dca70

Please sign in to comment.