Skip to content

Commit a9ea77f

Browse files
committed
Commit debug prints
1 parent c1aa478 commit a9ea77f

File tree

6 files changed

+226
-105
lines changed

6 files changed

+226
-105
lines changed

rts/motoko-rts/src/dump.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#![allow(dead_code)]
2+
3+
use crate::array::*;
4+
use crate::gc;
5+
use crate::print::*;
6+
use crate::types::*;
7+
8+
use core::fmt::Write;
9+
10+
pub(crate) unsafe fn dump_heap() {
11+
print_closure_table();
12+
print_static_roots();
13+
print_heap();
14+
}
15+
16+
pub(crate) unsafe fn print_closure_table() {
17+
let closure_tbl = gc::closure_table_loc().unskew() as *const SkewedPtr;
18+
19+
if (*closure_tbl).0 == 0 {
20+
println!(100, "Closure table not initialized");
21+
return;
22+
}
23+
24+
let len = (*((*closure_tbl).unskew() as *const Array)).len;
25+
26+
if len == 0 {
27+
println!(50, "Closure table empty");
28+
return;
29+
}
30+
31+
let arr = (*closure_tbl).unskew() as *const Array;
32+
33+
println!(50, "Closure table: {}", len);
34+
35+
let mut buf = [0u8; 1000];
36+
let mut write_buf = WriteBuf::new(&mut buf);
37+
38+
for i in 0..len {
39+
let elem = array_get(arr, i);
40+
if !gc::is_tagged_scalar(SkewedPtr(elem as usize)) {
41+
let _ = write!(&mut write_buf, "{}: {:#x} --> ", i, elem.wrapping_add(1));
42+
print_closure(&mut write_buf, SkewedPtr(elem as usize).unskew());
43+
print(&write_buf);
44+
write_buf.reset();
45+
}
46+
}
47+
println!(50, "End of closure table");
48+
}
49+
50+
pub(crate) unsafe fn print_static_roots() {
51+
let static_roots = gc::get_static_roots().unskew() as *const Array;
52+
let len = (*static_roots).len;
53+
54+
if len == 0 {
55+
println!(50, "Static roots empty");
56+
return;
57+
}
58+
59+
println!(50, "Static roots: {}", len);
60+
61+
let mut buf = [0u8; 1000];
62+
let mut write_buf = WriteBuf::new(&mut buf);
63+
64+
for i in 0..len {
65+
let obj = array_idx_unchecked(static_roots, i);
66+
let _ = write!(&mut write_buf, "{}: {:#x} --> ", i, obj.unskew());
67+
print_closure(&mut write_buf, obj.unskew());
68+
print(&write_buf);
69+
write_buf.reset();
70+
}
71+
72+
println!(50, "End of static roots");
73+
}
74+
75+
unsafe fn print_heap() {
76+
let heap_begin = gc::get_heap_base();
77+
let heap_end = gc::get_hp();
78+
79+
println!(
80+
200,
81+
"Heap begin={:#x}, heap end={:#x}, size={} bytes",
82+
heap_begin,
83+
heap_end,
84+
heap_end - heap_begin
85+
);
86+
87+
let mut buf = [0u8; 1000];
88+
let mut write_buf = WriteBuf::new(&mut buf);
89+
90+
let mut p = heap_begin;
91+
while p < heap_end {
92+
let _ = write!(&mut write_buf, "{:#x}: ", p);
93+
print_closure(&mut write_buf, p);
94+
print(&write_buf);
95+
write_buf.reset();
96+
97+
p += words_to_bytes(gc::object_size_(p)).0 as usize;
98+
}
99+
}
100+
101+
unsafe fn print_closure(buf: &mut WriteBuf, p: usize) {
102+
let obj = p as *const Obj;
103+
let tag = (*obj).tag;
104+
105+
match tag {
106+
TAG_OBJECT => {
107+
let _ = write!(buf, "<Object>");
108+
}
109+
TAG_OBJ_IND => {
110+
let _ = write!(buf, "<ObjInd>");
111+
}
112+
TAG_ARRAY => {
113+
let _ = write!(buf, "<Array>");
114+
}
115+
TAG_BITS64 => {
116+
let _ = write!(buf, "<Bits64>");
117+
}
118+
TAG_MUTBOX => {
119+
let _ = write!(buf, "<MutBox>");
120+
}
121+
TAG_CLOSURE => {
122+
let _ = write!(buf, "<Closure>");
123+
}
124+
TAG_SOME => {
125+
let _ = write!(buf, "<Some>");
126+
}
127+
TAG_VARIANT => {
128+
let _ = write!(buf, "<Variant>");
129+
}
130+
TAG_BLOB => {
131+
let _ = write!(buf, "<Blob>");
132+
}
133+
TAG_INDIRECTION => {
134+
let _ = write!(buf, "<Indirection>");
135+
}
136+
TAG_BITS32 => {
137+
let _ = write!(buf, "<Bits32>");
138+
}
139+
TAG_BIGINT => {
140+
let _ = write!(buf, "<BigInt>");
141+
}
142+
TAG_CONCAT => {
143+
let _ = write!(buf, "<Concat>");
144+
}
145+
TAG_STABLE_SEEN => {
146+
let _ = write!(buf, "<StableSeen>");
147+
}
148+
other => {
149+
let _ = write!(buf, "<??? {} ???>", other);
150+
}
151+
}
152+
}

rts/motoko-rts/src/gc.rs

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ use crate::types::*;
1414

1515
extern "C" {
1616
/// Get end_of_heap
17-
pub fn get_hp() -> usize;
17+
pub(crate) fn get_hp() -> usize;
1818

1919
/// Set end_of_heap
20-
pub fn set_hp(hp: usize);
20+
pub(crate) fn set_hp(hp: usize);
2121

2222
/// Get __heap_base
23-
fn get_heap_base() -> usize;
23+
pub(crate) fn get_heap_base() -> usize;
2424

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

28-
fn get_static_roots() -> SkewedPtr;
28+
pub(crate) fn get_static_roots() -> SkewedPtr;
2929
}
3030

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

91-
unsafe fn object_size_(obj: usize) -> Words<u32> {
91+
pub(crate) unsafe fn object_size_(obj: usize) -> Words<u32> {
9292
let obj = obj as *const Obj;
9393
match (*obj).tag {
9494
TAG_OBJECT => {
@@ -306,7 +306,6 @@ unsafe fn scav(
306306
}
307307

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

395394
// We have a special evacuation routine for "static roots" array: we don't evacuate elements of
396395
// "static roots", we scavenge them.
397-
unsafe fn evac_static_roots(
396+
#[no_mangle]
397+
pub unsafe extern "C" fn evac_static_roots(
398398
begin_from_space: usize,
399399
begin_to_space: usize,
400400
mut end_to_space: usize,
401401
roots: *const Array,
402402
) -> usize {
403-
println!(
404-
100,
405-
"evac_static_roots: Evacuating {} roots...",
406-
(*roots).len
407-
);
403+
// println!(
404+
// 100,
405+
// "evac_static_roots: Evacuating {} roots...",
406+
// (*roots).len
407+
// );
408408

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

430-
debug_print("### Evacuating roots");
430+
// debug_print("### Evacuating roots");
431431

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

434-
println!(
435-
200,
436-
"\n### begin_from_space={:#x}\n\
437-
### end_from_space={:#x}\n\
438-
### begin_to_space={:#x}\n\
439-
### end_to_space={:#x}\n\
440-
### static_roots={:#x}",
441-
begin_from_space,
442-
end_from_space,
443-
begin_to_space,
444-
end_to_space,
445-
static_roots as usize,
446-
);
434+
// println!(
435+
// 200,
436+
// "\n### begin_from_space={:#x}\n\
437+
// ### end_from_space={:#x}\n\
438+
// ### begin_to_space={:#x}\n\
439+
// ### end_to_space={:#x}\n\
440+
// ### static_roots={:#x}",
441+
// begin_from_space,
442+
// end_from_space,
443+
// begin_to_space,
444+
// end_to_space,
445+
// static_roots as usize,
446+
// );
447447

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

451-
debug_print("### Evacuated static roots, evacuating closure table");
451+
// debug_print("### Evacuated static roots, evacuating closure table");
452452

453453
end_to_space = evacuate(
454454
begin_from_space,
@@ -489,54 +489,7 @@ pub unsafe extern "C" fn rust_collect_garbage() {
489489
0,
490490
);
491491

492-
println!(100, "### GC finished, new hp = {:#x}", new_hp);
493-
}
494-
495-
unsafe fn print_closure_table() {
496-
let closure_tbl = closure_table_loc().unskew() as *const SkewedPtr;
497-
498-
let len = if (*closure_tbl).0 == 0 {
499-
/*
500-
println!(
501-
100,
502-
"closure table ({:#x} --> {:#x}) not initialized ({})",
503-
closure_tbl as usize,
504-
(*closure_tbl).0,
505-
closure_table_size()
506-
);
507-
*/
508-
0
509-
} else {
510-
/*
511-
println!(
512-
100,
513-
"closure table ({:#x} --> {:#x}) size: {} -- {}",
514-
closure_tbl as usize,
515-
(*closure_tbl).0.wrapping_add(1),
516-
(*((*closure_tbl).unskew() as *const Array)).len,
517-
closure_table_size()
518-
);
519-
*/
520-
(*((*closure_tbl).unskew() as *const Array)).len
521-
};
522-
523-
if len == 0 {
524-
println!(50, "### Closure table empty");
525-
return;
526-
}
492+
crate::dump::dump_heap();
527493

528-
let arr = (*closure_tbl).unskew() as *const Array;
529-
println!(
530-
100,
531-
"### Closure table (table address loc={:#x}, table address={:#x})",
532-
closure_tbl as usize,
533-
arr as usize
534-
);
535-
for i in 0..len {
536-
let elem = array_get(arr, i);
537-
if !is_tagged_scalar(SkewedPtr(elem as usize)) {
538-
println!(100, "{}: {:#x}", i, elem.wrapping_add(1));
539-
}
540-
}
541-
println!(50, "### End of closure table");
494+
println!(100, "### GC finished, new hp = {:#x}", new_hp);
542495
}

rts/motoko-rts/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ mod array;
1010
mod common;
1111
mod types;
1212

13+
#[cfg(target_arch = "wasm32")]
14+
mod dump;
15+
1316
#[cfg(target_arch = "wasm32")]
1417
mod alloc;
1518

1619
#[cfg(target_arch = "wasm32")]
17-
#[macro_use]
20+
mod sanity;
21+
22+
#[cfg(target_arch = "wasm32")]
1823
pub mod gc;
1924

2025
#[cfg(target_arch = "wasm32")]

0 commit comments

Comments
 (0)