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

Closed
wants to merge 4 commits into 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.17%. Comparing base (793c793) to head (1a588eb).
Report is 37 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.17%   89.17%           
=======================================
  Files         665      665           
  Lines      192602   192610    +8     
  Branches    37058    37053    -5     
=======================================
+ Hits       171748   171756    +8     
+ Misses      13664    13663    -1     
- Partials     7190     7191    +1     
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.28% <100.00%> (-0.30%) ⬇️
src/inspector_agent.cc 79.63% <0.00%> (-0.12%) ⬇️
src/inspector_io.cc 92.23% <75.00%> (-0.35%) ⬇️

... and 24 files with indirect coverage changes

@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

@RafaelGSS RafaelGSS force-pushed the use-uv-thread-setname branch 2 times, most recently 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
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 21, 2025
@nodejs-github-bot

This comment was marked as outdated.

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

Great addition!

@nodejs-github-bot
Copy link
Collaborator

@jasnell
Copy link
Member

jasnell commented Jan 22, 2025

@RafaelGSS ... there are relevant non-flaky test failures in CI

@jasnell jasnell removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jan 22, 2025
@RafaelGSS
Copy link
Member Author

I had to push a fix for AIX. Could you please re-approve so I can land it?

@RafaelGSS RafaelGSS added 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. labels Feb 5, 2025
@RafaelGSS RafaelGSS added the commit-queue Add this label to land a pull request using GitHub Actions. label Feb 6, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Feb 6, 2025
@nodejs-github-bot
Copy link
Collaborator

Landed in 16dc29d...d7aacbe

nodejs-github-bot pushed a commit that referenced this pull request Feb 6, 2025
This commit sets a default thread name whenever
the inspector thread is created (InspectorIo)

PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request Feb 6, 2025
Set the worker thread name using worker.name value
and changing the default to "WorkerThread"

PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request Feb 6, 2025
PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request Feb 6, 2025
PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
@RafaelGSS RafaelGSS added the notable-change PRs with changes that should be highlighted in changelogs. label Feb 6, 2025
Copy link
Contributor

github-actions bot commented Feb 6, 2025

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @RafaelGSS.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

Yeaseen pushed a commit to Yeaseen/node that referenced this pull request Feb 6, 2025
This commit sets a default thread name whenever
the inspector thread is created (InspectorIo)

PR-URL: nodejs#56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Yeaseen pushed a commit to Yeaseen/node that referenced this pull request Feb 6, 2025
Set the worker thread name using worker.name value
and changing the default to "WorkerThread"

PR-URL: nodejs#56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Yeaseen pushed a commit to Yeaseen/node that referenced this pull request Feb 6, 2025
PR-URL: nodejs#56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Yeaseen pushed a commit to Yeaseen/node that referenced this pull request Feb 6, 2025
PR-URL: nodejs#56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
targos pushed a commit that referenced this pull request Feb 7, 2025
This commit sets a default thread name whenever
the inspector thread is created (InspectorIo)

PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
targos pushed a commit that referenced this pull request Feb 7, 2025
Set the worker thread name using worker.name value
and changing the default to "WorkerThread"

PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
targos pushed a commit that referenced this pull request Feb 7, 2025
PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
targos pushed a commit that referenced this pull request Feb 7, 2025
PR-URL: #56416
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
targos added a commit that referenced this pull request Feb 11, 2025
Notable changes:

crypto:
  * (SEMVER-MINOR) support --use-system-ca on Windows (Joyee Cheung) #56833
  * (SEMVER-MINOR) added support for reading certificates from macOS system store (Tim Jacomb) #56599
deps:
  * update timezone to 2025a (Node.js GitHub Bot) #56876
  * (SEMVER-MINOR) update ada to v3.0.1 (Yagiz Nizipli) #56452
deps,tools:
  * (SEMVER-MINOR) add zstd 1.5.6 (Jan Krems) #52100
sqlite:
  * (SEMVER-MINOR) allow returning `ArrayBufferView`s from user-defined functions (René) #56790
src:
  * set signal inspector io thread name (RafaelGSS) #56416
  * set thread name for main thread and v8 worker (RafaelGSS) #56416
  * set worker thread name using worker.name (RafaelGSS) #56416
  * use a default thread name for inspector (RafaelGSS) #56416
src, quic:
  * (SEMVER-MINOR) refine more of the quic implementation (James M Snell) #56328
test:
  * (SEMVER-MINOR) add WPT for URLPattern (Yagiz Nizipli) #56452
url:
  * (SEMVER-MINOR) add URLPattern implementation (Yagiz Nizipli) #56452
zlib:
  * (SEMVER-MINOR) add zstd support (Jan Krems) #52100

PR-URL: TODO
nodejs-github-bot added a commit that referenced this pull request Feb 11, 2025
Notable changes:

crypto:
  * (SEMVER-MINOR) support --use-system-ca on Windows (Joyee Cheung) #56833
  * (SEMVER-MINOR) added support for reading certificates from macOS system store (Tim Jacomb) #56599
deps:
  * update timezone to 2025a (Node.js GitHub Bot) #56876
  * (SEMVER-MINOR) update ada to v3.0.1 (Yagiz Nizipli) #56452
deps,tools:
  * (SEMVER-MINOR) add zstd 1.5.6 (Jan Krems) #52100
sqlite:
  * (SEMVER-MINOR) allow returning `ArrayBufferView`s from user-defined functions (René) #56790
src:
  * set signal inspector io thread name (RafaelGSS) #56416
  * set thread name for main thread and v8 worker (RafaelGSS) #56416
  * set worker thread name using worker.name (RafaelGSS) #56416
  * use a default thread name for inspector (RafaelGSS) #56416
src, quic:
  * (SEMVER-MINOR) refine more of the quic implementation (James M Snell) #56328
test:
  * (SEMVER-MINOR) add WPT for URLPattern (Yagiz Nizipli) #56452
url:
  * (SEMVER-MINOR) add URLPattern implementation (Yagiz Nizipli) #56452
zlib:
  * (SEMVER-MINOR) add zstd support (Jan Krems) #52100

PR-URL: #57005
targos pushed a commit that referenced this pull request Feb 12, 2025
Notable changes:

crypto:
  * (SEMVER-MINOR) support --use-system-ca on Windows (Joyee Cheung) #56833
  * (SEMVER-MINOR) added support for reading certificates from macOS system store (Tim Jacomb) #56599
deps:
  * update timezone to 2025a (Node.js GitHub Bot) #56876
sqlite:
  * (SEMVER-MINOR) allow returning `ArrayBufferView`s from user-defined functions (René) #56790
src:
  * set signal inspector io thread name (RafaelGSS) #56416
  * set thread name for main thread and v8 worker (RafaelGSS) #56416
  * set worker thread name using worker.name (RafaelGSS) #56416
  * use a default thread name for inspector (RafaelGSS) #56416
url:
  * (SEMVER-MINOR) add URLPattern implementation (Yagiz Nizipli) #56452
zlib:
  * (SEMVER-MINOR) add zstd support (Jan Krems) #52100

PR-URL: #57005
targos pushed a commit that referenced this pull request Feb 13, 2025
Notable changes:

crypto:
  * (SEMVER-MINOR) support --use-system-ca on Windows (Joyee Cheung) #56833
  * (SEMVER-MINOR) added support for reading certificates from macOS system store (Tim Jacomb) #56599
deps:
  * update timezone to 2025a (Node.js GitHub Bot) #56876
sqlite:
  * (SEMVER-MINOR) allow returning `ArrayBufferView`s from user-defined functions (René) #56790
src:
  * set signal inspector io thread name (RafaelGSS) #56416
  * set thread name for main thread and v8 worker (RafaelGSS) #56416
  * set worker thread name using worker.name (RafaelGSS) #56416
  * use a default thread name for inspector (RafaelGSS) #56416
url:
  * (SEMVER-MINOR) add URLPattern implementation (Yagiz Nizipli) #56452
zlib:
  * (SEMVER-MINOR) add zstd support (Jan Krems) #52100

PR-URL: #57005
targos pushed a commit that referenced this pull request Feb 13, 2025
Notable changes:

crypto:
  * (SEMVER-MINOR) support --use-system-ca on Windows (Joyee Cheung) #56833
  * (SEMVER-MINOR) added support for reading certificates from macOS system store (Tim Jacomb) #56599
deps:
  * update timezone to 2025a (Node.js GitHub Bot) #56876
sqlite:
  * (SEMVER-MINOR) allow returning `ArrayBufferView`s from user-defined functions (René) #56790
src:
  * set signal inspector io thread name (RafaelGSS) #56416
  * set thread name for main thread and v8 worker (RafaelGSS) #56416
  * set worker thread name using worker.name (RafaelGSS) #56416
  * use a default thread name for inspector (RafaelGSS) #56416
url:
  * (SEMVER-MINOR) add URLPattern implementation (Yagiz Nizipli) #56452
zlib:
  * (SEMVER-MINOR) add zstd support (Jan Krems) #52100

PR-URL: #57005
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. notable-change PRs with changes that should be highlighted in changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants