Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory accounting #207

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"math.h": "c",
"stdbool.h": "c",
"emscripten.h": "c",
"quickjs-atom.h": "c"
"quickjs-atom.h": "c",
"malloc.h": "c"
},
"files.exclude": {
".yarn/releases/*": true
Expand Down
44 changes: 7 additions & 37 deletions c/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,43 +184,13 @@ void QTS_RuntimeSetMemoryLimit(JSRuntime *rt, size_t limit) {
* Memory diagnostics
*/

JSValue *QTS_RuntimeComputeMemoryUsage(JSRuntime *rt, JSContext *ctx) {
JSMemoryUsage s;
JS_ComputeMemoryUsage(rt, &s);

// Note that we're going to allocate more memory just to report the memory usage.
// A more sound approach would be to bind JSMemoryUsage struct directly - but that's
// a lot of work. This should be okay in the mean time.
JSValue result = JS_NewObject(ctx);

// Manually generated via editor-fu from JSMemoryUsage struct definition in quickjs.h
JS_SetPropertyStr(ctx, result, "malloc_limit", JS_NewInt64(ctx, s.malloc_limit));
JS_SetPropertyStr(ctx, result, "memory_used_size", JS_NewInt64(ctx, s.memory_used_size));
JS_SetPropertyStr(ctx, result, "malloc_count", JS_NewInt64(ctx, s.malloc_count));
JS_SetPropertyStr(ctx, result, "memory_used_count", JS_NewInt64(ctx, s.memory_used_count));
JS_SetPropertyStr(ctx, result, "atom_count", JS_NewInt64(ctx, s.atom_count));
JS_SetPropertyStr(ctx, result, "atom_size", JS_NewInt64(ctx, s.atom_size));
JS_SetPropertyStr(ctx, result, "str_count", JS_NewInt64(ctx, s.str_count));
JS_SetPropertyStr(ctx, result, "str_size", JS_NewInt64(ctx, s.str_size));
JS_SetPropertyStr(ctx, result, "obj_count", JS_NewInt64(ctx, s.obj_count));
JS_SetPropertyStr(ctx, result, "obj_size", JS_NewInt64(ctx, s.obj_size));
JS_SetPropertyStr(ctx, result, "prop_count", JS_NewInt64(ctx, s.prop_count));
JS_SetPropertyStr(ctx, result, "prop_size", JS_NewInt64(ctx, s.prop_size));
JS_SetPropertyStr(ctx, result, "shape_count", JS_NewInt64(ctx, s.shape_count));
JS_SetPropertyStr(ctx, result, "shape_size", JS_NewInt64(ctx, s.shape_size));
JS_SetPropertyStr(ctx, result, "js_func_count", JS_NewInt64(ctx, s.js_func_count));
JS_SetPropertyStr(ctx, result, "js_func_size", JS_NewInt64(ctx, s.js_func_size));
JS_SetPropertyStr(ctx, result, "js_func_code_size", JS_NewInt64(ctx, s.js_func_code_size));
JS_SetPropertyStr(ctx, result, "js_func_pc2line_count", JS_NewInt64(ctx, s.js_func_pc2line_count));
JS_SetPropertyStr(ctx, result, "js_func_pc2line_size", JS_NewInt64(ctx, s.js_func_pc2line_size));
JS_SetPropertyStr(ctx, result, "c_func_count", JS_NewInt64(ctx, s.c_func_count));
JS_SetPropertyStr(ctx, result, "array_count", JS_NewInt64(ctx, s.array_count));
JS_SetPropertyStr(ctx, result, "fast_array_count", JS_NewInt64(ctx, s.fast_array_count));
JS_SetPropertyStr(ctx, result, "fast_array_elements", JS_NewInt64(ctx, s.fast_array_elements));
JS_SetPropertyStr(ctx, result, "binary_object_count", JS_NewInt64(ctx, s.binary_object_count));
JS_SetPropertyStr(ctx, result, "binary_object_size", JS_NewInt64(ctx, s.binary_object_size));

return jsvalue_to_heap(result);
JSMemoryUsage *QTS_RuntimeComputeMemoryUsage(JSRuntime *rt) {
JSMemoryUsage *s = malloc(sizeof(JSMemoryUsage));
if (s == NULL) {
return NULL;
}
JS_ComputeMemoryUsage(rt, s);
return s;
}

OwnedHeapChar *QTS_RuntimeDumpMemoryUsage(JSRuntime *rt) {
Expand Down
16 changes: 8 additions & 8 deletions doc/@jitl/quickjs-ffi-types/exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ for the Emscripten stack.

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:246](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L246)
[packages/quickjs-ffi-types/src/emscripten-types.ts:248](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L248)

***

Expand Down Expand Up @@ -145,7 +145,7 @@ State of a promise.

#### Source

[packages/quickjs-ffi-types/src/ffi-types.ts:131](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L131)
[packages/quickjs-ffi-types/src/ffi-types.ts:136](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L136)

***

Expand Down Expand Up @@ -368,7 +368,7 @@ module code

#### Source

[packages/quickjs-ffi-types/src/ffi-types.ts:106](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L106)
[packages/quickjs-ffi-types/src/ffi-types.ts:111](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L111)

***

Expand Down Expand Up @@ -410,7 +410,7 @@ Bitfield options for QTS_GetOwnPropertyNames

#### Source

[packages/quickjs-ffi-types/src/ffi-types.ts:121](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L121)
[packages/quickjs-ffi-types/src/ffi-types.ts:126](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L126)

***

Expand Down Expand Up @@ -488,7 +488,7 @@ Bitfield options for QTS_NewContext intrinsics

#### Source

[packages/quickjs-ffi-types/src/ffi-types.ts:111](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L111)
[packages/quickjs-ffi-types/src/ffi-types.ts:116](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L116)

***

Expand All @@ -512,7 +512,7 @@ Bitfield options for QTS_NewContext intrinsics

#### Source

[packages/quickjs-ffi-types/src/ffi-types.ts:126](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L126)
[packages/quickjs-ffi-types/src/ffi-types.ts:131](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L131)

***

Expand All @@ -536,7 +536,7 @@ Bitfield options for QTS_NewContext intrinsics

#### Source

[packages/quickjs-ffi-types/src/ffi-types.ts:131](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L131)
[packages/quickjs-ffi-types/src/ffi-types.ts:136](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L136)

## Functions

Expand Down Expand Up @@ -569,7 +569,7 @@ Bitfield options for QTS_NewContext intrinsics

#### Source

[packages/quickjs-ffi-types/src/ffi-types.ts:136](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L136)
[packages/quickjs-ffi-types/src/ffi-types.ts:141](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/ffi-types.ts#L141)

***

Expand Down
153 changes: 42 additions & 111 deletions doc/@jitl/quickjs-ffi-types/interfaces/EmscriptenModule.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ QuickJS.

- [Extends](EmscriptenModule.md#extends)
- [Properties](EmscriptenModule.md#properties)
- [FAST\_MEMORY](EmscriptenModule.md#fast-memory)
- [HEAP16](EmscriptenModule.md#heap16)
- [HEAP32](EmscriptenModule.md#heap32)
- [HEAP8](EmscriptenModule.md#heap8)
- [HEAPF32](EmscriptenModule.md#heapf32)
- [HEAPF64](EmscriptenModule.md#heapf64)
- [HEAPU16](EmscriptenModule.md#heapu16)
- [HEAPU32](EmscriptenModule.md#heapu32)
- [HEAPU8](EmscriptenModule.md#heapu8)
- [TOTAL\_MEMORY](EmscriptenModule.md#total-memory)
- [TOTAL\_STACK](EmscriptenModule.md#total-stack)
- [wasmBinary?](EmscriptenModule.md#wasmbinary)
- [wasmMemory?](EmscriptenModule.md#wasmmemory)
- [Methods](EmscriptenModule.md#methods)
- [UTF8ToString()](EmscriptenModule.md#utf8tostring)
- [\_free()](EmscriptenModule.md#free)
- [\_malloc()](EmscriptenModule.md#malloc)
- [cwrap()](EmscriptenModule.md#cwrap)
- [getValue()](EmscriptenModule.md#getvalue)
- [instantiateWasm()?](EmscriptenModule.md#instantiatewasm)
- [lengthBytesUTF8()](EmscriptenModule.md#lengthbytesutf8)
- [locateFile()?](EmscriptenModule.md#locatefile)
Expand All @@ -43,113 +34,13 @@ QuickJS.

## Properties

### FAST\_MEMORY

> **FAST\_MEMORY**: `number`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:168](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L168)

***

### HEAP16

> **HEAP16**: `Int16Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:158](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L158)

***

### HEAP32

> **HEAP32**: `Int32Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:159](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L159)

***

### HEAP8

> **HEAP8**: `Int8Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:157](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L157)

***

### HEAPF32

> **HEAPF32**: `Float32Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:163](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L163)

***

### HEAPF64

> **HEAPF64**: `Float64Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:164](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L164)

***

### HEAPU16

> **HEAPU16**: `Uint16Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:161](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L161)

***

### HEAPU32

> **HEAPU32**: `Uint32Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:162](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L162)

***

### HEAPU8

> **HEAPU8**: `Uint8Array`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:160](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L160)

***

### TOTAL\_MEMORY

> **TOTAL\_MEMORY**: `number`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:167](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L167)

***

### TOTAL\_STACK

> **TOTAL\_STACK**: `number`

#### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:166](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L166)
[packages/quickjs-ffi-types/src/emscripten-types.ts:162](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L162)

***

Expand Down Expand Up @@ -277,6 +168,46 @@ https://emscripten.org/docs/api_reference/preamble.js.html#UTF8ToString

***

### getValue()

#### getValue(ptr, type)

> **getValue**(`ptr`, `type`): `number`

##### Parameters

• **ptr**: `number`

• **type**: `"i8"` \| `"i16"` \| `"i32"` \| `"float"` \| `"double"`

##### Returns

`number`

##### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:155](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L155)

#### getValue(ptr, type)

> **getValue**(`ptr`, `type`): `bigint`

##### Parameters

• **ptr**: `number`

• **type**: `"i64"`

##### Returns

`bigint`

##### Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:156](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L156)

***

### instantiateWasm()?

> **`optional`** **instantiateWasm**(`imports`, `onSuccess`): `Exports` \| `Promise`\<`Exports`\>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

## Source

[packages/quickjs-ffi-types/src/emscripten-types.ts:249](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L249)
[packages/quickjs-ffi-types/src/emscripten-types.ts:251](https://github.com/justjake/quickjs-emscripten/blob/main/packages/quickjs-ffi-types/src/emscripten-types.ts#L251)

***

Expand Down
Loading
Loading