Skip to content

Commit

Permalink
feat: Add Deno.memoryUsage() (denoland#9986)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO committed Apr 12, 2021
1 parent fefe93c commit bf99039
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 38 deletions.
9 changes: 9 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,15 @@ declare namespace Deno {
bytesReceived: number;
}

export interface MemoryUsage {
rss: number;
heapTotal: number;
heapUsed: number;
external: number;
}

export function memoryUsage(): MemoryUsage;

export interface RequestEvent {
readonly request: Request;
respondWith(r: Response | Promise<Response>): void;
Expand Down
8 changes: 4 additions & 4 deletions cli/tests/heapstats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"use strict";

function allocTest(alloc, allocAssert, deallocAssert) {
// Helper func that GCs then returns heapStats
// Helper func that GCs then returns memory usage
const sample = () => {
// deno-lint-ignore no-undef
gc();
return Deno.core.heapStats();
return Deno.memoryUsage();
};
const delta = (t1, t2) => t2.usedHeapSize - t1.usedHeapSize;
const delta = (t1, t2) => t2.heapUsed - t1.heapUsed;

// Sample "clean" heapStats
// Sample "clean" heap usage
const t1 = sample();

// Alloc
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3250,7 +3250,7 @@ console.log("finish");
});

itest!(heapstats {
args: "run --quiet --v8-flags=--expose-gc heapstats.js",
args: "run --quiet --unstable --v8-flags=--expose-gc heapstats.js",
output: "heapstats.js.out",
});

Expand Down
51 changes: 18 additions & 33 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ lazy_static::lazy_static! {
function: get_proxy_details.map_fn_to()
},
v8::ExternalReference {
function: heap_stats.map_fn_to(),
function: memory_usage.map_fn_to(),
},
]);
}
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn initialize_context<'s>(
set_func(scope, core_val, "deserialize", deserialize);
set_func(scope, core_val, "getPromiseDetails", get_promise_details);
set_func(scope, core_val, "getProxyDetails", get_proxy_details);
set_func(scope, core_val, "heapStats", heap_stats);
set_func(scope, core_val, "memoryUsage", memory_usage);

// Direct bindings on `window`.
set_func(scope, global, "queueMicrotask", queue_microtask);
Expand Down Expand Up @@ -862,50 +862,35 @@ fn throw_type_error(scope: &mut v8::HandleScope, message: impl AsRef<str>) {
scope.throw_exception(exception);
}

fn heap_stats(
fn memory_usage(
scope: &mut v8::HandleScope,
_args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
let stats = get_heap_stats(scope);
let stats = get_memory_usage(scope);
rv.set(to_v8(scope, stats).unwrap());
}

// HeapStats stores values from a isolate.get_heap_statistics() call
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct HeapStats {
total_heap_size: usize,
total_heap_size_executable: usize,
total_physical_size: usize,
total_available_size: usize,
total_global_handles_size: usize,
used_global_handles_size: usize,
used_heap_size: usize,
heap_size_limit: usize,
malloced_memory: usize,
external_memory: usize,
peak_malloced_memory: usize,
number_of_native_contexts: usize,
number_of_detached_contexts: usize,
struct MemoryUsage {
rss: usize,
heap_total: usize,
heap_used: usize,
external: usize,
// TODO: track ArrayBuffers, would require using a custom allocator to track
// but it's otherwise a subset of external so can be indirectly tracked
// array_buffers: usize,
}
fn get_heap_stats(isolate: &mut v8::Isolate) -> HeapStats {
fn get_memory_usage(isolate: &mut v8::Isolate) -> MemoryUsage {
let mut s = v8::HeapStatistics::default();
isolate.get_heap_statistics(&mut s);

HeapStats {
total_heap_size: s.total_heap_size(),
total_heap_size_executable: s.total_heap_size_executable(),
total_physical_size: s.total_physical_size(),
total_available_size: s.total_available_size(),
total_global_handles_size: s.total_global_handles_size(),
used_global_handles_size: s.used_global_handles_size(),
used_heap_size: s.used_heap_size(),
heap_size_limit: s.heap_size_limit(),
malloced_memory: s.malloced_memory(),
external_memory: s.external_memory(),
peak_malloced_memory: s.peak_malloced_memory(),
number_of_native_contexts: s.number_of_native_contexts(),
number_of_detached_contexts: s.number_of_detached_contexts(),
MemoryUsage {
rss: s.total_physical_size(),
heap_total: s.total_heap_size(),
heap_used: s.used_heap_size(),
external: s.external_memory(),
}
}
1 change: 1 addition & 0 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ delete Object.prototype.__proto__;
[internalSymbol]: internals,
resources: core.resources,
close: core.close,
memoryUsage: core.memoryUsage,
...denoNs,
};
Object.defineProperties(finalDenoNs, {
Expand Down

0 comments on commit bf99039

Please sign in to comment.