Skip to content

Commit

Permalink
Adjust total allocation statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-blaeser committed Aug 3, 2023
1 parent 05dd6f3 commit 2561c33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion rts/motoko-rts/src/gc/incremental/partitioned_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ pub struct PartitionedHeap {
bitmap_allocation_pointer: usize, // Free pointer for allocating the next mark bitmap.
gc_running: bool, // Create bitmaps for partitions when allocated during active GC.
precomputed_heap_size: usize, // Occupied heap size, excluding the dynamic heap in the allocation partition.
evacuated_size: usize, // Size of all evacuated objects during a GC run. Serves for accurate total allocation statistics.
}

/// Optimization: Avoiding `Option` or `LazyCell`.
Expand All @@ -367,6 +368,7 @@ pub const UNINITIALIZED_HEAP: PartitionedHeap = PartitionedHeap {
bitmap_allocation_pointer: 0,
gc_running: false,
precomputed_heap_size: 0,
evacuated_size: 0,
};

impl PartitionedHeap {
Expand Down Expand Up @@ -403,6 +405,7 @@ impl PartitionedHeap {
bitmap_allocation_pointer: 0,
gc_running: false,
precomputed_heap_size: heap_base,
evacuated_size: 0,
}
}

Expand Down Expand Up @@ -526,6 +529,7 @@ impl PartitionedHeap {
evacuation_space -= partition.marked_size();
partition.evacuate = true;
self.evacuating = true;
debug_assert_eq!(self.evacuated_size, 0);
}
}
}
Expand Down Expand Up @@ -557,7 +561,7 @@ impl PartitionedHeap {
if partition.to_be_evacuated() {
debug_assert!(partition.index != self.allocation_index);
debug_assert!(partition.dynamic_size >= marked_size);
self.reclaimed += partition.dynamic_size as u64;
self.reclaimed += (partition.dynamic_size - marked_size) as u64;
}
if partition.to_be_evacuated() || partition.temporary {
self.precomputed_heap_size -= partition.dynamic_size;
Expand All @@ -566,6 +570,7 @@ impl PartitionedHeap {
}
}
self.evacuating = false;
self.evacuated_size = 0;
self.bitmap_allocation_pointer = 0;
debug_assert!(self.gc_running);
self.gc_running = false;
Expand Down Expand Up @@ -631,6 +636,16 @@ impl PartitionedHeap {
Bytes(self.reclaimed)
}

pub fn increase_evacuated_size(&mut self, size: Words<u32>) {
self.evacuated_size += size.to_bytes().as_usize();
}

pub fn total_allocated_size(&self) -> Bytes<u64> {
debug_assert!(self.evacuated_size <= self.occupied_size().as_usize());
let heap_size_without_evacuations = self.occupied_size().as_usize() - self.evacuated_size;
Bytes(heap_size_without_evacuations as u64) + self.reclaimed_size()
}

pub unsafe fn allocate<M: Memory>(&mut self, mem: &mut M, words: Words<u32>) -> Value {
let size = words.to_bytes().as_usize();
if size <= PARTITION_SIZE {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl<'a, M: Memory + 'a> EvacuationIncrement<'a, M> {
debug_assert!(!original.is_forwarded());
let size = block_size(original as usize);
let new_address = self.mem.alloc_words(size);
self.heap.increase_evacuated_size(size);
let copy = new_address.get_ptr() as *mut Obj;
memcpy_words(copy as usize, original as usize, size);
(*copy).forward = new_address;
Expand All @@ -88,7 +89,6 @@ impl<'a, M: Memory + 'a> EvacuationIncrement<'a, M> {
// Marking is necessary to ensure field updates in the copy.
let unmarked_before = self.heap.mark_object(copy);
debug_assert!(unmarked_before);

// Determined by measurements in comparison to the mark and update phases.
const TIME_FRACTION_PER_WORD: f64 = 2.7;
self.time
Expand Down
2 changes: 1 addition & 1 deletion rts/motoko-rts/src/memory/ic/partitioned_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ unsafe extern "C" fn get_reclaimed() -> Bytes<u64> {

#[no_mangle]
pub unsafe extern "C" fn get_total_allocations() -> Bytes<u64> {
Bytes(u64::from(get_heap_size().as_u32())) + get_reclaimed()
crate::gc::incremental::get_partitioned_heap().total_allocated_size()
}

#[no_mangle]
Expand Down

0 comments on commit 2561c33

Please sign in to comment.