Skip to content
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

Adding feature to query the fragmentation of immixspace #1089

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static_assertions = "1.1.0"
strum = "0.25"
strum_macros = "0.25"
sysinfo = "0.29"
chrono = "*"
udesou marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
paste = "1.0.8"
Expand Down Expand Up @@ -152,6 +153,9 @@ malloc_counted_size = []
# Count the size of all live objects in GC
count_live_bytes_in_gc = []

# Dump memory stats about the plan (live bytes, live lines, live blocks, and used pages)
dump_memory_stats = []

# Workaround a problem where bpftrace scripts (see tools/tracing/timeline/capture.bt) cannot
# capture the type names of work packets.
bpftrace_workaround = []
Expand Down
4 changes: 4 additions & 0 deletions src/plan/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ pub trait Plan: 'static + HasSpaces + Sync + Downcast {
space.verify_side_metadata_sanity(&mut side_metadata_sanity_checker);
})
}

// Dump memory stats for the plan
#[cfg(feature = "dump_memory_stats")]
fn dump_memory_stats(&self) {}
}

impl_downcast!(Plan assoc VM);
Expand Down
6 changes: 6 additions & 0 deletions src/plan/immix/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ impl<VM: VMBinding> Plan for Immix<VM> {
fn common(&self) -> &CommonPlan<VM> {
&self.common
}

#[cfg(feature = "dump_memory_stats")]
fn dump_memory_stats(&self) {
self.immix_space.dump_memory_stats();
self.common.los.dump_memory_stats();
}
}

impl<VM: VMBinding> Immix<VM> {
Expand Down
6 changes: 6 additions & 0 deletions src/plan/sticky/immix/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ impl<VM: VMBinding> Plan for StickyImmix<VM> {
self.immix.common()
}

#[cfg(feature = "dump_memory_stats")]
fn dump_memory_stats(&self) {
self.immix.immix_space.dump_memory_stats();
self.common().los.dump_memory_stats();
}

fn schedule_collection(&'static self, scheduler: &crate::scheduler::GCWorkScheduler<Self::VM>) {
let is_full_heap = self.requires_full_heap_collection();
self.gc_full_heap.store(is_full_heap, Ordering::SeqCst);
Expand Down
83 changes: 83 additions & 0 deletions src/policy/immix/immixspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct ImmixSpace<VM: VMBinding> {
scheduler: Arc<GCWorkScheduler<VM>>,
/// Some settings for this space
space_args: ImmixSpaceArgs,
/// Keeping track of live bytes
#[cfg(feature = "dump_memory_stats")]
live_bytes: AtomicUsize,
}

/// Some arguments for Immix Space.
Expand Down Expand Up @@ -217,6 +220,10 @@ impl<VM: VMBinding> crate::policy::gc_work::PolicyTraceObject<VM> for ImmixSpace
debug_assert!(self.in_space(object));
self.mark_lines(object);
}

// count the bytes for each object in immixspace
#[cfg(feature = "dump_memory_stats")]
self.increase_live_bytes(VM::VMObjectModel::get_current_size(object));
}

fn may_move_objects<const KIND: TraceKind>() -> bool {
Expand Down Expand Up @@ -315,6 +322,8 @@ impl<VM: VMBinding> ImmixSpace<VM> {
mark_state: Self::MARKED_STATE,
scheduler: scheduler.clone(),
space_args,
#[cfg(feature = "dump_memory_stats")]
live_bytes: AtomicUsize::new(0),
}
}

Expand Down Expand Up @@ -436,6 +445,9 @@ impl<VM: VMBinding> ImmixSpace<VM> {
self.scheduler.work_buckets[WorkBucketStage::ClearVOBits].bulk_add(work_packets);
}
}

#[cfg(feature = "dump_memory_stats")]
self.set_live_bytes(0);
}

/// Release for the immix space. This is called when a GC finished.
Expand Down Expand Up @@ -467,6 +479,62 @@ impl<VM: VMBinding> ImmixSpace<VM> {
did_defrag
}

#[cfg(feature = "dump_memory_stats")]
pub(crate) fn dump_memory_stats(&self) {
#[derive(Default)]
struct Dist {
live_blocks: usize,
live_lines: usize,
}
let mut dist = Dist::default();
for chunk in self.chunk_map.all_chunks() {
if !self.address_in_space(chunk.start()) {
continue;
}

for block in chunk
.iter_region::<Block>()
.filter(|b| b.get_state() != BlockState::Unallocated)
{
dist.live_blocks += 1;
match block.get_state() {
BlockState::Marked => {
panic!("At this point the block should have been swept already");
}
BlockState::Unmarked => {
// Block is unmarked and cannot be reused (has no holes)
dist.live_lines += Block::LINES;
}
BlockState::Reusable { unavailable_lines } => {
dist.live_lines += unavailable_lines as usize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this number equivalent to linearly scan the line mark table and manually check the line mark bytes? Probably better to add an assertion to double check. Same as L506.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added an assertion in the latest commit that compares the number of live lines by just iterating the lines in the block and checking the mark table with the number from the block state. Is that what you meant?

}
BlockState::Unallocated => {}
}
}
}

println!(
"{} immixspace",
chrono::offset::Local::now().format("%Y-%m-%d %H:%M:%S")
);
println!("\tLive bytes = {}", self.get_live_bytes());
println!("\tReserved pages = {}", self.reserved_pages());
println!(
"\tReserved pages (bytes) = {}",
self.reserved_pages() << LOG_BYTES_IN_PAGE
);
println!("\tLive blocks = {}", dist.live_blocks);
println!(
"\tLive blocks (bytes) = {}",
dist.live_blocks << Block::LOG_BYTES
);
println!("\tLive lines = {}", dist.live_lines);
println!(
"\tLive lines (bytes) = {}",
dist.live_lines << Line::LOG_BYTES
);
}

/// Generate chunk sweep tasks
fn generate_sweep_tasks(&self) -> Vec<Box<dyn GCWork<VM>>> {
self.defrag.mark_histograms.lock().clear();
Expand Down Expand Up @@ -807,6 +875,21 @@ impl<VM: VMBinding> ImmixSpace<VM> {
self.mark_lines(object);
}
}

#[cfg(feature = "dump_memory_stats")]
pub fn get_live_bytes(&self) -> usize {
self.live_bytes.load(Ordering::SeqCst)
}

#[cfg(feature = "dump_memory_stats")]
pub fn set_live_bytes(&self, size: usize) {
self.live_bytes.store(size, Ordering::SeqCst)
}

#[cfg(feature = "dump_memory_stats")]
pub fn increase_live_bytes(&self, size: usize) {
self.live_bytes.fetch_add(size, Ordering::SeqCst);
}
}

/// A work packet to prepare each block for a major GC.
Expand Down
45 changes: 45 additions & 0 deletions src/policy/largeobjectspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ use crate::policy::sft::GCWorkerMutRef;
use crate::policy::sft::SFT;
use crate::policy::space::{CommonSpace, Space};
use crate::util::constants::BYTES_IN_PAGE;
#[cfg(feature = "dump_memory_stats")]
use crate::util::constants::LOG_BYTES_IN_PAGE;
use crate::util::heap::{FreeListPageResource, PageResource};
use crate::util::metadata;
use crate::util::opaque_pointer::*;
use crate::util::treadmill::TreadMill;
use crate::util::{Address, ObjectReference};
use crate::vm::ObjectModel;
use crate::vm::VMBinding;
#[cfg(feature = "dump_memory_stats")]
use std::sync::atomic::AtomicUsize;

#[allow(unused)]
const PAGE_MASK: usize = !(BYTES_IN_PAGE - 1);
Expand All @@ -28,6 +32,9 @@ pub struct LargeObjectSpace<VM: VMBinding> {
mark_state: u8,
in_nursery_gc: bool,
treadmill: TreadMill,
/// Keeping track of live bytes
#[cfg(feature = "dump_memory_stats")]
live_bytes: AtomicUsize,
}

impl<VM: VMBinding> SFT for LargeObjectSpace<VM> {
Expand Down Expand Up @@ -136,6 +143,11 @@ impl<VM: VMBinding> crate::policy::gc_work::PolicyTraceObject<VM> for LargeObjec
fn may_move_objects<const KIND: crate::policy::gc_work::TraceKind>() -> bool {
false
}

#[cfg(feature = "dump_memory_stats")]
fn post_scan_object(&self, object: ObjectReference) {
self.increase_live_bytes(VM::VMObjectModel::get_current_size(object));
}
}

impl<VM: VMBinding> LargeObjectSpace<VM> {
Expand All @@ -162,6 +174,8 @@ impl<VM: VMBinding> LargeObjectSpace<VM> {
mark_state: 0,
in_nursery_gc: false,
treadmill: TreadMill::new(),
#[cfg(feature = "dump_memory_stats")]
live_bytes: AtomicUsize::new(0),
}
}

Expand All @@ -172,6 +186,8 @@ impl<VM: VMBinding> LargeObjectSpace<VM> {
}
self.treadmill.flip(full_heap);
self.in_nursery_gc = !full_heap;
#[cfg(feature = "dump_memory_stats")]
self.set_live_bytes(0);
}

pub fn release(&mut self, full_heap: bool) {
Expand Down Expand Up @@ -303,6 +319,35 @@ impl<VM: VMBinding> LargeObjectSpace<VM> {
) & NURSERY_BIT
== NURSERY_BIT
}

#[cfg(feature = "dump_memory_stats")]
pub fn get_live_bytes(&self) -> usize {
self.live_bytes.load(Ordering::SeqCst)
}

#[cfg(feature = "dump_memory_stats")]
pub fn set_live_bytes(&self, size: usize) {
self.live_bytes.store(size, Ordering::SeqCst)
}

#[cfg(feature = "dump_memory_stats")]
pub fn increase_live_bytes(&self, size: usize) {
self.live_bytes.fetch_add(size, Ordering::SeqCst);
}

#[cfg(feature = "dump_memory_stats")]
pub(crate) fn dump_memory_stats(&self) {
println!(
"{} los",
chrono::offset::Local::now().format("%Y-%m-%d %H:%M:%S")
);
println!("\tLive bytes = {}", self.get_live_bytes());
println!("\tReserved pages = {}", self.reserved_pages());
println!(
"\tReserved pages (bytes) = {}",
self.reserved_pages() << LOG_BYTES_IN_PAGE
);
}
}

fn get_super_page(cell: Address) -> Address {
Expand Down
3 changes: 3 additions & 0 deletions src/scheduler/gc_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ impl<VM: VMBinding> GCWork<VM> for EndOfGC {
);
}

#[cfg(feature = "dump_memory_stats")]
mmtk.get_plan().dump_memory_stats();

// We assume this is the only running work packet that accesses plan at the point of execution
let plan_mut: &mut dyn Plan<VM = VM> = unsafe { mmtk.get_plan_mut() };
plan_mut.end_of_gc(worker.tls);
Expand Down
Loading