From cf84607875d5dd13e33dddf33cf457d9a5f25e2c Mon Sep 17 00:00:00 2001 From: Tommi Date: Sun, 17 May 2020 14:51:10 +0200 Subject: [PATCH] Reland "Use Windows 10 thread naming API" This reverts commit d0d55515c4b88a07446bc66a5c183f50ee896282. Reason for revert: Relanding with workaround. Original change's description: > Revert "Use Windows 10 thread naming API" > > This reverts commit e35004dffb42dd96b8cf37b33c9a3af4a5fd376c. > > Reason for revert: Reverting while downstream issue is resolved. > > Original change's description: > > Use Windows 10 thread naming API > > > > While profiling video chat in Chrome I noticed that some of the webrtc > > threads were not named. This change adds conditional use of the thread > > naming APIs. These thread names work even if you attach a debugger after > > the thread is named, and they show up in ETW traces, for easier > > profiling. > > > > The sctp_create_thread_adapter threads are still not named but since > > those are in C files they would require a C++-with-extern-C interface > > to fix, so I'm leaving them for now. > > > > Bug: webrtc:10745 > > Change-Id: I68f6aa780e2417ce706764d69e5b64cc48aba333 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175280 > > Commit-Queue: Tommi > > Reviewed-by: Tommi > > Reviewed-by: Karl Wiberg > > Cr-Commit-Position: refs/heads/master@{#31285} > > TBR=kwiberg@webrtc.org,tommi@webrtc.org,brucedawson@chromium.org > > Change-Id: Icf877afbd82918ebe0c42a93b8a763cdab9a73ce > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:10745 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175347 > Reviewed-by: Tommi > Commit-Queue: Tommi > Cr-Commit-Position: refs/heads/master@{#31289} TBR=kwiberg@webrtc.org,tommi@webrtc.org,brucedawson@chromium.org Bug: webrtc:10745 Change-Id: I51ee413fd8a0ff62f6b8b2a11f546b2a70168842 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175349 Reviewed-by: Tommi Commit-Queue: Tommi Cr-Commit-Position: refs/heads/master@{#31292} --- rtc_base/BUILD.gn | 1 + rtc_base/platform_thread_types.cc | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index a61ede4ac9..2d90898ce7 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -154,6 +154,7 @@ rtc_library("platform_thread_types") { "platform_thread_types.cc", "platform_thread_types.h", ] + deps = [ ":macromagic" ] } rtc_source_set("refcount") { diff --git a/rtc_base/platform_thread_types.cc b/rtc_base/platform_thread_types.cc index ed4a228262..b0243b41dc 100644 --- a/rtc_base/platform_thread_types.cc +++ b/rtc_base/platform_thread_types.cc @@ -15,6 +15,16 @@ #include #endif +#if defined(WEBRTC_WIN) +#include "rtc_base/arraysize.h" + +// The SetThreadDescription API was brought in version 1607 of Windows 10. +// For compatibility with various versions of winuser and avoid clashing with +// a potentially defined type, we use the RTC_ prefix. +typedef HRESULT(WINAPI* RTC_SetThreadDescription)(HANDLE hThread, + PCWSTR lpThreadDescription); +#endif + namespace rtc { PlatformThreadId CurrentThreadId() { @@ -58,6 +68,24 @@ bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) { void SetCurrentThreadName(const char* name) { #if defined(WEBRTC_WIN) + // The SetThreadDescription API works even if no debugger is attached. + // The names set with this API also show up in ETW traces. Very handy. + static auto set_thread_description_func = + reinterpret_cast(::GetProcAddress( + ::GetModuleHandleA("Kernel32.dll"), "SetThreadDescription")); + if (set_thread_description_func) { + // Convert from ASCII to UTF-16. + wchar_t wide_thread_name[64]; + for (size_t i = 0; i < arraysize(wide_thread_name) - 1; ++i) { + wide_thread_name[i] = name[i]; + if (wide_thread_name[i] == L'\0') + break; + } + // Guarantee null-termination. + wide_thread_name[arraysize(wide_thread_name) - 1] = L'\0'; + set_thread_description_func(::GetCurrentThread(), wide_thread_name); + } + // For details see: // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code #pragma pack(push, 8)