Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ShogunPanda
Copy link
Contributor

This PR add the threadCpuUsage method to process.
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.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. process Issues and PRs related to the process subsystem. labels Jan 4, 2025
@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 4, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 4, 2025
@nodejs-github-bot
Copy link
Collaborator

@santigimeno
Copy link
Member

What about moving the platform dependent code to libuv? Maybe taking libuv/libuv#3120 to the finish line would be a good idea.

Copy link
Member

@anonrig anonrig left a 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

src/node_process_methods.cc Outdated Show resolved Hide resolved
@@ -148,6 +149,46 @@ function wrapProcessMethods(binding) {
};
}

const threadCpuValues = new Float64Array(2);
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

src/node_process_methods.cc Show resolved Hide resolved
src/node_process_methods.cc Outdated Show resolved Hide resolved
src/node_process_methods.cc Outdated Show resolved Hide resolved
src/node_process_methods.cc Show resolved Hide resolved
Comment on lines +154 to +155
// Replace the native function with the JS version that calls the native
// function.
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, two reasons:

  1. I'm not totally familiar with C++ :)
  2. 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.

Comment on lines +158 to +172
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);
}
}
Copy link
Member

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?

Copy link
Contributor Author

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?

@ShogunPanda
Copy link
Contributor Author

What about moving the platform dependent code to libuv? Maybe taking libuv/libuv#3120 to the finish line would be a good idea.

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.

@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 7, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 7, 2025
@nodejs-github-bot
Copy link
Collaborator

@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 7, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 7, 2025
@nodejs-github-bot
Copy link
Collaborator

@juanarbol
Copy link
Member

What about moving the platform dependent code to libuv? Maybe taking libuv/libuv#3120 to the finish line would be a good idea.

if that PR is orphaned, I can work on that one. Can I @jasnell?

@ShogunPanda
Copy link
Contributor Author

@juanarbol Ok, I'll pause on this until you update me about libuv PR. Thanks for taking care of that!

@juanarbol
Copy link
Member

@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

@ShogunPanda
Copy link
Contributor Author

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?

@juanarbol
Copy link
Member

juanarbol commented Jan 9, 2025

Do you have an approximate

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.

@juanarbol
Copy link
Member

juanarbol commented Jan 14, 2025

@ShogunPanda Paolo, the feature is landed and it is about to be released see libuv/libuv#4667

The feature is released!

@ShogunPanda ShogunPanda marked this pull request as ready for review January 18, 2025 07:50
@ShogunPanda
Copy link
Contributor Author

@juanarbol Thanks for your effort on this.
Since #56616 has landed, I updated this to use the new API.
This is now ready to be reviewed!

@ShogunPanda
Copy link
Contributor Author

ShogunPanda commented Feb 4, 2025

@nodejs/build @juanarbol @jasnell I keep changing the worker thread schedule period in the failing test but the thread CPU usage ratio between threads is always very close to 1, while it shouldn't. Can you think of any characteristic of test machines that might explain this?

Most of them are virtual servers? (The implementation of this is hidden from us by most of the providers.)

So in this case I guess I can just check I receive numbers and do not care about the real value, isn't it? Do you think it's enough?

Thanks to the live (a.k.a. in person) suggestion of @mcollina I'm moving this to sequentials, let's hope it fixes it.

Nevermind, it hasn't changed anything. I've changed the test to only check we receive positive numbers.

@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 5, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 5, 2025
@nodejs-github-bot

This comment was marked as outdated.

@ShogunPanda ShogunPanda force-pushed the thread-cpu-usage branch 2 times, most recently from 585603d to f3786f6 Compare February 8, 2025 08:26
@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2025
@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2025
@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2025
@jasnell jasnell removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2025
@nodejs-github-bot

This comment was marked as outdated.

@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 10, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 10, 2025
@nodejs-github-bot

This comment was marked as outdated.

@ShogunPanda ShogunPanda force-pushed the thread-cpu-usage branch 2 times, most recently from 1d114aa to 620c4d6 Compare February 10, 2025 08:50
@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 10, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 10, 2025
@nodejs-github-bot

This comment was marked as outdated.

@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 10, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 10, 2025
@nodejs-github-bot
Copy link
Collaborator

@jasnell
Copy link
Member

jasnell commented Feb 10, 2025

There's at least one relevant failure in the latest CI run still.

// 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');
Copy link
Member

Choose a reason for hiding this comment

The 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.


// Replace the native function with the JS version that calls the native
// function.
function threadCpuUsage(prevValue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you throw a proper error if this is run on SmartOS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. process Issues and PRs related to the process subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants