Commit 254dfd5a authored by Sven Liefgen's avatar Sven Liefgen
Browse files

Wrap function calls into macros

Using macros allows the macro invocation with xlap disabled and without
definitions generated from the xlap.yml.
parent 23bb415e
......@@ -84,7 +84,7 @@ fn main() {
)
.unwrap();
println!("cargo:rerun-if-changed={}", xlap);
} else if !dest_path.exists() {
} else if cfg!(feature = "xlap") && !dest_path.exists() {
panic!("Specify the absolute path to your `xlap.yml` using the `XLAP` environment variable on the first build");
}
println!("cargo:rerun-if-changed=build.rs");
......
......@@ -18,6 +18,7 @@ pub unsafe fn current_cycle() -> u64 {
0
}
#[cfg(feature = "xlap")]
include!(concat!(env!("OUT_DIR"), "/xlap_generated.rs"));
//const TS_ROWS: usize = 2;
......@@ -60,64 +61,146 @@ pub struct TimestampTable {
#[cfg(feature = "xlap")]
static mut ZERO: Option<Instant> = None;
#[cfg(feature = "xlap")]
#[macro_export]
/// Update the clock and cycle value of a timestamp by setting them to en explicit value
macro_rules! set_timestamp {
( $kind:expr, $row: expr, $id:expr, $stamp:expr ) => {
#[cfg(feature = "xlap")]
$crate::set_timestamp($kind, $row, $id, $stamp);
};
}
#[cfg(feature = "xlap")]
#[inline(always)]
pub unsafe fn set_timestamp(kind: Kind, row: usize, id: TimestampId, (t, c): (Instant, u64)) {
set_timestamp_clock(kind, row, id, t);
set_timestamp_cycle(kind, row, id, c);
}
#[cfg(feature = "xlap")]
#[macro_export]
/// Update the clock value of a timestamp by setting it to an explicit value
macro_rules! set_timestamp_clock {
( $kind:expr, $row:expr, $id:expr, $value:expr ) => {
#[cfg(feature = "xlap")]
$crate::set_timestamp_clock($kind, $row, $id, $value);
};
}
#[cfg(feature = "xlap")]
#[inline(always)]
pub unsafe fn set_timestamp_clock(kind: Kind, row: usize, id: TimestampId, value: Instant) {
(*XLAP[kind as usize].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].t =
value;
}
#[cfg(feature = "xlap")]
#[macro_export]
/// Update the clock value of a cyclestamp by setting it to an explicit value
macro_rules! set_timestamp_cycle {
( $kind:expr, $row:expr, $id:expr, $value:expr ) => {
#[cfg(feature = "xlap")]
$crate::set_timestamp_cycle($kind, $row, $id, $value);
};
}
#[cfg(feature = "xlap")]
#[inline(always)]
pub unsafe fn set_timestamp_cycle(kind: Kind, row: usize, id: TimestampId, value: u64) {
(*XLAP[kind as usize].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].c =
value;
}
#[cfg(feature = "xlap")]
#[macro_export]
/// Update the clock value of a timestamp
macro_rules! timestamp_clock {
( $kind:expr, $row:expr, $id:expr ) => {
#[cfg(feature = "xlap")]
$crate::timestamp_clock($kind, $row, $id);
};
}
#[cfg(feature = "xlap")]
#[inline(always)]
pub unsafe fn timestamp_clock(kind: Kind, row: usize, id: TimestampId) {
(*XLAP[kind as usize].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].t =
Instant::now();
}
#[cfg(feature = "xlap")]
#[macro_export]
/// Update the cycle value of a timestamp
macro_rules! timestamp_cycle {
( $kind:expr, $row:expr, $id:expr ) => {
#[cfg(feature = "xlap")]
$crate::timestamp_cycle($kind, $row, $id);
};
}
#[cfg(feature = "xlap")]
#[inline(always)]
pub unsafe fn timestamp_cycle(kind: Kind, row: usize, id: TimestampId) {
(*XLAP[kind as usize].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].c =
current_cycle();
}
#[cfg(feature = "xlap")]
#[macro_export]
/// Create a clock value without saving it to a table
macro_rules! get_timestamp_clock {
() => {
$crate::get_timestamp_clock()
};
}
#[inline(always)]
pub fn get_timestamp_clock() -> Instant {
Instant::now()
}
#[cfg(feature = "xlap")]
#[macro_export]
/// Create a cycle value without saving it to a table
macro_rules! get_timestamp_cycle {
() => {
$crate::get_timestamp_cycle()
};
}
#[cfg(feature = "xlap")]
#[inline(always)]
pub fn get_timestamp_cycle() -> u64 {
unsafe { current_cycle() }
}
#[cfg(feature = "xlap")]
#[cfg(not(feature = "xlap"))]
#[inline(always)]
pub fn get_timestamp_cycle() -> u64 {
0
}
#[macro_export]
/// Install a timestamp table
///
/// Allocate memory for the empty timestamp tables and store pointers in the XLAP static
macro_rules! init {
() => {
#[cfg(feature = "xlap")]
$crate::init();
};
}
#[macro_export]
macro_rules! map_kind {
( $src:expr, $($type:expr => $kind:expr),* ) => {
{
#[cfg(feature = "xlap")]
match $src {
$($type => $kind),*,
_ => unreachable!(),
}
#[cfg(not(feature = "xlap"))]
$crate::Kind::NoKind
}
}
}
#[cfg(feature = "xlap")]
pub unsafe fn init() {
ZERO = Some(Instant::now());
for ptr in XLAP.iter() {
......@@ -130,14 +213,30 @@ pub unsafe fn init() {
}
}
#[macro_export]
/// Dump the collected timestamps to a csv file
macro_rules! dump_all {
( $file:expr ) => {
#[cfg(feature = "xlap")]
$crate::TimestampTable::dump_all($file)
};
}
#[macro_export]
/// Dump the collected timestamps of the given tables to a csv file
macro_rules! dump {
( $file:expr, $tables:expr ) => {
#[cfg(feature = "xlap")]
$crate::TimestampTable::dump($file, $tables)
};
}
#[cfg(feature = "xlap")]
impl TimestampTable {
/// Dump the collected timestamps to a csv file
pub unsafe fn dump_all(file: &str) -> std::io::Result<()> {
Self::dump(file, Kind::iter().collect::<Vec<_>>().as_slice())
}
/// Dump the collected timestamps of the given tables to a csv file
pub unsafe fn dump(file: &str, tables: &[Kind]) -> std::io::Result<()> {
let mut file = std::fs::File::create(file)?;
Self::dump_header(&mut file)?;
......@@ -171,72 +270,27 @@ impl TimestampTable {
pub struct TimestampTable;
#[cfg(not(feature = "xlap"))]
pub struct Kind;
#[cfg(not(feature = "xlap"))]
pub struct TimestampId;
#[cfg(not(feature = "xlap"))]
/// Update the clock and cycle value of a timestamp by setting them to en explicit value
#[inline(always)]
pub unsafe fn set_timestamp(_: Kind, _: usize, _: TimestampId, _: (Instant, u64)) {}
#[cfg(not(feature = "xlap"))]
/// Update the clock value of a timestamp by setting it to an explicit value
#[inline(always)]
pub unsafe fn set_timestamp_clock(_: Kind, _: usize, _: TimestampId, _: Instant) {}
#[cfg(not(feature = "xlap"))]
/// Update the clock value of a cyclestamp by setting it to an explicit value
#[inline(always)]
pub unsafe fn set_timestamp_cycle(_: Kind, _: usize, _: TimestampId, _: u64) {}
#[cfg(not(feature = "xlap"))]
/// Update the clock value of a timestamp
#[inline(always)]
pub unsafe fn timestamp_clock(_: Kind, _: usize, _: TimestampId) {}
#[cfg(not(feature = "xlap"))]
/// Update the cycle value of a timestamp
#[inline(always)]
pub unsafe fn timestamp_cycle(_: Kind, _: usize, _: TimestampId) {}
#[cfg(not(feature = "xlap"))]
/// Create a clock value without saving it to a table
#[inline(always)]
pub fn get_timestamp_clock() -> Instant {
Instant::now()
pub enum Kind {
NoKind,
}
#[cfg(not(feature = "xlap"))]
/// Create a cycle value without saving it to a table
#[inline(always)]
pub fn get_timestamp_cycle() -> u64 {
0
}
#[cfg(not(feature = "xlap"))]
/// Install a timestamp table
///
/// Allocate memory for the empty timestamp tables and store pointers in the XLAP static
pub unsafe fn init() {}
pub enum TimestampId {}
#[cfg(not(feature = "xlap"))]
impl TimestampTable {
/// Dump the collected timestamps to a csv file
pub unsafe fn dump_all(file: &str) -> std::io::Result<()> {
Self::dump(file, &[])
pub unsafe fn dump_all(_file: &str) -> std::io::Result<()> {
Ok(())
}
/// Dump the collected timestamps of the given tables to a csv file
pub unsafe fn dump(file: &str, _: &[Kind]) -> std::io::Result<()> {
let mut file = std::fs::File::create(file)?;
Self::dump_header(&mut file)?;
pub unsafe fn dump(_file: &str, _: &[Kind]) -> std::io::Result<()> {
Ok(())
}
fn dump_header(file: &mut std::fs::File) -> std::io::Result<()> {
write!(file, "XLAP was disabled")?;
#[allow(dead_code)]
fn dump_header(_file: &mut std::fs::File) -> std::io::Result<()> {
Ok(())
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment