Skip to content

Commit fd162b8

Browse files
zhaofengliarkivm
authored andcommitted
A bunch of hacks to make Maglev faster
1 parent c702e0e commit fd162b8

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

sys/lib/libbenchnet/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ usr = { path = "../../../sys/interfaces/usr", version = "0.1.0" }
1717
console = { path = "../console", version = "0.1.0" }
1818
hashbrown = "0.7.2"
1919

20+
sashstore-redleaf = { path = "../sashstore" }
21+
2022
[dependencies.fnv]
2123
git = "https://github.com/servo/rust-fnv"
2224
default-features = false

sys/lib/libbenchnet/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,9 @@ pub fn run_maglev_fwd_udptest_rref(net: &dyn Net, pkt_len: usize) -> Result<()>
10011001
#[cfg(feature = "noop")]
10021002
return Ok(());
10031003

1004+
let mut sender_mac = alloc::vec![0x90, 0xe2, 0xba, 0xb5, 0x13, 0x60];
1005+
let mut our_mac = alloc::vec![0x90, 0xe2, 0xba, 0xb5, 0x15, 0x74];
1006+
10041007
let batch_sz = BATCH_SIZE;
10051008
let mut maglev = maglev::Maglev::new(0..3);
10061009
let mut rx_submit = RRefDeque::<[u8; 1514], 32>::default();
@@ -1075,11 +1078,17 @@ pub fn run_maglev_fwd_udptest_rref(net: &dyn Net, pkt_len: usize) -> Result<()>
10751078
}
10761079
};
10771080

1081+
/*
10781082
if let Some(_) = backend {
10791083
for i in 0..6 {
10801084
(pkt).swap(i, 6 + i);
10811085
}
10821086
}
1087+
*/
1088+
unsafe {
1089+
ptr::copy(our_mac.as_ptr(), pkt.as_mut_ptr().offset(6), our_mac.capacity());
1090+
ptr::copy(sender_mac.as_ptr(), pkt.as_mut_ptr().offset(0), sender_mac.capacity());
1091+
}
10831092
}
10841093

10851094
mswap_elapsed += rdtsc() - ms_start;
@@ -1125,6 +1134,8 @@ pub fn run_maglev_fwd_udptest_rref(net: &dyn Net, pkt_len: usize) -> Result<()>
11251134

11261135
let adj_runtime = elapsed as f64 / CPU_MHZ as f64;
11271136

1137+
maglev.dump_stats();
1138+
11281139
if sum > 0 && fwd_sum > 0 {
11291140
println!("runtime: {:.2} seconds", adj_runtime);
11301141

sys/lib/libbenchnet/src/maglev.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ use fnv::FnvHasher;
2727
use twox_hash::XxHash;
2828
use lru::LruCache;
2929

30+
use core::default::Default;
31+
3032
use hashbrown::HashMap;
33+
use sashstore_redleaf::indexmap::Index;
34+
35+
use console::println;
3136

3237
const TABLE_SIZE: usize = 65537;
33-
const CACHE_SIZE: usize = 1000;
38+
const CACHE_SIZE: usize = 1 << 24;
39+
40+
static mut HIT_COUNT: usize = 0;
41+
static mut HASHMAP_TOTAL: usize = 0;
3442

3543
type FnvHashFactory = BuildHasherDefault<FnvHasher>;
3644
type XxHashFactory = BuildHasherDefault<XxHash>;
@@ -39,8 +47,9 @@ type XxHashFactory = BuildHasherDefault<XxHash>;
3947
pub struct Maglev<N> {
4048
pub nodes: Vec<N>,
4149
pub lookup: Vec<isize>,
42-
pub cache: RefCell<HashMap<usize, usize>>,
4350
// pub cache: RefCell<LruCache<usize, usize>>, // hash -> backend
51+
// pub cache: RefCell<HashMap<usize, usize, FnvHashFactory>>,
52+
pub cache: RefCell<Index<usize, usize>>,
4453
}
4554

4655
impl<N: Hash + Eq> Maglev<N> {
@@ -49,7 +58,8 @@ impl<N: Hash + Eq> Maglev<N> {
4958
let nodes = nodes.into_iter().collect::<Vec<_>>();
5059
let lookup = Self::populate(&nodes);
5160
// let cache = RefCell::new(LruCache::new(CACHE_SIZE));
52-
let cache = RefCell::new(HashMap::with_capacity(3_000_000));
61+
// let cache = RefCell::new(HashMap::with_capacity_and_hasher(CACHE_SIZE, Default::default()));
62+
let cache = RefCell::new(Index::with_capacity(CACHE_SIZE));
5363

5464
Maglev { nodes, lookup, cache }
5565
}
@@ -134,18 +144,26 @@ impl<N: Hash + Eq> Maglev<N> {
134144
#[inline]
135145
pub fn get_index_from_hash(&self, hash: usize) -> usize {
136146
let mut cache = self.cache.borrow_mut();
137-
match cache.get(&hash) {
147+
let mut set_cache = false;
148+
let x = match cache.get(&hash) {
138149
None => {
139150
// Use lookup directly
140-
let idx = self.lookup[hash % self.lookup.len()] as usize;
141-
cache.insert(hash, idx);
142-
idx
151+
set_cache = true;
152+
self.lookup[hash % self.lookup.len()] as usize
143153
},
144154
Some(idx) => {
145155
// Use cached backend
156+
unsafe { HIT_COUNT += 1; }
146157
*idx
147158
},
159+
};
160+
unsafe { HASHMAP_TOTAL += 1; }
161+
162+
if set_cache {
163+
cache.insert(hash, x);
148164
}
165+
166+
x
149167
}
150168

151169
#[inline]
@@ -155,4 +173,11 @@ impl<N: Hash + Eq> Maglev<N> {
155173
{
156174
&self.nodes[self.get_index(key)]
157175
}
176+
177+
pub fn dump_stats(&self) {
178+
unsafe {
179+
println!("Hits: {}, total: {}", HIT_COUNT, HASHMAP_TOTAL);
180+
}
181+
sashstore_redleaf::indexmap::print_stats();
182+
}
158183
}

0 commit comments

Comments
 (0)