Skip to content

Commit 3fc49cb

Browse files
committed
Enable heap snapshot tests
1 parent 19ceb1f commit 3fc49cb

File tree

13 files changed

+394
-244
lines changed

13 files changed

+394
-244
lines changed

Cargo.lock

Lines changed: 348 additions & 212 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dora-runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fixedbitset = "*"
2828
num_cpus = "*"
2929
memoffset = "*"
3030
num_enum = "*"
31+
tempfile = "*"
3132

3233
[target.'cfg(windows)'.dependencies.windows-sys]
3334
version = "0.52"

dora-runtime/src/gc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use parking_lot::Mutex;
22

33
use std::cmp::{Ord, Ordering, PartialOrd};
44
use std::fmt;
5-
use std::path::PathBuf;
5+
use std::fs::File;
66
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
77
use std::sync::Arc;
88

@@ -137,7 +137,7 @@ impl Gc {
137137
}
138138

139139
if let Some(ref snapshot_path) = vm.flags.snapshot_on_oom {
140-
let snapshot_path = PathBuf::from(snapshot_path);
140+
let snapshot_path = File::create(snapshot_path).expect("Failed to create file.");
141141
let snapshot = SnapshotGenerator::new(vm, snapshot_path).unwrap();
142142
snapshot.generate().expect("Failed to generate snapshot");
143143
}

dora-runtime/src/gc/pmarking.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::thread;
33
use std::time::Duration;
44

55
use crossbeam_deque::{Injector, Steal, Stealer, Worker};
6-
use rand::distributions::{Distribution, Uniform};
7-
use rand::thread_rng;
6+
use rand::distr::uniform::{UniformSampler, UniformUsize};
87
use scoped_threadpool::Pool;
98

109
use crate::gc::root::Slot;
@@ -184,8 +183,9 @@ impl<'a> MarkingTask<'a> {
184183
return None;
185184
}
186185

187-
let mut rng = thread_rng();
188-
let range = Uniform::new(0, self.stealers.len());
186+
let mut rng = rand::rng();
187+
let range =
188+
UniformUsize::new(0, self.stealers.len()).expect("failed to create UniformUsize.");
189189

190190
for _ in 0..2 * self.stealers.len() {
191191
let mut stealer_id = self.task_id;

dora-runtime/src/gc/swiper/minor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use crate::threads::DoraThread;
1616
use crate::vm::VM;
1717

1818
use crossbeam_deque::{Injector, Steal, Stealer, Worker};
19-
use rand::distributions::{Distribution, Uniform};
20-
use rand::thread_rng;
19+
use rand::distr::uniform::{UniformSampler, UniformUsize};
2120
use scoped_threadpool::Pool;
2221

2322
pub struct MinorCollector<'a> {
@@ -728,8 +727,9 @@ impl<'a> CopyTask<'a> {
728727
return None;
729728
}
730729

731-
let mut rng = thread_rng();
732-
let range = Uniform::new(0, self.stealers.len());
730+
let mut rng = rand::rng();
731+
let range =
732+
UniformUsize::new(0, self.stealers.len()).expect("failed to create UniformUsize.");
733733

734734
for _ in 0..2 * self.stealers.len() {
735735
let mut stealer_id = self.task_id;

dora-runtime/src/snapshot.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::HashMap;
22
use std::fs::File;
33
use std::io::{BufWriter, Result as IoResult};
4-
use std::path::PathBuf;
54
use std::sync::Arc;
65

76
use crate::gc::root::iterate_strong_roots;
@@ -45,9 +44,8 @@ pub struct SnapshotGenerator<'a> {
4544
}
4645

4746
impl<'a> SnapshotGenerator<'a> {
48-
pub fn new(vm: &'a VM, path: PathBuf) -> IoResult<SnapshotGenerator<'a>> {
49-
let f = File::create(path)?;
50-
let writer = BufWriter::new(f);
47+
pub fn new(vm: &'a VM, file: File) -> IoResult<SnapshotGenerator<'a>> {
48+
let writer = BufWriter::new(file);
5149
let placeholder = StringId(0);
5250

5351
Ok(SnapshotGenerator {
@@ -618,7 +616,6 @@ impl<'a> SnapshotGenerator<'a> {
618616
let array: Ref<Array<u8>> = address.into();
619617
let length = array.len();
620618

621-
assert_eq!(shape.instance_size(), 0);
622619
let element_size = shape.element_size();
623620
let mut element_addr = array.data_address();
624621

dora-runtime/src/stdlib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use libc;
22

33
use std::char;
4+
use std::fs::File;
45
use std::io::Write;
56
use std::mem;
67
use std::str;
@@ -89,6 +90,10 @@ pub const STDLIB_FUNCTIONS: &[(&'static str, FctImplementation)] = &[
8990
"stdlib::takeHeapSnapshot",
9091
N(stdlib::take_heap_snapshot as *const u8),
9192
),
93+
(
94+
"stdlib::takeHeapSnapshotForTesting",
95+
N(stdlib::take_heap_snapshot_for_testing as *const u8),
96+
),
9297
// Bool
9398
(
9499
"stdlib::traits::Equals for stdlib::primitives::Bool#equals",
@@ -1089,9 +1094,18 @@ pub extern "C" fn condition_wakeup_all(cond: Handle<Object>) {
10891094

10901095
pub extern "C" fn take_heap_snapshot() {
10911096
use crate::snapshot::SnapshotGenerator;
1092-
use std::path::PathBuf;
10931097

10941098
let vm = get_vm();
1095-
let snapshot = SnapshotGenerator::new(vm, PathBuf::from("dora.heapsnapshot")).unwrap();
1099+
let file = File::create("dora.heapsnapshot").expect("Failed to create file");
1100+
let snapshot = SnapshotGenerator::new(vm, file).unwrap();
1101+
snapshot.generate().expect("Failed to generate snapshot");
1102+
}
1103+
1104+
pub extern "C" fn take_heap_snapshot_for_testing() {
1105+
use crate::snapshot::SnapshotGenerator;
1106+
1107+
let vm = get_vm();
1108+
let file = tempfile::tempfile().expect("Failed to open temporary file.");
1109+
let snapshot = SnapshotGenerator::new(vm, file).unwrap();
10961110
snapshot.generate().expect("Failed to generate snapshot");
10971111
}

pkgs/std/std.dora

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,4 @@ impl StacktraceElement {
200200
}
201201

202202
@internal pub fn takeHeapSnapshot();
203+
@internal pub fn takeHeapSnapshotForTesting();

tests/snapshot/snapshot1.dora

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//= ignore
2-
31
class Foo {
42
name: String,
53
x: Int32,
@@ -11,5 +9,5 @@ class Foo {
119

1210
fn main() {
1311
let foo = Foo(name = "This is a test", x = 1i32, y = 2, f1 = 3.0f32, f2 = 4.5, b = true);
14-
std::takeHeapSnapshot();
12+
std::takeHeapSnapshotForTesting();
1513
}

tests/snapshot/snapshot2.dora

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
//= ignore
2-
31
fn main() {
42
let foo = Vec[String]::new();
53
foo.push("foo");
64
foo.push("bar");
75
foo.push("baz");
8-
std::takeHeapSnapshot();
6+
std::takeHeapSnapshotForTesting();
97
}

0 commit comments

Comments
 (0)