qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] win32: set threads name
@ 2022-09-30 14:03 marcandre.lureau
  2022-09-30 14:09 ` Richard Henderson
  2022-10-08  2:25 ` Bin Meng
  0 siblings, 2 replies; 9+ messages in thread
From: marcandre.lureau @ 2022-09-30 14:03 UTC (permalink / raw)
  To: qemu-devel
  Cc: bin.meng, richard.henderson, Marc-André Lureau, Stefan Weil

From: Marc-André Lureau <marcandre.lureau@redhat.com>

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.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 util/qemu-thread-win32.c | 56 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)

diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
index a2d5a6e825..90c9fa614b 100644
--- a/util/qemu-thread-win32.c
+++ b/util/qemu-thread-win32.c
@@ -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");
+        name_threads = false;
+    }
 }
 
 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()) {
+      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 && name && !set_thread_description(hThread, name)) {
+        fprintf(stderr, "qemu: failed to set thread description: %s\n", name);
+    }
     CloseHandle(hThread);
+
     thread->data = data;
 }
 
-- 
2.37.3



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-09-30 14:03 [PATCH v3] win32: set threads name marcandre.lureau
@ 2022-09-30 14:09 ` Richard Henderson
  2022-10-03  7:33   ` Marc-André Lureau
  2022-10-03  7:38   ` Marc-André Lureau
  2022-10-08  2:25 ` Bin Meng
  1 sibling, 2 replies; 9+ messages in thread
From: Richard Henderson @ 2022-09-30 14:09 UTC (permalink / raw)
  To: marcandre.lureau, qemu-devel; +Cc: bin.meng, Stefan Weil

On 9/30/22 07:03, marcandre.lureau@redhat.com wrote:
> +static bool
> +set_thread_description(HANDLE h, const char *name)
> +{
> +  HRESULT hr;
> +  g_autofree wchar_t *namew = NULL;
> +
> +  if (!load_set_thread_description()) {
> +      return false;
> +  }

I don't understand why you're retaining this.
What is your logic?


r~


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-09-30 14:09 ` Richard Henderson
@ 2022-10-03  7:33   ` Marc-André Lureau
  2022-10-03  7:38   ` Marc-André Lureau
  1 sibling, 0 replies; 9+ messages in thread
From: Marc-André Lureau @ 2022-10-03  7:33 UTC (permalink / raw)
  To: Richard Henderson; +Cc: qemu-devel, bin.meng, Stefan Weil

Hi Richard

On Fri, Sep 30, 2022 at 6:10 PM Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 9/30/22 07:03, marcandre.lureau@redhat.com wrote:
> > +static bool
> > +set_thread_description(HANDLE h, const char *name)
> > +{
> > +  HRESULT hr;
> > +  g_autofree wchar_t *namew = NULL;
> > +
> > +  if (!load_set_thread_description()) {
> > +      return false;
> > +  }
>
> I don't understand why you're retaining this.
> What is your logic?

The function should be MT-safe that way (load_set_thread_description
should be too). But since we changed the way we invoke it, by relying
on non-protected "name_threads" and we set it to false when the
function cannot be loaded, we can just drop this. We can also drop
g_once usage from load_set_thread_description. There is no explicit
guarantee on when qemu_thread_naming() is called, and I am pretty sure
qemu_thread_create() is called from different threads too.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-09-30 14:09 ` Richard Henderson
  2022-10-03  7:33   ` Marc-André Lureau
@ 2022-10-03  7:38   ` Marc-André Lureau
  2022-10-06 12:51     ` Marc-André Lureau
  1 sibling, 1 reply; 9+ messages in thread
From: Marc-André Lureau @ 2022-10-03  7:38 UTC (permalink / raw)
  To: Richard Henderson; +Cc: qemu-devel, bin.meng, Stefan Weil

Hi

On Fri, Sep 30, 2022 at 6:10 PM Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 9/30/22 07:03, marcandre.lureau@redhat.com wrote:
> > +static bool
> > +set_thread_description(HANDLE h, const char *name)
> > +{
> > +  HRESULT hr;
> > +  g_autofree wchar_t *namew = NULL;
> > +
> > +  if (!load_set_thread_description()) {
> > +      return false;
> > +  }
>
> I don't understand why you're retaining this.
> What is your logic?
>

Also, if we change the "static bool name_threads" to be true by
default, then set_thread_description() might be called without calling
qemu_thread_naming()

It really shouldn't hurt to keep it that way.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-10-03  7:38   ` Marc-André Lureau
@ 2022-10-06 12:51     ` Marc-André Lureau
  2022-10-06 21:04       ` Richard Henderson
  0 siblings, 1 reply; 9+ messages in thread
From: Marc-André Lureau @ 2022-10-06 12:51 UTC (permalink / raw)
  To: Richard Henderson; +Cc: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 922 bytes --]

Hi Richard

On Mon, Oct 3, 2022 at 11:39 AM Marc-André Lureau <
marcandre.lureau@redhat.com> wrote:

> Hi
>
> On Fri, Sep 30, 2022 at 6:10 PM Richard Henderson
> <richard.henderson@linaro.org> wrote:
> >
> > On 9/30/22 07:03, marcandre.lureau@redhat.com wrote:
> > > +static bool
> > > +set_thread_description(HANDLE h, const char *name)
> > > +{
> > > +  HRESULT hr;
> > > +  g_autofree wchar_t *namew = NULL;
> > > +
> > > +  if (!load_set_thread_description()) {
> > > +      return false;
> > > +  }
> >
> > I don't understand why you're retaining this.
> > What is your logic?
> >
>
> Also, if we change the "static bool name_threads" to be true by
> default, then set_thread_description() might be called without calling
> qemu_thread_naming()
>
> It really shouldn't hurt to keep it that way.
>
>
>
Let me know if the current form is ok for you, thanks

-- 
Marc-André Lureau

[-- Attachment #2: Type: text/html, Size: 1601 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-10-06 12:51     ` Marc-André Lureau
@ 2022-10-06 21:04       ` Richard Henderson
  2022-10-07  9:52         ` Marc-André Lureau
  0 siblings, 1 reply; 9+ messages in thread
From: Richard Henderson @ 2022-10-06 21:04 UTC (permalink / raw)
  To: Marc-André Lureau; +Cc: qemu-devel

On 10/6/22 05:51, Marc-André Lureau wrote:
> Hi Richard
> 
> On Mon, Oct 3, 2022 at 11:39 AM Marc-André Lureau <marcandre.lureau@redhat.com 
> <mailto:marcandre.lureau@redhat.com>> wrote:
> 
>     Hi
> 
>     On Fri, Sep 30, 2022 at 6:10 PM Richard Henderson
>     <richard.henderson@linaro.org <mailto:richard.henderson@linaro.org>> wrote:
>      >
>      > On 9/30/22 07:03, marcandre.lureau@redhat.com <mailto:marcandre.lureau@redhat.com>
>     wrote:
>      > > +static bool
>      > > +set_thread_description(HANDLE h, const char *name)
>      > > +{
>      > > +  HRESULT hr;
>      > > +  g_autofree wchar_t *namew = NULL;
>      > > +
>      > > +  if (!load_set_thread_description()) {
>      > > +      return false;
>      > > +  }
>      >
>      > I don't understand why you're retaining this.
>      > What is your logic?
>      >
> 
>     Also, if we change the "static bool name_threads" to be true by
>     default, then set_thread_description() might be called without calling
>     qemu_thread_naming()
> 
>     It really shouldn't hurt to keep it that way.
> 
> 
> 
> Let me know if the current form is ok for you, thanks

I guess it's ok, sure.


r~


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-10-06 21:04       ` Richard Henderson
@ 2022-10-07  9:52         ` Marc-André Lureau
  2022-10-07 15:01           ` Richard Henderson
  0 siblings, 1 reply; 9+ messages in thread
From: Marc-André Lureau @ 2022-10-07  9:52 UTC (permalink / raw)
  To: Richard Henderson; +Cc: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1462 bytes --]

Hi

On Fri, Oct 7, 2022 at 1:04 AM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 10/6/22 05:51, Marc-André Lureau wrote:
> > Hi Richard
> >
> > On Mon, Oct 3, 2022 at 11:39 AM Marc-André Lureau <
> marcandre.lureau@redhat.com
> > <mailto:marcandre.lureau@redhat.com>> wrote:
> >
> >     Hi
> >
> >     On Fri, Sep 30, 2022 at 6:10 PM Richard Henderson
> >     <richard.henderson@linaro.org <mailto:richard.henderson@linaro.org>>
> wrote:
> >      >
> >      > On 9/30/22 07:03, marcandre.lureau@redhat.com <mailto:
> marcandre.lureau@redhat.com>
> >     wrote:
> >      > > +static bool
> >      > > +set_thread_description(HANDLE h, const char *name)
> >      > > +{
> >      > > +  HRESULT hr;
> >      > > +  g_autofree wchar_t *namew = NULL;
> >      > > +
> >      > > +  if (!load_set_thread_description()) {
> >      > > +      return false;
> >      > > +  }
> >      >
> >      > I don't understand why you're retaining this.
> >      > What is your logic?
> >      >
> >
> >     Also, if we change the "static bool name_threads" to be true by
> >     default, then set_thread_description() might be called without
> calling
> >     qemu_thread_naming()
> >
> >     It really shouldn't hurt to keep it that way.
> >
> >
> >
> > Let me know if the current form is ok for you, thanks
>
> I guess it's ok, sure.
>

Should I take that for an Ack?  :)

thanks

-- 
Marc-André Lureau

[-- Attachment #2: Type: text/html, Size: 2637 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-10-07  9:52         ` Marc-André Lureau
@ 2022-10-07 15:01           ` Richard Henderson
  0 siblings, 0 replies; 9+ messages in thread
From: Richard Henderson @ 2022-10-07 15:01 UTC (permalink / raw)
  To: Marc-André Lureau; +Cc: qemu-devel

On 10/7/22 02:52, Marc-André Lureau wrote:
> Hi
> 
> On Fri, Oct 7, 2022 at 1:04 AM Richard Henderson <richard.henderson@linaro.org 
> <mailto:richard.henderson@linaro.org>> wrote:
> 
>     On 10/6/22 05:51, Marc-André Lureau wrote:
>      > Hi Richard
>      >
>      > On Mon, Oct 3, 2022 at 11:39 AM Marc-André Lureau <marcandre.lureau@redhat.com
>     <mailto:marcandre.lureau@redhat.com>
>      > <mailto:marcandre.lureau@redhat.com <mailto:marcandre.lureau@redhat.com>>> wrote:
>      >
>      >     Hi
>      >
>      >     On Fri, Sep 30, 2022 at 6:10 PM Richard Henderson
>      >     <richard.henderson@linaro.org <mailto:richard.henderson@linaro.org>
>     <mailto:richard.henderson@linaro.org <mailto:richard.henderson@linaro.org>>> wrote:
>      >      >
>      >      > On 9/30/22 07:03, marcandre.lureau@redhat.com
>     <mailto:marcandre.lureau@redhat.com> <mailto:marcandre.lureau@redhat.com
>     <mailto:marcandre.lureau@redhat.com>>
>      >     wrote:
>      >      > > +static bool
>      >      > > +set_thread_description(HANDLE h, const char *name)
>      >      > > +{
>      >      > > +  HRESULT hr;
>      >      > > +  g_autofree wchar_t *namew = NULL;
>      >      > > +
>      >      > > +  if (!load_set_thread_description()) {
>      >      > > +      return false;
>      >      > > +  }
>      >      >
>      >      > I don't understand why you're retaining this.
>      >      > What is your logic?
>      >      >
>      >
>      >     Also, if we change the "static bool name_threads" to be true by
>      >     default, then set_thread_description() might be called without calling
>      >     qemu_thread_naming()
>      >
>      >     It really shouldn't hurt to keep it that way.
>      >
>      >
>      >
>      > Let me know if the current form is ok for you, thanks
> 
>     I guess it's ok, sure.
> 
> 
> Should I take that for an Ack?  :)

Yes, that was sloppy.
Acked-by: Richard Henderson <richard.henderson@linaro.org>


r~


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] win32: set threads name
  2022-09-30 14:03 [PATCH v3] win32: set threads name marcandre.lureau
  2022-09-30 14:09 ` Richard Henderson
@ 2022-10-08  2:25 ` Bin Meng
  1 sibling, 0 replies; 9+ messages in thread
From: Bin Meng @ 2022-10-08  2:25 UTC (permalink / raw)
  To: marcandre.lureau; +Cc: qemu-devel, bin.meng, richard.henderson, Stefan Weil

On Fri, Sep 30, 2022 at 10:19 PM <marcandre.lureau@redhat.com> wrote:
>
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> 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.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  util/qemu-thread-win32.c | 56 ++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 54 insertions(+), 2 deletions(-)
>
> diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
> index a2d5a6e825..90c9fa614b 100644
> --- a/util/qemu-thread-win32.c
> +++ b/util/qemu-thread-win32.c
> @@ -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;

nits: the NULL assignment to the static variables is not necessary

> +
> +static bool

nits: incorrect style, should be in the same line with below

> +load_set_thread_description(void)
> +{
> +  static gsize _init_once = 0;

nits: the spaces should be 4 instead of 2, please fix this globally in
this patch

> +
> +  if (g_once_init_enter(&_init_once)) {
> +      kernel32_module = LoadLibraryW(L"kernel32.dll");

nits: use LoadLibrary() and let the compiler to decide which function to use?

> +      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");
> +        name_threads = false;
> +    }
>  }
>
>  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()) {
> +      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 && name && !set_thread_description(hThread, name)) {
> +        fprintf(stderr, "qemu: failed to set thread description: %s\n", name);
> +    }
>      CloseHandle(hThread);
> +
>      thread->data = data;
>  }
>

Regards,
Bin


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-10-08  2:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-30 14:03 [PATCH v3] win32: set threads name marcandre.lureau
2022-09-30 14:09 ` Richard Henderson
2022-10-03  7:33   ` Marc-André Lureau
2022-10-03  7:38   ` Marc-André Lureau
2022-10-06 12:51     ` Marc-André Lureau
2022-10-06 21:04       ` Richard Henderson
2022-10-07  9:52         ` Marc-André Lureau
2022-10-07 15:01           ` Richard Henderson
2022-10-08  2:25 ` Bin Meng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).