forked from mmtk/mmtk-julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rs
525 lines (462 loc) · 15.9 KB
/
api.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// All functions here are extern function. There is no point for marking them as unsafe.
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use crate::JuliaVM;
use crate::JULIA_HEADER_SIZE;
use crate::SINGLETON;
use crate::{BUILDER, DISABLED_GC, MUTATORS, USER_TRIGGERED_GC};
use libc::c_char;
use log::*;
use mmtk::memory_manager;
use mmtk::scheduler::GCWorker;
use mmtk::util::api_util::NullableObjectReference;
use mmtk::util::opaque_pointer::*;
use mmtk::util::{Address, ObjectReference, OpaquePointer};
use mmtk::AllocationSemantics;
use mmtk::Mutator;
use std::ffi::CStr;
use std::sync::atomic::AtomicIsize;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[no_mangle]
pub extern "C" fn mmtk_gc_init(
min_heap_size: usize,
max_heap_size: usize,
n_gcthreads: usize,
header_size: usize,
buffer_tag: usize,
) {
unsafe {
crate::JULIA_HEADER_SIZE = header_size;
crate::JULIA_BUFF_TAG = buffer_tag;
};
{
let mut builder = BUILDER.lock().unwrap();
// Set plan
use mmtk::util::options::PlanSelector;
let force_plan = if cfg!(feature = "nogc") {
Some(PlanSelector::NoGC)
} else if cfg!(feature = "marksweep") {
Some(PlanSelector::MarkSweep)
} else if cfg!(feature = "immix") {
Some(PlanSelector::Immix)
} else if cfg!(feature = "stickyimmix") {
Some(PlanSelector::StickyImmix)
} else {
None
};
if let Some(plan) = force_plan {
builder.options.plan.set(plan);
}
// Set heap size
let success =
// By default min and max heap size are 0, and we use the Stock GC heuristics
if min_heap_size == 0 && max_heap_size == 0 {
info!(
"Setting mmtk heap size to use Stock GC heuristics as defined in gc_trigger.rs",
);
builder
.options
.gc_trigger
.set(mmtk::util::options::GCTriggerSelector::Delegated)
} else if min_heap_size != 0 {
info!(
"Setting mmtk heap size to a variable size with min-max of {}-{} (in bytes)",
min_heap_size, max_heap_size
);
builder.options.gc_trigger.set(
mmtk::util::options::GCTriggerSelector::DynamicHeapSize(
min_heap_size,
max_heap_size,
),
)
} else {
info!(
"Setting mmtk heap size to a fixed max of {} (in bytes)",
max_heap_size
);
builder.options.gc_trigger.set(
mmtk::util::options::GCTriggerSelector::FixedHeapSize(max_heap_size),
)
};
assert!(
success,
"Failed to set heap size to {}-{}",
min_heap_size, max_heap_size
);
// Set using weak references
let success = builder.options.no_reference_types.set(false);
assert!(success, "Failed to set no_reference_types to false");
// Set GC threads
if n_gcthreads > 0 {
let success = builder.options.threads.set(n_gcthreads);
assert!(success, "Failed to set GC threads to {}", n_gcthreads);
}
}
// Make sure that we haven't initialized MMTk (by accident) yet
assert!(!crate::MMTK_INITIALIZED.load(Ordering::SeqCst));
// Make sure we initialize MMTk here
lazy_static::initialize(&SINGLETON);
// Hijack the panic hook to make sure that if we crash in the GC threads, the process aborts.
crate::set_panic_hook();
// Assert to make sure our fastpath allocation is correct.
{
// If the assertion failed, check the allocation fastpath in Julia
// - runtime fastpath: mmtk_immix_alloc_fast and mmtk_immortal_alloc_fast in julia.h
// - compiler inserted fastpath: llvm-final-gc-lowering.cpp
use mmtk::util::alloc::AllocatorSelector;
let default_allocator = memory_manager::get_allocator_mapping::<JuliaVM>(
&SINGLETON,
AllocationSemantics::Default,
);
assert_eq!(default_allocator, AllocatorSelector::Immix(0));
let immortal_allocator = memory_manager::get_allocator_mapping::<JuliaVM>(
&SINGLETON,
AllocationSemantics::Immortal,
);
assert_eq!(immortal_allocator, AllocatorSelector::BumpPointer(0));
}
// Assert to make sure alignment used in C is correct
{
// If the assertion failed, check MMTK_MIN_ALIGNMENT in julia.h
assert_eq!(<JuliaVM as mmtk::vm::VMBinding>::MIN_ALIGNMENT, 4);
}
}
#[no_mangle]
pub extern "C" fn mmtk_bind_mutator(tls: VMMutatorThread, tid: usize) -> *mut Mutator<JuliaVM> {
let mutator_box = memory_manager::bind_mutator(&SINGLETON, tls);
let res = Box::into_raw(mutator_box);
info!("Binding mutator {:?} to thread id = {}", res, tid);
res
}
#[no_mangle]
pub extern "C" fn mmtk_post_bind_mutator(
mutator: *mut Mutator<JuliaVM>,
original_box_mutator: *mut Mutator<JuliaVM>,
) {
// We have to store the original boxed mutator. Otherwise, we may have dangling pointers in mutator.
MUTATORS.write().unwrap().insert(
Address::from_mut_ptr(mutator),
Address::from_mut_ptr(original_box_mutator),
);
}
#[no_mangle]
pub extern "C" fn mmtk_destroy_mutator(mutator: *mut Mutator<JuliaVM>) {
// destroy the mutator with MMTk.
memory_manager::destroy_mutator(unsafe { &mut *mutator });
let mut mutators = MUTATORS.write().unwrap();
let key = Address::from_mut_ptr(mutator);
// Clear the original boxed mutator
let orig_mutator = mutators.get(&key).unwrap();
let _ = unsafe { Box::from_raw(orig_mutator.to_mut_ptr::<Mutator<JuliaVM>>()) };
// Remove from our hashmap
mutators.remove(&key);
}
#[no_mangle]
pub extern "C" fn mmtk_alloc(
mutator: *mut Mutator<JuliaVM>,
size: usize,
align: usize,
offset: usize,
semantics: AllocationSemantics,
) -> Address {
debug_assert!(
mmtk::util::conversions::raw_is_aligned(
size,
<JuliaVM as mmtk::vm::VMBinding>::MIN_ALIGNMENT
),
"Alloc size {} is not aligned to min alignment",
size
);
memory_manager::alloc::<JuliaVM>(unsafe { &mut *mutator }, size, align, offset, semantics)
}
#[no_mangle]
pub extern "C" fn mmtk_alloc_large(
mutator: *mut Mutator<JuliaVM>,
size: usize,
align: usize,
offset: usize,
) -> Address {
memory_manager::alloc::<JuliaVM>(
unsafe { &mut *mutator },
size,
align,
offset,
AllocationSemantics::Los,
)
}
#[no_mangle]
pub extern "C" fn mmtk_post_alloc(
mutator: *mut Mutator<JuliaVM>,
refer: ObjectReference,
bytes: usize,
semantics: AllocationSemantics,
) {
memory_manager::post_alloc::<JuliaVM>(unsafe { &mut *mutator }, refer, bytes, semantics)
}
#[no_mangle]
pub extern "C" fn mmtk_will_never_move(object: ObjectReference) -> bool {
!object.is_movable()
}
#[no_mangle]
pub extern "C" fn mmtk_start_worker(tls: VMWorkerThread, worker: *mut GCWorker<JuliaVM>) {
let worker = unsafe { Box::from_raw(worker) };
memory_manager::start_worker::<JuliaVM>(&SINGLETON, tls, worker)
}
#[no_mangle]
pub extern "C" fn mmtk_initialize_collection(tls: VMThread) {
memory_manager::initialize_collection(&SINGLETON, tls);
}
#[no_mangle]
pub extern "C" fn mmtk_used_bytes() -> usize {
memory_manager::used_bytes(&SINGLETON)
}
#[no_mangle]
pub extern "C" fn mmtk_free_bytes() -> usize {
memory_manager::free_bytes(&SINGLETON)
}
#[no_mangle]
pub extern "C" fn mmtk_total_bytes() -> usize {
memory_manager::total_bytes(&SINGLETON)
}
#[no_mangle]
pub extern "C" fn mmtk_is_live_object(object: ObjectReference) -> bool {
object.is_live()
}
#[no_mangle]
pub extern "C" fn mmtk_is_mapped_address(address: Address) -> bool {
address.is_mapped()
}
#[no_mangle]
pub extern "C" fn mmtk_handle_user_collection_request(tls: VMMutatorThread, collection: u8) {
AtomicIsize::fetch_add(&USER_TRIGGERED_GC, 1, Ordering::SeqCst);
if AtomicBool::load(&DISABLED_GC, Ordering::SeqCst) {
AtomicIsize::fetch_add(&USER_TRIGGERED_GC, -1, Ordering::SeqCst);
return;
}
// See jl_gc_collection_t
match collection {
// auto
0 => memory_manager::handle_user_collection_request::<JuliaVM>(&SINGLETON, tls),
// full
1 => SINGLETON.handle_user_collection_request(tls, true, true),
// incremental
2 => SINGLETON.handle_user_collection_request(tls, true, false),
_ => unreachable!(),
};
}
#[no_mangle]
pub extern "C" fn mmtk_add_weak_candidate(reff: ObjectReference) {
memory_manager::add_weak_candidate(&SINGLETON, reff)
}
#[no_mangle]
pub extern "C" fn mmtk_add_soft_candidate(reff: ObjectReference) {
memory_manager::add_soft_candidate(&SINGLETON, reff)
}
#[no_mangle]
pub extern "C" fn mmtk_add_phantom_candidate(reff: ObjectReference) {
memory_manager::add_phantom_candidate(&SINGLETON, reff)
}
#[no_mangle]
pub extern "C" fn mmtk_harness_begin(tls: VMMutatorThread) {
memory_manager::harness_begin(&SINGLETON, tls)
}
#[no_mangle]
pub extern "C" fn mmtk_harness_end(_tls: OpaquePointer) {
memory_manager::harness_end(&SINGLETON)
}
#[no_mangle]
pub extern "C" fn mmtk_process(name: *const c_char, value: *const c_char) -> bool {
let name_str: &CStr = unsafe { CStr::from_ptr(name) };
let value_str: &CStr = unsafe { CStr::from_ptr(value) };
let mut builder = BUILDER.lock().unwrap();
memory_manager::process(
&mut builder,
name_str.to_str().unwrap(),
value_str.to_str().unwrap(),
)
}
#[no_mangle]
pub extern "C" fn mmtk_starting_heap_address() -> Address {
memory_manager::starting_heap_address()
}
#[no_mangle]
pub extern "C" fn mmtk_last_heap_address() -> Address {
memory_manager::last_heap_address()
}
// Accessed from C to count the bytes we allocated with jl_gc_counted_malloc etc.
#[no_mangle]
pub static JULIA_MALLOC_BYTES: AtomicUsize = AtomicUsize::new(0);
#[no_mangle]
pub extern "C" fn mmtk_gc_poll(tls: VMMutatorThread) {
memory_manager::gc_poll(&SINGLETON, tls);
}
#[no_mangle]
pub extern "C" fn mmtk_runtime_panic() {
panic!("Panicking at runtime!")
}
#[no_mangle]
pub extern "C" fn mmtk_unreachable() {
unreachable!()
}
#[no_mangle]
#[allow(mutable_transmutes)]
pub extern "C" fn mmtk_set_vm_space(start: Address, size: usize) {
let mmtk: &mmtk::MMTK<JuliaVM> = &SINGLETON;
let mmtk_mut: &mut mmtk::MMTK<JuliaVM> = unsafe { std::mem::transmute(mmtk) };
memory_manager::set_vm_space(mmtk_mut, start, size);
#[cfg(feature = "stickyimmix")]
set_side_log_bit_for_region(start, size);
}
#[no_mangle]
pub extern "C" fn mmtk_memory_region_copy(
mutator: *mut Mutator<JuliaVM>,
src_obj: ObjectReference,
src_addr: Address,
dst_obj: ObjectReference,
dst_addr: Address,
count: usize,
) {
use crate::slots::JuliaMemorySlice;
let src = JuliaMemorySlice {
owner: src_obj,
start: src_addr,
count,
};
let dst = JuliaMemorySlice {
owner: dst_obj,
start: dst_addr,
count,
};
let mutator = unsafe { &mut *mutator };
memory_manager::memory_region_copy(mutator, src, dst);
}
#[no_mangle]
#[allow(unused_variables)] // Args are only used for sticky immix.
pub extern "C" fn mmtk_immortal_region_post_alloc(start: Address, size: usize) {
#[cfg(feature = "stickyimmix")]
set_side_log_bit_for_region(start, size);
}
#[cfg(feature = "stickyimmix")]
fn set_side_log_bit_for_region(start: Address, size: usize) {
debug!("Bulk set {} to {} ({} bytes)", start, start + size, size);
use crate::mmtk::vm::ObjectModel;
match <JuliaVM as mmtk::vm::VMBinding>::VMObjectModel::GLOBAL_LOG_BIT_SPEC.as_spec() {
mmtk::util::metadata::MetadataSpec::OnSide(side) => side.bset_metadata(start, size),
_ => unimplemented!(),
}
}
#[no_mangle]
pub extern "C" fn mmtk_object_reference_write_post(
mutator: *mut Mutator<JuliaVM>,
src: ObjectReference,
target: NullableObjectReference,
) {
let mutator = unsafe { &mut *mutator };
memory_manager::object_reference_write_post(
mutator,
src,
crate::slots::JuliaVMSlot::Simple(mmtk::vm::slot::SimpleSlot::from_address(Address::ZERO)),
target.into(),
)
}
#[no_mangle]
pub extern "C" fn mmtk_object_reference_write_slow(
mutator: &'static mut Mutator<JuliaVM>,
src: ObjectReference,
target: NullableObjectReference,
) {
use mmtk::MutatorContext;
mutator.barrier().object_reference_write_slow(
src,
crate::slots::JuliaVMSlot::Simple(mmtk::vm::slot::SimpleSlot::from_address(Address::ZERO)),
target.into(),
);
}
/// Side log bit is the first side metadata spec starting.
#[no_mangle]
pub static MMTK_SIDE_LOG_BIT_BASE_ADDRESS: Address =
mmtk::util::metadata::side_metadata::GLOBAL_SIDE_METADATA_VM_BASE_ADDRESS;
#[no_mangle]
pub extern "C" fn mmtk_object_is_managed_by_mmtk(addr: usize) -> bool {
crate::api::mmtk_is_mapped_address(unsafe { Address::from_usize(addr) })
}
#[no_mangle]
pub extern "C" fn mmtk_start_spawned_worker_thread(
tls: VMWorkerThread,
ctx: *mut GCWorker<JuliaVM>,
) {
mmtk_start_worker(tls, ctx);
}
#[inline(always)]
pub fn store_obj_size(obj: ObjectReference, size: usize) {
let addr_size = obj.to_raw_address() - 16;
unsafe {
addr_size.store::<u64>(size as u64);
}
}
#[no_mangle]
pub extern "C" fn mmtk_store_obj_size_c(obj: ObjectReference, size: usize) {
let addr_size = obj.to_raw_address() - 16;
unsafe {
addr_size.store::<u64>(size as u64);
}
}
#[no_mangle]
pub extern "C" fn mmtk_get_obj_size(obj: ObjectReference) -> usize {
unsafe {
let addr_size = obj.to_raw_address() - 2 * JULIA_HEADER_SIZE;
addr_size.load::<u64>() as usize
}
}
#[cfg(all(feature = "object_pinning", not(feature = "non_moving")))]
#[no_mangle]
pub extern "C" fn mmtk_pin_object(object: ObjectReference) -> bool {
// We may in the future replace this with a check for the immix space (bound check), which should be much cheaper.
if mmtk_object_is_managed_by_mmtk(object.to_raw_address().as_usize()) {
memory_manager::pin_object(object)
} else {
debug!("Object is not managed by mmtk - (un)pinning it via this function isn't supported.");
false
}
}
#[cfg(all(feature = "object_pinning", not(feature = "non_moving")))]
#[no_mangle]
pub extern "C" fn mmtk_unpin_object(object: ObjectReference) -> bool {
if mmtk_object_is_managed_by_mmtk(object.to_raw_address().as_usize()) {
memory_manager::unpin_object(object)
} else {
debug!("Object is not managed by mmtk - (un)pinning it via this function isn't supported.");
false
}
}
#[cfg(all(feature = "object_pinning", not(feature = "non_moving")))]
#[no_mangle]
pub extern "C" fn mmtk_is_pinned(object: ObjectReference) -> bool {
if mmtk_object_is_managed_by_mmtk(object.to_raw_address().as_usize()) {
memory_manager::is_pinned(object)
} else {
debug!("Object is not managed by mmtk - checking via this function isn't supported.");
false
}
}
// If the `non-moving` feature is selected, pinning/unpinning is a noop and simply returns false
#[cfg(all(feature = "object_pinning", feature = "non_moving"))]
#[no_mangle]
pub extern "C" fn mmtk_pin_object(_object: ObjectReference) -> bool {
false
}
#[cfg(all(feature = "object_pinning", feature = "non_moving"))]
#[no_mangle]
pub extern "C" fn mmtk_unpin_object(_object: ObjectReference) -> bool {
false
}
#[cfg(all(feature = "object_pinning", feature = "non_moving"))]
#[no_mangle]
pub extern "C" fn mmtk_is_pinned(_object: ObjectReference) -> bool {
false
}
#[no_mangle]
pub extern "C" fn get_mmtk_version() -> *const c_char {
crate::build_info::MMTK_JULIA_FULL_VERSION_STRING
.as_c_str()
.as_ptr() as _
}