Commit 55e977b8 authored by Sven Liefgen's avatar Sven Liefgen
Browse files

Rename feature

parent c7bce8af
......@@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
xlap = []
use-xlap = []
[dependencies]
strum = { version = "0.20", features = ["derive"] }
......
......@@ -25,7 +25,7 @@ fn main() {
.len();
let mut static_xlap = format!(
"#[cfg(feature = \"xlap\")]\n\
"#[cfg(feature = \"use-xlap\")]\n\
static XLAP: [AtomicPtr<TimestampTable>; {}] = [\n",
packet_types_count
);
......@@ -36,7 +36,7 @@ fn main() {
// Create constants for kinds
let mut kind_enum = String::from("#[cfg(feature = \"xlap\")]\n");
let mut kind_enum = String::from("#[cfg(feature = \"use-xlap\")]\n");
kind_enum.push_str("#[derive(EnumIter, Copy, Clone)]\n");
kind_enum.push_str("#[allow(non_camel_case_types)]\n");
kind_enum.push_str("pub enum Kind {\n");
......@@ -45,14 +45,13 @@ fn main() {
kind_enum.push_str(kind.as_str().unwrap().to_uppercase().as_str());
kind_enum.push_str(",\n");
}
kind_enum.push_str(" NoKind,\n");
kind_enum.push_str("}\n");
// Create timestamps
let timestamps = &yaml["stamps"];
let mut timestamp_id_enum = String::from("#[cfg(feature = \"xlap\")]");
let mut timestamp_id_enum = String::from("#[cfg(feature = \"use-xlap\")]");
timestamp_id_enum.push_str("#[derive(EnumCount, EnumIter, AsRefStr, Copy, Clone)]\n");
timestamp_id_enum.push_str("pub enum TimestampId {\n");
for timestamp_id in timestamps
......@@ -71,7 +70,7 @@ fn main() {
let count = &yaml["rows"].as_i64().unwrap_or(4098);
let ts_rows = format!(
"#[cfg(feature = \"xlap\")]\n\
"#[cfg(feature = \"use-xlap\")]\n\
const TS_ROWS: usize = {};\n",
count + 1
);
......
use std::time::Instant;
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
use std::{
convert::AsRef,
io::prelude::*,
sync::atomic::{AtomicPtr, Ordering},
};
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
use strum::{AsRefStr, EnumCount, EnumIter, IntoEnumIterator};
// Include the correct function for cycle counts depending on the architecture
#[cfg(all(target_arch = "x86", feature = "xlap"))]
#[cfg(all(target_arch = "x86", feature = "use-xlap"))]
use core::arch::x86::_rdtsc as current_cycle;
#[cfg(all(target_arch = "x86_64", feature = "xlap"))]
#[cfg(all(target_arch = "x86_64", feature = "use-xlap"))]
use core::arch::x86_64::_rdtsc as current_cycle;
// On all other architectures there is no cycle counting, so just set it to zero
#[cfg(all(
not(any(target_arch = "x86", target_arch = "x86_64")),
feature = "xlap"
feature = "use-xlap"
))]
#[inline(always)]
pub unsafe fn current_cycle() -> u64 {
0
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
include!(concat!(env!("OUT_DIR"), "/xlap_generated.rs"));
//const TS_ROWS: usize = 2;
......@@ -47,39 +47,39 @@ include!(concat!(env!("OUT_DIR"), "/xlap_generated.rs"));
// ---------------------------------------------
/// A moment in time, expressed in cycles and time
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
struct Timestamp {
c: u64,
t: Instant,
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
/// A row wit timestamps
struct TimestampTableRow {
timestamp: [Timestamp; TimestampId::COUNT],
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
/// Table containing all the timestamps
pub struct TimestampTable {
rows: [TimestampTableRow; TS_ROWS],
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
static mut ZERO: Option<Instant> = None;
#[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")]
#[cfg(feature = "use-xlap")]
unsafe {
$crate::set_timestamp($kind, $row, $id, $stamp);
}
};
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-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);
......@@ -90,14 +90,14 @@ pub unsafe fn set_timestamp(kind: Kind, row: usize, id: TimestampId, (t, c): (In
/// 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")]
#[cfg(feature = "use-xlap")]
unsafe {
$crate::set_timestamp_clock($kind, $row, $id, $value);
}
};
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-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 =
......@@ -108,14 +108,14 @@ pub unsafe fn set_timestamp_clock(kind: Kind, row: usize, id: TimestampId, value
/// 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")]
#[cfg(feature = "use-xlap")]
unsafe {
$crate::set_timestamp_cycle($kind, $row, $id, $value);
}
};
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-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 =
......@@ -126,14 +126,14 @@ pub unsafe fn set_timestamp_cycle(kind: Kind, row: usize, id: TimestampId, value
/// Update the clock value of a timestamp
macro_rules! timestamp_clock {
( $kind:expr, $row:expr, $id:expr ) => {
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
unsafe {
$crate::timestamp_clock($kind, $row, $id);
}
};
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-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 =
......@@ -144,14 +144,14 @@ pub unsafe fn timestamp_clock(kind: Kind, row: usize, id: TimestampId) {
/// Update the cycle value of a timestamp
macro_rules! timestamp_cycle {
( $kind:expr, $row:expr, $id:expr ) => {
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
unsafe {
$crate::timestamp_cycle($kind, $row, $id);
}
};
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-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 =
......@@ -179,13 +179,13 @@ macro_rules! get_timestamp_cycle {
};
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
#[inline(always)]
pub fn get_timestamp_cycle() -> u64 {
unsafe { current_cycle() }
}
#[cfg(not(feature = "xlap"))]
#[cfg(not(feature = "use-xlap"))]
#[inline(always)]
pub fn get_timestamp_cycle() -> u64 {
0
......@@ -195,27 +195,30 @@ pub fn get_timestamp_cycle() -> u64 {
/// Install a timestamp table
///
/// Allocate memory for the empty timestamp tables and store pointers in the XLAP static
#[cfg(feature = "use-xlap")]
macro_rules! init {
() => {
#[cfg(feature = "xlap")]
unsafe {
$crate::init();
}
};
}
#[macro_export]
#[cfg(not(feature = "use-xlap"))]
macro_rules! init {() => {}}
#[macro_export]
macro_rules! map_kind {
( $src:expr, $($type:expr => $kind:expr),* ) => {
( $src:expr, $($type:pat => $kind:expr),* ) => {
{
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
{
match $src {
$($type => $kind),*,
_ => unreachable!(),
}
}
#[cfg(not(feature = "xlap"))]
#[cfg(not(feature = "use-xlap"))]
{
$crate::Kind::NoKind
}
......@@ -227,16 +230,19 @@ macro_rules! map_kind {
macro_rules! kind {
( $kind:expr ) => {
{
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
{ $kind }
#[cfg(not(feature = "xlap"))]
#[cfg(not(feature = "use-xlap"))]
$crate::Kind::NoKind
}
};
}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
pub unsafe fn init() {
#[cfg(debug_assertions)] {
println!("Initialised XLAP with {} rows.", TS_ROWS)
}
ZERO = Some(Instant::now());
for ptr in XLAP.iter() {
// Create a zeroed Timestamp table
......@@ -250,27 +256,33 @@ pub unsafe fn init() {
#[macro_export]
/// Dump the collected timestamps to a csv file
#[cfg(feature = "use-xlap")]
macro_rules! dump_all {
( $file:expr ) => {
#[cfg(feature = "xlap")]
unsafe {
$crate::TimestampTable::dump_all($file)
}
};
}
#[macro_export]
#[cfg(not(feature = "use-xlap"))]
macro_rules! dump_all {($file:expr) => {}}
#[macro_export]
/// Dump the collected timestamps of the given tables to a csv file
#[cfg(feature = "use-xlap")]
macro_rules! dump {
( $file:expr, $tables:expr ) => {
#[cfg(feature = "xlap")]
unsafe {
$crate::TimestampTable::dump($file, $tables)
}
};
}
#[macro_export]
#[cfg(not(feature = "use-xlap"))]
macro_rules! dump {($file:expr, $table:expr) => {}}
#[cfg(feature = "xlap")]
#[cfg(feature = "use-xlap")]
impl TimestampTable {
pub unsafe fn dump_all(file: &str) -> std::io::Result<()> {
Self::dump(file, Kind::iter().collect::<Vec<_>>().as_slice())
......@@ -280,6 +292,7 @@ impl TimestampTable {
let mut file = std::fs::File::create(file)?;
Self::dump_header(&mut file)?;
for kind in tables {
println!("Writing to file {:?}", file);
let table = &*XLAP[*kind as usize].load(Ordering::Acquire);
// TODO: skip the first one?
for (i, row) in table.rows.iter().enumerate().skip(1) {
......@@ -304,19 +317,19 @@ impl TimestampTable {
}
}
#[cfg(not(feature = "xlap"))]
#[cfg(not(feature = "use-xlap"))]
/// Table containing all the timestamps
pub struct TimestampTable;
#[cfg(not(feature = "xlap"))]
#[cfg(not(feature = "use-xlap"))]
pub enum Kind {
NoKind,
}
#[cfg(not(feature = "xlap"))]
#[cfg(not(feature = "use-xlap"))]
pub enum TimestampId {}
#[cfg(not(feature = "xlap"))]
#[cfg(not(feature = "use-xlap"))]
impl TimestampTable {
/// Dump the collected timestamps to a csv file
pub unsafe fn dump_all(_file: &str) -> std::io::Result<()> {
......
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