Skip to content

Commit

Permalink
export the function manually
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Aug 6, 2024
1 parent 1a774eb commit 9156d88
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
17 changes: 11 additions & 6 deletions router-bridge/js-src/plan_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand Down Expand Up @@ -128,7 +132,7 @@ type ApiSchemaResult = {
schema: string;
};
type MemoryUsageResult = {
rss: number;
//rss: number;
heapTotal: number;
heapUsed: number;
external: number;
Expand Down Expand Up @@ -280,6 +284,7 @@ async function run() {
try {
const { id, payload: event } = await receive();
messageId = id;

try {
switch (event?.kind) {
case PlannerEventKind.UpdateSchema:
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion router-bridge/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ where
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct HeapStatistics {

Check warning on line 403 in router-bridge/src/planner.rs

View workflow job for this annotation

GitHub Actions / Build Supergraph for macOS x86-64

missing documentation for a struct
pub rss: u64,
//pub rss: u64,
pub heap_total: u64,

Check warning on line 405 in router-bridge/src/planner.rs

View workflow job for this annotation

GitHub Actions / Build Supergraph for macOS x86-64

missing documentation for a struct field
pub heap_used: u64,

Check warning on line 406 in router-bridge/src/planner.rs

View workflow job for this annotation

GitHub Actions / Build Supergraph for macOS x86-64

missing documentation for a struct field
pub external: u64,

Check warning on line 407 in router-bridge/src/planner.rs

View workflow job for this annotation

GitHub Actions / Build Supergraph for macOS x86-64

missing documentation for a struct field
Expand Down Expand Up @@ -787,6 +787,7 @@ mod tests {
use futures::stream::StreamExt;
use futures::stream::{self};

use core::panic;
use std::collections::BTreeMap;

use super::*;
Expand Down
25 changes: 24 additions & 1 deletion router-bridge/src/worker.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9156d88

Please sign in to comment.