Skip to content

Commit

Permalink
Commit debug prints
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Aug 20, 2020
1 parent b6a0e33 commit 1b4f353
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 105 deletions.
152 changes: 152 additions & 0 deletions rts/motoko-rts/src/dump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#![allow(dead_code)]

use crate::array::*;
use crate::gc;
use crate::print::*;
use crate::types::*;

use core::fmt::Write;

pub(crate) unsafe fn dump_heap() {
print_closure_table();
print_static_roots();
print_heap();
}

pub(crate) unsafe fn print_closure_table() {
let closure_tbl = gc::closure_table_loc().unskew() as *const SkewedPtr;

if (*closure_tbl).0 == 0 {
println!(100, "Closure table not initialized");
return;
}

let len = (*((*closure_tbl).unskew() as *const Array)).len;

if len == 0 {
println!(50, "Closure table empty");
return;
}

let arr = (*closure_tbl).unskew() as *const Array;

println!(50, "Closure table: {}", len);

let mut buf = [0u8; 1000];
let mut write_buf = WriteBuf::new(&mut buf);

for i in 0..len {
let elem = array_get(arr, i);
if !gc::is_tagged_scalar(SkewedPtr(elem as usize)) {
let _ = write!(&mut write_buf, "{}: {:#x} --> ", i, elem.wrapping_add(1));
print_closure(&mut write_buf, SkewedPtr(elem as usize).unskew());
print(&write_buf);
write_buf.reset();
}
}
println!(50, "End of closure table");
}

pub(crate) unsafe fn print_static_roots() {
let static_roots = gc::get_static_roots().unskew() as *const Array;
let len = (*static_roots).len;

if len == 0 {
println!(50, "Static roots empty");
return;
}

println!(50, "Static roots: {}", len);

let mut buf = [0u8; 1000];
let mut write_buf = WriteBuf::new(&mut buf);

for i in 0..len {
let obj = array_idx_unchecked(static_roots, i);
let _ = write!(&mut write_buf, "{}: {:#x} --> ", i, obj.unskew());
print_closure(&mut write_buf, obj.unskew());
print(&write_buf);
write_buf.reset();
}

println!(50, "End of static roots");
}

unsafe fn print_heap() {
let heap_begin = gc::get_heap_base();
let heap_end = gc::get_hp();

println!(
200,
"Heap begin={:#x}, heap end={:#x}, size={} bytes",
heap_begin,
heap_end,
heap_end - heap_begin
);

let mut buf = [0u8; 1000];
let mut write_buf = WriteBuf::new(&mut buf);

let mut p = heap_begin;
while p < heap_end {
let _ = write!(&mut write_buf, "{:#x}: ", p);
print_closure(&mut write_buf, p);
print(&write_buf);
write_buf.reset();

p += words_to_bytes(gc::object_size_(p)).0 as usize;
}
}

unsafe fn print_closure(buf: &mut WriteBuf, p: usize) {
let obj = p as *const Obj;
let tag = (*obj).tag;

match tag {
TAG_OBJECT => {
let _ = write!(buf, "<Object>");
}
TAG_OBJ_IND => {
let _ = write!(buf, "<ObjInd>");
}
TAG_ARRAY => {
let _ = write!(buf, "<Array>");
}
TAG_BITS64 => {
let _ = write!(buf, "<Bits64>");
}
TAG_MUTBOX => {
let _ = write!(buf, "<MutBox>");
}
TAG_CLOSURE => {
let _ = write!(buf, "<Closure>");
}
TAG_SOME => {
let _ = write!(buf, "<Some>");
}
TAG_VARIANT => {
let _ = write!(buf, "<Variant>");
}
TAG_BLOB => {
let _ = write!(buf, "<Blob>");
}
TAG_INDIRECTION => {
let _ = write!(buf, "<Indirection>");
}
TAG_BITS32 => {
let _ = write!(buf, "<Bits32>");
}
TAG_BIGINT => {
let _ = write!(buf, "<BigInt>");
}
TAG_CONCAT => {
let _ = write!(buf, "<Concat>");
}
TAG_STABLE_SEEN => {
let _ = write!(buf, "<StableSeen>");
}
other => {
let _ = write!(buf, "<??? {} ???>", other);
}
}
}
109 changes: 31 additions & 78 deletions rts/motoko-rts/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ use crate::types::*;

extern "C" {
/// Get end_of_heap
pub fn get_hp() -> usize;
pub(crate) fn get_hp() -> usize;

/// Set end_of_heap
pub fn set_hp(hp: usize);
pub(crate) fn set_hp(hp: usize);

/// Get __heap_base
fn get_heap_base() -> usize;
pub(crate) fn get_heap_base() -> usize;

/// Skewed pointer to a skewed pointer to an array. See closure-table.c for details.
fn closure_table_loc() -> SkewedPtr;
pub(crate) fn closure_table_loc() -> SkewedPtr;

fn get_static_roots() -> SkewedPtr;
pub(crate) fn get_static_roots() -> SkewedPtr;
}

/// Maximum live data retained in a GC, in bytes.
Expand Down Expand Up @@ -88,7 +88,7 @@ pub unsafe extern "C" fn object_size(obj: SkewedPtr) -> Words<u32> {
object_size_(obj.unskew())
}

unsafe fn object_size_(obj: usize) -> Words<u32> {
pub(crate) unsafe fn object_size_(obj: usize) -> Words<u32> {
let obj = obj as *const Obj;
match (*obj).tag {
TAG_OBJECT => {
Expand Down Expand Up @@ -306,7 +306,6 @@ unsafe fn scav(
}

TAG_ARRAY => {
println!(100, "Scavenging array...");
let array = obj as *mut Array;
let array_payload = array.offset(1) as *mut SkewedPtr;
for i in 0..(*array).len as isize {
Expand Down Expand Up @@ -394,22 +393,23 @@ unsafe fn scav(

// We have a special evacuation routine for "static roots" array: we don't evacuate elements of
// "static roots", we scavenge them.
unsafe fn evac_static_roots(
#[no_mangle]
pub unsafe extern "C" fn evac_static_roots(
begin_from_space: usize,
begin_to_space: usize,
mut end_to_space: usize,
roots: *const Array,
) -> usize {
println!(
100,
"evac_static_roots: Evacuating {} roots...",
(*roots).len
);
// println!(
// 100,
// "evac_static_roots: Evacuating {} roots...",
// (*roots).len
// );

// Roots are in a static array which we don't evacuate. Only evacuate elements.
for i in 0..(*roots).len {
let obj = array_idx_unchecked(roots, i);
println!(100, "Scavenging root {}: {:#x}", i, obj.unskew());
// println!(100, "Scavenging root {}: {:#x}", i, obj.unskew());
end_to_space = scav(begin_from_space, begin_to_space, end_to_space, obj.unskew());
}
end_to_space
Expand All @@ -427,28 +427,28 @@ pub unsafe extern "C" fn rust_collect_garbage() {
let begin_to_space = end_from_space;
let mut end_to_space = begin_to_space;

debug_print("### Evacuating roots");
// debug_print("### Evacuating roots");

let static_roots = get_static_roots().unskew() as *const Array;

println!(
200,
"\n### begin_from_space={:#x}\n\
### end_from_space={:#x}\n\
### begin_to_space={:#x}\n\
### end_to_space={:#x}\n\
### static_roots={:#x}",
begin_from_space,
end_from_space,
begin_to_space,
end_to_space,
static_roots as usize,
);
// println!(
// 200,
// "\n### begin_from_space={:#x}\n\
// ### end_from_space={:#x}\n\
// ### begin_to_space={:#x}\n\
// ### end_to_space={:#x}\n\
// ### static_roots={:#x}",
// begin_from_space,
// end_from_space,
// begin_to_space,
// end_to_space,
// static_roots as usize,
// );

// Evacuate roots
end_to_space = evac_static_roots(begin_from_space, begin_to_space, end_to_space, static_roots);

debug_print("### Evacuated static roots, evacuating closure table");
// debug_print("### Evacuated static roots, evacuating closure table");

end_to_space = evacuate(
begin_from_space,
Expand Down Expand Up @@ -489,54 +489,7 @@ pub unsafe extern "C" fn rust_collect_garbage() {
0,
);

println!(100, "### GC finished, new hp = {:#x}", new_hp);
}

unsafe fn print_closure_table() {
let closure_tbl = closure_table_loc().unskew() as *const SkewedPtr;

let len = if (*closure_tbl).0 == 0 {
/*
println!(
100,
"closure table ({:#x} --> {:#x}) not initialized ({})",
closure_tbl as usize,
(*closure_tbl).0,
closure_table_size()
);
*/
0
} else {
/*
println!(
100,
"closure table ({:#x} --> {:#x}) size: {} -- {}",
closure_tbl as usize,
(*closure_tbl).0.wrapping_add(1),
(*((*closure_tbl).unskew() as *const Array)).len,
closure_table_size()
);
*/
(*((*closure_tbl).unskew() as *const Array)).len
};

if len == 0 {
println!(50, "### Closure table empty");
return;
}
crate::dump::dump_heap();

let arr = (*closure_tbl).unskew() as *const Array;
println!(
100,
"### Closure table (table address loc={:#x}, table address={:#x})",
closure_tbl as usize,
arr as usize
);
for i in 0..len {
let elem = array_get(arr, i);
if !is_tagged_scalar(SkewedPtr(elem as usize)) {
println!(100, "{}: {:#x}", i, elem.wrapping_add(1));
}
}
println!(50, "### End of closure table");
println!(100, "### GC finished, new hp = {:#x}", new_hp);
}
7 changes: 6 additions & 1 deletion rts/motoko-rts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ mod array;
mod common;
mod types;

#[cfg(target_arch = "wasm32")]
mod dump;

#[cfg(target_arch = "wasm32")]
mod alloc;

#[cfg(target_arch = "wasm32")]
#[macro_use]
mod sanity;

#[cfg(target_arch = "wasm32")]
pub mod gc;

#[cfg(target_arch = "wasm32")]
Expand Down
Loading

0 comments on commit 1b4f353

Please sign in to comment.