use mozjs::context::JSContext;
use mozjs::glue::BaoRuntimeStatsPOD;
use mozjs::jsapi::ServoSizes;
use mozjs::rust::wrappers2::BaoCollectRuntimeStats;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EngineMemoryStats {
pub gc_heap_chunk_total: usize,
pub gc_heap_gc_things: usize,
pub zone_unused_gc_things: usize,
pub zone_live_gc_things: usize,
pub realm_live_gc_things: usize,
pub zone_count: usize,
pub realm_count: usize,
pub servo_gc_heap_used: usize,
pub servo_gc_heap_unused: usize,
pub servo_gc_heap_admin: usize,
pub servo_gc_heap_decommitted: usize,
pub servo_malloc_heap: usize,
pub servo_non_heap: usize,
}
impl EngineMemoryStats {
pub fn live_gc_things_parts_sum(&self) -> usize {
self.zone_live_gc_things + self.realm_live_gc_things
}
}
#[doc(hidden)]
pub unsafe fn collect_runtime_stats(cx: *mut mozjs::jsapi::JSContext) -> Result<EngineMemoryStats, String> {
let cx_nn = match std::ptr::NonNull::new(cx) {
Some(nn) => nn,
None => return Err("collect_runtime_stats: null JSContext".into()),
};
let mut cx = JSContext::from_ptr(cx_nn);
let mut servo = ServoSizes {
gcHeapUsed: 0,
gcHeapUnused: 0,
gcHeapAdmin: 0,
gcHeapDecommitted: 0,
mallocHeap: 0,
nonHeap: 0,
};
let mut pod = BaoRuntimeStatsPOD {
gcHeapChunkTotal: 0,
gcHeapGCThings: 0,
zoneUnusedGcThings: 0,
zoneLiveGcThings: 0,
realmLiveGcThings: 0,
zoneCount: 0,
realmCount: 0,
};
if !unsafe { BaoCollectRuntimeStats(&mut cx, &mut servo, &mut pod) } {
return Err("JS::CollectRuntimeStats failed (engine OOM or traversal error)".into());
}
Ok(EngineMemoryStats {
gc_heap_chunk_total: pod.gcHeapChunkTotal,
gc_heap_gc_things: pod.gcHeapGCThings,
zone_unused_gc_things: pod.zoneUnusedGcThings,
zone_live_gc_things: pod.zoneLiveGcThings,
realm_live_gc_things: pod.realmLiveGcThings,
zone_count: pod.zoneCount,
realm_count: pod.realmCount,
servo_gc_heap_used: servo.gcHeapUsed,
servo_gc_heap_unused: servo.gcHeapUnused,
servo_gc_heap_admin: servo.gcHeapAdmin,
servo_gc_heap_decommitted: servo.gcHeapDecommitted,
servo_malloc_heap: servo.mallocHeap,
servo_non_heap: servo.nonHeap,
})
}