-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
process: add threadCpuUsage #56467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
process: add threadCpuUsage #56467
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,87 @@ | ||
'use strict'; | ||
|
||
const { isSunOS } = require('../common'); | ||
|
||
const { ok, throws, notStrictEqual } = require('assert'); | ||
|
||
function validateResult(result) { | ||
notStrictEqual(result, null); | ||
|
||
ok(Number.isFinite(result.user)); | ||
ok(Number.isFinite(result.system)); | ||
|
||
ok(result.user >= 0); | ||
ok(result.system >= 0); | ||
} | ||
|
||
// Test that process.threadCpuUsage() works on the main thread | ||
// The if check and the else branch should be removed once SmartOS support is fixed in | ||
// https://github.com/libuv/libuv/issues/4706 | ||
if (!isSunOS) { | ||
const result = process.threadCpuUsage(); | ||
|
||
// Validate the result of calling with no previous value argument. | ||
validateResult(process.threadCpuUsage()); | ||
|
||
// Validate the result of calling with a previous value argument. | ||
validateResult(process.threadCpuUsage(result)); | ||
|
||
// Ensure the results are >= the previous. | ||
let thisUsage; | ||
let lastUsage = process.threadCpuUsage(); | ||
for (let i = 0; i < 10; i++) { | ||
thisUsage = process.threadCpuUsage(); | ||
validateResult(thisUsage); | ||
ok(thisUsage.user >= lastUsage.user); | ||
ok(thisUsage.system >= lastUsage.system); | ||
lastUsage = thisUsage; | ||
} | ||
} else { | ||
throws( | ||
() => process.threadCpuUsage(), | ||
{ | ||
code: 'ERR_OPERATION_FAILED', | ||
name: 'Error', | ||
message: 'Operation failed: threadCpuUsage is not available on SunOS' | ||
} | ||
); | ||
} | ||
|
||
// Test argument validaton | ||
{ | ||
throws( | ||
() => process.threadCpuUsage(123), | ||
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError', | ||
message: 'The "prevValue" argument must be of type object. Received type number (123)' | ||
} | ||
); | ||
|
||
throws( | ||
() => process.threadCpuUsage([]), | ||
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError', | ||
message: 'The "prevValue" argument must be of type object. Received an instance of Array' | ||
} | ||
); | ||
|
||
throws( | ||
() => process.threadCpuUsage({ user: -123 }), | ||
{ | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
name: 'RangeError', | ||
message: "The property 'prevValue.user' is invalid. Received -123" | ||
} | ||
); | ||
|
||
throws( | ||
() => process.threadCpuUsage({ user: 0, system: 'bar' }), | ||
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError', | ||
message: "The \"prevValue.system\" property must be of type number. Received type string ('bar')" | ||
} | ||
); | ||
} |
91 changes: 91 additions & 0 deletions
91
test/parallel/test-process-threadCpuUsage-worker-threads.js
This file contains hidden or 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,91 @@ | ||
'use strict'; | ||
|
||
const { mustCall, platformTimeout, hasCrypto, skip, isSunOS } = require('../common'); | ||
|
||
if (!hasCrypto) { | ||
skip('missing crypto'); | ||
}; | ||
|
||
// This block can be removed once SmartOS support is fixed in | ||
// https://github.com/libuv/libuv/issues/4706 | ||
// The behavior on SunOS is tested in | ||
// test/parallel/test-process-threadCpuUsage-main-thread.js | ||
if (isSunOS) { | ||
skip('Operation not supported yet on SmartOS'); | ||
} | ||
|
||
const { ok } = require('assert'); | ||
const { randomBytes, createHash } = require('crypto'); | ||
const { once } = require('events'); | ||
const { Worker, parentPort, workerData } = require('worker_threads'); | ||
|
||
const FREQUENCIES = [100, 500, 1000]; | ||
|
||
function performLoad() { | ||
const buffer = randomBytes(1e8); | ||
|
||
// Do some work | ||
return setInterval(() => { | ||
createHash('sha256').update(buffer).end(buffer); | ||
}, platformTimeout(workerData?.frequency ?? 100)); | ||
} | ||
|
||
function getUsages() { | ||
return { process: process.cpuUsage(), thread: process.threadCpuUsage() }; | ||
} | ||
|
||
function validateResults(results) { | ||
// This test should have checked that the CPU usage of each thread is greater | ||
// than the previous one, while the process one was not. | ||
// Unfortunately, the real values are not really predictable on the CI so we | ||
// just check that all the values are positive numbers. | ||
for (let i = 0; i < 3; i++) { | ||
ok(typeof results[i].process.user === 'number'); | ||
ok(results[i].process.user >= 0); | ||
|
||
ok(typeof results[i].process.system === 'number'); | ||
ok(results[i].process.system >= 0); | ||
|
||
ok(typeof results[i].thread.user === 'number'); | ||
ok(results[i].thread.user >= 0); | ||
|
||
ok(typeof results[i].thread.system === 'number'); | ||
ok(results[i].thread.system >= 0); | ||
} | ||
} | ||
|
||
// The main thread will spawn three more threads, then after a while it will ask all of them to | ||
// report the thread CPU usage and exit. | ||
if (!workerData?.frequency) { // Do not use isMainThread here otherwise test will not run in --worker mode | ||
const workers = []; | ||
for (const frequency of FREQUENCIES) { | ||
workers.push(new Worker(__filename, { workerData: { frequency } })); | ||
} | ||
|
||
setTimeout(mustCall(async () => { | ||
clearInterval(interval); | ||
|
||
const results = [getUsages()]; | ||
|
||
for (const worker of workers) { | ||
const statusPromise = once(worker, 'message'); | ||
|
||
worker.postMessage('done'); | ||
const [status] = await statusPromise; | ||
results.push(status); | ||
worker.terminate(); | ||
} | ||
|
||
validateResults(results); | ||
}), platformTimeout(5000)); | ||
|
||
} else { | ||
parentPort.on('message', () => { | ||
clearInterval(interval); | ||
parentPort.postMessage(getUsages()); | ||
process.exit(0); | ||
}); | ||
} | ||
|
||
// Perform load on each thread | ||
const interval = performLoad(); |
This file contains hidden or 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 hidden or 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,15 @@ | ||
interface CpuUsageValue { | ||
user: number; | ||
system: number; | ||
} | ||
|
||
declare namespace InternalProcessBinding { | ||
interface Process { | ||
cpuUsage(previousValue?: CpuUsageValue): CpuUsageValue; | ||
threadCpuUsage(previousValue?: CpuUsageValue): CpuUsageValue; | ||
} | ||
} | ||
|
||
export interface ProcessBinding { | ||
process: InternalProcessBinding.Process; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend moving this to C++ side and updating it. A similar implementation exist in node url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will look it up. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to look it up in the code but I couldn't find it.
Do you mind linking a reference to the similar implementation so I can check it out?