@samitouri / QOSamiQemu / commits / 1b65aeed2a

system: unconditionally enable thread naming

When thread naming was introduced years ago, it was disabled by default and put behind a command line flag: commit 8f480de0c91a18d550721f8d9af969ebfbda0793 Author: Dr. David Alan Gilbert <dgilbert@redhat.com> Date: Thu Jan 30 10:20:31 2014 +0000 Add 'debug-threads' suboption to --name This was done based on a concern that something might depend on the historical thread naming. Thread names, however, were never promised to be part of QEMU's public API. The defaults will vary across platforms, so no assumptions should ever be made about naming. An opt-in behaviour is also unfortunately incompatible with RCU which creates its thread from an constructor function which is run before command line args are parsed. Thus the RCU thread lacks any name. libvirt has unconditionally enabled debug-threads=yes on all VMs it creates for 10 years. Interestingly this DID expose a bug in libvirt, as it parsed /proc/$PID/stat and could not cope with a space in the thread name. This was a latent pre-existing bug in libvirt though, and not a part of QEMU's API. Having thread names always available, will allow thread names to be included in error reports and log messags QEMU prints by default, which will improve ability to triage QEMU bugs. Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Daniel P. Berrangé committed Aug 6, 2025 at 13:37 UTC 1b65aeed2a04cc067308e42811c7e220f32216f3
5 files changed +21 -44
docs/about/deprecated.rst
+7
@@ -54,6 +54,13 @@ as short-form boolean values, and passed to plugins as ``arg_name=on``.
54 However, short-form booleans are deprecated and full explicit ``arg_name=on``
55 form is preferred.
56
57 +``debug-threads`` option for ``-name`` (since 11.0)
58 +'''''''''''''''''''''''''''''''''''''''''''''''''''
59 +
60 +The ``debug-threads`` option of the ``-name`` argument is now
61 +ignored. Thread naming is unconditionally enabled for all platforms
62 +where it is supported.
63 +
64 QEMU Machine Protocol (QMP) commands
65 ------------------------------------
66
include/qemu/thread.h
-1
@@ -215,7 +215,6 @@ void *qemu_thread_join(QemuThread *thread);
215 void qemu_thread_get_self(QemuThread *thread);
216 bool qemu_thread_is_self(QemuThread *thread);
217 G_NORETURN void qemu_thread_exit(void *retval);
218 -void qemu_thread_naming(bool enable);
218
219 struct Notifier;
220 /**
system/vl.c
+7 -5
@@ -403,9 +403,8 @@ static QemuOptsList qemu_name_opts = {
403 }, {
404 .name = "debug-threads",
405 .type = QEMU_OPT_BOOL,
406 - .help = "When enabled, name the individual threads; defaults off.\n"
407 - "NOTE: The thread names are for debugging and not a\n"
408 - "stable API.",
406 + .help = "Enable thread names"
407 + "(deprecated, always enabled where supported)",
408 },
409 { /* End of list */ }
410 },
@@ -554,9 +553,12 @@ static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
553 {
554 const char *proc_name;
555
557 - if (qemu_opt_get(opts, "debug-threads")) {
558 - qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
556 + if (qemu_opt_get(opts, "debug-threads") &&
557 + !qemu_opt_get_bool(opts, "debug-threads", false)) {
558 + fprintf(stderr, "Ignoring deprecated 'debug-threads=no' option, " \
559 + "thread naming is unconditionally enabled\n");
560 }
561 +
562 qemu_name = qemu_opt_get(opts, "guest");
563
564 proc_name = qemu_opt_get(opts, "process");
util/qemu-thread-posix.c
+1 -17
@@ -22,22 +22,6 @@
22 #include <pthread_np.h>
23 #endif
24
25 -static bool name_threads;
26 -
27 -void qemu_thread_naming(bool enable)
28 -{
29 - name_threads = enable;
30 -
31 -#if !defined CONFIG_PTHREAD_SETNAME_NP_W_TID && \
32 - !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID && \
33 - !defined CONFIG_PTHREAD_SET_NAME_NP
34 - /* This is a debugging option, not fatal */
35 - if (enable) {
36 - fprintf(stderr, "qemu: thread naming not supported on this host\n");
37 - }
38 -#endif
39 -}
40 -
25 static void error_exit(int err, const char *msg)
26 {
27 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
@@ -361,7 +345,7 @@ static void *qemu_thread_start(void *args)
345 /* Attempt to set the threads name; note that this is for debug, so
346 * we're not going to fail if we can't set it.
347 */
364 - if (name_threads && qemu_thread_args->name) {
348 + if (qemu_thread_args->name) {
349 # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
350 pthread_setname_np(pthread_self(), qemu_thread_args->name);
351 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
util/qemu-thread-win32.c
+6 -21
@@ -17,8 +17,6 @@
17 #include "qemu-thread-common.h"
18 #include <process.h>
19
20 -static bool name_threads;
21 -
20 typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
21 PCWSTR lpThreadDescription);
22 static pSetThreadDescription SetThreadDescriptionFunc;
@@ -44,16 +42,6 @@ static bool load_set_thread_description(void)
42 return !!SetThreadDescriptionFunc;
43 }
44
47 -void qemu_thread_naming(bool enable)
48 -{
49 - name_threads = enable;
50 -
51 - if (enable && !load_set_thread_description()) {
52 - fprintf(stderr, "qemu: thread naming not supported on this host\n");
53 - name_threads = false;
54 - }
55 -}
56 -
45 static void error_exit(int err, const char *msg)
46 {
47 char *pstr;
@@ -328,23 +316,20 @@ void *qemu_thread_join(QemuThread *thread)
316 return ret;
317 }
318
331 -static bool set_thread_description(HANDLE h, const char *name)
319 +static void set_thread_description(HANDLE h, const char *name)
320 {
333 - HRESULT hr;
321 g_autofree wchar_t *namew = NULL;
322
323 if (!load_set_thread_description()) {
337 - return false;
324 + return;
325 }
326
327 namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
328 if (!namew) {
342 - return false;
329 + return;
330 }
331
345 - hr = SetThreadDescriptionFunc(h, namew);
346 -
347 - return SUCCEEDED(hr);
332 + SetThreadDescriptionFunc(h, namew);
333 }
334
335 void qemu_thread_create(QemuThread *thread, const char *name,
@@ -370,8 +355,8 @@ void qemu_thread_create(QemuThread *thread, const char *name,
355 if (!hThread) {
356 error_exit(GetLastError(), __func__);
357 }
373 - if (name_threads && name && !set_thread_description(hThread, name)) {
374 - fprintf(stderr, "qemu: failed to set thread description: %s\n", name);
358 + if (name) {
359 + set_thread_description(hThread, name);
360 }
361 CloseHandle(hThread);
362