thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-util-main-loop-Introduce-the-main-loop-into-QOM.patch

29b115
From b4969662de01848f887a3918e97e516efc213f71 Mon Sep 17 00:00:00 2001
29b115
From: Nicolas Saenz Julienne <nsaenzju@redhat.com>
29b115
Date: Mon, 25 Apr 2022 09:57:22 +0200
29b115
Subject: [PATCH 02/16] util/main-loop: Introduce the main loop into QOM
29b115
29b115
RH-Author: Nicolas Saenz Julienne <nsaenzju@redhat.com>
29b115
RH-MergeRequest: 93: util/thread-pool: Expose minimum and maximum size
29b115
RH-Commit: [2/3] a481b77e25ad50d13dcbe26b36c551b18c89bddd
29b115
RH-Bugzilla: 2031024
29b115
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
29b115
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
29b115
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
29b115
29b115
'event-loop-base' provides basic property handling for all 'AioContext'
29b115
based event loops. So let's define a new 'MainLoopClass' that inherits
29b115
from it. This will permit tweaking the main loop's properties through
29b115
qapi as well as through the command line using the '-object' keyword[1].
29b115
Only one instance of 'MainLoopClass' might be created at any time.
29b115
29b115
'EventLoopBaseClass' learns a new callback, 'can_be_deleted()' so as to
29b115
mark 'MainLoop' as non-deletable.
29b115
29b115
[1] For example:
29b115
      -object main-loop,id=main-loop,aio-max-batch=<value>
29b115
29b115
Signed-off-by: Nicolas Saenz Julienne <nsaenzju@redhat.com>
29b115
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
29b115
Acked-by: Markus Armbruster <armbru@redhat.com>
29b115
Message-id: 20220425075723.20019-3-nsaenzju@redhat.com
29b115
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
29b115
(cherry picked from commit 70ac26b9e5ca8374bb3ef3f30b871726673c9f27)
29b115
---
29b115
 event-loop-base.c                | 13 ++++++++
29b115
 include/qemu/main-loop.h         | 10 ++++++
29b115
 include/sysemu/event-loop-base.h |  1 +
29b115
 meson.build                      |  3 +-
29b115
 qapi/qom.json                    | 13 ++++++++
29b115
 util/main-loop.c                 | 56 ++++++++++++++++++++++++++++++++
29b115
 6 files changed, 95 insertions(+), 1 deletion(-)
29b115
29b115
diff --git a/event-loop-base.c b/event-loop-base.c
29b115
index a924c73a7c..e7f99a6ec8 100644
29b115
--- a/event-loop-base.c
29b115
+++ b/event-loop-base.c
29b115
@@ -73,10 +73,23 @@ static void event_loop_base_complete(UserCreatable *uc, Error **errp)
29b115
     }
29b115
 }
29b115
 
29b115
+static bool event_loop_base_can_be_deleted(UserCreatable *uc)
29b115
+{
29b115
+    EventLoopBaseClass *bc = EVENT_LOOP_BASE_GET_CLASS(uc);
29b115
+    EventLoopBase *backend = EVENT_LOOP_BASE(uc);
29b115
+
29b115
+    if (bc->can_be_deleted) {
29b115
+        return bc->can_be_deleted(backend);
29b115
+    }
29b115
+
29b115
+    return true;
29b115
+}
29b115
+
29b115
 static void event_loop_base_class_init(ObjectClass *klass, void *class_data)
29b115
 {
29b115
     UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
29b115
     ucc->complete = event_loop_base_complete;
29b115
+    ucc->can_be_deleted = event_loop_base_can_be_deleted;
29b115
 
29b115
     object_class_property_add(klass, "aio-max-batch", "int",
29b115
                               event_loop_base_get_param,
29b115
diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h
29b115
index d3750c8e76..20c9387654 100644
29b115
--- a/include/qemu/main-loop.h
29b115
+++ b/include/qemu/main-loop.h
29b115
@@ -26,9 +26,19 @@
29b115
 #define QEMU_MAIN_LOOP_H
29b115
 
29b115
 #include "block/aio.h"
29b115
+#include "qom/object.h"
29b115
+#include "sysemu/event-loop-base.h"
29b115
 
29b115
 #define SIG_IPI SIGUSR1
29b115
 
29b115
+#define TYPE_MAIN_LOOP  "main-loop"
29b115
+OBJECT_DECLARE_TYPE(MainLoop, MainLoopClass, MAIN_LOOP)
29b115
+
29b115
+struct MainLoop {
29b115
+    EventLoopBase parent_obj;
29b115
+};
29b115
+typedef struct MainLoop MainLoop;
29b115
+
29b115
 /**
29b115
  * qemu_init_main_loop: Set up the process so that it can run the main loop.
29b115
  *
29b115
diff --git a/include/sysemu/event-loop-base.h b/include/sysemu/event-loop-base.h
29b115
index 8e77d8b69f..fced4c9fea 100644
29b115
--- a/include/sysemu/event-loop-base.h
29b115
+++ b/include/sysemu/event-loop-base.h
29b115
@@ -25,6 +25,7 @@ struct EventLoopBaseClass {
29b115
 
29b115
     void (*init)(EventLoopBase *base, Error **errp);
29b115
     void (*update_params)(EventLoopBase *base, Error **errp);
29b115
+    bool (*can_be_deleted)(EventLoopBase *base);
29b115
 };
29b115
 
29b115
 struct EventLoopBase {
29b115
diff --git a/meson.build b/meson.build
29b115
index b9c919a55e..5a7c10e639 100644
29b115
--- a/meson.build
29b115
+++ b/meson.build
29b115
@@ -2832,7 +2832,8 @@ libqemuutil = static_library('qemuutil',
29b115
                              sources: util_ss.sources() + stub_ss.sources() + genh,
29b115
                              dependencies: [util_ss.dependencies(), libm, threads, glib, socket, malloc, pixman])
29b115
 qemuutil = declare_dependency(link_with: libqemuutil,
29b115
-                              sources: genh + version_res)
29b115
+                              sources: genh + version_res,
29b115
+                              dependencies: [event_loop_base])
29b115
 
29b115
 if have_system or have_user
29b115
   decodetree = generator(find_program('scripts/decodetree.py'),
29b115
diff --git a/qapi/qom.json b/qapi/qom.json
29b115
index a2439533c5..7d4a2ac1b9 100644
29b115
--- a/qapi/qom.json
29b115
+++ b/qapi/qom.json
29b115
@@ -540,6 +540,17 @@
29b115
             '*poll-grow': 'int',
29b115
             '*poll-shrink': 'int' } }
29b115
 
29b115
+##
29b115
+# @MainLoopProperties:
29b115
+#
29b115
+# Properties for the main-loop object.
29b115
+#
29b115
+# Since: 7.1
29b115
+##
29b115
+{ 'struct': 'MainLoopProperties',
29b115
+  'base': 'EventLoopBaseProperties',
29b115
+  'data': {} }
29b115
+
29b115
 ##
29b115
 # @MemoryBackendProperties:
29b115
 #
29b115
@@ -830,6 +841,7 @@
29b115
     { 'name': 'input-linux',
29b115
       'if': 'CONFIG_LINUX' },
29b115
     'iothread',
29b115
+    'main-loop',
29b115
     { 'name': 'memory-backend-epc',
29b115
       'if': 'CONFIG_LINUX' },
29b115
     'memory-backend-file',
29b115
@@ -895,6 +907,7 @@
29b115
       'input-linux':                { 'type': 'InputLinuxProperties',
29b115
                                       'if': 'CONFIG_LINUX' },
29b115
       'iothread':                   'IothreadProperties',
29b115
+      'main-loop':                  'MainLoopProperties',
29b115
       'memory-backend-epc':         { 'type': 'MemoryBackendEpcProperties',
29b115
                                       'if': 'CONFIG_LINUX' },
29b115
       'memory-backend-file':        'MemoryBackendFileProperties',
29b115
diff --git a/util/main-loop.c b/util/main-loop.c
29b115
index b7b0ce4ca0..5b13f456fa 100644
29b115
--- a/util/main-loop.c
29b115
+++ b/util/main-loop.c
29b115
@@ -33,6 +33,7 @@
29b115
 #include "qemu/error-report.h"
29b115
 #include "qemu/queue.h"
29b115
 #include "qemu/compiler.h"
29b115
+#include "qom/object.h"
29b115
 
29b115
 #ifndef _WIN32
29b115
 #include <sys/wait.h>
29b115
@@ -184,6 +185,61 @@ int qemu_init_main_loop(Error **errp)
29b115
     return 0;
29b115
 }
29b115
 
29b115
+static void main_loop_update_params(EventLoopBase *base, Error **errp)
29b115
+{
29b115
+    if (!qemu_aio_context) {
29b115
+        error_setg(errp, "qemu aio context not ready");
29b115
+        return;
29b115
+    }
29b115
+
29b115
+    aio_context_set_aio_params(qemu_aio_context, base->aio_max_batch, errp);
29b115
+}
29b115
+
29b115
+MainLoop *mloop;
29b115
+
29b115
+static void main_loop_init(EventLoopBase *base, Error **errp)
29b115
+{
29b115
+    MainLoop *m = MAIN_LOOP(base);
29b115
+
29b115
+    if (mloop) {
29b115
+        error_setg(errp, "only one main-loop instance allowed");
29b115
+        return;
29b115
+    }
29b115
+
29b115
+    main_loop_update_params(base, errp);
29b115
+
29b115
+    mloop = m;
29b115
+    return;
29b115
+}
29b115
+
29b115
+static bool main_loop_can_be_deleted(EventLoopBase *base)
29b115
+{
29b115
+    return false;
29b115
+}
29b115
+
29b115
+static void main_loop_class_init(ObjectClass *oc, void *class_data)
29b115
+{
29b115
+    EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(oc);
29b115
+
29b115
+    bc->init = main_loop_init;
29b115
+    bc->update_params = main_loop_update_params;
29b115
+    bc->can_be_deleted = main_loop_can_be_deleted;
29b115
+}
29b115
+
29b115
+static const TypeInfo main_loop_info = {
29b115
+    .name = TYPE_MAIN_LOOP,
29b115
+    .parent = TYPE_EVENT_LOOP_BASE,
29b115
+    .class_init = main_loop_class_init,
29b115
+    .instance_size = sizeof(MainLoop),
29b115
+};
29b115
+
29b115
+static void main_loop_register_types(void)
29b115
+{
29b115
+    type_register_static(&main_loop_info);
29b115
+}
29b115
+
29b115
+type_init(main_loop_register_types)
29b115
+
29b115
 static int max_priority;
29b115
 
29b115
 #ifndef _WIN32
29b115
-- 
29b115
2.31.1
29b115