-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
process: add threadCpuUsage #56467
base: main
Are you sure you want to change the base?
process: add threadCpuUsage #56467
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ function nop() {} | |
function wrapProcessMethods(binding) { | ||
const { | ||
cpuUsage: _cpuUsage, | ||
threadCpuUsage: _threadCpuUsage, | ||
memoryUsage: _memoryUsage, | ||
rss, | ||
resourceUsage: _resourceUsage, | ||
|
@@ -148,6 +149,46 @@ function wrapProcessMethods(binding) { | |
}; | ||
} | ||
|
||
const threadCpuValues = new Float64Array(2); | ||
|
||
// Replace the native function with the JS version that calls the native | ||
// function. | ||
Comment on lines
+154
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we just implement the whole thing in C++? I’m a bit confused about this particular comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, two reasons:
|
||
function threadCpuUsage(prevValue) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you throw a proper error if this is run on SmartOS? |
||
// If a previous value was passed in, ensure it has the correct shape. | ||
if (prevValue) { | ||
if (!previousValueIsValid(prevValue.user)) { | ||
validateObject(prevValue, 'prevValue'); | ||
|
||
validateNumber(prevValue.user, 'prevValue.user'); | ||
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user', | ||
prevValue.user); | ||
} | ||
|
||
if (!previousValueIsValid(prevValue.system)) { | ||
validateNumber(prevValue.system, 'prevValue.system'); | ||
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.system', | ||
prevValue.system); | ||
} | ||
} | ||
Comment on lines
+158
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a lot of this functionality can be removed if we move the implementation to cpp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. :) |
||
|
||
// Call the native function to get the current values. | ||
_threadCpuUsage(threadCpuValues); | ||
|
||
// If a previous value was passed in, return diff of current from previous. | ||
if (prevValue) { | ||
return { | ||
user: threadCpuValues[0] - prevValue.user, | ||
system: threadCpuValues[1] - prevValue.system, | ||
}; | ||
} | ||
|
||
// If no previous value passed in, return current value. | ||
return { | ||
user: threadCpuValues[0], | ||
system: threadCpuValues[1], | ||
}; | ||
} | ||
|
||
// Ensure that a previously passed in value is valid. Currently, the native | ||
// implementation always returns numbers <= Number.MAX_SAFE_INTEGER. | ||
function previousValueIsValid(num) { | ||
|
@@ -263,6 +304,7 @@ function wrapProcessMethods(binding) { | |
return { | ||
_rawDebug, | ||
cpuUsage, | ||
threadCpuUsage, | ||
resourceUsage, | ||
memoryUsage, | ||
kill, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'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/nodejs/node/pull/56467#issuecomment-2628877767 | ||
|
||
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: 'ENOTSUP', | ||
name: 'Error', | ||
message: 'ENOTSUP: operation not supported on socket, uv_getrusage_thread' | ||
} | ||
); | ||
} | ||
|
||
// 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')" | ||
} | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'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/nodejs/node/pull/56467#issuecomment-2628877767 | ||
if (isSunOS) { | ||
skip('Operation not supported on SmartOS'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given it's a public API, I think having a test showing the behavior that happens in SmartOS is sound. Also, don't link to a comment, but open an issue on libuv for this. |
||
} | ||
|
||
const { ok, deepStrictEqual } = require('assert'); | ||
const { randomBytes, createHash } = require('crypto'); | ||
const { once } = require('events'); | ||
const { Worker, isMainThread, parentPort, threadId, 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(isMainThread ? 100 : workerData.frequency)); | ||
} | ||
|
||
function getUsages() { | ||
return { threadId, process: process.cpuUsage(), thread: process.threadCpuUsage() }; | ||
} | ||
|
||
function validateResults(results) { | ||
for (let i = 0; i < 4; i++) { | ||
deepStrictEqual(results[i].threadId, i); | ||
} | ||
|
||
// 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 (isMainThread) { | ||
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'); | ||
const exitPromise = once(worker, 'exit'); | ||
|
||
worker.postMessage('done'); | ||
const [status] = await statusPromise; | ||
results.push(status); | ||
await exitPromise; | ||
} | ||
|
||
validateResults(results); | ||
}), platformTimeout(5000)); | ||
|
||
} else { | ||
parentPort.on('message', () => { | ||
clearInterval(interval); | ||
parentPort.postMessage(getUsages()); | ||
process.exit(0); | ||
}); | ||
} | ||
|
||
// Perform load on each thread | ||
const interval = performLoad(); |
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; | ||
} |
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?