From ab8f38b55105d8c58cfc5d31b0305603f6db1475 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 29 Jan 2020 16:41:26 +0000 Subject: [PATCH] worker: add ability to take heap snapshot from parent thread PR-URL: https://github.com/nodejs/node/pull/31569 Reviewed-By: Denys Otrishko Reviewed-By: James M Snell Reviewed-By: Gus Caplan Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau Reviewed-By: Rich Trott --- doc/api/errors.md | 5 ++ doc/api/worker_threads.md | 17 +++++ lib/internal/errors.js | 1 + lib/internal/heap_utils.js | 41 ++++++++++ lib/internal/worker.js | 12 +++ lib/v8.js | 37 +--------- node.gyp | 1 + src/async_wrap.h | 1 + src/env.h | 3 +- src/heap_utils.cc | 74 ++++++++++--------- src/node_internals.h | 10 +++ src/node_worker.cc | 60 +++++++++++++++ src/node_worker.h | 1 + test/common/heap.js | 11 ++- test/parallel/test-worker-heapdump-failure.js | 15 ++++ test/pummel/test-worker-take-heapsnapshot.js | 22 ++++++ test/sequential/test-async-wrap-getasyncid.js | 1 + 17 files changed, 235 insertions(+), 77 deletions(-) create mode 100644 lib/internal/heap_utils.js create mode 100644 test/parallel/test-worker-heapdump-failure.js create mode 100644 test/pummel/test-worker-take-heapsnapshot.js diff --git a/doc/api/errors.md b/doc/api/errors.md index 34ea548cba62c2..4a3c8e3c8d7520 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2069,6 +2069,11 @@ The `Worker` initialization failed. The `execArgv` option passed to the `Worker` constructor contains invalid flags. + +### `ERR_WORKER_NOT_RUNNING` + +An operation failed because the `Worker` instance is not currently running. + ### `ERR_WORKER_OUT_OF_MEMORY` diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index f9fb8cf4d62d9b..c69d5bfc0f9e2c 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -684,6 +684,21 @@ inside the worker thread. If `stdout: true` was not passed to the [`Worker`][] constructor, then data will be piped to the parent thread's [`process.stdout`][] stream. +### `worker.takeHeapSnapshot()` + + +* Returns: {Promise} A promise for a Readable Stream containing + a V8 heap snapshot + +Returns a readable stream for a V8 snapshot of the current state of the Worker. +See [`v8.getHeapSnapshot()`][] for more details. + +If the Worker thread is no longer running, which may occur before the +[`'exit'` event][] is emitted, the returned `Promise` will be rejected +immediately with an [`ERR_WORKER_NOT_RUNNING`][] error. + ### `worker.terminate()`