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 12 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.
  • 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.

This commit sets a default thread name whenever
the inspector thread is created (InspectorIo)
Set the worker thread name using worker.name value
and changing the default to "WorkerThread"
@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 88.03%. Comparing base (b3f82fe) to head (a22bc75).
Report is 14 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   88.53%   88.03%   -0.50%     
==========================================
  Files         657      657              
  Lines      190719   190769      +50     
  Branches    36605    36311     -294     
==========================================
- Hits       168848   167952     -896     
- Misses      15040    15957     +917     
- Partials     6831     6860      +29     
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 83.81% <100.00%> (-0.14%) ⬇️
src/inspector_agent.cc 80.02% <0.00%> (-0.12%) ⬇️
src/inspector_io.cc 92.23% <75.00%> (-0.35%) ⬇️

... and 115 files with indirect coverage changes

doc/api/worker_threads.md Outdated Show resolved Hide resolved
Comment on lines 1199 to 1203
* Maximum sizes:
* Windows: 32,767 characters
* macOS: 64 characters
* Other systems: 16 characters
**Default:** `'WorkerThread'`.
Copy link
Member

@santigimeno santigimeno Jan 3, 2025

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants