Skip to content

Commit

Permalink
worker: pass resource limits to child.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Mar 14, 2019
1 parent 3947a98 commit c5af49f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/process/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand Down
31 changes: 21 additions & 10 deletions lib/process/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
})
};

Expand Down

0 comments on commit c5af49f

Please sign in to comment.