Skip to content

Commit

Permalink
Add memoryUsage()
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Sep 2, 2024
1 parent 18c2846 commit 3e2f744
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,35 @@ bare_os_resource_usage (js_env_t *env, js_callback_info_t *info) {
return result;
}

static js_value_t *
bare_os_memory_usage (js_env_t *env, js_callback_info_t *info) {
int err;

js_heap_statistics_t stats = {.version = 0};
err = js_get_heap_statistics(env, &stats);
assert(err == 0);

js_value_t *result;
err = js_create_object(env, &result);
assert(err == 0);

#define V(name, property) \
{ \
js_value_t *value; \
err = js_create_int64(env, stats.property, &value); \
assert(err == 0); \
\
err = js_set_named_property(env, result, name, value); \
assert(err == 0); \
}

V("heapTotal", total_heap_size)
V("heapUsed", used_heap_size)
#undef V

return result;
}

static js_value_t *
bare_os_get_process_title (js_env_t *env, js_callback_info_t *info) {
int err;
Expand Down Expand Up @@ -625,6 +654,7 @@ bare_os_exports (js_env_t *env, js_value_t *exports) {
V("hostname", bare_os_hostname)
V("kill", bare_os_kill)
V("resourceUsage", bare_os_resource_usage)
V("memoryUsage", bare_os_memory_usage)
V("getProcessTitle", bare_os_get_process_title)
V("setProcessTitle", bare_os_set_process_title)
V("getEnvKeys", bare_os_get_env_keys)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exports.kill = function kill (pid, signal = constants.signals.SIGTERM) {
}

exports.resourceUsage = binding.resourceUsage
exports.memoryUsage = binding.memoryUsage
exports.getProcessTitle = binding.getProcessTitle

exports.setProcessTitle = function setProcessTitle (title) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"url": "https://github.com/holepunchto/bare-os/issues"
},
"homepage": "https://github.com/holepunchto/bare-os#readme",
"engines": {
"bare": ">=1.6.0"
},
"devDependencies": {
"brittle": "^3.1.1",
"standard": "^17.0.0"
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ test('process title', (t) => {
test('resource usage', (t) => {
t.comment(os.resourceUsage())
})

test('memory usage', (t) => {
t.comment(os.memoryUsage())
})

0 comments on commit 3e2f744

Please sign in to comment.