From nobody Sat Jul 5 16:22:16 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1521626582701393.6436802744515; Wed, 21 Mar 2018 03:03:02 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2AB5E5D67A; Wed, 21 Mar 2018 10:03:01 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CEABB7A40B; Wed, 21 Mar 2018 10:03:00 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 6DD00180BAD5; Wed, 21 Mar 2018 10:03:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w2LA2p1o023270 for ; Wed, 21 Mar 2018 06:02:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 87A31215CDC7; Wed, 21 Mar 2018 10:02:51 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E8EA215CDB7 for ; Wed, 21 Mar 2018 10:02:51 +0000 (UTC) From: Pavel Hrdina To: libvir-list@redhat.com Date: Wed, 21 Mar 2018 11:02:46 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com Subject: [libvirt] [dbus PATCH v3 4/5] main: introduce thread pool to process D-Bus messages X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 21 Mar 2018 10:03:01 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The default thread count is currently 4 and it is also configurable via --threads/-t paramter for the libvirt-dbus daemon. Signed-off-by: Pavel Hrdina Reviewed-by: Daniel P. Berrang=C3=A9 --- src/connect.c | 4 +++ src/connect.h | 1 + src/gdbus.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++---------= ---- src/gdbus.h | 4 +++ src/main.c | 10 ++++++ 5 files changed, 95 insertions(+), 21 deletions(-) diff --git a/src/connect.c b/src/connect.c index 70af8ac..bf97cd5 100644 --- a/src/connect.c +++ b/src/connect.c @@ -56,6 +56,8 @@ gboolean virtDBusConnectOpen(virtDBusConnect *connect, GError **error) { + g_autoptr(GMutexLocker) lock =3D g_mutex_locker_new(&connect->lock); + if (connect->connection) { if (virConnectIsAlive(connect->connection)) return TRUE; @@ -213,6 +215,8 @@ virtDBusConnectNew(virtDBusConnect **connectp, =20 connect =3D g_new0(virtDBusConnect, 1); =20 + g_mutex_init(&connect->lock); + for (gint i =3D 0; i < VIR_DOMAIN_EVENT_ID_LAST; i +=3D 1) connect->callback_ids[i] =3D -1; =20 diff --git a/src/connect.h b/src/connect.h index bfe2995..9572857 100644 --- a/src/connect.h +++ b/src/connect.h @@ -14,6 +14,7 @@ struct virtDBusConnect { const gchar *connectPath; gchar *domainPath; virConnectPtr connection; + GMutex lock; =20 gint callback_ids[VIR_DOMAIN_EVENT_ID_LAST]; }; diff --git a/src/gdbus.c b/src/gdbus.c index cbfd07d..688dc38 100644 --- a/src/gdbus.c +++ b/src/gdbus.c @@ -16,6 +16,16 @@ struct _virtDBusGDBusSubtreeData { }; typedef struct _virtDBusGDBusSubtreeData virtDBusGDBusSubtreeData; =20 +struct _virtDBusGDBusThreadData { + const gchar *objectPath; + const gchar *interfaceName; + const gchar *methodName; + GVariant *parameters; + GDBusMethodInvocation *invocation; + virtDBusGDBusMethodData *methodData; +}; +typedef struct _virtDBusGDBusThreadData virtDBusGDBusThreadData; + static const gchar *dbusInterfacePrefix =3D NULL; =20 /** @@ -226,6 +236,38 @@ virtDBusGDBusHandleMethod(GVariant *parameters, outFDs); } =20 +static void +virtDBusGDBusMethodCallThread(gpointer threadData, + gpointer userData G_GNUC_UNUSED) +{ + g_autofree virtDBusGDBusThreadData *data =3D threadData; + + if (g_strcmp0(data->interfaceName, "org.freedesktop.DBus.Properties") = =3D=3D 0) { + if (g_strcmp0(data->methodName, "Get") =3D=3D 0) { + virtDBusGDBusHandlePropertyGet(data->parameters, data->invocat= ion, + data->objectPath, data->methodD= ata); + } else if (g_strcmp0(data->methodName, "Set") =3D=3D 0) { + virtDBusGDBusHandlePropertySet(data->parameters, data->invocat= ion, + data->objectPath, data->methodD= ata); + } else if (g_strcmp0(data->methodName, "GetAll") =3D=3D 0) { + virtDBusGDBusHandlePropertyGetAll(data->invocation, data->obje= ctPath, + data->methodData); + } else { + g_dbus_method_invocation_return_error(data->invocation, + G_DBUS_ERROR, + G_DBUS_ERROR_UNKNOWN_MET= HOD, + "unknown method '%s'", + data->methodName); + } + } else { + virtDBusGDBusHandleMethod(data->parameters, data->invocation, + data->objectPath, data->methodName, + data->methodData); + } +} + +GThreadPool *threadPool; + static void virtDBusGDBusHandleMethodCall(GDBusConnection *connection G_GNUC_UNUSED, const gchar *sender G_GNUC_UNUSED, @@ -236,27 +278,18 @@ virtDBusGDBusHandleMethodCall(GDBusConnection *connec= tion G_GNUC_UNUSED, GDBusMethodInvocation *invocation, gpointer userData) { - virtDBusGDBusMethodData *data =3D userData; - - if (g_strcmp0(interfaceName, "org.freedesktop.DBus.Properties") =3D=3D= 0) { - if (g_strcmp0(methodName, "Get") =3D=3D 0) { - virtDBusGDBusHandlePropertyGet(parameters, invocation, - objectPath, data); - } else if (g_strcmp0(methodName, "Set") =3D=3D 0) { - virtDBusGDBusHandlePropertySet(parameters, invocation, - objectPath, data); - } else if (g_strcmp0(methodName, "GetAll") =3D=3D 0) { - virtDBusGDBusHandlePropertyGetAll(invocation, objectPath, data= ); - } else { - g_dbus_method_invocation_return_error(invocation, - G_DBUS_ERROR, - G_DBUS_ERROR_UNKNOWN_MET= HOD, - "unknown method '%s'", m= ethodName); - } - } else { - virtDBusGDBusHandleMethod(parameters, invocation, objectPath, - methodName, data); - } + virtDBusGDBusThreadData *data =3D g_new0(virtDBusGDBusThreadData, 1); + + g_assert(data); + + data->objectPath =3D objectPath; + data->interfaceName =3D interfaceName; + data->methodName =3D methodName; + data->parameters =3D parameters; + data->invocation =3D invocation; + data->methodData =3D userData; + + g_thread_pool_push(threadPool, data, NULL); } =20 static const GDBusInterfaceVTable virtDBusGDBusVtable =3D { @@ -396,3 +429,25 @@ virtDBusGDBusRegisterSubtree(GDBusConnection *bus, virtDBusGDBusSubtreeDataFree, NULL); } + +/** + * virtDBusGDBusPrepareThreadPool: + * @maxThreads: the number of maximum threads in thread pool + * @error: return location for error or NULL + * + * Initializes thread pool to be used to process D-Bus messages. + * + * Returns TRUE on success, FALSE on error and sets @error. + */ +gboolean +virtDBusGDBusPrepareThreadPool(gint maxThreads, + GError **error) +{ + threadPool =3D g_thread_pool_new(virtDBusGDBusMethodCallThread, + NULL, + maxThreads, + FALSE, + error); + + return !!threadPool; +} diff --git a/src/gdbus.h b/src/gdbus.h index 6c1a8a3..5ab9cd0 100644 --- a/src/gdbus.h +++ b/src/gdbus.h @@ -104,5 +104,9 @@ virtDBusGDBusRegisterSubtree(GDBusConnection *bus, virtDBusGDBusPropertyTable *properties, gpointer userData); =20 +gboolean +virtDBusGDBusPrepareThreadPool(gint maxThreads, + GError **error); + G_DEFINE_AUTO_CLEANUP_FREE_FUNC(virtDBusGDBusSource, g_source_remove, 0); G_DEFINE_AUTO_CLEANUP_FREE_FUNC(virtDBusGDBusOwner, g_bus_unown_name, 0); diff --git a/src/main.c b/src/main.c index bbbec09..7afde4f 100644 --- a/src/main.c +++ b/src/main.c @@ -89,11 +89,14 @@ virtDBusRegisterDataFree(virtDBusRegisterData *data) } G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virtDBusRegisterData, virtDBusRegisterDat= aFree); =20 +#define VIRT_DBUS_MAX_THREADS 4 + int main(gint argc, gchar *argv[]) { static gboolean systemOpt =3D FALSE; static gboolean sessionOpt =3D FALSE; + static gint maxThreads =3D VIRT_DBUS_MAX_THREADS; GBusType busType; g_auto(virtDBusGDBusSource) sigintSource =3D 0; g_auto(virtDBusGDBusSource) sigtermSource =3D 0; @@ -108,6 +111,8 @@ main(gint argc, gchar *argv[]) "Connect to the system bus", NULL }, { "session", 0, 0, G_OPTION_ARG_NONE, &sessionOpt, "Connect to the session bus", NULL }, + { "threads", 't', 0, G_OPTION_ARG_INT, &maxThreads, + "Configure maximal number of worker threads", "N" }, { NULL } }; =20 @@ -145,6 +150,11 @@ main(gint argc, gchar *argv[]) } data.connectList =3D g_new0(virtDBusConnect *, data.ndrivers + 1); =20 + if (!virtDBusGDBusPrepareThreadPool(maxThreads, &error)) { + g_printerr("%s\n", error->message); + exit(EXIT_FAILURE); + } + loop =3D g_main_loop_new(NULL, FALSE); =20 sigtermSource =3D g_unix_signal_add(SIGTERM, --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list