From 406c024407b915f5267344f3af2f7f51b305bdba Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 29 Jan 2020 16:41:26 +0000 Subject: [PATCH 1/6] worker: add ability to take heap snapshot from parent thread --- doc/api/errors.md | 7 +- 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 | 73 ++++++++++--------- src/node_internals.h | 10 +++ src/node_worker.cc | 59 +++++++++++++++ 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 ++++++ 16 files changed, 233 insertions(+), 78 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..08360e435551f2 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` @@ -2445,7 +2450,7 @@ such as `process.stdout.on('data')`. [`'uncaughtException'`]: process.html#process_event_uncaughtexception [`--force-fips`]: cli.html#cli_force_fips [`Class: assert.AssertionError`]: assert.html#assert_class_assert_assertionerror -[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE +[`ERR_INVALID_ARG_TYPE`]: #errors_err_worker_not_running [`EventEmitter`]: events.html#events_class_eventemitter [`REPL`]: repl.html [`Writable`]: stream.html#stream_class_stream_writable diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 6812e6966dae66..75210466499b4c 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()`