Skip to contentSkip to conversation

win32: set threads name

PATCH Superseded [email protected] 3 participants 6 messages Sep 29, 2022 – Sep 30, 2022

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 win32: set threads name

From: Marc-André Lureau <[email protected]>

As described in:
https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2022

SetThreadDescription() is available since Windows 10, version 1607 and in some versions only by "Run Time Dynamic Linking". Its declaration is not yet in mingw, so we lookup the function the same way glib does.

Tested with Visual Studio Community 2022 debugger.

---
 util/qemu-thread-win32.c | 54 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)
util/qemu-thread-win32.c +53 -1
index a2d5a6e825..9861ec5b89 100644
@@ -19,12 +19,40 @@
 
 static bool name_threads;
 
+typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
+                                                 PCWSTR lpThreadDescription);
+static pSetThreadDescription SetThreadDescriptionFunc = NULL;
+static HMODULE kernel32_module = NULL;
+
+static bool
+load_set_thread_description(void)
+{
+  static gsize _init_once = 0;
+
+  if (g_once_init_enter(&_init_once)) {
+      kernel32_module = LoadLibraryW(L"kernel32.dll");
+      if (kernel32_module) {
+          SetThreadDescriptionFunc =
+              (pSetThreadDescription)GetProcAddress(kernel32_module,
+                                                    "SetThreadDescription");
+          if (!SetThreadDescriptionFunc) {
+              FreeLibrary(kernel32_module);
+          }
+      }
+      g_once_init_leave(&_init_once, 1);
+  }
+
+  return !!SetThreadDescriptionFunc;
+}
+
 void qemu_thread_naming(bool enable)
 {
     /* But note we don't actually name them on Windows yet */
     name_threads = enable;
 
-    fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    if (enable && !load_set_thread_description()) {
+        fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    }
 }
 
 static void error_exit(int err, const char *msg)
@@ -400,6 +428,26 @@ void *qemu_thread_join(QemuThread *thread)
     return ret;
 }
 
+static bool
+set_thread_description(HANDLE h, const char *name)
+{
+  HRESULT hr;
+  g_autofree wchar_t *namew = NULL;
+
+  if (!load_set_thread_description() || !name) {
+      return false;
+  }
+
+  namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
+  if (!namew) {
+      return false;
+  }
+
+  hr = SetThreadDescriptionFunc(h, namew);
+
+  return SUCCEEDED(hr);
+}
+
 void qemu_thread_create(QemuThread *thread, const char *name,
                        void *(*start_routine)(void *),
                        void *arg, int mode)
@@ -423,7 +471,11 @@ void qemu_thread_create(QemuThread *thread, const char *name,
     if (!hThread) {
         error_exit(GetLastError(), __func__);
     }
+    if (name_threads) {
+        set_thread_description(hThread, name);
+    }
     CloseHandle(hThread);
+
     thread->data = data;
 }
 
Signature · 1 line
2.37.3
#1 lore raw

util/qemu-thread-win32.c line 56

Quoted 10 lines · On 9/29/22 06:41, [email protected] wrote:
  void qemu_thread_naming(bool enable)
  {
      /* But note we don't actually name them on Windows yet */
      name_threads = enable;
util/qemu-thread-win32.c +3 -1
-    fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    if (enable && !load_set_thread_description()) {
+        fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    }
  }

Comment is out of date, and I think it would be better to *not* set name_threads if not supported, rather than...

util/qemu-thread-win32.c void *qemu_thread_join(QemuThread *thread) · line 439

Quoted 9 lines
util/qemu-thread-win32.c +9 -0
+static bool
+set_thread_description(HANDLE h, const char *name)
+{
+  HRESULT hr;
+  g_autofree wchar_t *namew = NULL;
+
+  if (!load_set_thread_description() || !name) {
+      return false;
+  }

... have to re-query load_set_thread_description later.

Also, unused return value; might as well be void.

r~

#2 lore raw

Hi

Quoted 9 lines · Richard Henderson

util/qemu-thread-win32.c

Quoted 8 lines · On 9/29/22 06:41, [email protected] wrote:
  void qemu_thread_naming(bool enable)
  {
      /* But note we don't actually name them on Windows yet */
      name_threads = enable;
util/qemu-thread-win32.c +2 -1
-    fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    if (enable && !load_set_thread_description()) {
+        fprintf(stderr, "qemu: thread naming not supported on this
Quoted 8 lines

host\n");

Quoted 2 lines

+ }
}

Comment is out of date, and I think it would be better to *not* set name_threads if not
supported, rather than...

Comment removed.

Quoted 14 lines

util/qemu-thread-win32.c void *qemu_thread_join(QemuThread *thread) · line 439

Quoted 11 lines
util/qemu-thread-win32.c +9 -0
+static bool
+set_thread_description(HANDLE h, const char *name)
+{
+  HRESULT hr;
+  g_autofree wchar_t *namew = NULL;
+
+  if (!load_set_thread_description() || !name) {
+      return false;
+  }

... have to re-query load_set_thread_description later.

The load_set_thread_description() function is actually a "one-time" function, it doesn't re-load.

Quoted 3 lines

Also, unused return value; might as well be void.

Right, maybe it should warn if it failed to set the name?

Signature · 1 line
Marc-André Lureau
#3 lore raw
Quoted 37 lines · Marc-André Lureau

Hi

On Thu, Sep 29, 2022 at 9:53 PM Richard Henderson <[email protected] <mailto:[email protected]>> wrote:

    On 9/29/22 06:41, [email protected] <mailto:[email protected]> wrote:
     >   void qemu_thread_naming(bool enable)
     >   {
     >       /* But note we don't actually name them on Windows yet */
     >       name_threads = enable;
     >
     > -    fprintf(stderr, "qemu: thread naming not supported on this host\n");
     > +    if (enable && !load_set_thread_description()) {
     > +        fprintf(stderr, "qemu: thread naming not supported on this host\n");
     > +    }
     >   }

    Comment is out of date, and I think it would be better to *not* set name_threads if not
    supported, rather than...

Comment removed.

     > +static bool
     > +set_thread_description(HANDLE h, const char *name)
     > +{
     > +  HRESULT hr;
     > +  g_autofree wchar_t *namew = NULL;
     > +
     > +  if (!load_set_thread_description() || !name) {
     > +      return false;
     > +  }

    ... have to re-query load_set_thread_description later.
Quoted 1 line

The load_set_thread_description() function is actually a "one-time" function, it doesn't re-load.

You're calling it again. That has some cost in the mutex/spinlock that's behind that one-time operation, when you're already making the call to set_thread_description conditional.

Quoted 1 line

Right, maybe it should warn if it failed to set the name?

After you've already printed an error in qemu_thread_naming()? Doesn't seem useful. Or did you mean in the case we think it should work, but didn't?

r~

#4 lore raw

Hi

Quoted 44 lines · Richard Henderson
Quoted 3 lines · Marc-André Lureau

Hi

On Thu, Sep 29, 2022 at 9:53 PM Richard Henderson <

[email protected]

Quoted 3 lines

<mailto:[email protected]>> wrote:

    On 9/29/22 06:41, [email protected] <mailto:
Quoted 6 lines · [email protected]> wrote:

> void qemu_thread_naming(bool enable)
> {
> /* But note we don't actually name them on Windows yet */
> name_threads = enable;
>
> - fprintf(stderr, "qemu: thread naming not supported on this

host\n");

Quoted 2 lines

> + if (enable && !load_set_thread_description()) {
> + fprintf(stderr, "qemu: thread naming not supported on

this host\n");

Quoted 4 lines
     > +    }
     >   }

    Comment is out of date, and I think it would be better to *not* set

name_threads if not

Quoted 20 lines

supported, rather than...

Comment removed.

     > +static bool
     > +set_thread_description(HANDLE h, const char *name)
     > +{
     > +  HRESULT hr;
     > +  g_autofree wchar_t *namew = NULL;
     > +
     > +  if (!load_set_thread_description() || !name) {
     > +      return false;
     > +  }

    ... have to re-query load_set_thread_description later.
Quoted 9 lines
Quoted 1 line

The load_set_thread_description() function is actually a "one-time"

function, it doesn't

Quoted 1 line

re-load.

You're calling it again. That has some cost in the mutex/spinlock that's behind that
one-time operation, when you're already making the call to set_thread_description conditional.

That seems pretty marginal given the frequencies of this call, when we are creating threads.

So you suggest simply setting "name_threads" to false when loading the function failed?

Quoted 7 lines
Quoted 2 lines

Right, maybe it should warn if it failed to set the name?

After you've already printed an error in qemu_thread_naming()? Doesn't seem useful. Or
did you mean in the case we think it should work, but didn't?

During qemu_thread_create(), if set_thread_description() failed.

Signature · 1 line
Marc-André Lureau
#5 lore raw
Quoted 1 line · Marc-André Lureau

So you suggest simply setting "name_threads" to false when loading the function failed?

Yes.

r~