All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
@ 2026-11-26 22:00 Philippe Mathieu-Daudé
  2026-11-26 22:34 ` Mohamed Mediouni
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-11-26 22:00 UTC (permalink / raw)
  To: qemu-devel
  Cc: Paolo Bonzini, Pierrick Bouvier, Mohamed Mediouni,
	Philippe Mathieu-Daudé

qemu_get_thread_id() fell back to getpid() on macOS, so
every thread, including each vCPU reported by query-cpus-fast,
shared the process id. Use pthread_threadid_np(), keeping the
value positive.

For example running '-M virt -smp 2 -accel tcg,thread=multi',
QMP 'query-cpus-fast' returned before:

  {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
              {"thread-id": 58412, "cpu-index": 1, ...}]}

and now:

  {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
              {"thread-id": 75188617, "cpu-index": 1, ...}]}

Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
 util/oslib-posix.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index dc001da66dd4..71fd369e42f1 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
     return _lwp_self();
 #elif defined(__OpenBSD__)
     return getthrid();
+#elif defined(__APPLE__)
+    uint64_t tid;
+    pthread_threadid_np(NULL, &tid);
+    return tid & INT_MAX;
 #else
     return getpid();
 #endif
-- 
2.55.0



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

* Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
  2026-11-26 22:00 [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS Philippe Mathieu-Daudé
@ 2026-11-26 22:34 ` Mohamed Mediouni
  2026-11-26 23:16 ` Pierrick Bouvier
  2026-11-27  9:25 ` Daniel P. Berrangé
  2 siblings, 0 replies; 5+ messages in thread
From: Mohamed Mediouni @ 2026-11-26 22:34 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé; +Cc: qemu-devel, Paolo Bonzini, Pierrick Bouvier



> On 26. Nov 2026, at 23:00, Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> wrote:
> 
> qemu_get_thread_id() fell back to getpid() on macOS, so
> every thread, including each vCPU reported by query-cpus-fast,
> shared the process id. Use pthread_threadid_np(), keeping the
> value positive.
> 
> For example running '-M virt -smp 2 -accel tcg,thread=multi',
> QMP 'query-cpus-fast' returned before:
> 
>  {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
>              {"thread-id": 58412, "cpu-index": 1, ...}]}
> 
> and now:
> 
>  {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
>              {"thread-id": 75188617, "cpu-index": 1, ...}]}
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Reviewed-by: Mohamed Mediouni <mohamed.mediouni@linaro.org>
> ---
> util/oslib-posix.c | 4 ++++
> 1 file changed, 4 insertions(+)
> 
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index dc001da66dd4..71fd369e42f1 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
>     return _lwp_self();
> #elif defined(__OpenBSD__)
>     return getthrid();
> +#elif defined(__APPLE__)
> +    uint64_t tid;
> +    pthread_threadid_np(NULL, &tid);
> +    return tid & INT_MAX;
> #else
>     return getpid();
> #endif
> -- 
> 2.55.0
> 



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

* Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
  2026-11-26 22:00 [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS Philippe Mathieu-Daudé
  2026-11-26 22:34 ` Mohamed Mediouni
@ 2026-11-26 23:16 ` Pierrick Bouvier
  2026-11-27  9:25 ` Daniel P. Berrangé
  2 siblings, 0 replies; 5+ messages in thread
From: Pierrick Bouvier @ 2026-11-26 23:16 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel; +Cc: Paolo Bonzini, Mohamed Mediouni

On 11/26/2026 2:00 PM, Philippe Mathieu-Daudé wrote:
> qemu_get_thread_id() fell back to getpid() on macOS, so
> every thread, including each vCPU reported by query-cpus-fast,
> shared the process id. Use pthread_threadid_np(), keeping the
> value positive.
> 
> For example running '-M virt -smp 2 -accel tcg,thread=multi',
> QMP 'query-cpus-fast' returned before:
> 
>   {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
>               {"thread-id": 58412, "cpu-index": 1, ...}]}
> 
> and now:
> 
>   {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
>               {"thread-id": 75188617, "cpu-index": 1, ...}]}
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
>  util/oslib-posix.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>


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

* Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
  2026-11-26 22:00 [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS Philippe Mathieu-Daudé
  2026-11-26 22:34 ` Mohamed Mediouni
  2026-11-26 23:16 ` Pierrick Bouvier
@ 2026-11-27  9:25 ` Daniel P. Berrangé
  2026-11-27 17:17   ` Philippe Mathieu-Daudé
  2 siblings, 1 reply; 5+ messages in thread
From: Daniel P. Berrangé @ 2026-11-27  9:25 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Paolo Bonzini, Pierrick Bouvier, Mohamed Mediouni

On Thu, Nov 26, 2026 at 11:00:01PM +0100, Philippe Mathieu-Daudé wrote:
> qemu_get_thread_id() fell back to getpid() on macOS, so
> every thread, including each vCPU reported by query-cpus-fast,
> shared the process id. Use pthread_threadid_np(), keeping the
> value positive.
> 
> For example running '-M virt -smp 2 -accel tcg,thread=multi',
> QMP 'query-cpus-fast' returned before:
> 
>   {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
>               {"thread-id": 58412, "cpu-index": 1, ...}]}
> 
> and now:
> 
>   {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
>               {"thread-id": 75188617, "cpu-index": 1, ...}]}
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
>  util/oslib-posix.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index dc001da66dd4..71fd369e42f1 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
>      return _lwp_self();
>  #elif defined(__OpenBSD__)
>      return getthrid();
> +#elif defined(__APPLE__)
> +    uint64_t tid;
> +    pthread_threadid_np(NULL, &tid);
> +    return tid & INT_MAX;
>  #else

I wonder how likely it is that a thread ID would exceed 'int' ?
IOW, do  we need to consider changing return type to uint64_t

>      return getpid();

Separately, it is probably worth also adding

   warn_report_once("No qemu_get_thread_id() impl on this platform")

so that we nag people to fix remaining platform gaps they
might care about.

With regards,
Daniel
-- 
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|



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

* Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
  2026-11-27  9:25 ` Daniel P. Berrangé
@ 2026-11-27 17:17   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-11-27 17:17 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: qemu-devel, Paolo Bonzini, Pierrick Bouvier, Mohamed Mediouni

On 27/11/26 10:25, Daniel P. Berrangé wrote:
> On Thu, Nov 26, 2026 at 11:00:01PM +0100, Philippe Mathieu-Daudé wrote:
>> qemu_get_thread_id() fell back to getpid() on macOS, so
>> every thread, including each vCPU reported by query-cpus-fast,
>> shared the process id. Use pthread_threadid_np(), keeping the
>> value positive.
>>
>> For example running '-M virt -smp 2 -accel tcg,thread=multi',
>> QMP 'query-cpus-fast' returned before:
>>
>>    {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
>>                {"thread-id": 58412, "cpu-index": 1, ...}]}
>>
>> and now:
>>
>>    {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
>>                {"thread-id": 75188617, "cpu-index": 1, ...}]}
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
>> ---
>>   util/oslib-posix.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
>> index dc001da66dd4..71fd369e42f1 100644
>> --- a/util/oslib-posix.c
>> +++ b/util/oslib-posix.c
>> @@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
>>       return _lwp_self();
>>   #elif defined(__OpenBSD__)
>>       return getthrid();
>> +#elif defined(__APPLE__)
>> +    uint64_t tid;
>> +    pthread_threadid_np(NULL, &tid);
>> +    return tid & INT_MAX;
>>   #else
> 
> I wonder how likely it is that a thread ID would exceed 'int' ?

IIUC macOS thread ids come from one system-wide 64-bit counter that
started at boot, and it never reuses a value. A long-running host that
creates many threads could get there. Going past INT_MAX is possible
but still rare.


> IOW, do  we need to consider changing return type to uint64_t

Yeah I think so. The good news, QAPI thread-id is 'int', which is
int64 on the wire, so QMP needs no change.
Widening to int64_t is a separate cleanup across those structs, and
the mask is enough for now.

> 
>>       return getpid();
> 
> Separately, it is probably worth also adding
> 
>     warn_report_once("No qemu_get_thread_id() impl on this platform")
> 
> so that we nag people to fix remaining platform gaps they
> might care about.

OK.

> 
> With regards,
> Daniel



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

end of thread, other threads:[~2026-11-27 17:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 22:00 [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS Philippe Mathieu-Daudé
2026-11-26 22:34 ` Mohamed Mediouni
2026-11-26 23:16 ` Pierrick Bouvier
2026-11-27  9:25 ` Daniel P. Berrangé
2026-11-27 17:17   ` Philippe Mathieu-Daudé

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.