Skip to content

Commit ced1662

Browse files
committed
rt: Generate heap snapshot also on OOM in GC
1 parent 22b122a commit ced1662

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

dora-runtime/src/gc.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,9 @@ impl Gc {
136136
}
137137
}
138138

139-
if let Some(ref snapshot_path) = vm.flags.snapshot_on_oom {
140-
let snapshot_path = File::create(snapshot_path).expect("Failed to create file.");
141-
let snapshot = SnapshotGenerator::new(vm, snapshot_path).unwrap();
142-
snapshot.generate().expect("Failed to generate snapshot");
143-
}
144-
145-
stdlib::trap(Trap::OOM as u8 as u32);
146-
unreachable!()
139+
safepoint::stop_the_world(vm, |threads| {
140+
report_out_of_memory_error(vm, threads);
141+
})
147142
}
148143

149144
fn alloc_in_lab(&self, vm: &VM, size: usize) -> Option<Address> {
@@ -234,6 +229,19 @@ impl Gc {
234229
}
235230
}
236231

232+
fn report_out_of_memory_error(vm: &VM, threads: &[Arc<DoraThread>]) -> ! {
233+
if let Some(ref snapshot_path) = vm.flags.snapshot_on_oom {
234+
let snapshot_path = File::create(snapshot_path).expect("Failed to create file.");
235+
let snapshot = SnapshotGenerator::new(vm, snapshot_path).unwrap();
236+
snapshot
237+
.generate(threads)
238+
.expect("Failed to generate snapshot");
239+
}
240+
241+
stdlib::trap(Trap::OOM as u8 as u32);
242+
unreachable!()
243+
}
244+
237245
pub trait Collector {
238246
// Allocate object of given size.
239247
fn alloc_tlab_area(&self, vm: &VM, size: usize) -> Option<Region>;

dora-runtime/src/gc/swiper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ impl Collector for Swiper {
323323
&self.young,
324324
&vm.flags,
325325
reason,
326+
threads,
326327
);
327328
}
328329

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::time::Instant;
77

88
use crate::gc::swiper::young::YoungGen;
99
use crate::gc::swiper::{CollectionKind, Heap, PAGE_SIZE, align_page_down, align_page_up};
10-
use crate::gc::{AllNumbers, GcReason, formatted_size};
11-
use crate::stdlib;
12-
use crate::vm::{Trap, VM, VmFlags};
10+
use crate::gc::{AllNumbers, GcReason, formatted_size, report_out_of_memory_error};
11+
use crate::threads::DoraThread;
12+
use crate::vm::{VM, VmFlags};
1313

1414
pub fn init(config: &mut HeapController, args: &VmFlags) {
1515
assert!(config.min_heap_size <= config.max_heap_size);
@@ -58,6 +58,7 @@ pub fn stop(
5858
young: &YoungGen,
5959
args: &VmFlags,
6060
reason: GcReason,
61+
threads: &[Arc<DoraThread>],
6162
) {
6263
let mut config = config.lock();
6364

@@ -85,7 +86,7 @@ pub fn stop(
8586
let young_size = align_page_down(target_young_size / 2) * 2;
8687

8788
if old_size + young_size > config.max_heap_size {
88-
stdlib::trap(Trap::OOM as u32);
89+
report_out_of_memory_error(vm, threads);
8990
}
9091

9192
young.resize_after_gc(vm, young_size);

dora-runtime/src/snapshot.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ impl<'a> SnapshotGenerator<'a> {
7171
})
7272
}
7373

74-
pub fn generate(mut self) -> IoResult<()> {
75-
safepoint::stop_the_world(self.vm, |threads| {
76-
self.initialize_strings();
77-
self.iterate_roots(threads);
78-
self.iterate_heap();
79-
self.verify_snapshot();
80-
self.serialize()
81-
})
74+
pub fn generate(mut self, threads: &[Arc<DoraThread>]) -> IoResult<()> {
75+
self.initialize_strings();
76+
self.iterate_roots(threads);
77+
self.iterate_heap();
78+
self.verify_snapshot();
79+
self.serialize()
80+
}
81+
82+
pub fn generate_in_safepoint(self) -> IoResult<()> {
83+
safepoint::stop_the_world(self.vm, |threads| self.generate(threads))
8284
}
8385

8486
fn initialize_strings(&mut self) {

dora-runtime/src/stdlib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,9 @@ pub extern "C" fn take_heap_snapshot() {
10981098
let vm = get_vm();
10991099
let file = File::create("dora.heapsnapshot").expect("Failed to create file");
11001100
let snapshot = SnapshotGenerator::new(vm, file).unwrap();
1101-
snapshot.generate().expect("Failed to generate snapshot");
1101+
snapshot
1102+
.generate_in_safepoint()
1103+
.expect("Failed to generate snapshot");
11021104
}
11031105

11041106
pub extern "C" fn take_heap_snapshot_for_testing() {
@@ -1107,5 +1109,7 @@ pub extern "C" fn take_heap_snapshot_for_testing() {
11071109
let vm = get_vm();
11081110
let file = tempfile::tempfile().expect("Failed to open temporary file.");
11091111
let snapshot = SnapshotGenerator::new(vm, file).unwrap();
1110-
snapshot.generate().expect("Failed to generate snapshot");
1112+
snapshot
1113+
.generate_in_safepoint()
1114+
.expect("Failed to generate snapshot");
11111115
}

0 commit comments

Comments
 (0)