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

Draft
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

return THROW_ERR_THREAD_CPU_USAGE_FAILED(env);
}

fields[0] = ((((uint64_t)user_time.dwHighDateTime << 32) |
Copy link
Member

Choose a reason for hiding this comment

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

Can we use static cast rather than C cast?

@@ -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

//
// Returns those values as Float64 microseconds in the elements of the array
// passed to the function.
static void ThreadCPUUsage(const FunctionCallbackInfo<Value>& args) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: we can add a fast api call for this as well.


struct rusage usage;

if (getrusage(RUSAGE_THREAD, &usage) == -1) {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment to each of these mysterious functions and explain what they do? It might be tricky for someone else who comes across this code in a few years.

fields[1] = MICROS_PER_SEC * usage.system_time.seconds +
usage.system_time.microseconds;
#elif _WIN32
HANDLE thread_ = GetCurrentThread();
Copy link
Member

Choose a reason for hiding this comment

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

Can this throw? I'm not familiar with the API

@@ -650,6 +715,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, target, "availableMemory", GetAvailableMemory);
SetMethod(isolate, target, "rss", Rss);
SetMethod(isolate, target, "cpuUsage", CPUUsage);
SetMethod(isolate, target, "threadCpuUsage", ThreadCPUUsage);
Copy link
Member

Choose a reason for hiding this comment

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

Could you also update the typings folder with the correct type? That way, we’ll have even better intellisense support!

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.

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?

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants