-
Notifications
You must be signed in to change notification settings - Fork 92
Name KvikIO thread-pool workers for profiling #1014
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
Open
vyasr
wants to merge
3
commits into
rapidsai:main
Choose a base branch
from
vyasr:feat/name_threads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,58 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <atomic> | ||
| #include <cstdio> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include <BS_thread_pool.hpp> | ||
|
|
||
| #include <pthread.h> | ||
|
|
||
| namespace kvikio { | ||
|
|
||
| /** | ||
| * @brief Thread pool type used for parallel I/O operations. | ||
| */ | ||
| using ThreadPool = BS::thread_pool; | ||
|
|
||
| /** | ||
| * @brief Build a `BS::thread_pool` init task that names each worker thread. | ||
| * | ||
| * The returned functor is intended to be passed to `BS::thread_pool`'s | ||
| * constructor or `reset()` overloads that accept an `init_task`. It runs | ||
| * once per worker as the pool starts each thread, setting the OS-level | ||
| * thread name (`comm` on Linux) to `"<prefix>-<index>"` so profilers such | ||
| * as nsys, `top -H`, and `/proc/<pid>/task/<tid>/comm` show meaningful | ||
| * names instead of the parent process name. | ||
| * | ||
| * The per-thread index is assigned via an atomic counter captured by the | ||
| * returned functor, so different pools can share the same prefix without | ||
| * colliding. | ||
| * | ||
| * Linux caps thread names at 15 characters plus NUL, so keep @p prefix | ||
| * short (typically 10 characters or fewer). | ||
| * | ||
| * @param prefix Name prefix, e.g. `"kvikio"` or `"kvikio-dev"`. | ||
| * @return An init task suitable for `BS::thread_pool`. | ||
| */ | ||
| [[nodiscard]] inline std::function<void()> make_thread_pool_init_task(std::string prefix) | ||
| { | ||
| auto counter = std::make_shared<std::atomic<unsigned int>>(0); | ||
| return [counter = std::move(counter), prefix = std::move(prefix)]() { | ||
| unsigned int const idx = counter->fetch_add(1, std::memory_order_relaxed); | ||
| // Linux comm limit is 15 chars + NUL. | ||
| std::array<char, 16> name{}; | ||
| std::snprintf(name.data(), name.size(), "%s-%u", prefix.c_str(), idx); | ||
| pthread_setname_np(pthread_self(), name.data()); | ||
| }; | ||
| } | ||
|
|
||
| } // namespace kvikio | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. | ||
| * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
|
|
@@ -80,7 +80,8 @@ ThreadPool* get_thread_pool_per_block_device(std::string const& file_path) | |
| } | ||
|
|
||
| // First file on this block device: create a new dedicated thread pool | ||
| auto thread_pool = std::make_shared<ThreadPool>(defaults::num_threads()); | ||
| auto thread_pool = std::make_shared<ThreadPool>(defaults::num_threads(), | ||
| make_thread_pool_init_task("kvikio-dev")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we call it "kvikio-block-device" / "kvikio-block-dev" or just "kvikio", to avoid confusion with "development"? |
||
| dev_id_to_thread_pool_map.emplace(block_dev_info.id, thread_pool); | ||
| file_path_to_thread_pool_map.emplace(file_path, thread_pool); | ||
| return thread_pool.get(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NVTX has a function dedicated to thread renaming:
nvtxNameOsThread. But it only worked with global NVTX domain, not with kvikio's custom domain ( https://nvidia.slack.com/archives/C07722DM4MA/p1739200643850559). What is interesting is that its documentation has the following statement:I was not sure if the "use OS-specific thread ID instead of pthread_t" recommendation applied to
nvtxNameOsThreadspecifically, or to the profiling tools in general (including nsys) that want to consume NVTX.But I think whatever works in nsys-UI is the one we should pick, so the method in this PR should be good.