From c5af49feeacf0ac60ec4d397b2b924073bd31717 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 13 Mar 2019 17:57:20 -0700 Subject: [PATCH] worker: pass resource limits to child. preparing for: https://github.com/nodejs/node/pull/26628#issuecomment-472599019 --- lib/process/parent.js | 4 +++- lib/process/worker.js | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/process/parent.js b/lib/process/parent.js index dc41d71..a383ba3 100644 --- a/lib/process/parent.js +++ b/lib/process/parent.js @@ -50,7 +50,8 @@ const { const { BTHREADS_WORKER_ID: WORKER_ID, BTHREADS_WORKER_DATA: WORKER_DATA, - BTHREADS_WORKER_STDIN: WORKER_STDIN + BTHREADS_WORKER_STDIN: WORKER_STDIN, + BTHREADS_WORKER_LIMITS: WORKER_LIMITS } = process.env; /** @@ -63,6 +64,7 @@ class Parent extends MessagePortBase { this._workerId = WORKER_ID >>> 0; this._workerData = encoding.parse(WORKER_DATA); + this._workerLimits = encoding.parse(WORKER_LIMITS); this._parser = new Parser(this); this._ports = new Map(); this._closed = false; diff --git a/lib/process/worker.js b/lib/process/worker.js index 7e80a32..2530927 100644 --- a/lib/process/worker.js +++ b/lib/process/worker.js @@ -108,7 +108,6 @@ class Worker extends EventEmitter { _init(file, options) { const bin = process.execPath || process.argv[0]; - const limits = options.resourceLimits; const args = []; // Validate filename. @@ -187,18 +186,29 @@ class Worker extends EventEmitter { } // Enforce resource limits. - if (limits) { + const limits = new Uint32Array(3); + + if (options.resourceLimits) { const argsLen = args.length; - const maxOld = limits.maxOldSpaceSizeMb; - const maxSemi = limits.maxSemiSpaceSizeMb; + const { maxOldSpaceSizeMb, + maxSemiSpaceSizeMb, + codeRangeSizeMb } = options.resourceLimits; + + if (typeof maxOldSpaceSizeMb === 'number') + limits[0] = Math.max(maxOldSpaceSizeMb, 2); + + if (typeof maxSemiSpaceSizeMb === 'number') + limits[1] = maxSemiSpaceSizeMb; + + if (typeof codeRangeSizeMb === 'number') + limits[2] = codeRangeSizeMb; - if (typeof maxOld === 'number') - args.push(`--max-old-space-size=${Math.max(maxOld, 2)}`); + if (limits[0] > 0) + args.push(`--max-old-space-size=${limits[0]}`); - if (typeof maxSemi === 'number') - args.push(`--max-semi-space-size=${maxSemi}`); + if (limits[1] > 0) + args.push(`--max-semi-space-size=${limits[1]}`); - // Todo: figure out how to do codeRangeSizeMb. this._limits = args.length > argsLen; } @@ -226,7 +236,8 @@ class Worker extends EventEmitter { BTHREADS_WORKER_ID: this.threadId.toString(10), BTHREADS_WORKER_DATA: encoding.stringify(options.workerData), BTHREADS_WORKER_STDIN: options.stdin ? '1' : '0', - BTHREADS_WORKER_EVAL: options.eval ? '1' : '0' + BTHREADS_WORKER_EVAL: options.eval ? '1' : '0', + BTHREADS_WORKER_LIMITS: encoding.stringify(limits) }) };