From 875a4d1a58d6dec80518c9d2a05ae6107c70b066 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 18396f0ef8dfad..b186275807aee7 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2053,6 +2053,11 @@ The WASI instance has already started. 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 6812e6966dae66..6ae0ada97f193a 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -674,6 +674,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()`