From 7a8b65937983f780ffce4e8f584c04fe171682bc Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 8 Mar 2019 19:35:40 +0100 Subject: [PATCH] worker: allow specifying resource limits Allow specifying resource limits for the JS engine instance created as part of a Worker. PR-URL: https://github.com/nodejs/node/pull/26628 Reviewed-By: Joyee Cheung Reviewed-By: Gireesh Punathil Reviewed-By: Benjamin Gruenbaum Reviewed-By: Franziska Hinkelmann --- doc/api/errors.md | 5 + doc/api/worker_threads.md | 48 ++++++ lib/internal/errors.js | 2 + lib/internal/worker.js | 52 +++++- lib/worker_threads.js | 2 + src/node_worker.cc | 157 ++++++++++++++++--- src/node_worker.h | 17 ++ test/parallel/test-worker-resource-limits.js | 58 +++++++ 8 files changed, 315 insertions(+), 26 deletions(-) create mode 100644 test/parallel/test-worker-resource-limits.js diff --git a/doc/api/errors.md b/doc/api/errors.md index 151a6bb5581fa1..01bd6676d89b91 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1990,6 +1990,11 @@ meaning of the error depends on the specific function. The `execArgv` option passed to the `Worker` constructor contains invalid flags. + +### `ERR_WORKER_OUT_OF_MEMORY` + +The `Worker` instance terminated because it reached its memory limit. + ### `ERR_WORKER_PATH` diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 34fd6b7aa94736..e0eac312149c3d 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -157,6 +157,22 @@ console.log(receiveMessageOnPort(port2)); When this function is used, no `'message'` event will be emitted and the `onmessage` listener will not be invoked. +### `worker.resourceLimits` + + +* {Object|undefined} + * `maxYoungGenerationSizeMb` {number} + * `maxOldGenerationSizeMb` {number} + * `codeRangeSizeMb` {number} + +Provides the set of JS engine resource constraints inside this Worker thread. +If the `resourceLimits` option was passed to the [`Worker`][] constructor, +this matches its values. + +If this is used in the main thread, its value is an empty object. + ## `worker.SHARE_ENV` * `filename` {string} The path to the Worker’s main script. Must be either an absolute path or a relative path (i.e. relative to the @@ -519,6 +542,16 @@ if (isMainThread) { occur as described in the [HTML structured clone algorithm][], and an error will be thrown if the object cannot be cloned (e.g. because it contains `function`s). + * `resourceLimits` {Object} An optional set of resource limits for the new + JS engine instance. Reaching these limits will lead to termination of the + `Worker` instance. These limits only affect the JS engine, and no external + data, including no `ArrayBuffer`s. Even if these limits are set, the process + may still abort if it encounters a global out-of-memory situation. + * `maxOldGenerationSizeMb` {number} The maximum size of the main heap in MB. + * `maxYoungGenerationSizeMb` {number} The maximum size of a heap space for + recently created objects. + * `codeRangeSizeMb` {number} The size of a pre-allocated memory range + used for generated code. ### Event: `'error'` + +* {Object} + * `maxYoungGenerationSizeMb` {number} + * `maxOldGenerationSizeMb` {number} + * `codeRangeSizeMb` {number} + +Provides the set of JS engine resource constraints for this Worker thread. +If the `resourceLimits` option was passed to the [`Worker`][] constructor, +this matches its values. + +If the worker has stopped, the return value is an empty object. ### `worker.stderr`