Skip to content

Commit ca7280a

Browse files
authored
Merge pull request #382 from Pivot-Studio/feat/gc_bench
feat: add gc benchmark code
2 parents ced72cd + e7b834c commit ca7280a

File tree

10 files changed

+141
-6
lines changed

10 files changed

+141
-6
lines changed

immix/src/collector.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ impl Collector {
245245
|| (unsafe { self.thread_local_allocator.as_mut().unwrap().should_gc() })
246246
{
247247
self.collect_fast_unwind(sp);
248+
return;
248249
}
249250
let status = self.status.borrow();
250251
if status.collect_threshold < status.bytes_allocated_since_last_gc {
@@ -263,6 +264,9 @@ impl Collector {
263264
return std::ptr::null_mut();
264265
}
265266
unsafe {
267+
let mut status = self.status.borrow_mut();
268+
status.bytes_allocated_since_last_gc += ((size - 1) / LINE_SIZE + 1) * LINE_SIZE;
269+
drop(status);
266270
let ptr = self
267271
.thread_local_allocator
268272
.as_mut()

immix/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub const EVA_BLOCK_PROPORTION: f64 = 0.025;
1212

1313
pub const ALIGN: usize = 4096;
1414

15-
pub const THRESHOLD_PROPORTION: f64 = 1000.75;
15+
pub const THRESHOLD_PROPORTION: f64 = 10.75;
1616

1717
pub const LLVM_GC_STRATEGY_NAME: &str = "plimmix";
1818

planglib/core/gc.pi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub fn DioGC__disable_auto_collect() void;
88

99
pub fn DioGC__enable_auto_collect() void;
1010

11+
pub fn DioGC__get_stw_num() i64;
12+
1113
fn DioGC__stuck_begin(sp:i64) void;
1214

1315
fn DioGC__stuck_end() void;

src/ast/builder/llvmbuilder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,10 +1637,11 @@ impl<'a, 'ctx> LLVMBuilder<'a, 'ctx> {
16371637
// }
16381638
// self.module.strip_debug_info();
16391639
// self.module.strip_debug_info(); // FIXME
1640-
self.module.verify().unwrap_or_else(|_| {
1640+
self.module.verify().unwrap_or_else(|e| {
16411641
panic!(
1642-
"module {} is not valid",
1643-
self.module.get_name().to_str().unwrap()
1642+
"module {} is not valid, err msg: {}",
1643+
self.module.get_name().to_str().unwrap(),
1644+
e
16441645
)
16451646
});
16461647
let used = self.used.borrow();

src/ast/node/macro_nodes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ impl Node for MacroCallNode {
237237
ctx: &'b mut Ctx<'a>,
238238
builder: &'b BuilderEnum<'a, '_>,
239239
) -> NodeResult {
240+
let ctx = &mut ctx.new_child(self.range.start, builder);
241+
ctx.macro_loop_idx = Default::default();
242+
ctx.macro_loop_len = Default::default();
243+
ctx.macro_loop = false;
240244
match &*self.callee {
241245
NodeEnum::ExternIdNode(ex_node) => {
242246
for ns in &ex_node.namespace {

test/gcbench/Kagari.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
entry = "main.pi"
2+
project = "gcbench"

test/gcbench/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GC Benchmark
2+
3+
This is a simple benchmark for testing our gc performance.

test/gcbench/main.pi

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
pub fn main() i64 {
2+
// bench();
3+
for let i = 0; i < 3; i = i + 1 {
4+
bench();
5+
}
6+
let start = millitime();
7+
for let i = 0; i < 30; i = i + 1 {
8+
bench();
9+
}
10+
let end = millitime();
11+
println!("time: ", (end - start)/30);
12+
13+
let stw = gc::DioGC__get_stw_num();
14+
println!("gc stw: ", stw);
15+
return 0;
16+
}
17+
18+
19+
fn millitime() i64;
20+
21+
fn bench() void {
22+
let root = &GCTestObj{};
23+
populate(*K_LONG_LIVED_TREE_DEPTH, root);
24+
let d = *K_MIN_TREE_DEPTH;
25+
while d <= *K_MAX_TREE_DEPTH {
26+
time_construct(d);
27+
d = d + 2;
28+
}
29+
30+
keep_on_stack(root);
31+
return;
32+
}
33+
34+
fn keep_on_stack(p:*GCTestObj) void;
35+
36+
fn time_construct(depth: i64) void {
37+
let i_num_iters = num_iters(depth);
38+
// println!(i_num_iters);
39+
for let i = 0; i < i_num_iters; i = i + 1 {
40+
let temp_tree = make_tree(depth);
41+
populate(depth, temp_tree);
42+
}
43+
for let i = 0; i < i_num_iters; i = i + 1 {
44+
make_tree(depth);
45+
}
46+
return;
47+
}
48+
49+
50+
51+
52+
fn tree_size(i: i64) i64 {
53+
54+
return (1<<(i+1)) -1;
55+
}
56+
57+
58+
var K_STRETCH_TREE_DEPTH = &18;
59+
var K_LONG_LIVED_TREE_DEPTH = & 16;
60+
var K_MIN_TREE_DEPTH = & 4;
61+
var K_MAX_TREE_DEPTH = & 16;
62+
63+
64+
fn num_iters(i:i64) i64 {
65+
66+
return 2 * tree_size(*K_STRETCH_TREE_DEPTH) / tree_size(i);
67+
}
68+
69+
struct GCTestObj {
70+
b:* GCTestObj;
71+
d:u64;
72+
e:*GCTestObj;
73+
d0:u64;
74+
d1:u64;
75+
d2:u64;
76+
d3:u64;
77+
d4:u64;
78+
d5:u64;
79+
d6:u64;
80+
d7:u64;
81+
d8:u64;
82+
d9:u64;
83+
d10:u64;
84+
d11:u64;
85+
86+
87+
}
88+
89+
fn populate(idepth: i64, thisnode: * GCTestObj) void {
90+
if idepth <= 0 {
91+
return;
92+
}
93+
thisnode.b = &GCTestObj{};
94+
thisnode.e = &GCTestObj{};
95+
populate(idepth-1, thisnode.b);
96+
populate(idepth-1, thisnode.e);
97+
return;
98+
}
99+
100+
101+
fn make_tree(idepth: i64) * GCTestObj {
102+
if idepth <= 0 {
103+
return &GCTestObj{};
104+
}
105+
let left = make_tree(idepth-1);
106+
let right = make_tree(idepth-1);
107+
let thisnode = &GCTestObj{b: left, e: right};
108+
return thisnode;
109+
}

vm/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,13 @@ thread_local! {
288288
fn exit_now(code: i64) {
289289
std::process::exit(code as _);
290290
}
291+
292+
#[is_runtime]
293+
fn millitime() -> i64 {
294+
let now = std::time::SystemTime::now();
295+
let duration = now.duration_since(std::time::UNIX_EPOCH).unwrap();
296+
duration.as_millis() as _
297+
}
298+
299+
#[is_runtime]
300+
fn keep_on_stack(_p: i64) {}

vm/src/libcwrap/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ fn getrandom_inner(
120120
) -> libc::ssize_t {
121121
unsafe {
122122
if getentropy(buf as *mut u8, buflen) == 0 {
123-
return buflen as libc::ssize_t;
123+
buflen as libc::ssize_t
124124
} else {
125-
return -1;
125+
-1
126126
}
127127
}
128128
}

0 commit comments

Comments
 (0)