From 9156d88c4e8626580ba187ba4cbf47ada0544e4c Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Tue, 6 Aug 2024 16:55:08 +0200 Subject: [PATCH] export the function manually --- router-bridge/js-src/plan_worker.ts | 17 +++++++++++------ router-bridge/src/planner.rs | 3 ++- router-bridge/src/worker.rs | 25 ++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/router-bridge/js-src/plan_worker.ts b/router-bridge/js-src/plan_worker.ts index 0be826b8c..7b0bb8953 100644 --- a/router-bridge/js-src/plan_worker.ts +++ b/router-bridge/js-src/plan_worker.ts @@ -15,6 +15,10 @@ declare namespace Deno { } } +function memoryUsage(): MemoryUsage { + return Deno.core.ops.op_runtime_memory_usage(); +} + let logFunction: (message: string) => void; declare let logger: { trace: typeof logFunction; @@ -25,9 +29,9 @@ declare let logger: { }; export interface MemoryUsage { - /** The number of bytes of the current Deno's process resident set size, + /* The number of bytes of the current Deno's process resident set size, * which is the amount of memory occupied in main memory (RAM). */ - rss: number; + //rss: number; /** The total size of the heap for V8, in bytes. */ heapTotal: number; /** The amount of the heap used for V8, in bytes. */ @@ -128,7 +132,7 @@ type ApiSchemaResult = { schema: string; }; type MemoryUsageResult = { - rss: number; + //rss: number; heapTotal: number; heapUsed: number; external: number; @@ -280,6 +284,7 @@ async function run() { try { const { id, payload: event } = await receive(); messageId = id; + try { switch (event?.kind) { case PlannerEventKind.UpdateSchema: @@ -321,12 +326,12 @@ async function run() { case PlannerEventKind.GetHeapStatistics: logger.info(`received event: ${JSON.stringify(event)}`); - const mem = Deno.memoryUsage(); - //const mem = memoryUsage(); + //const mem = Deno.memoryUsage(); + const mem = memoryUsage(); logger.info(`got memoryUsage`); const result: MemoryUsageResult = { - rss: mem.rss, + //rss: mem.rss, heapTotal: mem.heapTotal, heapUsed: mem.heapUsed, external: mem.external, diff --git a/router-bridge/src/planner.rs b/router-bridge/src/planner.rs index 941acae3f..c4ab88cfa 100644 --- a/router-bridge/src/planner.rs +++ b/router-bridge/src/planner.rs @@ -401,7 +401,7 @@ where #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct HeapStatistics { - pub rss: u64, + //pub rss: u64, pub heap_total: u64, pub heap_used: u64, pub external: u64, @@ -787,6 +787,7 @@ mod tests { use futures::stream::StreamExt; use futures::stream::{self}; + use core::panic; use std::collections::BTreeMap; use super::*; diff --git a/router-bridge/src/worker.rs b/router-bridge/src/worker.rs index b789ee415..d54fa72e7 100644 --- a/router-bridge/src/worker.rs +++ b/router-bridge/src/worker.rs @@ -1,7 +1,7 @@ use crate::error::Error; use async_channel::{bounded, Receiver, Sender}; -use deno_core::Op; use deno_core::{op, Extension, OpState}; +use deno_core::{v8, Op}; use rand::rngs::StdRng; use rand::{thread_rng, Rng}; use serde::de::DeserializeOwned; @@ -79,6 +79,7 @@ impl JsWorker { log_warn::DECL, log_error::DECL, op_crypto_get_random_values::DECL, + op_runtime_memory_usage::DECL, ]), op_state_fn: Some(Box::new(move |state| { state.put(response_sender.clone()); @@ -302,6 +303,28 @@ fn op_crypto_get_random_values(state: &mut OpState, out: &mut [u8]) -> Result<() Ok(()) } +// HeapStats stores values from a isolate.get_heap_statistics() call +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct MemoryUsage { + //rss: usize, + heap_total: usize, + heap_used: usize, + external: usize, +} + +#[op(v8)] +fn op_runtime_memory_usage(scope: &mut v8::HandleScope<'_>) -> MemoryUsage { + let mut s = v8::HeapStatistics::default(); + scope.get_heap_statistics(&mut s); + MemoryUsage { + //rss: rss(), + heap_total: s.total_heap_size(), + heap_used: s.used_heap_size(), + external: s.external_memory(), + } +} + #[cfg(test)] mod worker_tests { use super::JsWorker;