-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: allow retrieving elu from parent
Usually, when extracting the ELU from a specific JS thread is better to do it from a different thread as the event loop we're observing might already be blocked. The `Worker.performance.eventLoopUtilization()` method allows us to do this for worker threads, but there's not a way to do this for the main thread. This new API, which allows us to retrieve the ELU of the parent thread from a specific worker, is going to enable this. For the moment, I have defined this new API in ``` require('worker_threads').parent.performance.eventLoopUtilization() ``` though I haven't added documentation yet as a) I want to know first whether this approach is acceptable, and in case it is, b) I'm not really sure whether that's the place the API should live in. Would love receiving feedback on this.
- Loading branch information
1 parent
28bf031
commit c47419e
Showing
7 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
test/parallel/test-worker-parent-performance-eventlooputil.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
const { mustCall } = require('../common'); | ||
|
||
const TIMEOUT = 10; | ||
const SPIN_DUR = 50; | ||
|
||
const assert = require('assert'); | ||
const { Worker, parent, workerData } = require('worker_threads'); | ||
|
||
// Do not use isMainThread directly, otherwise the test would time out in case | ||
// it's started inside of another worker thread. | ||
if (!process.env.HAS_STARTED_WORKER) { | ||
process.env.HAS_STARTED_WORKER = '1'; | ||
const i32arr = new Int32Array(new SharedArrayBuffer(4)); | ||
const w = new Worker(__filename, { workerData: i32arr }); | ||
w.on('online', mustCall(() => { | ||
Atomics.wait(i32arr, 0, 0); | ||
|
||
const t = Date.now(); | ||
while (Date.now() - t < SPIN_DUR); | ||
|
||
Atomics.store(i32arr, 0, 0); | ||
Atomics.notify(i32arr, 0); | ||
Atomics.wait(i32arr, 0, 0); | ||
})); | ||
} else { | ||
setTimeout(() => { | ||
const { eventLoopUtilization } = parent.performance; | ||
const i32arr = workerData; | ||
const elu1 = eventLoopUtilization(); | ||
|
||
Atomics.store(i32arr, 0, 1); | ||
Atomics.notify(i32arr, 0); | ||
Atomics.wait(i32arr, 0, 1); | ||
|
||
const elu2 = eventLoopUtilization(elu1); | ||
const elu3 = eventLoopUtilization(); | ||
const elu4 = eventLoopUtilization(elu3, elu1); | ||
|
||
assert.strictEqual(elu2.idle, 0); | ||
assert.strictEqual(elu4.idle, 0); | ||
assert.strictEqual(elu2.utilization, 1); | ||
assert.strictEqual(elu4.utilization, 1); | ||
assert.strictEqual(elu3.active - elu1.active, elu4.active); | ||
assert.ok(elu2.active > SPIN_DUR - 10, `${elu2.active} <= ${SPIN_DUR - 10}`); | ||
assert.ok(elu2.active < elu4.active, `${elu2.active} >= ${elu4.active}`); | ||
assert.ok(elu3.active > elu2.active, `${elu3.active} <= ${elu2.active}`); | ||
assert.ok(elu3.active > elu4.active, `${elu3.active} <= ${elu4.active}`); | ||
|
||
Atomics.store(i32arr, 0, 1); | ||
Atomics.notify(i32arr, 0); | ||
}, TIMEOUT); | ||
} |