[PATCH v5 07/24] util: set the name for the 'main' thread

Daniel P. Berrangé posted 24 patches 3 weeks, 3 days ago
There is a newer version of this series
[PATCH v5 07/24] util: set the name for the 'main' thread
Posted by Daniel P. Berrangé 3 weeks, 3 days ago
The default main thread name is undefined, so use a constructor to
explicitly set it to 'main'. This constructor is marked to run early
as the thread name is intended to be used in error reporting / logs
which may be triggered very early in QEMU execution.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 util/qemu-thread-posix.c | 6 ++++++
 util/qemu-thread-win32.c | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index b1c127dbe3..afeac9ecad 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -22,6 +22,12 @@
 #include <pthread_np.h>
 #endif
 
+static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
+qemu_thread_init(void)
+{
+    qemu_thread_set_name("main");
+}
+
 static void error_exit(int err, const char *msg)
 {
     fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
index 4d2d663a9a..8ca6429ad3 100644
--- a/util/qemu-thread-win32.c
+++ b/util/qemu-thread-win32.c
@@ -22,6 +22,12 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
 static pSetThreadDescription SetThreadDescriptionFunc;
 static HMODULE kernel32_module;
 
+static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
+qemu_thread_init(void)
+{
+    qemu_thread_set_name("main");
+}
+
 static bool load_set_thread_description(void)
 {
     static gsize _init_once = 0;
-- 
2.52.0


Re: [PATCH v5 07/24] util: set the name for the 'main' thread
Posted by Paolo Bonzini via Devel 3 weeks, 2 days ago
On 1/8/26 18:03, Daniel P. Berrangé wrote:
> The default main thread name is undefined, so use a constructor to
> explicitly set it to 'main'. This constructor is marked to run early
> as the thread name is intended to be used in error reporting / logs
> which may be triggered very early in QEMU execution.

At least on Linux I don't think we want to call the pthread function.

Since pthread_setname_np writes to /proc/self/task/tid/comm, wouldn't 
that cause ps to show qemu processes as "main" instead of "qemu" or 
"qemu-kvm"?

Paolo

> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   util/qemu-thread-posix.c | 6 ++++++
>   util/qemu-thread-win32.c | 6 ++++++
>   2 files changed, 12 insertions(+)
> 
> diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> index b1c127dbe3..afeac9ecad 100644
> --- a/util/qemu-thread-posix.c
> +++ b/util/qemu-thread-posix.c
> @@ -22,6 +22,12 @@
>   #include <pthread_np.h>
>   #endif
>   
> +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> +qemu_thread_init(void)
> +{
> +    qemu_thread_set_name("main");
> +}
> +
>   static void error_exit(int err, const char *msg)
>   {
>       fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
> diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
> index 4d2d663a9a..8ca6429ad3 100644
> --- a/util/qemu-thread-win32.c
> +++ b/util/qemu-thread-win32.c
> @@ -22,6 +22,12 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
>   static pSetThreadDescription SetThreadDescriptionFunc;
>   static HMODULE kernel32_module;
>   
> +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> +qemu_thread_init(void)
> +{
> +    qemu_thread_set_name("main");
> +}
> +
>   static bool load_set_thread_description(void)
>   {
>       static gsize _init_once = 0;

Re: [PATCH v5 07/24] util: set the name for the 'main' thread
Posted by Daniel P. Berrangé 3 weeks, 2 days ago
On Fri, Jan 09, 2026 at 12:45:03PM +0100, Paolo Bonzini wrote:
> On 1/8/26 18:03, Daniel P. Berrangé wrote:
> > The default main thread name is undefined, so use a constructor to
> > explicitly set it to 'main'. This constructor is marked to run early
> > as the thread name is intended to be used in error reporting / logs
> > which may be triggered very early in QEMU execution.
> 
> At least on Linux I don't think we want to call the pthread function.
> 
> Since pthread_setname_np writes to /proc/self/task/tid/comm, wouldn't that
> cause ps to show qemu processes as "main" instead of "qemu" or "qemu-kvm"?

Hmmm, it depends on the 'ps' options you use:

$ ./build/qemu-system-x86_64  &
[3] 3859009

⚙️ [oci:fedora-43 qemu]$ ps  | grep 3859009
3859009 pts/10   00:00:04 main
⚙️ [oci:fedora-43 qemu]$ ps -a | grep 3859009
3859009 pts/10   00:00:03 main
⚙️ [oci:fedora-43 qemu]$ ps -ax | grep 3859009
3859009 pts/10   Sl     0:03 ./build/qemu-system-x86_64
⚙️ [oci:fedora-43 qemu]$ ps -au | grep 3859009
berrange 3859009  8.6  0.1 3893224 123700 pts/10 Sl   11:49   0:04 ./build/qemu-system-x86_64
⚙️ [oci:fedora-43 qemu]$ ps -af | grep 3859009
berrange 3859009 2085609  8 11:49 pts/10   00:00:04 ./build/qemu-system-x86_64
⚙️ [oci:fedora-43 qemu]$ ps -axuwf | grep 3859009
berrange 3859009 53.4  0.1 3905536 124108 pts/10 Sl   11:49   0:03  |       \_ ./build/qemu-system-x86_64


and I hadn't noticed since I always use one of the latter
sets of options that give the true name.

> 
> Paolo
> 
> > Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> > Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org>
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> >   util/qemu-thread-posix.c | 6 ++++++
> >   util/qemu-thread-win32.c | 6 ++++++
> >   2 files changed, 12 insertions(+)
> > 
> > diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> > index b1c127dbe3..afeac9ecad 100644
> > --- a/util/qemu-thread-posix.c
> > +++ b/util/qemu-thread-posix.c
> > @@ -22,6 +22,12 @@
> >   #include <pthread_np.h>
> >   #endif
> > +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> > +qemu_thread_init(void)
> > +{
> > +    qemu_thread_set_name("main");
> > +}
> > +
> >   static void error_exit(int err, const char *msg)
> >   {
> >       fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
> > diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
> > index 4d2d663a9a..8ca6429ad3 100644
> > --- a/util/qemu-thread-win32.c
> > +++ b/util/qemu-thread-win32.c
> > @@ -22,6 +22,12 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
> >   static pSetThreadDescription SetThreadDescriptionFunc;
> >   static HMODULE kernel32_module;
> > +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> > +qemu_thread_init(void)
> > +{
> > +    qemu_thread_set_name("main");
> > +}
> > +
> >   static bool load_set_thread_description(void)
> >   {
> >       static gsize _init_once = 0;
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Re: [PATCH v5 07/24] util: set the name for the 'main' thread
Posted by Paolo Bonzini via Devel 3 weeks, 2 days ago
On 1/9/26 12:52, Daniel P. Berrangé wrote:
> On Fri, Jan 09, 2026 at 12:45:03PM +0100, Paolo Bonzini wrote:
>> On 1/8/26 18:03, Daniel P. Berrangé wrote:
>>> The default main thread name is undefined, so use a constructor to
>>> explicitly set it to 'main'. This constructor is marked to run early
>>> as the thread name is intended to be used in error reporting / logs
>>> which may be triggered very early in QEMU execution.
>>
>> At least on Linux I don't think we want to call the pthread function.
>>
>> Since pthread_setname_np writes to /proc/self/task/tid/comm, wouldn't that
>> cause ps to show qemu processes as "main" instead of "qemu" or "qemu-kvm"?
> 
> Hmmm, it depends on the 'ps' options you use:
> 
> $ ./build/qemu-system-x86_64  &
> [3] 3859009
> 
> ⚙️ [oci:fedora-43 qemu]$ ps  | grep 3859009
> 3859009 pts/10   00:00:04 main
> ⚙️ [oci:fedora-43 qemu]$ ps -a | grep 3859009
> 3859009 pts/10   00:00:03 main
> ⚙️ [oci:fedora-43 qemu]$ ps -ax | grep 3859009
> 3859009 pts/10   Sl     0:03 ./build/qemu-system-x86_64
> ⚙️ [oci:fedora-43 qemu]$ ps -au | grep 3859009
> berrange 3859009  8.6  0.1 3893224 123700 pts/10 Sl   11:49   0:04 ./build/qemu-system-x86_64
> ⚙️ [oci:fedora-43 qemu]$ ps -af | grep 3859009
> berrange 3859009 2085609  8 11:49 pts/10   00:00:04 ./build/qemu-system-x86_64
> ⚙️ [oci:fedora-43 qemu]$ ps -axuwf | grep 3859009
> berrange 3859009 53.4  0.1 3905536 124108 pts/10 Sl   11:49   0:03  |       \_ ./build/qemu-system-x86_64
> 
> 
> and I hadn't noticed since I always use one of the latter
> sets of options that give the true name.

FWIW "top" also changes its process name to "main"; there's precedent 
for Firefox *not* changing it:

$ ps -o "%c" $(pidof firefox)
COMMAND
firefox
forkserver
Socket Process
Privileged Cont
...

Since we can't really know how the COMM is being used by monitoring 
tools, and there's a simple workaround within QEMU, I'd prefer avoiding 
this.  Sorry for not spotting it earlier.

Paolo