Commit cce95d95 authored by Sven Liefgen's avatar Sven Liefgen
Browse files

Generate code from xlap.yml

parent fa6b275c
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "heck"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "linked-hash-map"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]]
name = "proc-macro2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strum"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-segmentation"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "xlap"
version = "0.1.0"
dependencies = [
"strum",
"yaml-rust",
]
......
......@@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
strum = { version = "0.20", features = ["derive"] }
[build-dependencies]
yaml-rust = "0.4"
......@@ -5,22 +5,59 @@ use std::path::Path;
use yaml_rust::YamlLoader;
fn main() {
//let out_dir = env::var("OUT_DIR").unwrap();
//let dest_path = Path::new(&out_dir).join("xlap_generated.rs");
//let yaml = fs::read_to_string("xlap.yml").unwrap();
//let yaml = YamlLoader::load_from_str(yaml.as_str()).unwrap();
//fs::write(
//&dest_path,
//format!(
//"{:?}\
//",
//yaml
//)
//)
//.unwrap();
//println!("cargo::rerun-if-changed=build.rs");
//println!("cargo::rerun-if-changed=src/lib.rs");
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("xlap_generated.rs");
let xlap = env::var("XLAP").unwrap();
let yaml = fs::read_to_string(&xlap).unwrap();
let yaml = YamlLoader::load_from_str(yaml.as_str()).unwrap().pop().unwrap();
// Create XLAP array containing a table for each kind
let packet_types = &yaml["packet_types"];
let packet_types_count = packet_types.as_hash().unwrap().len();
let mut static_xlap = format!("static XLAP: [AtomicPtr<TimestampTable>; {}] = [\n", packet_types_count);
for _ in 0..packet_types_count {
static_xlap.push_str(" AtomicPtr::new(std::ptr::null_mut()),\n");
}
static_xlap.push_str("];\n");
// Create constants for kinds
let mut kind_consts = String::new();
for (i, kind) in packet_types.as_hash().unwrap().keys().enumerate() {
kind_consts.push_str("pub const TS_");
kind_consts.push_str(kind.as_str().unwrap().to_uppercase().as_str());
kind_consts.push_str(": usize = ");
kind_consts.push_str(i.to_string().as_str());
kind_consts.push_str(";\n");
}
// Create timestamps
let timestamps = &yaml["stamps"];
let mut timestamp_id_enum = String::from("#[derive(EnumCount, EnumIter, AsRefStr)]\n");
timestamp_id_enum.push_str("pub enum TimestampId {\n");
for timestamp_id in timestamps.as_hash().unwrap().keys() {
timestamp_id_enum.push_str(" ");
timestamp_id_enum.push_str(timestamp_id.as_str().unwrap());
timestamp_id_enum.push_str(",\n");
}
timestamp_id_enum.push_str("}\n");
fs::write(
&dest_path,
format!("{}\n{}\n{}\n",
static_xlap,
kind_consts,
timestamp_id_enum,
)
)
.unwrap();
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=src/lib.rs");
println!("cargo::rerun-if-changed={}", xlap);
}
......@@ -2,6 +2,9 @@ use std::time::Instant;
use std::sync::atomic::AtomicPtr;
use std::sync::atomic::Ordering;
use std::io::prelude::*;
use std::convert::AsRef;
use strum::{EnumCount, EnumIter, IntoEnumIterator, AsRefStr};
// Include the correct function for cycle counts depending on the architecture
#[cfg(target_arch = "x86")]
......@@ -16,7 +19,10 @@ pub unsafe fn current_cycle() -> u64 {
}
const TS_ROWS: usize = 128;
const TS_COUNT: usize = 4;
include!(concat!(env!("OUT_DIR"), "/xlap_generated.rs"));
// ---------------------------------------------
/// A moment in time, expressed in cycles and time
struct Timestamp {
......@@ -26,7 +32,7 @@ struct Timestamp {
/// A row wit timestamps
struct TimestampTableRow {
timestamp: [Timestamp; TS_COUNT],
timestamp: [Timestamp; TimestampId::COUNT],
}
/// Table containing all the timestamps
......@@ -34,39 +40,30 @@ pub struct TimestampTable {
rows: [TimestampTableRow; TS_ROWS],
}
pub static TS_DATA: usize = 0;
pub static TS_REDUNDANCY: usize = 1;
static XLAP: [AtomicPtr<TimestampTable>; 2] = [
AtomicPtr::new(std::ptr::null_mut()),
AtomicPtr::new(std::ptr::null_mut()),
];
static mut ZERO: Option<Instant> = None;
/// Update the clock value of a timestamp by setting it to an explicit value
#[inline(always)]
pub unsafe fn timestamp_value(kind: usize, row: usize, id: usize, value: Instant) {
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id].t = value;
pub unsafe fn timestamp_value(kind: usize, row: usize, id: TimestampId, value: Instant) {
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].t = value;
}
/// Update the clock value of a cyclestamp by setting it to an explicit value
#[inline(always)]
pub unsafe fn cyclestamp_value(kind: usize, row: usize, id: usize, value: u64) {
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id].c = value;
pub unsafe fn cyclestamp_value(kind: usize, row: usize, id: TimestampId, value: u64) {
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].c = value;
}
/// Update the clock value of a timestamp
#[inline(always)]
pub unsafe fn timestamp_clock(kind: usize, row: usize, id: usize) {
println!("Write {:?} to table {}, row {}, id {}", ZERO.unwrap().elapsed() , kind, row, id);
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id].t = Instant::now();
pub unsafe fn timestamp_clock(kind: usize, row: usize, id: TimestampId) {
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].t = Instant::now();
}
/// Update the cycle value of a timestamp
#[inline(always)]
pub unsafe fn timestamp_cycle(kind: usize, row: usize, id: usize) {
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id].c = current_cycle();
pub unsafe fn timestamp_cycle(kind: usize, row: usize, id: TimestampId) {
(*XLAP[kind].load(Ordering::Acquire)).rows[row % TS_ROWS].timestamp[id as usize].c = current_cycle();
}
impl TimestampTable {
......@@ -105,6 +102,9 @@ impl TimestampTable {
fn dump_header(file: &mut std::fs::File) -> std::io::Result<()> {
write!(file, "SeqNo, Kind")?;
for ts in TimestampId::iter() {
write!(file, ", {0}_T, {0}_C", ts.as_ref())?;
}
writeln!(file)?;
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