Skip to content

Commit

Permalink
rust support
Browse files Browse the repository at this point in the history
  • Loading branch information
dlqs committed Jul 9, 2024
1 parent a639a3b commit 4bae506
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
cirronlib.so
main
97 changes: 97 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"

[dependencies]
libloading = "0.8"
cty = "0.2.2"
64 changes: 64 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use libloading::{Library, Symbol};
use cty;

const CIRRONLIB: &str = "./cirronlib.so";
// this means its in rust/cirronlib.so

#[repr(C)]
#[derive(Default)]
#[derive(Debug)]
pub struct Counter {
pub time_enabled_ns: cty::c_ulong,
pub instruction_count: cty::c_ulong,
pub branch_misses: cty::c_ulong,
pub page_faults: cty::c_ulong
}

fn get_cirronlib() -> Library {
unsafe {
libloading::Library::new(CIRRONLIB)
.expect("Failed to load lib")
}
}

fn start() -> Result<u32, Box<dyn std::error::Error>> {
let lib = get_cirronlib();
unsafe {
let start: Symbol<unsafe extern fn() -> u32> = lib.get(b"start").unwrap();
Ok(start())
}
}

fn end(fd: u32, counter: &mut Counter) -> Result<u32, Box<dyn std::error::Error>> {
let lib = get_cirronlib();

unsafe {
let end: Symbol<unsafe extern fn(u32, *mut Counter) -> u32> = lib.get(b"end").unwrap();
Ok(end(fd, counter))
}
}

pub fn main() {

let mut counter = Counter { ..Default::default() };

let result = start().unwrap();
println!("Hello");
end(result, &mut counter).expect("Failed to start");

println!("{:#?}", counter);
println!("Goodbye!");
}

// Need sudo priveleges
// sudo cargo test
#[test]
fn test_start() {
assert_eq!(start().expect("Failed to start"), 0);
}

#[test]
fn test_end() {
let mut counter = Counter { ..Default::default() };
assert_eq!(end(0, &mut counter).expect("Failed to start"), 0);
}

0 comments on commit 4bae506

Please sign in to comment.