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

src,lib: use uv_thread_setname to a better multi-thread debugging #56416

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RafaelGSS
Copy link
Member

@RafaelGSS RafaelGSS commented Dec 30, 2024

Note: This PR depends libuv/libuv#4599 to be released by libuv team.

TODO:

  • Work on tests
  • I'm also patching uv to set the default thread name, but it's up to them to accept it, I don't know if there's a way from Node.js to access their thread or call an API to change their default thread name Done in src: set a default thread name for workers libuv/libuv#4664
  • A default thread name for the inspector thread has been added
  • A default thread nathe me for signal inspector has been added
  • A default thread name for each one of the Node Platform Workers has been added
  • I'm using worker.name to set the thread name when a worker thread gets created (it defaults to WorkerThread).

cc: @santigimeno


With this PR users will be able to get a more meaningful diagnostic when looking at Node.js threads, for instance, they will be able to see on which thread their app is spending more time (libuv, workers, main thread...). See the image:

Consider the following server (please do not run it in production):

const http = require('http')
const fs = require('node:fs')
const { Worker, isMainThread } = require('node:worker_threads')

if (!isMainThread) {
  console.log('Done :)')
  setTimeout(() => {
    process.exit(0);
  }, 5 * 1000);
  return;
}

let i = 0;
http.createServer((req, res) => {
  fs.readFile(__filename, () => {
    res.end('hello')
  })
  new Worker(__filename, { name: `RafaelGSS (${++i})` });
}).listen(3000)

On Node.js startup if you monitor using htop you should be able to see:

image

Then once it gets a request:

image

V8Worker and libuv threads number depends on the parameters & environments

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Dec 30, 2024
@RafaelGSS RafaelGSS changed the title Use uv thread setname src,lib: use uv_thread_setname to a better multi-thread debugging Dec 30, 2024
@santigimeno
Copy link
Member

The code generally looks fine to me.

  • I'm also patching uv to set the default thread name, but it's up to them to accept it, I don't know if there's a way from Node.js to access their thread or call an API to change their default thread name.

The API to execute code in the the threadpool is uv_queue_work() but I don't think we want that.
Personally, I'd be ok with libuv hard-coding the name to the threads in the threadpool, but maybe a new api could be added for that like a new flag to uv_loop_configure(). I would just open an issue in the libuv repo to discuss this.

@RafaelGSS RafaelGSS force-pushed the use-uv-thread-setname branch from 18c23f5 to ad00536 Compare December 31, 2024 18:57
@@ -1165,6 +1165,7 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
}

if (!(flags & ProcessInitializationFlags::kNoInitializeNodeV8Platform)) {
uv_thread_setname("MainThread");
Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if that's the best place to do it.

@RafaelGSS RafaelGSS marked this pull request as ready for review December 31, 2024 19:05
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm, this looks great. I needed this for a while, but never took the time for the libuv PR.

Copy link

codecov bot commented Dec 31, 2024

Codecov Report

Attention: Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.

Project coverage is 89.21%. Comparing base (fdad2fa) to head (0e1e38e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/inspector_agent.cc 0.00% 1 Missing ⚠️
src/inspector_io.cc 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #56416      +/-   ##
==========================================
+ Coverage   89.20%   89.21%   +0.01%     
==========================================
  Files         662      662              
  Lines      191899   191907       +8     
  Branches    36940    36944       +4     
==========================================
+ Hits       171184   171217      +33     
+ Misses      13546    13545       -1     
+ Partials     7169     7145      -24     
Files with missing lines Coverage Δ
lib/internal/worker.js 99.81% <100.00%> (ø)
src/node.cc 73.54% <100.00%> (+0.03%) ⬆️
src/node_platform.cc 87.86% <100.00%> (+0.02%) ⬆️
src/node_worker.cc 84.49% <100.00%> (+0.34%) ⬆️
src/inspector_agent.cc 80.02% <0.00%> (+0.02%) ⬆️
src/inspector_io.cc 92.23% <75.00%> (-0.35%) ⬇️

... and 28 files with indirect coverage changes

doc/api/worker_threads.md Outdated Show resolved Hide resolved
@RafaelGSS
Copy link
Member Author

Waiting for libuv/libuv#4664 to land & release

Copy link
Member

@santigimeno santigimeno left a comment

Choose a reason for hiding this comment

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

LGTM with a couple of comments

and to the worker title for debugging/identification purposes,
making the final title as `[worker ${id}] ${name}`.
This parameter has a maximum allowed size, depending on the operating
system. If the provided name exceeds the limit, it will be truncated
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
system. If the provided name exceeds the limit, it will be truncated
system. If the provided name exceeds the limit, it will be truncated.

test/addons/uv-thread-name/binding.cc Outdated Show resolved Hide resolved
This commit sets a default thread name whenever
the inspector thread is created (InspectorIo)
@RafaelGSS RafaelGSS force-pushed the use-uv-thread-setname branch from a22bc75 to 4d6293c Compare January 20, 2025 22:08
@RafaelGSS RafaelGSS force-pushed the use-uv-thread-setname branch from 4d6293c to 0e1e38e Compare January 20, 2025 22:10
@RafaelGSS RafaelGSS added the commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. label Jan 20, 2025
@RafaelGSS
Copy link
Member Author

This is now ready to go. PTAL @mcollina @santigimeno @jasnell

Copy link
Member

@santigimeno santigimeno left a comment

Choose a reason for hiding this comment

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

Still LGTM

@RafaelGSS RafaelGSS added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. request-ci Add this label to start a Jenkins CI on a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants