From a802d317f8c863d60ddf5f0507a7157317b637a3 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 2 Jul 2015 22:15:04 -0700 Subject: [PATCH] Add glue to use SpiderMonkey's memory reporting from Rust code. --- src/glue.rs | 1 + src/jsapi.rs | 9 +++++++++ src/jsglue.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/glue.rs b/src/glue.rs index bab3d01a1..1a59585a2 100644 --- a/src/glue.rs +++ b/src/glue.rs @@ -229,4 +229,5 @@ extern "C" { pub fn AppendToAutoObjectVector(v: *mut AutoObjectVector, obj: *mut JSObject) -> u8; pub fn DeleteAutoObjectVector(v: *mut AutoObjectVector); + pub fn CollectServoSizes(rt: *mut JSRuntime, sizes: *mut ServoSizes) -> bool; } diff --git a/src/jsapi.rs b/src/jsapi.rs index 453c221a9..e6fcdf1f5 100644 --- a/src/jsapi.rs +++ b/src/jsapi.rs @@ -130,6 +130,15 @@ pub enum JSVersion { JSVERSION_UNKNOWN = -1, JSVERSION_LATEST = 185, } +#[repr(C)] +pub struct ServoSizes { + pub gcHeapUsed: ::libc::size_t, + pub gcHeapUnused: ::libc::size_t, + pub gcHeapAdmin: ::libc::size_t, + pub gcHeapDecommitted: ::libc::size_t, + pub mallocHeap: ::libc::size_t, + pub nonHeap: ::libc::size_t, +} #[repr(i32)] #[derive(Copy, Clone)] pub enum JSType { diff --git a/src/jsglue.cpp b/src/jsglue.cpp index 6a9fa4364..330ecb116 100644 --- a/src/jsglue.cpp +++ b/src/jsglue.cpp @@ -17,6 +17,7 @@ #include "js/Proxy.h" #include "js/Class.h" #include "jswrapper.h" +#include "js/MemoryMetrics.h" #include "assert.h" @@ -640,4 +641,32 @@ DeleteAutoObjectVector(JS::AutoObjectVector* v) delete v; } +#if defined(__linux__) + #include +#elif defined(__APPLE__) + #include +#else + #error "unsupported platform" +#endif + +// SpiderMonkey-in-Rust currently uses system malloc, not jemalloc. +static size_t MallocSizeOf(const void* aPtr) +{ +#if defined(__linux__) + return malloc_usable_size((void*)aPtr); +#elif defined(__APPLE__) + return malloc_size((void*)aPtr); +#else + #error "unsupported platform" +#endif +} + +bool +CollectServoSizes(JSRuntime *rt, JS::ServoSizes *sizes) +{ + mozilla::PodZero(sizes); + return JS::AddServoSizeOf(rt, MallocSizeOf, + /* ObjectPrivateVisitor = */ nullptr, sizes); +} + } // extern "C"