diff --git a/libs/pika/runtime/src/runtime.cpp b/libs/pika/runtime/src/runtime.cpp index 58d0faac9..e8e15c2c6 100644 --- a/libs/pika/runtime/src/runtime.cpp +++ b/libs/pika/runtime/src/runtime.cpp @@ -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 @@ -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)) { @@ -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(); @@ -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) diff --git a/libs/pika/thread_support/include/pika/thread_support/thread_name.hpp b/libs/pika/thread_support/include/pika/thread_support/thread_name.hpp index d84efb8bb..e51dce437 100644 --- a/libs/pika/thread_support/include/pika/thread_support/thread_name.hpp +++ b/libs/pika/thread_support/include/pika/thread_support/thread_name.hpp @@ -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 diff --git a/libs/pika/thread_support/src/thread_name.cpp b/libs/pika/thread_support/src/thread_name.cpp index 7b6231b84..57af6c140 100644 --- a/libs/pika/thread_support/src/thread_name.cpp +++ b/libs/pika/thread_support/src/thread_name.cpp @@ -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 ""; - return thread_name; + std::string const& name = detail::get_thread_name_internal(); + if (name.empty()) return ""; + return name; } #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) @@ -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; @@ -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) {