Skip to contentSkip to conversation

iothread: Set the GSource "name" field

PATCH Superseded Fabiano Rosas 4 participants 5 messages Sep 4, 2023 – Sep 5, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iothread: Set the GSource "name" field

Having a name in the source helps with debugging core dumps when one might not have access to TLS data to cross-reference AioContexts with their addresses.

---
 iothread.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
iothread.c +9 -4
index b41c305bd9..8cfb9d5be6 100644
@@ -138,15 +138,19 @@ static void iothread_instance_finalize(Object *obj)
     qemu_sem_destroy(&iothread->init_done_sem);
 }
 
-static void iothread_init_gcontext(IOThread *iothread)
+static void iothread_init_gcontext(IOThread *iothread, char *thread_name)
 {
     GSource *source;
+    gchar *name = g_strdup_printf("%s aio-context", thread_name);
 
     iothread->worker_context = g_main_context_new();
     source = aio_get_g_source(iothread_get_aio_context(iothread));
+    g_source_set_name(source, name);
     g_source_attach(source, iothread->worker_context);
     g_source_unref(source);
     iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
+
+    g_free(name);
 }
 
 static void iothread_set_aio_context_params(EventLoopBase *base, Error **errp)
@@ -189,11 +193,14 @@ static void iothread_init(EventLoopBase *base, Error **errp)
         return;
     }
 
+    thread_name = g_strdup_printf("IO %s",
+                        object_get_canonical_path_component(OBJECT(base)));
+
     /*
      * Init one GMainContext for the iothread unconditionally, even if
      * it's not used
      */
-    iothread_init_gcontext(iothread);
+    iothread_init_gcontext(iothread, thread_name);
 
     iothread_set_aio_context_params(base, &local_error);
     if (local_error) {
@@ -206,8 +213,6 @@ static void iothread_init(EventLoopBase *base, Error **errp)
     /* This assumes we are called from a thread with useful CPU affinity for us
      * to inherit.
      */
-    thread_name = g_strdup_printf("IO %s",
-                        object_get_canonical_path_component(OBJECT(base)));
     qemu_thread_create(&iothread->thread, thread_name, iothread_run,
                        iothread, QEMU_THREAD_JOINABLE);
     g_free(thread_name);
Signature · 1 line
2.35.3
#2 lore raw
In reply to Fabiano Rosas
Quoted 6 lines · Fabiano Rosas
iothread.c +2 -0
@@ -189,11 +193,14 @@ static void iothread_init(EventLoopBase *base, Error **errp)
         return;
     }
 
+    thread_name = g_strdup_printf("IO %s",
+                        object_get_canonical_path_component(OBJECT(base)));

iothread.c static void iothread_init(EventLoopBase *base, Error **errp) · line 206

Quoted 10 lines
iothread.c +2 -1
+
     /*
      * Init one GMainContext for the iothread unconditionally, even if
      * it's not used
      */
-    iothread_init_gcontext(iothread);
+    iothread_init_gcontext(iothread, thread_name);
 
     iothread_set_aio_context_params(base, &local_error);
     if (local_error) {

I think thread_name might be leaked if error here. Thanks,

Signature · 1 line
Peter Xu
#3 lore raw
Quoted 15 lines · Peter Xu
Quoted 14 lines · Fabiano Rosas
iothread.c +4 -1
@@ -189,11 +193,14 @@ static void iothread_init(EventLoopBase *base, Error **errp)
          return;
      }
 
+    thread_name = g_strdup_printf("IO %s",
+                        object_get_canonical_path_component(OBJECT(base)));
+
      /*
       * Init one GMainContext for the iothread unconditionally, even if
       * it's not used
       */
-    iothread_init_gcontext(iothread);
+    iothread_init_gcontext(iothread, thread_name);
 
Quoted 4 lines

iothread.c static void iothread_init(EventLoopBase *base, Error **errp) · line 206

Quoted 2 lines
iothread.c +0 -0
      iothread_set_aio_context_params(base, &local_error);
      if (local_error) {

I think thread_name might be leaked if error here. Thanks,

Oops, good catch. Better switch to g_autofree.

#4 lore raw
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Peter Xu
Quoted 14 lines · Fabiano Rosas
iothread.c +4 -1
@@ -189,11 +193,14 @@ static void iothread_init(EventLoopBase *base, Error **errp)
          return;
      }
 
+    thread_name = g_strdup_printf("IO %s",
+                        object_get_canonical_path_component(OBJECT(base)));
+
      /*
       * Init one GMainContext for the iothread unconditionally, even if
       * it's not used
       */
-    iothread_init_gcontext(iothread);
+    iothread_init_gcontext(iothread, thread_name);
 
Quoted 6 lines
Quoted 4 lines

iothread.c static void iothread_init(EventLoopBase *base, Error **errp) · line 206

Quoted 2 lines
iothread.c +0 -0
      iothread_set_aio_context_params(base, &local_error);
      if (local_error) {

I think thread_name might be leaked if error here. Thanks,

Oops, good catch. Better switch to g_autofree.

Yes, please.

And also make the iothread_init_gcontext(..., char *thread_name) argument const char * to indicate that the name is owned by the caller and not modified by iothread_init_gcontext().

Thanks,
Stefan