From 33d8e03d9dfb934cf4821feb93f2d38690981480 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 17 Apr 2025 12:30:59 +0200 Subject: [PATCH] worker: add worker.getHeapStatistics() Adds worker.getHeapStatistics() so that the heap usage of the worker could be observer from the parent thread. Signed-off-by: Matteo Collina PR-URL: https://github.com/nodejs/node/pull/57888 Reviewed-By: Yagiz Nizipli Reviewed-By: Chengzhong Wu Reviewed-By: Darshan Sen Reviewed-By: Stephen Belanger --- doc/api/worker_threads.md | 13 ++ lib/internal/worker.js | 11 ++ src/async_wrap.h | 1 + src/env_properties.h | 1 + src/node_worker.cc | 126 ++++++++++++++++++ src/node_worker.h | 2 + test/parallel/test-worker-heap-statistics.js | 63 +++++++++ test/sequential/test-async-wrap-getasyncid.js | 1 + typings/internalBinding/worker.d.ts | 1 + 9 files changed, 219 insertions(+) create mode 100644 test/parallel/test-worker-heap-statistics.js diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index e9219bbea0d..813efe731fc 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -1337,6 +1337,18 @@ If the Worker thread is no longer running, which may occur before the [`'exit'` event][] is emitted, the returned `Promise` is rejected immediately with an [`ERR_WORKER_NOT_RUNNING`][] error. +### `worker.getHeapStatistics()` + + + +* Returns: {Promise} + +This method returns a `Promise` that will resolve to an object identical to [`v8.getHeapStatistics()`][], +or reject with an [`ERR_WORKER_NOT_RUNNING`][] error if the worker is no longer running. +This methods allows the statistics to be observed from outside the actual thread. + ### `worker.performance`