Skip to contentSkip to conversation

iothread: allow to create internal iothreads

PATCH Superseded Peter Xu 6 participants 30 messages Sep 22, 2017 – Oct 8, 2017

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

cover iothread: allow to create internal iothreads

When trying to support monitor OOB (out-of-band) commands, I found that the monitor IO thread I did looks just like iothread. It would be best if I can use iothread directly. However it seems that it was mostly used by "-object iothread" before but not friendly to internal usages. This series tries to export essential functions to do it.

Also, I think patch 2 also fixes a bug in iothread_stop().

Please review. Thanks.

Peter Xu (3):
iothread: provide helpers for internal use
iothread: export iothread_stop()
iothread: delay the context release to finalize

 include/sysemu/iothread.h |  9 +++++++++
 iothread.c                | 51 +++++++++++++++++++++++++++++++++++++----------
 2 files changed, 50 insertions(+), 10 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/3 iothread: provide helpers for internal use

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +21 -0
index 44c8944..74e400c 100644
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}
+
+IOThread *iothread_create(const char *id, Error **errp)
+{
+    Object *obj;
+
+    obj = object_new_with_props(TYPE_IOTHREAD,
+                                iothread_get_internal_parent(),
+                                id, errp, NULL);
+
+    return IOTHREAD(obj);
+}
+
+void iothread_destroy(IOThread *iothread)
+{
+    object_unparent(OBJECT(iothread));
+}
Signature · 1 line
2.7.4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
author
#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 49 lines · Daniel P. Berrange
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Quoted 5 lines

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Yeah, I can split the patch if we want it to be exposed to public.

In that case, would the name "object_get_internal_root" be good?

Thanks,

Signature · 1 line
Peter Xu
#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 56 lines · Peter Xu
Quoted 49 lines · Daniel P. Berrange
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Quoted 5 lines

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Quoted 3 lines

Yeah, I can split the patch if we want it to be exposed to public.

In that case, would the name "object_get_internal_root" be good?

Yeah that's fine with me, but lets see if Paolo has any thoughts from the QOM side.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#5 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 57 lines · Daniel P. Berrange
Quoted 56 lines · Peter Xu
Quoted 54 lines · Daniel P. Berrange
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Quoted 6 lines
Quoted 3 lines

Yeah, I can split the patch if we want it to be exposed to public.

In that case, would the name "object_get_internal_root" be good?

Yeah that's fine with me, but lets see if Paolo has any thoughts from the QOM side.

No, I don't. However, I wonder if query-iothreads should have an argument to include internal iothreads.

Paolo

#6 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 60 lines · Paolo Bonzini
Quoted 59 lines · Daniel P. Berrange
Quoted 58 lines · Peter Xu
Quoted 49 lines · Daniel P. Berrange
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Quoted 5 lines

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Yeah, I can split the patch if we want it to be exposed to public.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

In that case, would the name "object_get_internal_root" be good?

Yeah that's fine with me, but lets see if Paolo has any thoughts from the QOM side.

No, I don't. However, I wonder if query-iothreads should have an argument to include internal iothreads.

I guess it depends whether we consider use of iothreads for migration to be a private impl detail that may be ripped out & replaced at any time, or a semi-public detail that apps may rely on. Personally I would suggest it should be a private impl detail of migration code that is never exposed to anything outside QEMU, so we have flexibility to change it at will later.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#7 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 69 lines · Daniel P. Berrange
Quoted 60 lines · Paolo Bonzini
Quoted 59 lines · Daniel P. Berrange
Quoted 58 lines · Peter Xu
Quoted 49 lines · Daniel P. Berrange
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Quoted 5 lines

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Yeah, I can split the patch if we want it to be exposed to public.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

In that case, would the name "object_get_internal_root" be good?

Yeah that's fine with me, but lets see if Paolo has any thoughts from the QOM side.

No, I don't. However, I wonder if query-iothreads should have an argument to include internal iothreads.

Quoted 6 lines

I guess it depends whether we consider use of iothreads for migration to be a private impl detail that may be ripped out & replaced at any time, or a semi-public detail that apps may rely on. Personally I would suggest it should be a private impl detail of migration code that is never exposed to anything outside QEMU, so we have flexibility to change it at will later.

As far as I understood it's not migration, it's the whole monitor that moves to the iothread. So it's a thing that lasts for the whole existence of the QEMU process. But I may be wrong.

Paolo

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 77 lines · Paolo Bonzini
Quoted 69 lines · Daniel P. Berrange
Quoted 60 lines · Paolo Bonzini
Quoted 59 lines · Daniel P. Berrange
Quoted 58 lines · Peter Xu
Quoted 49 lines · Daniel P. Berrange
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Quoted 5 lines

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Yeah, I can split the patch if we want it to be exposed to public.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

In that case, would the name "object_get_internal_root" be good?

Yeah that's fine with me, but lets see if Paolo has any thoughts from the QOM side.

No, I don't. However, I wonder if query-iothreads should have an argument to include internal iothreads.

Quoted 6 lines

I guess it depends whether we consider use of iothreads for migration to be a private impl detail that may be ripped out & replaced at any time, or a semi-public detail that apps may rely on. Personally I would suggest it should be a private impl detail of migration code that is never exposed to anything outside QEMU, so we have flexibility to change it at will later.

Quoted 3 lines

As far as I understood it's not migration, it's the whole monitor that moves to the iothread. So it's a thing that lasts for the whole existence of the QEMU process. But I may be wrong.

Sorry yes, my bad - its the iothread behind the monitor. I still think that is a detail worth keeping private in case we want to refactor how the monitor threading works later.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#9 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 3 lines · Daniel P. Berrange

Sorry yes, my bad - its the iothread behind the monitor. I still think that is a detail worth keeping private in case we want to refactor how the monitor threading works later.

I agree. I convinced Peter to reuse IOThread just because we can, but we don't need to expose it to user just because we can. Like Daniel, I also see no reason to do that at this point, and I think we can always do it later when necesssary.

Fam

#10 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 7 lines · Fam Zheng
Quoted 3 lines · Daniel P. Berrange

Sorry yes, my bad - its the iothread behind the monitor. I still think that is a detail worth keeping private in case we want to refactor how the monitor threading works later.

I agree. I convinced Peter to reuse IOThread just because we can, but we don't need to expose it to user just because we can. Like Daniel, I also see no reason to do that at this point, and I think we can always do it later when necesssary.

Great. :)

Paolo

author
#11 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

In reply to Paolo Bonzini
Quoted 77 lines · Paolo Bonzini
Quoted 69 lines · Daniel P. Berrange
Quoted 60 lines · Paolo Bonzini
Quoted 59 lines · Daniel P. Berrange
Quoted 58 lines · Peter Xu
Quoted 49 lines · Daniel P. Berrange
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

I tend to think we might benefit from having this generalized in the QOM API instead. We have object_get_objects_root() for things that are created by the mgmt app / user via CLI / QMP. A parallel method object_get_internal_root() could be useful for cases like this where we want to create user-creatable objects, but not have them be visible to the mgmt app / user, as that would confuse the mgmt app.

Quoted 5 lines

Example for this scenario - libvirt calls query-iothreads to identify IO thread PIDs, and would get very unhappy if the IOThread used by the monitor would appear in that response, which is why Peter has put it under /internal-iothreads. I think this scenario will apply more broadly, so benefit from us having a general helper in QOM.

Yeah, I can split the patch if we want it to be exposed to public.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

In that case, would the name "object_get_internal_root" be good?

Yeah that's fine with me, but lets see if Paolo has any thoughts from the QOM side.

No, I don't. However, I wonder if query-iothreads should have an argument to include internal iothreads.

Quoted 6 lines

I guess it depends whether we consider use of iothreads for migration to be a private impl detail that may be ripped out & replaced at any time, or a semi-public detail that apps may rely on. Personally I would suggest it should be a private impl detail of migration code that is never exposed to anything outside QEMU, so we have flexibility to change it at will later.

Quoted 3 lines

As far as I understood it's not migration, it's the whole monitor that moves to the iothread. So it's a thing that lasts for the whole existence of the QEMU process. But I may be wrong.

Yes it is about the monitor thread.

One thing to mention is that, it's not the whole monitor thread - my plan is only to handle QMP IOs in that thread (and execute oob commands only, while oob commands should be really rare). Most of the QMP command dispatching and execution will still be in main thread (which I suppose should be most of the real workload).

IMHO I would prefer user know nothing of this thread, or tune it in any way. Maybe there will be a time when we would like user to tune monitor threads, but IMHO current OOB threading is not that far yet.

Thanks,

Signature · 1 line
Peter Xu
#12 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

In reply to Peter Xu
Quoted 32 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 8 lines
iothread.c +5 -0
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

Markus, please advise on the following QMP API design issue.

The query-iothreads QMP command lists IOThread objects. This patch introduces "internal" IOThreads whose lifetime is dictated by QEMU, not the user's command-line or object-add command.

QMP clients might still want to know the thread IDs of internal IOThreads for CPU affinity. Clients may also want to tweak the AioContext polling properties on the internal objects for performance tuning.

I suggest adding internal IOThreads alongside user-created IOThreads instead of hiding them. IOThread also needs a bool user_created field and a UserCreatableClass->can_be_deleted() function:

  static bool iothread_can_be_deleted(UserCreatable *uc)
  {
      return IOTHREAD(uc)->user_created;
  }

This way users cannot delete internal IOThreads.

But how should object ids be handled? In theory existing -object iothread,id=<id> users could use any name. How can QEMU generate ids for internal IOThreads without conflicting with existing users's ids?

Quoted 19 lines
iothread.c +16 -0
+
+IOThread *iothread_create(const char *id, Error **errp)
+{
+    Object *obj;
+
+    obj = object_new_with_props(TYPE_IOTHREAD,
+                                iothread_get_internal_parent(),
+                                id, errp, NULL);
+
+    return IOTHREAD(obj);
+}
+
+void iothread_destroy(IOThread *iothread)
+{
+    object_unparent(OBJECT(iothread));
+}
Signature · 1 line
2.7.4
#13 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 9 lines · Stefan Hajnoczi

I suggest adding internal IOThreads alongside user-created IOThreads instead of hiding them. IOThread also needs a bool user_created field and a UserCreatableClass->can_be_deleted() function:

  static bool iothread_can_be_deleted(UserCreatable *uc)
  {
      return IOTHREAD(uc)->user_created;
  }
Quoted 5 lines

This way users cannot delete internal IOThreads.

But how should object ids be handled? In theory existing -object iothread,id=<id> users could use any name. How can QEMU generate ids for internal IOThreads without conflicting with existing users's ids?

I would add an 'internal' boolean to query-iothreads' response and a new 'show-internal' boolean to the command. This way, applications that request internal iothreads would know that the "primary key" is (internal, id) rather than just the id.

Paolo

#14 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 12 lines · Paolo Bonzini
Quoted 11 lines · Stefan Hajnoczi

I suggest adding internal IOThreads alongside user-created IOThreads instead of hiding them. IOThread also needs a bool user_created field and a UserCreatableClass->can_be_deleted() function:

  static bool iothread_can_be_deleted(UserCreatable *uc)
  {
      return IOTHREAD(uc)->user_created;
  }

This way users cannot delete internal IOThreads.

Quoted 8 lines
Quoted 3 lines

But how should object ids be handled? In theory existing -object iothread,id=<id> users could use any name. How can QEMU generate ids for internal IOThreads without conflicting with existing users's ids?

I would add an 'internal' boolean to query-iothreads' response and a new 'show-internal' boolean to the command. This way, applications that request internal iothreads would know that the "primary key" is (internal, id) rather than just the id.

What is the app going to do with iothreads if it sees "internal" flag set ? They have no way of knowing what part of QEMU internally is using this iothread, so I don't see that they can do anything intelligent once they find out they exist.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#15 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 22 lines · Daniel P. Berrange
Quoted 12 lines · Paolo Bonzini
Quoted 11 lines · Stefan Hajnoczi

I suggest adding internal IOThreads alongside user-created IOThreads instead of hiding them. IOThread also needs a bool user_created field and a UserCreatableClass->can_be_deleted() function:

  static bool iothread_can_be_deleted(UserCreatable *uc)
  {
      return IOTHREAD(uc)->user_created;
  }

This way users cannot delete internal IOThreads.

Quoted 8 lines
Quoted 3 lines

But how should object ids be handled? In theory existing -object iothread,id=<id> users could use any name. How can QEMU generate ids for internal IOThreads without conflicting with existing users's ids?

I would add an 'internal' boolean to query-iothreads' response and a new 'show-internal' boolean to the command. This way, applications that request internal iothreads would know that the "primary key" is (internal, id) rather than just the id.

Quoted 4 lines

What is the app going to do with iothreads if it sees "internal" flag set ? They have no way of knowing what part of QEMU internally is using this iothread, so I don't see that they can do anything intelligent once they find out they exist.

The application could apply them default settings for scheduler policy or CPU affinity.

Unlike the main or the I/O thread, the monitor thread doesn't interrupt the CPU, so it need not run at SCHED_FIFO even in real-time settings. Alternatively, the application could ensure that such threads would not get in the way of VCPU or I/O threads, providing slightly more stable performance.

Paolo

#16 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 28 lines · Paolo Bonzini
Quoted 22 lines · Daniel P. Berrange
Quoted 12 lines · Paolo Bonzini
Quoted 11 lines · Stefan Hajnoczi

I suggest adding internal IOThreads alongside user-created IOThreads instead of hiding them. IOThread also needs a bool user_created field and a UserCreatableClass->can_be_deleted() function:

  static bool iothread_can_be_deleted(UserCreatable *uc)
  {
      return IOTHREAD(uc)->user_created;
  }

This way users cannot delete internal IOThreads.

Quoted 8 lines
Quoted 3 lines

But how should object ids be handled? In theory existing -object iothread,id=<id> users could use any name. How can QEMU generate ids for internal IOThreads without conflicting with existing users's ids?

I would add an 'internal' boolean to query-iothreads' response and a new 'show-internal' boolean to the command. This way, applications that request internal iothreads would know that the "primary key" is (internal, id) rather than just the id.

Quoted 4 lines

What is the app going to do with iothreads if it sees "internal" flag set ? They have no way of knowing what part of QEMU internally is using this iothread, so I don't see that they can do anything intelligent once they find out they exist.

Quoted 5 lines

The application could apply them default settings for scheduler policy or CPU affinity.

Unlike the main or the I/O thread, the monitor thread doesn't interrupt the CPU, so it need not run at SCHED_FIFO even in real-time settings.

There's no way for the application to know that. ALl it sees is that there is an extra unexpected IOthread created internally by QEMU. There's no way to decide whether it nedes SCHED_FIFO or not as we've no clue what it is being used for.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#17 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 32 lines · Daniel P. Berrange
Quoted 31 lines · Paolo Bonzini
Quoted 22 lines · Daniel P. Berrange
Quoted 12 lines · Paolo Bonzini
Quoted 11 lines · Stefan Hajnoczi

I suggest adding internal IOThreads alongside user-created IOThreads instead of hiding them. IOThread also needs a bool user_created field and a UserCreatableClass->can_be_deleted() function:

  static bool iothread_can_be_deleted(UserCreatable *uc)
  {
      return IOTHREAD(uc)->user_created;
  }

This way users cannot delete internal IOThreads.

Quoted 8 lines
Quoted 3 lines

But how should object ids be handled? In theory existing -object iothread,id=<id> users could use any name. How can QEMU generate ids for internal IOThreads without conflicting with existing users's ids?

I would add an 'internal' boolean to query-iothreads' response and a new 'show-internal' boolean to the command. This way, applications that request internal iothreads would know that the "primary key" is (internal, id) rather than just the id.

Quoted 4 lines

What is the app going to do with iothreads if it sees "internal" flag set ? They have no way of knowing what part of QEMU internally is using this iothread, so I don't see that they can do anything intelligent once they find out they exist.

The application could apply them default settings for scheduler policy or CPU affinity.

Quoted 7 lines
Quoted 2 lines

Unlike the main or the I/O thread, the monitor thread doesn't interrupt the CPU, so it need not run at SCHED_FIFO even in real-time settings.

There's no way for the application to know that. ALl it sees is that there is an extra unexpected IOthread created internally by QEMU. There's no way to decide whether it nedes SCHED_FIFO or not as we've no clue what it is being used for.

For observability (troubleshooting, debugging, etc) I would like all IOThreads to be visible. That makes it much easier to troubleshoot in a unified way rather than having to get at internal IOThreads through different means (e.g. gdb).

If QEMU uses a well-known ID for the internal monitor IOThread then libvirt could also interact with it (e.g. CPU affinity).

One day maybe the main loop thread will also be an internal IOThread.

Stefan

#18 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 41 lines · Stefan Hajnoczi
Quoted 32 lines · Daniel P. Berrange
Quoted 31 lines · Paolo Bonzini
Quoted 22 lines · Daniel P. Berrange
Quoted 12 lines · Paolo Bonzini
Quoted 11 lines · Stefan Hajnoczi

I suggest adding internal IOThreads alongside user-created IOThreads instead of hiding them. IOThread also needs a bool user_created field and a UserCreatableClass->can_be_deleted() function:

  static bool iothread_can_be_deleted(UserCreatable *uc)
  {
      return IOTHREAD(uc)->user_created;
  }

This way users cannot delete internal IOThreads.

Quoted 8 lines
Quoted 3 lines

But how should object ids be handled? In theory existing -object iothread,id=<id> users could use any name. How can QEMU generate ids for internal IOThreads without conflicting with existing users's ids?

I would add an 'internal' boolean to query-iothreads' response and a new 'show-internal' boolean to the command. This way, applications that request internal iothreads would know that the "primary key" is (internal, id) rather than just the id.

Quoted 4 lines

What is the app going to do with iothreads if it sees "internal" flag set ? They have no way of knowing what part of QEMU internally is using this iothread, so I don't see that they can do anything intelligent once they find out they exist.

The application could apply them default settings for scheduler policy or CPU affinity.

Quoted 7 lines
Quoted 2 lines

Unlike the main or the I/O thread, the monitor thread doesn't interrupt the CPU, so it need not run at SCHED_FIFO even in real-time settings.

There's no way for the application to know that. ALl it sees is that there is an extra unexpected IOthread created internally by QEMU. There's no way to decide whether it nedes SCHED_FIFO or not as we've no clue what it is being used for.

Quoted 7 lines

For observability (troubleshooting, debugging, etc) I would like all IOThreads to be visible. That makes it much easier to troubleshoot in a unified way rather than having to get at internal IOThreads through different means (e.g. gdb).

If QEMU uses a well-known ID for the internal monitor IOThread then libvirt could also interact with it (e.g. CPU affinity).

I don't think we should abuse ID values by turning them into stable ABI - currently they are opaque tokens whose value has no special meaning and make change at will over time.

If we need to be able to report the IOthread for the monitor, then it would have to be done via a formal API, but that turns the use of IOThreads for the monitor into a feature we have to keep indefinitely, even if we wanted to re-implement it differently later.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#19 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

In reply to Stefan Hajnoczi
Quoted 34 lines · Stefan Hajnoczi
Quoted 33 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
Quoted 9 lines

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 7 lines
iothread.c +5 -0
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

Markus, please advise on the following QMP API design issue.

Sorry for the slow response. Is my advice still needed, or are you good?

author
#20 lore raw

Re: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use

Quoted 46 lines · Markus Armbruster
Quoted 34 lines · Stefan Hajnoczi
Quoted 33 lines · Peter Xu

IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.

---
 include/sysemu/iothread.h |  8 ++++++++
 iothread.c                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/sysemu/iothread.h +8 -0
index d2985b3..b07663f 100644
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);
 void iothread_stop_all(void);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
+/*
+ * Helpers used to allocate iothreads for internal use.  These
+ * iothreads will not be seen by monitor clients when query using
+ * "query-iothreads".
+ */
+IOThread *iothread_create(const char *id, Error **errp);
+void iothread_destroy(IOThread *iothread);
+
 #endif /* IOTHREAD_H */
iothread.c +0 -0
index 44c8944..74e400c 100644
@@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)
 
Quoted 9 lines

iothread.c GMainContext *iothread_get_g_main_context(IOThread *iothread) · line 361

Quoted 7 lines
iothread.c +5 -0
     return iothread->worker_context;
 }
+
+static Object *iothread_get_internal_parent(void)
+{
+    return container_get(object_get_root(), "/internal-iothreads");
+}

Markus, please advise on the following QMP API design issue.

Quoted 2 lines

Sorry for the slow response. Is my advice still needed, or are you good?

I see that the patches are in master now (latest version, not this one), and the consensus should be that internally used iothreads won't affect results of query-iothreads. So I think we are good now. Thanks,

Signature · 1 line
Peter Xu
author
#21 lore raw

PATCH 2/3 iothread: export iothread_stop()

In reply to Peter Xu

So that internal iothread users can explicitly stop one iothread without destroying it.

Since at it, fix iothread_stop() to allow re-entrance. Before this patch we may call iothread_stop() twice on single iothread, while that may not be correct since qemu_thread_join() is not allowed to run twice.

Quoted 1 line

From manual of pthread_join():

Joining with a thread that has previously been joined results in undefined behavior.

---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 24 ++++++++++++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)
include/sysemu/iothread.h +1 -0
index b07663f..110329b 100644
@@ -52,6 +52,7 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread);
  * "query-iothreads".
  */
 IOThread *iothread_create(const char *id, Error **errp);
+void iothread_stop(IOThread *iothread);
 void iothread_destroy(IOThread *iothread);
 
 #endif /* IOTHREAD_H */
iothread.c +16 -8
index 74e400c..894756b 100644
@@ -80,13 +80,10 @@ static void *iothread_run(void *opaque)
     return NULL;
 }
 
-static int iothread_stop(Object *object, void *opaque)
+void iothread_stop(IOThread *iothread)
 {
-    IOThread *iothread;
-
-    iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
-    if (!iothread || !iothread->ctx) {
-        return 0;
+    if (iothread->stopping) {
+        return;
     }
     iothread->stopping = true;
     aio_notify(iothread->ctx);
@@ -94,6 +91,17 @@ static int iothread_stop(Object *object, void *opaque)
         g_main_loop_quit(iothread->main_loop);
     }
     qemu_thread_join(&iothread->thread);
+}
+
+static int iothread_stop_iter(Object *object, void *opaque)
+{
+    IOThread *iothread;
+
+    iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
+    if (!iothread || !iothread->ctx) {
+        return 0;
+    }
+    iothread_stop(iothread);
     return 0;
 }
 
@@ -108,7 +116,7 @@ static void iothread_instance_finalize(Object *obj)
 {
     IOThread *iothread = IOTHREAD(obj);
 
-    iothread_stop(obj, NULL);
+    iothread_stop(iothread);
     qemu_cond_destroy(&iothread->init_done_cond);
     qemu_mutex_destroy(&iothread->init_done_lock);
     if (!iothread->ctx) {
@@ -328,7 +336,7 @@ void iothread_stop_all(void)
         aio_context_release(ctx);
     }
 
-    object_child_foreach(container, iothread_stop, NULL);
+    object_child_foreach(container, iothread_stop_iter, NULL);
 }
 
 static gpointer iothread_g_main_context_init(gpointer opaque)
Signature · 1 line
2.7.4
#22 lore raw

Re: [Qemu-devel] [PATCH 2/3] iothread: export iothread_stop()

Quoted 4 lines · Peter Xu

So that internal iothread users can explicitly stop one iothread without destroying it.

Since at it, fix iothread_stop() to allow re-entrance. Before this

I don't think there is any re-entrace here. Maybe you mean

s/re-entrance/calling multiple times/

?

Quoted 3 lines

patch we may call iothread_stop() twice on single iothread, while that may not be correct since qemu_thread_join() is not allowed to run twice. From manual of pthread_join():

Is one call from iothread_stop_all() and one from object finalize?

Quoted 47 lines

Joining with a thread that has previously been joined results in undefined behavior.

---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 24 ++++++++++++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)
include/sysemu/iothread.h +1 -0
index b07663f..110329b 100644
@@ -52,6 +52,7 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread);
  * "query-iothreads".
  */
 IOThread *iothread_create(const char *id, Error **errp);
+void iothread_stop(IOThread *iothread);
 void iothread_destroy(IOThread *iothread);
 
 #endif /* IOTHREAD_H */
iothread.c +3 -6
index 74e400c..894756b 100644
@@ -80,13 +80,10 @@ static void *iothread_run(void *opaque)
     return NULL;
 }
 
-static int iothread_stop(Object *object, void *opaque)
+void iothread_stop(IOThread *iothread)
 {
-    IOThread *iothread;
-
-    iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
-    if (!iothread || !iothread->ctx) {
-        return 0;
+    if (iothread->stopping) {
+        return;
     }
     iothread->stopping = true;
     aio_notify(iothread->ctx);
@@ -94,6 +91,17 @@ static int iothread_stop(Object *object, void *opaque)
         g_main_loop_quit(iothread->main_loop);
     }
     qemu_thread_join(&iothread->thread);

iothread.c static int iothread_stop(Object *object, void *opaque) · line 103

Quoted 10 lines
iothread.c +10 -0
+}
+
+static int iothread_stop_iter(Object *object, void *opaque)
+{
+    IOThread *iothread;
+
+    iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
+    if (!iothread || !iothread->ctx) {
+        return 0;
+    }

I think the check of iothread->ctx can be moved to iothread_stop() too.

Fam

Quoted 26 lines
iothread.c +3 -2
+    iothread_stop(iothread);
     return 0;
 }
 
@@ -108,7 +116,7 @@ static void iothread_instance_finalize(Object *obj)
 {
     IOThread *iothread = IOTHREAD(obj);
 
-    iothread_stop(obj, NULL);
+    iothread_stop(iothread);
     qemu_cond_destroy(&iothread->init_done_cond);
     qemu_mutex_destroy(&iothread->init_done_lock);
     if (!iothread->ctx) {
@@ -328,7 +336,7 @@ void iothread_stop_all(void)
         aio_context_release(ctx);
     }
 
-    object_child_foreach(container, iothread_stop, NULL);
+    object_child_foreach(container, iothread_stop_iter, NULL);
 }
 
 static gpointer iothread_g_main_context_init(gpointer opaque)
Signature · 1 line
2.7.4
author
#23 lore raw

Re: [Qemu-devel] [PATCH 2/3] iothread: export iothread_stop()

Quoted 11 lines · Fam Zheng
Quoted 4 lines · Peter Xu

So that internal iothread users can explicitly stop one iothread without destroying it.

Since at it, fix iothread_stop() to allow re-entrance. Before this

I don't think there is any re-entrace here. Maybe you mean

s/re-entrance/calling multiple times/

?

Yes, you are right.

Quoted 6 lines
Quoted 4 lines

patch we may call iothread_stop() twice on single iothread, while that may not be correct since qemu_thread_join() is not allowed to run twice. From manual of pthread_join():

Is one call from iothread_stop_all() and one from object finalize?

Yes.

Quoted 52 lines
Quoted 52 lines

Joining with a thread that has previously been joined results in undefined behavior.

---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 24 ++++++++++++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)
include/sysemu/iothread.h +1 -0
index b07663f..110329b 100644
@@ -52,6 +52,7 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread);
  * "query-iothreads".
  */
 IOThread *iothread_create(const char *id, Error **errp);
+void iothread_stop(IOThread *iothread);
 void iothread_destroy(IOThread *iothread);
 
 #endif /* IOTHREAD_H */
iothread.c +7 -6
index 74e400c..894756b 100644
@@ -80,13 +80,10 @@ static void *iothread_run(void *opaque)
     return NULL;
 }
 
-static int iothread_stop(Object *object, void *opaque)
+void iothread_stop(IOThread *iothread)
 {
-    IOThread *iothread;
-
-    iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
-    if (!iothread || !iothread->ctx) {
-        return 0;
+    if (iothread->stopping) {
+        return;
     }
     iothread->stopping = true;
     aio_notify(iothread->ctx);
@@ -94,6 +91,17 @@ static int iothread_stop(Object *object, void *opaque)
         g_main_loop_quit(iothread->main_loop);
     }
     qemu_thread_join(&iothread->thread);
+}
+
+static int iothread_stop_iter(Object *object, void *opaque)
+{
Quoted 8 lines

iothread.c static int iothread_stop(Object *object, void *opaque) · line 103

Quoted 6 lines
iothread.c +6 -0
+    IOThread *iothread;
+
+    iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
+    if (!iothread || !iothread->ctx) {
+        return 0;
+    }

I think the check of iothread->ctx can be moved to iothread_stop() too.

Yes, will do.

Signature · 1 line
Peter Xu
author
#24 lore raw

PATCH 3/3 iothread: delay the context release to finalize

In reply to Peter Xu

When gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point.

Delay the destruction of gcontext to iothread finalize. Then we can do:

  iothread_stop(thread);
  some_cleanup_on_resources();
  iothread_destroy(thread);
---
 iothread.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
iothread.c +4 -2
index 894756b..b95369b 100644
@@ -71,8 +71,6 @@ static void *iothread_run(void *opaque)
             g_main_loop_unref(loop);
 
             g_main_context_pop_thread_default(iothread->worker_context);
-            g_main_context_unref(iothread->worker_context);
-            iothread->worker_context = NULL;
         }
     }
 
@@ -117,6 +115,10 @@ static void iothread_instance_finalize(Object *obj)
     IOThread *iothread = IOTHREAD(obj);
 
     iothread_stop(iothread);
+    if (iothread->worker_context) {
+        g_main_context_unref(iothread->worker_context);
+        iothread->worker_context = NULL;
+    }
     qemu_cond_destroy(&iothread->init_done_cond);
     qemu_mutex_destroy(&iothread->init_done_lock);
     if (!iothread->ctx) {
Signature · 1 line
2.7.4
Reviewed-by on v1 3/3
#25 lore raw

Re: [Qemu-devel] [PATCH 3/3] iothread: delay the context release to finalize

Quoted 4 lines · Peter Xu

When gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point.

Would be nice if you can also mention the glib bug that "required" this in the commit message.

Quoted 40 lines

Delay the destruction of gcontext to iothread finalize. Then we can do:

  iothread_stop(thread);
  some_cleanup_on_resources();
  iothread_destroy(thread);
---
 iothread.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
iothread.c +4 -2
index 894756b..b95369b 100644
@@ -71,8 +71,6 @@ static void *iothread_run(void *opaque)
             g_main_loop_unref(loop);
 
             g_main_context_pop_thread_default(iothread->worker_context);
-            g_main_context_unref(iothread->worker_context);
-            iothread->worker_context = NULL;
         }
     }
 
@@ -117,6 +115,10 @@ static void iothread_instance_finalize(Object *obj)
     IOThread *iothread = IOTHREAD(obj);
 
     iothread_stop(iothread);
+    if (iothread->worker_context) {
+        g_main_context_unref(iothread->worker_context);
+        iothread->worker_context = NULL;
+    }
     qemu_cond_destroy(&iothread->init_done_cond);
     qemu_mutex_destroy(&iothread->init_done_lock);
     if (!iothread->ctx) {
Signature · 1 line
2.7.4
author
#26 lore raw

Re: [Qemu-devel] [PATCH 3/3] iothread: delay the context release to finalize

Quoted 8 lines · Fam Zheng
Quoted 4 lines · Peter Xu

When gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point.

Would be nice if you can also mention the glib bug that "required" this in the commit message.

I can add it, but I am not sure it's very closely related (and I'm afraid that may confuse more people). Say, even without that bug, I would still think it not a good idea to free the context in the loop, especially considering that we have the finalize function there. Thanks,

Quoted 43 lines
Quoted 40 lines

Delay the destruction of gcontext to iothread finalize. Then we can do:

  iothread_stop(thread);
  some_cleanup_on_resources();
  iothread_destroy(thread);
---
 iothread.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
iothread.c +4 -2
index 894756b..b95369b 100644
@@ -71,8 +71,6 @@ static void *iothread_run(void *opaque)
             g_main_loop_unref(loop);
 
             g_main_context_pop_thread_default(iothread->worker_context);
-            g_main_context_unref(iothread->worker_context);
-            iothread->worker_context = NULL;
         }
     }
 
@@ -117,6 +115,10 @@ static void iothread_instance_finalize(Object *obj)
     IOThread *iothread = IOTHREAD(obj);
 
     iothread_stop(iothread);
+    if (iothread->worker_context) {
+        g_main_context_unref(iothread->worker_context);
+        iothread->worker_context = NULL;
+    }
     qemu_cond_destroy(&iothread->init_done_cond);
     qemu_mutex_destroy(&iothread->init_done_lock);
     if (!iothread->ctx) {
Signature · 1 line
2.7.4
Signature · 1 line
Peter Xu
#27 lore raw

Re: [Qemu-devel] [PATCH 3/3] iothread: delay the context release to finalize

Quoted 7 lines · Peter Xu
Quoted 6 lines · Fam Zheng
Quoted 4 lines · Peter Xu

When gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point.

Quoted 7 lines
Quoted 2 lines

Would be nice if you can also mention the glib bug that "required" this in the commit message.

I can add it, but I am not sure it's very closely related (and I'm afraid that may confuse more people). Say, even without that bug, I would still think it not a good idea to free the context in the loop, especially considering that we have the finalize function there. Thanks,

It's interesting to know if or not your future change will break without this patch, this is especially useful for backport.

Fam

author
#28 lore raw

Re: [Qemu-devel] [PATCH 3/3] iothread: delay the context release to finalize

Quoted 11 lines · Fam Zheng
Quoted 10 lines · Peter Xu
Quoted 8 lines · Fam Zheng
Quoted 4 lines · Peter Xu

When gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point.

Would be nice if you can also mention the glib bug that "required" this in the commit message.

Quoted 7 lines
Quoted 4 lines

I can add it, but I am not sure it's very closely related (and I'm afraid that may confuse more people). Say, even without that bug, I would still think it not a good idea to free the context in the loop, especially considering that we have the finalize function there. Thanks,

It's interesting to know if or not your future change will break without this patch, this is especially useful for backport.

I haven't tried to run with iothread and without this patch, but I think it should fail, so this patch should be needed.

The point is that we should not destroy the context before explicitly calling remove_fd_in_watch() if the context is running chardevs. Without this patch, this rule does not satisfy. And IIUC this rule comes from the glib bug.

Anyway, I'll mention it in commit message to clarify.

Signature · 1 line
Peter Xu
#29 lore raw

Re: [Qemu-devel] [PATCH 3/3] iothread: delay the context release to finalize

Quoted 23 lines · Peter Xu
Quoted 11 lines · Fam Zheng
Quoted 10 lines · Peter Xu
Quoted 8 lines · Fam Zheng
Quoted 4 lines · Peter Xu

When gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point.

Would be nice if you can also mention the glib bug that "required" this in the commit message.

Quoted 7 lines
Quoted 4 lines

I can add it, but I am not sure it's very closely related (and I'm afraid that may confuse more people). Say, even without that bug, I would still think it not a good idea to free the context in the loop, especially considering that we have the finalize function there. Thanks,

It's interesting to know if or not your future change will break without this patch, this is especially useful for backport.

I haven't tried to run with iothread and without this patch, but I think it should fail, so this patch should be needed.

Quoted 6 lines

The point is that we should not destroy the context before explicitly calling remove_fd_in_watch() if the context is running chardevs. Without this patch, this rule does not satisfy. And IIUC this rule comes from the glib bug.

Anyway, I'll mention it in commit message to clarify.

OK, thanks for the explanations! My r-b still stands with the amended commit log.

Fam