-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Conversation
Review requested:
|
What about moving the platform dependent code to libuv? Maybe taking libuv/libuv#3120 to the finish line would be a good idea. |
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.
LGTM except couple of questions and concerns
@@ -148,6 +149,46 @@ function wrapProcessMethods(binding) { | |||
}; | |||
} | |||
|
|||
const threadCpuValues = new Float64Array(2); |
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?
// Replace the native function with the JS version that calls the native | ||
// function. |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Well, two reasons:
- I'm not totally familiar with C++ :)
- I want to keep the most parts we can in JS so that will be easier for a bigger chunk of contributors to chime in.
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); | ||
} | ||
} |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
See above. :)
Anyway, we would have to perform the validation in the C++ side anyway, isn't it?
I'm sorry but I have very limited bandwidth lately. Trying to contribute to a codebase I never already did to it's not feasible for me. Once this lands everyone is absolutely welcome to port this to libuv. |
if that PR is orphaned, I can work on that one. Can I @jasnell? |
@juanarbol Ok, I'll pause on this until you update me about libuv PR. Thanks for taking care of that! |
There we go libuv/libuv#4666 |
Amazing! Keep up posted when this is released in libuv so I can update the dependencies and make this PR much leaner :) Do you have an approximate ETA? |
I do not. They asked for macOS support, I'm gonna send patches on my spare time, I think I have some slots today and tomorrow after work. But idk when is the next release. I just gave support to macOS and addressed all the comments. |
@ShogunPanda Paolo, The feature is released! |
1fdd439
to
5b09f78
Compare
@juanarbol Thanks for your effort on this. |
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.
lgtm
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #56467 +/- ##
==========================================
- Coverage 89.21% 89.21% -0.01%
==========================================
Files 662 662
Lines 191883 191937 +54
Branches 36941 36939 -2
==========================================
+ Hits 171196 171242 +46
- Misses 13536 13540 +4
- Partials 7151 7155 +4
|
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.
LGTM with a minor nit
## `process.threadCpuUsage([previousValue])` | ||
|
||
<!-- YAML | ||
added: v6.1.0 |
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.
This is not true
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.
still lgtm
// previous thread. In theory this should give each thread a load about half of the previous one. | ||
// But since we can't really predict CPU scheduling, we just check a monotonic increasing sequence. | ||
// | ||
ok(threadDifference > 1.2); |
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.
hmmm, are we certain this isn't going to be too flaky across multiple systems in CI?
This PR add the
threadCpuUsage
method toprocess
.The method works exactly like
cpuUsage
but it returns thread specific metrics.This is already implemented (by me :)) in user-land in
https://www.npmjs.com/package/thread-cpu-usage
.The PR is currently a draft as I'm gonna use the CI to see which platform don't support this. I'll add documentation updates once this is solved.