Skip to content

Commit

Permalink
Set short thread name for pthread_setname
Browse files Browse the repository at this point in the history
Abbreviate thread name to "pika/<first char of context>/<first char of pool name>/<local thread
index>", e.g. "pika/w/d/3" for the fourth worker thread on the [d]efault pool, which is a [w]orker
thread.
  • Loading branch information
msimberg committed Nov 4, 2024
1 parent d0fbdf0 commit 87205f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
21 changes: 16 additions & 5 deletions libs/pika/runtime/src/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,9 @@ namespace pika::detail {
cond.notify_all();
}

std::string thread_name("main-thread#wait_helper");
detail::set_thread_name(thread_name);
std::string fullname("pika/main-thread#wait_helper");
std::string shortname("pika/waithelper");
detail::set_thread_name(fullname, shortname);

#if defined(PIKA_HAVE_APEX)
// not registering helper threads - for now
Expand Down Expand Up @@ -1381,9 +1382,18 @@ namespace pika::detail {
std::size_t global_thread_num, char const* pool_name, char const* postfix)
// NOLINTEND(bugprone-easily-swappable-parameters)
{
PIKA_ASSERT(context != nullptr);
PIKA_ASSERT(context[0]);

std::ostringstream fullname;
std::ostringstream shortname;
fullname << "pika/" << context;
if (pool_name && *pool_name) { fullname << "/pool:" << pool_name; }
shortname << "pika/" << context[0];
if (pool_name && *pool_name)
{
fullname << "/pool:" << pool_name;
shortname << '/' << pool_name[0];
}
if (postfix && *postfix) { fullname << '/' << postfix; }
if (global_thread_num != std::size_t(-1))
{
Expand All @@ -1392,10 +1402,11 @@ namespace pika::detail {
if (local_thread_num != std::size_t(-1))
{
fullname << "/local:" + std::to_string(local_thread_num);
shortname << '/' << std::to_string(local_thread_num);
}

PIKA_ASSERT(detail::get_thread_name_internal().empty());
detail::set_thread_name(PIKA_MOVE(fullname).str());
detail::set_thread_name(fullname.str(), shortname.str());
#if defined(PIKA_HAVE_APEX) || defined(PIKA_HAVE_TRACY)
char const* name = detail::get_thread_name().c_str();

Expand Down Expand Up @@ -1423,7 +1434,7 @@ namespace pika::detail {
if (on_stop_func_) { on_stop_func_(global_thread_num, global_thread_num, "", context); }

// reset thread local storage
detail::set_thread_name("");
detail::set_thread_name("", "");
}

void runtime::add_pre_startup_function(startup_function_type f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
namespace pika::detail {
PIKA_EXPORT std::string& get_thread_name_internal();
PIKA_EXPORT std::string get_thread_name();
PIKA_EXPORT void set_thread_name(std::string_view name);
PIKA_EXPORT void set_thread_name(std::string_view name, std::string_view short_name);
} // namespace pika::detail
22 changes: 11 additions & 11 deletions libs/pika/thread_support/src/thread_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace pika::detail {

std::string get_thread_name()
{
std::string const& thread_name = detail::get_thread_name_internal();
if (thread_name.empty()) return "<unknown>";
return thread_name;
std::string const& name = detail::get_thread_name_internal();
if (name.empty()) return "<unknown>";
return name;
}

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
Expand All @@ -47,15 +47,15 @@ namespace pika::detail {
# pragma pack(pop)

// Set the name of the thread shown in the Visual Studio debugger
void set_thread_name(std::string_view thread_name)
void set_thread_name(std::string_view name, std::string_view)
{
auto& name = get_thread_name_internal();
name = thread_name;
auto& name_internal = get_thread_name_internal();
name_internal = name;

DWORD dwThreadID = -1;
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name.c_str();
info.szName = name_internal.c_str();
info.dwThreadID = dwThreadID;
info.dwFlags = 0;

Expand All @@ -69,15 +69,15 @@ namespace pika::detail {
}
}
#else
void set_thread_name(std::string_view thread_name)
void set_thread_name(std::string_view name, std::string_view short_name)
{
auto& name = get_thread_name_internal();
name = thread_name;
auto& name_internal = get_thread_name_internal();
name_internal = name;
# if defined(PIKA_HAVE_PTHREAD_SETNAME_NP)
// https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html:
// The thread name is a meaningful C language string, whose length is restricted to 16
// characters, including the terminating null byte ('\0').
std::string pthread_name{thread_name, 0, 15};
std::string pthread_name{short_name, 0, 15};
int rc = pthread_setname_np(pthread_self(), pthread_name.c_str());
if (rc != 0)
{
Expand Down

0 comments on commit 87205f5

Please sign in to comment.