Skip to content

Commit 1618632

Browse files
committed
draft
1 parent 18cff60 commit 1618632

File tree

5 files changed

+101
-49
lines changed

5 files changed

+101
-49
lines changed

c/interface.c

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -184,43 +184,13 @@ void QTS_RuntimeSetMemoryLimit(JSRuntime *rt, size_t limit) {
184184
* Memory diagnostics
185185
*/
186186

187-
JSValue *QTS_RuntimeComputeMemoryUsage(JSRuntime *rt, JSContext *ctx) {
188-
JSMemoryUsage s;
189-
JS_ComputeMemoryUsage(rt, &s);
190-
191-
// Note that we're going to allocate more memory just to report the memory usage.
192-
// A more sound approach would be to bind JSMemoryUsage struct directly - but that's
193-
// a lot of work. This should be okay in the mean time.
194-
JSValue result = JS_NewObject(ctx);
195-
196-
// Manually generated via editor-fu from JSMemoryUsage struct definition in quickjs.h
197-
JS_SetPropertyStr(ctx, result, "malloc_limit", JS_NewInt64(ctx, s.malloc_limit));
198-
JS_SetPropertyStr(ctx, result, "memory_used_size", JS_NewInt64(ctx, s.memory_used_size));
199-
JS_SetPropertyStr(ctx, result, "malloc_count", JS_NewInt64(ctx, s.malloc_count));
200-
JS_SetPropertyStr(ctx, result, "memory_used_count", JS_NewInt64(ctx, s.memory_used_count));
201-
JS_SetPropertyStr(ctx, result, "atom_count", JS_NewInt64(ctx, s.atom_count));
202-
JS_SetPropertyStr(ctx, result, "atom_size", JS_NewInt64(ctx, s.atom_size));
203-
JS_SetPropertyStr(ctx, result, "str_count", JS_NewInt64(ctx, s.str_count));
204-
JS_SetPropertyStr(ctx, result, "str_size", JS_NewInt64(ctx, s.str_size));
205-
JS_SetPropertyStr(ctx, result, "obj_count", JS_NewInt64(ctx, s.obj_count));
206-
JS_SetPropertyStr(ctx, result, "obj_size", JS_NewInt64(ctx, s.obj_size));
207-
JS_SetPropertyStr(ctx, result, "prop_count", JS_NewInt64(ctx, s.prop_count));
208-
JS_SetPropertyStr(ctx, result, "prop_size", JS_NewInt64(ctx, s.prop_size));
209-
JS_SetPropertyStr(ctx, result, "shape_count", JS_NewInt64(ctx, s.shape_count));
210-
JS_SetPropertyStr(ctx, result, "shape_size", JS_NewInt64(ctx, s.shape_size));
211-
JS_SetPropertyStr(ctx, result, "js_func_count", JS_NewInt64(ctx, s.js_func_count));
212-
JS_SetPropertyStr(ctx, result, "js_func_size", JS_NewInt64(ctx, s.js_func_size));
213-
JS_SetPropertyStr(ctx, result, "js_func_code_size", JS_NewInt64(ctx, s.js_func_code_size));
214-
JS_SetPropertyStr(ctx, result, "js_func_pc2line_count", JS_NewInt64(ctx, s.js_func_pc2line_count));
215-
JS_SetPropertyStr(ctx, result, "js_func_pc2line_size", JS_NewInt64(ctx, s.js_func_pc2line_size));
216-
JS_SetPropertyStr(ctx, result, "c_func_count", JS_NewInt64(ctx, s.c_func_count));
217-
JS_SetPropertyStr(ctx, result, "array_count", JS_NewInt64(ctx, s.array_count));
218-
JS_SetPropertyStr(ctx, result, "fast_array_count", JS_NewInt64(ctx, s.fast_array_count));
219-
JS_SetPropertyStr(ctx, result, "fast_array_elements", JS_NewInt64(ctx, s.fast_array_elements));
220-
JS_SetPropertyStr(ctx, result, "binary_object_count", JS_NewInt64(ctx, s.binary_object_count));
221-
JS_SetPropertyStr(ctx, result, "binary_object_size", JS_NewInt64(ctx, s.binary_object_size));
222-
223-
return jsvalue_to_heap(result);
187+
JSMemoryUsage *QTS_RuntimeComputeMemoryUsage(JSRuntime *rt) {
188+
JSMemoryUsage *s = malloc(sizeof(JSMemoryUsage));
189+
if (s == NULL) {
190+
return NULL;
191+
}
192+
JS_ComputeMemoryUsage(rt, s);
193+
return s;
224194
}
225195

226196
OwnedHeapChar *QTS_RuntimeDumpMemoryUsage(JSRuntime *rt) {

exportedRuntimeMethods.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
["cwrap", "stringToUTF8", "lengthBytesUTF8", "UTF8ToString", "___lsan_do_recoverable_leak_check"]
1+
[
2+
"cwrap",
3+
"stringToUTF8",
4+
"lengthBytesUTF8",
5+
"UTF8ToString",
6+
"___lsan_do_recoverable_leak_check",
7+
"getValue"
8+
]

packages/quickjs-emscripten-core/src/runtime.ts

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import type { Disposable } from "./lifetime"
1414
import { DisposableResult, Lifetime, Scope, UsingDisposable } from "./lifetime"
1515
import { ModuleMemory } from "./memory"
1616
import type { QuickJSModuleCallbacks, RuntimeCallbacks } from "./module"
17-
import type { ContextOptions, JSModuleLoader, JSModuleNormalizer, QuickJSHandle } from "./types"
17+
import type {
18+
ContextOptions,
19+
JSMemoryUsage,
20+
JSModuleLoader,
21+
JSModuleNormalizer,
22+
QuickJSHandle,
23+
} from "./types"
1824
import { intrinsicsToFlags } from "./types"
1925

2026
/**
@@ -287,17 +293,53 @@ export class QuickJSRuntime extends UsingDisposable implements Disposable {
287293
}
288294

289295
/**
290-
* Compute memory usage for this runtime. Returns the result as a handle to a
291-
* JSValue object. Use {@link QuickJSContext#dump} to convert to a native object.
292-
* Calling this method will allocate more memory inside the runtime. The information
293-
* is accurate as of just before the call to `computeMemoryUsage`.
294-
* For a human-digestible representation, see {@link dumpMemoryUsage}.
296+
* Compute memory usage for this runtime. The information is accurate as of
297+
* just before the call to `computeMemoryUsage`. For a human-digestible
298+
* representation, see {@link dumpMemoryUsage}.
295299
*/
296-
computeMemoryUsage(): QuickJSHandle {
297-
const serviceContextMemory = this.getSystemContext().getMemory(this.rt.value)
298-
return serviceContextMemory.heapValueHandle(
299-
this.ffi.QTS_RuntimeComputeMemoryUsage(this.rt.value, serviceContextMemory.ctx.value),
300-
)
300+
computeMemoryUsage(): JSMemoryUsage {
301+
const ptr = this.ffi.QTS_RuntimeComputeMemoryUsage(this.rt.value)
302+
if (ptr === 0) {
303+
throw new Error("Failed to compute memory usage")
304+
}
305+
306+
let offset = 0
307+
const getNextInt64 = () => {
308+
const value = this.module.getValue(ptr + offset, "i64")
309+
offset += 8
310+
return value
311+
}
312+
const usage: JSMemoryUsage = {
313+
malloc_size: getNextInt64(),
314+
malloc_limit: getNextInt64(),
315+
memory_used_size: getNextInt64(),
316+
malloc_count: getNextInt64(),
317+
memory_used_count: getNextInt64(),
318+
atom_count: getNextInt64(),
319+
atom_size: getNextInt64(),
320+
str_count: getNextInt64(),
321+
str_size: getNextInt64(),
322+
obj_count: getNextInt64(),
323+
obj_size: getNextInt64(),
324+
prop_count: getNextInt64(),
325+
prop_size: getNextInt64(),
326+
shape_count: getNextInt64(),
327+
shape_size: getNextInt64(),
328+
js_func_count: getNextInt64(),
329+
js_func_size: getNextInt64(),
330+
js_func_code_size: getNextInt64(),
331+
js_func_pc2line_count: getNextInt64(),
332+
js_func_pc2line_size: getNextInt64(),
333+
c_func_count: getNextInt64(),
334+
array_count: getNextInt64(),
335+
fast_array_count: getNextInt64(),
336+
fast_array_elements: getNextInt64(),
337+
binary_object_count: getNextInt64(),
338+
binary_object_size: getNextInt64(),
339+
}
340+
341+
this.module._free(ptr)
342+
return usage
301343
}
302344

303345
/**

packages/quickjs-emscripten-core/src/types.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,35 @@ export function concat<T>(...values: Array<T[] | T | undefined>): T[] {
344344
}
345345
return result
346346
}
347+
348+
/**
349+
* Result of computing memory usage of a runtime with {@link QuickJSRuntime#computeMemoryUsage}.
350+
*/
351+
export interface JSMemoryUsage {
352+
malloc_size: number
353+
malloc_limit: number
354+
memory_used_size: number
355+
malloc_count: number
356+
memory_used_count: number
357+
atom_count: number
358+
atom_size: number
359+
str_count: number
360+
str_size: number
361+
obj_count: number
362+
obj_size: number
363+
prop_count: number
364+
prop_size: number
365+
shape_count: number
366+
shape_size: number
367+
js_func_count: number
368+
js_func_size: number
369+
js_func_code_size: number
370+
js_func_pc2line_count: number
371+
js_func_pc2line_size: number
372+
c_func_count: number
373+
array_count: number
374+
fast_array_count: number
375+
fast_array_elements: number
376+
binary_object_count: number
377+
binary_object_size: number
378+
}

packages/quickjs-ffi-types/src/emscripten-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export interface EmscriptenModule extends EmscriptenModuleLoaderOptions {
152152
argTypes: Emscripten.ValueType[],
153153
opts?: Emscripten.CCallOpts,
154154
): (...args: any[]) => any
155+
getValue(ptr: number, type: "i8" | "i16" | "i32" | "i64" | "float" | "double"): number
155156

156157
// USE_TYPED_ARRAYS == 2
157158
HEAP8: Int8Array

0 commit comments

Comments
 (0)