Merge pull request #9044 from shuffle2/thread-names

Thread names
This commit is contained in:
Jordan Woyak 2020-08-23 06:01:05 -05:00 committed by GitHub
commit c36ae84b96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 9 deletions

View File

@ -5,9 +5,11 @@
#include "Common/Thread.h" #include "Common/Thread.h"
#include "Common/CommonFuncs.h" #include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/StringUtil.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <Windows.h>
#include <processthreadsapi.h>
#else #else
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -64,7 +66,7 @@ void SwitchCurrentThread()
// Sets the debugger-visible name of the current thread. // Sets the debugger-visible name of the current thread.
// Uses trick documented in: // Uses trick documented in:
// https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
void SetCurrentThreadName(const char* szThreadName) static void SetCurrentThreadNameViaException(const char* name)
{ {
static const DWORD MS_VC_EXCEPTION = 0x406D1388; static const DWORD MS_VC_EXCEPTION = 0x406D1388;
@ -79,7 +81,7 @@ void SetCurrentThreadName(const char* szThreadName)
#pragma pack(pop) #pragma pack(pop)
info.dwType = 0x1000; info.dwType = 0x1000;
info.szName = szThreadName; info.szName = name;
info.dwThreadID = static_cast<DWORD>(-1); info.dwThreadID = static_cast<DWORD>(-1);
info.dwFlags = 0; info.dwFlags = 0;
@ -92,6 +94,25 @@ void SetCurrentThreadName(const char* szThreadName)
} }
} }
static void SetCurrentThreadNameViaApi(const char* name)
{
// If possible, also set via the newer API. On some versions of Server it needs to be manually
// resolved. This API allows being able to observe the thread name even if a debugger wasn't
// attached when the name was set (see above link for more info).
static auto pSetThreadDescription = (decltype(&SetThreadDescription))GetProcAddress(
GetModuleHandleA("kernel32"), "SetThreadDescription");
if (pSetThreadDescription)
{
pSetThreadDescription(GetCurrentThread(), UTF8ToWString(name).c_str());
}
}
void SetCurrentThreadName(const char* name)
{
SetCurrentThreadNameViaException(name);
SetCurrentThreadNameViaApi(name);
}
#else // !WIN32, so must be POSIX threads #else // !WIN32, so must be POSIX threads
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
@ -129,22 +150,22 @@ void SwitchCurrentThread()
usleep(1000 * 1); usleep(1000 * 1);
} }
void SetCurrentThreadName(const char* szThreadName) void SetCurrentThreadName(const char* name)
{ {
#ifdef __APPLE__ #ifdef __APPLE__
pthread_setname_np(szThreadName); pthread_setname_np(name);
#elif defined __FreeBSD__ || defined __OpenBSD__ #elif defined __FreeBSD__ || defined __OpenBSD__
pthread_set_name_np(pthread_self(), szThreadName); pthread_set_name_np(pthread_self(), name);
#elif defined __HAIKU__ #elif defined __HAIKU__
rename_thread(find_thread(nullptr), szThreadName); rename_thread(find_thread(nullptr), name);
#else #else
// linux doesn't allow to set more than 16 bytes, including \0. // linux doesn't allow to set more than 16 bytes, including \0.
pthread_setname_np(pthread_self(), std::string(szThreadName).substr(0, 15).c_str()); pthread_setname_np(pthread_self(), std::string(name).substr(0, 15).c_str());
#endif #endif
#ifdef USE_VTUNE #ifdef USE_VTUNE
// VTune uses OS thread names by default but probably supports longer names when set via its own // VTune uses OS thread names by default but probably supports longer names when set via its own
// API. // API.
__itt_thread_set_name(szThreadName); __itt_thread_set_name(name);
#endif #endif
} }

View File

@ -10,6 +10,7 @@
#include "Common/Event.h" #include "Common/Event.h"
#include "Common/Flag.h" #include "Common/Flag.h"
#include "Common/Thread.h"
// A thread that executes the given function for every item placed into its queue. // A thread that executes the given function for every item placed into its queue.
@ -53,6 +54,8 @@ private:
void ThreadLoop() void ThreadLoop()
{ {
Common::SetCurrentThreadName("WorkQueueThread");
while (true) while (true)
{ {
m_wakeup.Wait(); m_wakeup.Wait();

View File

@ -69,6 +69,8 @@ void DiscordHandler::ShowNewJoinRequest(const std::string& id, const std::string
void DiscordHandler::Run() void DiscordHandler::Run()
{ {
Common::SetCurrentThreadName("DiscordHandler");
while (!m_stop_requested.IsSet()) while (!m_stop_requested.IsSet())
{ {
Discord::CallPendingCallbacks(); Discord::CallPendingCallbacks();

View File

@ -134,6 +134,8 @@ static void HandleFrameStepHotkeys()
void HotkeyScheduler::Run() void HotkeyScheduler::Run()
{ {
Common::SetCurrentThreadName("HotkeyScheduler");
while (!m_stop_requested.IsSet()) while (!m_stop_requested.IsSet())
{ {
Common::SleepCurrentThread(1000 / 60); Common::SleepCurrentThread(1000 / 60);

View File

@ -13,6 +13,7 @@
#include "Common/Event.h" #include "Common/Event.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/ScopeGuard.h" #include "Common/ScopeGuard.h"
#include "Common/Thread.h"
#include "InputCommon/ControllerInterface/DInput/DInput.h" #include "InputCommon/ControllerInterface/DInput/DInput.h"
#include "InputCommon/ControllerInterface/XInput/XInput.h" #include "InputCommon/ControllerInterface/XInput/XInput.h"
@ -43,6 +44,8 @@ void ciface::Win32::Init(void* hwnd)
std::promise<HWND> message_window_promise; std::promise<HWND> message_window_promise;
s_thread = std::thread([&message_window_promise] { s_thread = std::thread([&message_window_promise] {
Common::SetCurrentThreadName("ciface::Win32 Message Loop");
HWND message_window = nullptr; HWND message_window = nullptr;
Common::ScopeGuard promise_guard([&] { message_window_promise.set_value(message_window); }); Common::ScopeGuard promise_guard([&] { message_window_promise.set_value(message_window); });

View File

@ -9,6 +9,7 @@
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
#include "Common/Thread.h"
#include "VideoBackends/Vulkan/VulkanContext.h" #include "VideoBackends/Vulkan/VulkanContext.h"
@ -185,6 +186,8 @@ bool CommandBufferManager::CreateSubmitThread()
{ {
m_submit_loop = std::make_unique<Common::BlockingLoop>(); m_submit_loop = std::make_unique<Common::BlockingLoop>();
m_submit_thread = std::thread([this]() { m_submit_thread = std::thread([this]() {
Common::SetCurrentThreadName("Vulkan CommandBufferManager SubmitThread");
m_submit_loop->Run([this]() { m_submit_loop->Run([this]() {
PendingCommandBufferSubmit submit; PendingCommandBufferSubmit submit;
{ {

View File

@ -6,6 +6,7 @@
#include <thread> #include <thread>
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/Thread.h"
namespace VideoCommon namespace VideoCommon
{ {
@ -192,6 +193,8 @@ void AsyncShaderCompiler::WorkerThreadExit(void* param)
void AsyncShaderCompiler::WorkerThreadEntryPoint(void* param) void AsyncShaderCompiler::WorkerThreadEntryPoint(void* param)
{ {
Common::SetCurrentThreadName("AsyncShaderCompiler Worker");
// Initialize worker thread with backend-specific method. // Initialize worker thread with backend-specific method.
if (!WorkerThreadInitWorkerThread(param)) if (!WorkerThreadInitWorkerThread(param))
{ {