From 2ba35cab732076fb61c36f8ee2ce833fe1a84679 Mon Sep 17 00:00:00 2001 From: comex Date: Sun, 2 Aug 2020 10:57:08 -0700 Subject: [PATCH] Fix thread naming on Linux, which limits names to 15 bytes. - In `SetCurrentThreadName`, when on Linux, truncate to 15 bytes, as (at least on glibc) `pthread_set_name_np` will otherwise return `ERANGE` and do nothing. - Also, add logging in case `pthread_set_name_np` returns an error anyway. This is Linux-specific, as the Apple and BSD versions of `pthread_set_name_np return `void`. - Change the name for CPU threads in multi-core mode from "yuzu:CoreCPUThread_N" (19 bytes) to "yuzu:CPUCore_N" (14 bytes) so it fits into the Linux limit. Some other thread names are also cut off, but I didn't bother addressing them as you can guess them from the truncated versions. For a CPU thread, truncation means you can't see which core it is! --- src/common/thread.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/common/thread.cpp b/src/common/thread.cpp index af180b43d..394d19345 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp @@ -2,6 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "common/common_funcs.h" +#include "common/logging/log.h" #include "common/thread.h" #ifdef __APPLE__ #include @@ -18,6 +20,7 @@ #ifndef _WIN32 #include #endif +#include #ifdef __FreeBSD__ #define cpu_set_t cpuset_t @@ -64,6 +67,14 @@ void SetCurrentThreadName(const char* name) { pthread_set_name_np(pthread_self(), name); #elif defined(__NetBSD__) pthread_setname_np(pthread_self(), "%s", (void*)name); +#elif defined(__linux__) + // Linux limits thread names to 15 characters and will outright reject any + // attempt to set a longer name with ERANGE. + std::string truncated(name, std::min(strlen(name), static_cast(15))); + if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) { + errno = e; + LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg()); + } #else pthread_setname_np(pthread_self(), name); #endif