From dca0b156b43b68dc66ae60923ff20d3f65b47df9 Mon Sep 17 00:00:00 2001 From: guillaume didier Date: Mon, 6 Apr 2020 11:14:05 +0200 Subject: [PATCH] Add complex addressing function computation and output those when calibrating --- cache_utils/src/calibration.rs | 19 ++++++++++++++++--- cache_utils/src/complex_addressing.rs | 21 +++++++++++++++++++++ cache_utils/src/lib.rs | 1 + 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cache_utils/src/calibration.rs b/cache_utils/src/calibration.rs index 36d6b56..c286d7c 100644 --- a/cache_utils/src/calibration.rs +++ b/cache_utils/src/calibration.rs @@ -14,6 +14,7 @@ pub enum Verbosity { extern crate alloc; use crate::calibration::Verbosity::*; +use crate::complex_addressing::AddressHasher; use alloc::vec; use alloc::vec::Vec; use core::cmp::min; @@ -219,6 +220,16 @@ fn calibrate_impl( num_iterations: u32, verbosity_level: Verbosity, ) -> Vec { + // TODO : adapt this to detect CPU generation and grab the correct masks. + // These are the skylake masks. + let masks: [usize; 3] = [ + 0b1111_0011_0011_0011_0010_0100_1100_0100_000000, + 0b1011_1010_1101_0111_1110_1010_1010_0010_000000, + 0b0110_1101_0111_1101_0101_1101_0101_0001_000000, + ]; + + let hasher = AddressHasher::new(&masks); + if verbosity_level >= Thresholds { println!( "Calibrating {}...", @@ -231,7 +242,7 @@ fn calibrate_impl( let mut ret = Vec::new(); if verbosity_level >= Thresholds { println!( - "CSV: address, {} min, {} median, {} max", + "CSV: address, hash, {} min, {} median, {} max", operations.iter().map(|(_, name)| name).format(" min, "), operations.iter().map(|(_, name)| name).format(" median, "), operations.iter().map(|(_, name)| name).format(" max, ") @@ -239,9 +250,10 @@ fn calibrate_impl( } for i in (0..len).step_by(increment) { let pointer = unsafe { p.offset(i) }; + let hash = hasher.hash(pointer as usize); if verbosity_level >= Thresholds { - println!("Calibration for {:p}", pointer); + println!("Calibration for {:p} (hash: {:x})", pointer, hash); } // TODO add some useful impl to CalibrateResults @@ -325,8 +337,9 @@ fn calibrate_impl( ); } println!( - "CSV: {:p}, {}, {}, {}", + "CSV: {:p}; {:x}, {}, {}, {}", pointer, + hash, calibrate_result.min.iter().format(", "), calibrate_result.median.iter().format(", "), calibrate_result.max.iter().format(", ") diff --git a/cache_utils/src/complex_addressing.rs b/cache_utils/src/complex_addressing.rs index e69de29..afb665d 100644 --- a/cache_utils/src/complex_addressing.rs +++ b/cache_utils/src/complex_addressing.rs @@ -0,0 +1,21 @@ +pub struct AddressHasher<'a> { + masks: &'a [usize], +} + +fn hash(addr: usize, mask: usize) -> u32 { + (addr & mask).count_ones() & 1 +} + +impl AddressHasher<'_> { + pub fn new(masks: &[usize]) -> AddressHasher { + AddressHasher { masks } + } + pub fn hash(&self, addr: usize) -> u32 { + let mut res = 0; + for mask in self.masks { + res <<= 1; + res |= hash(addr, *mask); + } + res + } +} diff --git a/cache_utils/src/lib.rs b/cache_utils/src/lib.rs index a715c1a..f3546a2 100644 --- a/cache_utils/src/lib.rs +++ b/cache_utils/src/lib.rs @@ -12,6 +12,7 @@ assert_cfg!( pub mod cache_info; pub mod calibration; +pub mod complex_addressing; #[cfg(feature = "std")] pub mod mmap; pub mod prefetcher;