-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Heap traversal #1174
Merged
Merged
Heap traversal #1174
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
b0d297d
WIP: enumerate objects and regions
wks c69a68b
WIP: linear scanning metadata
wks a837987
Refactor iterate_meta_bits
wks b1b975a
Merge branch 'master' into feature/each-object
wks 7d3f79d
Fix warning
wks acaf942
Benchmarks
wks 553f210
Merge branch 'fix/bulk-metadata-visit-simple' into feature/each-object
wks f4a08b3
Remove ByteWordRange
wks a51cdc4
Fix clippy warnings
wks b1486e3
Formatting
wks ab28a79
Move iterate_meta_bits to ranges::break_bit_range
wks e2bafd5
Add tests and fix bugs
wks c3c3dfa
Just use u8 for the type of bit offset.
wks 19a366f
Merge branch 'fix/bulk-metadata-visit-simple' into feature/each-object
wks 3a4747c
Fix and comment
wks 765da3b
Formatting
wks a73061c
Comments
wks 5f1e99e
Merge branch 'fix/bulk-metadata-visit-simple' into feature/each-object
wks 419655f
Minor changes
wks 6eb3073
Mock test for heap traversal
wks f3c80af
Add benchmark for bitmap scanning
wks d994f6e
Merge branch 'master' into fix/bulk-metadata-visit-simple
wks 323d39e
Revert criterion version update
wks 95cd6df
Fix comments
wks 32b4ab8
Merge branch 'fix/bulk-metadata-visit-simple' into feature/each-object
wks 8b91a21
Support VMSpace
wks 70e8933
Renamed to Space::enumerate_object_coarse
wks 4f6d1bd
Revert `Region::as_range()`.
wks e1669d2
Merge branch 'master' into feature/each-object
wks 38edbc9
Merge branch 'master' into feature/each-object
wks 557e7c4
Remove the "bench" feature
wks ec194aa
Comments
wks ce3c723
Rename back to Space::enumerate_objects
wks 81a9fab
Use vo_bit::get_object_ref_for_vo_addr
wks d11aa54
Update comments of MMTK::enumerate_objects
wks 7130211
Rename remaining enumerate_objects_coarse
wks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//! Benchmarks for scanning side metadata for non-zero bits. | ||
|
||
use criterion::Criterion; | ||
use mmtk::util::{ | ||
constants::LOG_BITS_IN_WORD, test_private::scan_non_zero_bits_in_metadata_bytes, Address, | ||
}; | ||
use rand::{seq::IteratorRandom, SeedableRng}; | ||
use rand_chacha::ChaCha8Rng; | ||
|
||
fn allocate_aligned(size: usize) -> Address { | ||
let ptr = unsafe { | ||
std::alloc::alloc_zeroed(std::alloc::Layout::from_size_align(size, size).unwrap()) | ||
}; | ||
Address::from_mut_ptr(ptr) | ||
} | ||
|
||
const BLOCK_BYTES: usize = 32768usize; // Match an Immix block size. | ||
|
||
// Asssume one-bit-per-word metadata (matching VO bits). | ||
const BLOCK_META_BYTES: usize = BLOCK_BYTES >> LOG_BITS_IN_WORD; | ||
|
||
/// Set this many distinct bits in the bitmap. | ||
const NUM_OBJECTS: usize = 200; | ||
|
||
/// Get a deterministic seeded Rng. | ||
fn get_rng() -> ChaCha8Rng { | ||
// Create an Rng from a seed and an explicit Rng type. | ||
// Not secure at all, but completely deterministic and reproducible. | ||
// The following seed is read from /dev/random | ||
const SEED64: u64 = 0x4050cb1b5ab26c70; | ||
ChaCha8Rng::seed_from_u64(SEED64) | ||
} | ||
|
||
/// A bitmap, with known location of each bit for assertion. | ||
struct PreparedBitmap { | ||
start: Address, | ||
end: Address, | ||
set_bits: Vec<(Address, u8)>, | ||
} | ||
|
||
/// Make a bitmap of the desired size and set bits. | ||
fn make_standard_bitmap() -> PreparedBitmap { | ||
let start = allocate_aligned(BLOCK_META_BYTES); | ||
let end = start + BLOCK_META_BYTES; | ||
let mut rng = get_rng(); | ||
|
||
let mut set_bits = (0..(BLOCK_BYTES >> LOG_BITS_IN_WORD)) | ||
.choose_multiple(&mut rng, NUM_OBJECTS) | ||
.iter() | ||
.map(|total_bit_offset| { | ||
let word_offset = total_bit_offset >> LOG_BITS_IN_WORD; | ||
let bit_offset = total_bit_offset & ((1 << LOG_BITS_IN_WORD) - 1); | ||
(start + (word_offset << LOG_BITS_IN_WORD), bit_offset as u8) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
set_bits.sort(); | ||
|
||
for (addr, bit) in set_bits.iter() { | ||
let word = unsafe { addr.load::<usize>() }; | ||
let new_word = word | (1 << bit); | ||
unsafe { addr.store::<usize>(new_word) }; | ||
} | ||
|
||
PreparedBitmap { | ||
start, | ||
end, | ||
set_bits, | ||
} | ||
} | ||
|
||
pub fn bench(c: &mut Criterion) { | ||
c.bench_function("bscan_block", |b| { | ||
let bitmap = make_standard_bitmap(); | ||
let mut holder: Vec<(Address, u8)> = Vec::with_capacity(NUM_OBJECTS); | ||
|
||
b.iter(|| { | ||
holder.clear(); | ||
scan_non_zero_bits_in_metadata_bytes(bitmap.start, bitmap.end, &mut |addr, shift| { | ||
holder.push((addr, shift)); | ||
}); | ||
}); | ||
|
||
assert_eq!(holder.len(), NUM_OBJECTS); | ||
assert_eq!(holder, bitmap.set_bits); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub mod bscan; | ||
pub mod bzero_bset; | ||
|
||
pub use criterion::Criterion; | ||
|
||
pub fn bench(c: &mut Criterion) { | ||
bscan::bench(c); | ||
bzero_bset::bench(c); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how it would be helpful to save the visited objects and visit them later. If the GC is happening, the visited objects may be reclaimed during this call, and the saved object references may become invalid.
As this function uses VO bit (which is changed during GC and allocation), and the function does not block GC and does not block allocation, it should be undefined behavior if GC or allocation happens at the same time. We cannot guarantee anything if GC/allocation is happenning. We may call the closure for an object that seems valid, but when the closure visits the object, the object may be reclaimed. We may visit objects that are copied (but their VO bits are not yet cleared). We may miss objects that are just allocated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The VM binding needs to treat the saved list of object references as roots. In the way, if GC happens, those objects will be guaranteed to live through the next GC.
The list may contain the last reference to some objects because the object may be unlinked from their original parents after the last GC. That's expected. Such heap traversal APIs are allowed to pick up objects that have become unreachable, finalized, etc., and the VM binding needs to be aware of that. If such unreachable objects points to other objects, their outgoing edges will still remain valid because (1) if GC has not happened, their edges remain valid, and (2) if GC happens, those objects are rooted and will be traced and forwarded.
If the VM can update the roots (in the same way updating JNI handles or other native roots), the saved list can be updated if a copying GC happens. Alternatively, if the VM only intends to support plans that support pinning, it can treat the list as pinning roots, and those objects will not be moved while iterating through the list.
I have tested it on Ruby by manually triggering a GC between saving the list (as a pinning root) and iterating through the list, and also triggering GC after visiting every few objects. It works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you said does not contradict with what I said. I suggested that 'it should be undefined behavior if GC or allocation happens at the same time (during the enumerating function call)'. After the enumerating function, the binding can do whatever they need to make an 'illusion' that the user is enumerating objects during a GC. But this function itself is not allowed during GC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it should have undefined behavior if GC happens.
I thought allocation should be benign because allocation strictly adds VO bits but does not remove VO bits. But after thinking about it, I find that it is easier to just specify that it has undefined behavior if allocation happens concurrently, too. Reasons include
enumerate_objects
is called" is not precisely defined unless we make all threads stop, or at least synchornize with the current thread.It is OK for Ruby because we currently only supports a single Ractor (in which only one thread is active at any time). For OpenJDK, it will require a
VMOperation
to stop the world before enumerating objects.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the comment. I made it clear that it is a data race if either allocation or GC happens concurrently with
enumerate_objects
. I also added some comments on keeping the saved object references in roots if the VM binding implements an API that allows allocation while enumerating objects.