|
| 1 | +//! Benchmarks for scanning side metadata for non-zero bits. |
| 2 | +
|
| 3 | +use criterion::Criterion; |
| 4 | +use mmtk::util::{ |
| 5 | + constants::LOG_BITS_IN_WORD, test_private::scan_non_zero_bits_in_metadata_bytes, Address, |
| 6 | +}; |
| 7 | +use rand::{seq::IteratorRandom, SeedableRng}; |
| 8 | +use rand_chacha::ChaCha8Rng; |
| 9 | + |
| 10 | +fn allocate_aligned(size: usize) -> Address { |
| 11 | + let ptr = unsafe { |
| 12 | + std::alloc::alloc_zeroed(std::alloc::Layout::from_size_align(size, size).unwrap()) |
| 13 | + }; |
| 14 | + Address::from_mut_ptr(ptr) |
| 15 | +} |
| 16 | + |
| 17 | +const BLOCK_BYTES: usize = 32768usize; // Match an Immix block size. |
| 18 | + |
| 19 | +// Asssume one-bit-per-word metadata (matching VO bits). |
| 20 | +const BLOCK_META_BYTES: usize = BLOCK_BYTES >> LOG_BITS_IN_WORD; |
| 21 | + |
| 22 | +/// Set this many distinct bits in the bitmap. |
| 23 | +const NUM_OBJECTS: usize = 200; |
| 24 | + |
| 25 | +/// Get a deterministic seeded Rng. |
| 26 | +fn get_rng() -> ChaCha8Rng { |
| 27 | + // Create an Rng from a seed and an explicit Rng type. |
| 28 | + // Not secure at all, but completely deterministic and reproducible. |
| 29 | + // The following seed is read from /dev/random |
| 30 | + const SEED64: u64 = 0x4050cb1b5ab26c70; |
| 31 | + ChaCha8Rng::seed_from_u64(SEED64) |
| 32 | +} |
| 33 | + |
| 34 | +/// A bitmap, with known location of each bit for assertion. |
| 35 | +struct PreparedBitmap { |
| 36 | + start: Address, |
| 37 | + end: Address, |
| 38 | + set_bits: Vec<(Address, u8)>, |
| 39 | +} |
| 40 | + |
| 41 | +/// Make a bitmap of the desired size and set bits. |
| 42 | +fn make_standard_bitmap() -> PreparedBitmap { |
| 43 | + let start = allocate_aligned(BLOCK_META_BYTES); |
| 44 | + let end = start + BLOCK_META_BYTES; |
| 45 | + let mut rng = get_rng(); |
| 46 | + |
| 47 | + let mut set_bits = (0..(BLOCK_BYTES >> LOG_BITS_IN_WORD)) |
| 48 | + .choose_multiple(&mut rng, NUM_OBJECTS) |
| 49 | + .iter() |
| 50 | + .map(|total_bit_offset| { |
| 51 | + let word_offset = total_bit_offset >> LOG_BITS_IN_WORD; |
| 52 | + let bit_offset = total_bit_offset & ((1 << LOG_BITS_IN_WORD) - 1); |
| 53 | + (start + (word_offset << LOG_BITS_IN_WORD), bit_offset as u8) |
| 54 | + }) |
| 55 | + .collect::<Vec<_>>(); |
| 56 | + |
| 57 | + set_bits.sort(); |
| 58 | + |
| 59 | + for (addr, bit) in set_bits.iter() { |
| 60 | + let word = unsafe { addr.load::<usize>() }; |
| 61 | + let new_word = word | (1 << bit); |
| 62 | + unsafe { addr.store::<usize>(new_word) }; |
| 63 | + } |
| 64 | + |
| 65 | + PreparedBitmap { |
| 66 | + start, |
| 67 | + end, |
| 68 | + set_bits, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +pub fn bench(c: &mut Criterion) { |
| 73 | + c.bench_function("bscan_block", |b| { |
| 74 | + let bitmap = make_standard_bitmap(); |
| 75 | + let mut holder: Vec<(Address, u8)> = Vec::with_capacity(NUM_OBJECTS); |
| 76 | + |
| 77 | + b.iter(|| { |
| 78 | + holder.clear(); |
| 79 | + scan_non_zero_bits_in_metadata_bytes(bitmap.start, bitmap.end, &mut |addr, shift| { |
| 80 | + holder.push((addr, shift)); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + assert_eq!(holder.len(), NUM_OBJECTS); |
| 85 | + assert_eq!(holder, bitmap.set_bits); |
| 86 | + }); |
| 87 | +} |
0 commit comments