[libvirt] [PATCH 4/5] Convert QEMU driver mutex into a rwlock

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



A number of driver API methods which acquire the driver mutex
only ever used the driver object in a read-only fashion. All
these uses are converted to call qemuDriverLockRO() allowing
for greater concurrency.

* src/qemu/qemu_conf.h: s/Mutex/RWLock/
* src/qemu/qemu_driver.c: Add a qemuDriverLockRO() method and use
  it anywhere that doesn't require a write lock on the driver
---
 src/qemu/qemu_conf.h   |    2 +-
 src/qemu/qemu_driver.c |  100 +++++++++++++++++++++++++++--------------------
 2 files changed, 58 insertions(+), 44 deletions(-)

diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index f9a970f..a6e68f8 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -75,7 +75,7 @@ enum qemud_cmd_flags {
 
 /* Main driver state */
 struct qemud_driver {
-    virMutex lock;
+    virRWLock lock;
 
     int privileged;
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 1eb7797..19187ac 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -78,13 +78,19 @@
 
 static int qemudShutdown(void);
 
+/* Obtains a full write lock */
 static void qemuDriverLock(struct qemud_driver *driver)
 {
-    virMutexLock(&driver->lock);
+    virRWLockWrite(&driver->lock);
+}
+/* Obtains a read-only lock */
+static void qemuDriverLockRO(struct qemud_driver *driver)
+{
+    virRWLockRead(&driver->lock);
 }
 static void qemuDriverUnlock(struct qemud_driver *driver)
 {
-    virMutexUnlock(&driver->lock);
+    virRWLockUnlock(&driver->lock);
 }
 
 static void qemuDomainEventFlush(int timer, void *opaque);
@@ -431,8 +437,8 @@ qemudStartup(int privileged) {
     if (VIR_ALLOC(qemu_driver) < 0)
         return -1;
 
-    if (virMutexInit(&qemu_driver->lock) < 0) {
-        VIR_ERROR("%s", _("cannot initialize mutex"));
+    if (virRWLockInit(&qemu_driver->lock) < 0) {
+        VIR_ERROR("%s", _("cannot initialize rwlock"));
         VIR_FREE(qemu_driver);
         return -1;
     }
@@ -743,7 +749,7 @@ qemudShutdown(void) {
     virCgroupFree(&qemu_driver->cgroup);
 
     qemuDriverUnlock(qemu_driver);
-    virMutexDestroy(&qemu_driver->lock);
+    virRWLockDestroy(&qemu_driver->lock);
     VIR_FREE(qemu_driver);
 
     return 0;
@@ -2262,7 +2268,7 @@ qemudDispatchVMEvent(int watch, int fd, int events, void *opaque) {
      * the VM must be running. Nowhere is allowed to remove
      * a domain while it is running, so it is safe to not
      * lock the driver here... */
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     virDomainObjLock(vm);
     qemuDriverUnlock(driver);
 
@@ -2520,7 +2526,7 @@ static virDomainPtr qemudDomainLookupByID(virConnectPtr conn,
     virDomainObjPtr vm;
     virDomainPtr dom = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm  = virDomainFindByID(&driver->domains, id);
     qemuDriverUnlock(driver);
 
@@ -2545,7 +2551,7 @@ static virDomainPtr qemudDomainLookupByUUID(virConnectPtr conn,
     virDomainObjPtr vm;
     virDomainPtr dom = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, uuid);
     qemuDriverUnlock(driver);
 
@@ -2572,7 +2578,7 @@ static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
     virDomainObjPtr vm;
     virDomainPtr dom = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByName(&driver->domains, name);
     qemuDriverUnlock(driver);
 
@@ -2626,7 +2632,7 @@ static int qemudListDomains(virConnectPtr conn, int *ids, int nids) {
     struct qemud_driver *driver = conn->privateData;
     int n;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     n = virDomainObjListGetActiveIDs(&driver->domains, ids, nids);
     qemuDriverUnlock(driver);
 
@@ -2637,7 +2643,7 @@ static int qemudNumDomains(virConnectPtr conn) {
     struct qemud_driver *driver = conn->privateData;
     int n;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     n = virDomainObjListNumOfDomains(&driver->domains, 1);
     qemuDriverUnlock(driver);
 
@@ -2731,7 +2737,7 @@ static int qemudDomainSuspend(virDomainPtr dom) {
     int ret = -1;
     virDomainEventPtr event = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
 
     if (!vm) {
@@ -2761,7 +2767,9 @@ static int qemudDomainSuspend(virDomainPtr dom) {
 cleanup:
     if (vm)
         virDomainObjUnlock(vm);
+    qemuDriverUnlock(driver);
 
+    qemuDriverLock(driver);
     if (event)
         qemuDomainEventQueue(driver, event);
     qemuDriverUnlock(driver);
@@ -2775,7 +2783,7 @@ static int qemudDomainResume(virDomainPtr dom) {
     int ret = -1;
     virDomainEventPtr event = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
 
     if (!vm) {
@@ -2809,6 +2817,8 @@ static int qemudDomainResume(virDomainPtr dom) {
 cleanup:
     if (vm)
         virDomainObjUnlock(vm);
+    qemuDriverUnlock(driver);
+    qemuDriverLock(driver);
     if (event)
         qemuDomainEventQueue(driver, event);
     qemuDriverUnlock(driver);
@@ -2821,7 +2831,7 @@ static int qemudDomainShutdown(virDomainPtr dom) {
     virDomainObjPtr vm;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -2898,7 +2908,7 @@ static char *qemudDomainGetOSType(virDomainPtr dom) {
     virDomainObjPtr vm;
     char *type = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
     if (!vm) {
@@ -2924,7 +2934,7 @@ static unsigned long qemudDomainGetMaxMemory(virDomainPtr dom) {
     virDomainObjPtr vm;
     unsigned long ret = 0;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -2949,7 +2959,7 @@ static int qemudDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) {
     virDomainObjPtr vm;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -2982,7 +2992,7 @@ static int qemudDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
     virDomainObjPtr vm;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
     if (!vm) {
@@ -3029,7 +3039,7 @@ static int qemudDomainGetInfo(virDomainPtr dom,
     int err;
     unsigned long balloon;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
     if (!vm) {
@@ -3257,7 +3267,7 @@ static int qemudDomainCoreDump(virDomainPtr dom,
         NULL,
     };
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -3314,7 +3324,7 @@ static int qemudDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
     int ret = -1;
     const char *type;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -3375,7 +3385,7 @@ qemudDomainPinVcpu(virDomainPtr dom,
     virNodeInfo nodeinfo;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -3444,7 +3454,7 @@ qemudDomainGetVcpus(virDomainPtr dom,
     int i, v, maxcpu;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -3533,7 +3543,7 @@ static int qemudDomainGetMaxVcpus(virDomainPtr dom) {
     const char *type;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -3567,7 +3577,7 @@ static int qemudDomainGetSecurityLabel(virDomainPtr dom, virSecurityLabelPtr sec
     const char *type;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
 
     memset(seclabel, 0, sizeof(*seclabel));
@@ -3627,7 +3637,7 @@ static int qemudNodeGetSecurityModel(virConnectPtr conn,
     char *p;
     int ret = 0;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     if (!driver->securityDriver) {
         memset(secmodel, 0, sizeof (*secmodel));
         goto cleanup;
@@ -3843,7 +3853,7 @@ static char *qemudDomainDumpXML(virDomainPtr dom,
     unsigned long balloon;
     int err;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -3891,7 +3901,7 @@ static char *qemuDomainXMLFromNative(virConnectPtr conn,
         goto cleanup;
     }
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     def = qemuParseCommandLineString(conn, driver->caps, config);
     qemuDriverUnlock(driver);
     if (!def)
@@ -3921,7 +3931,7 @@ static char *qemuDomainXMLToNative(virConnectPtr conn,
     char *ret = NULL;
     int i;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
 
     if (STRNEQ(format, QEMU_CONFIG_FORMAT_ARGV)) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_INVALID_ARG,
@@ -4038,7 +4048,7 @@ static int qemudListDefinedDomains(virConnectPtr conn,
     struct qemud_driver *driver = conn->privateData;
     int n;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     n = virDomainObjListGetInactiveNames(&driver->domains, names, nnames);
     qemuDriverUnlock(driver);
     return n;
@@ -4048,7 +4058,7 @@ static int qemudNumDefinedDomains(virConnectPtr conn) {
     struct qemud_driver *driver = conn->privateData;
     int n;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     n = virDomainObjListNumOfDomains(&driver->domains, 0);
     qemuDriverUnlock(driver);
 
@@ -4062,7 +4072,7 @@ static int qemudDomainStart(virDomainPtr dom) {
     int ret = -1;
     virDomainEventPtr event = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
 
     if (!vm) {
@@ -4082,6 +4092,8 @@ static int qemudDomainStart(virDomainPtr dom) {
 cleanup:
     if (vm)
         virDomainObjUnlock(vm);
+    qemuDriverUnlock(driver);
+    qemuDriverLock(driver);
     if (event)
         qemuDomainEventQueue(driver, event);
     qemuDriverUnlock(driver);
@@ -5151,7 +5163,7 @@ static int qemudDomainGetAutostart(virDomainPtr dom,
     virDomainObjPtr vm;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -5179,7 +5191,7 @@ static int qemudDomainSetAutostart(virDomainPtr dom,
     char *configFile = NULL, *autostartLink = NULL;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
 
     if (!vm) {
@@ -5249,7 +5261,7 @@ static char *qemuGetSchedulerType(virDomainPtr dom,
     struct qemud_driver *driver = dom->conn->privateData;
     char *ret = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                          __FUNCTION__);
@@ -5278,7 +5290,7 @@ static int qemuSetSchedulerParameters(virDomainPtr dom,
     virDomainObjPtr vm = NULL;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                          __FUNCTION__);
@@ -5343,7 +5355,7 @@ static int qemuGetSchedulerParameters(virDomainPtr dom,
     int ret = -1;
     int rc;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                          __FUNCTION__);
@@ -5410,7 +5422,7 @@ qemudDomainBlockStats (virDomainPtr dom,
     virDomainObjPtr vm;
     virDomainDiskDefPtr disk = NULL;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
     if (!vm) {
@@ -5471,7 +5483,7 @@ qemudDomainInterfaceStats (virDomainPtr dom,
     int i;
     int ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -5531,7 +5543,7 @@ qemudDomainBlockPeek (virDomainPtr dom,
     virDomainObjPtr vm;
     int fd = -1, ret = -1, i;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -5604,7 +5616,7 @@ qemudDomainMemoryPeek (virDomainPtr dom,
     char *tmp = NULL;
     int fd = -1, ret = -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
@@ -6759,7 +6771,7 @@ qemudDomainMigratePerform (virDomainPtr dom,
     int ret = -1;
     int paused = 0;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     if (!vm) {
         char uuidstr[VIR_UUID_STRING_BUFLEN];
@@ -6831,6 +6843,8 @@ cleanup:
 
     if (vm)
         virDomainObjUnlock(vm);
+    qemuDriverUnlock(driver);
+    qemuDriverLock(driver);
     if (event)
         qemuDomainEventQueue(driver, event);
     qemuDriverUnlock(driver);
@@ -7038,7 +7052,7 @@ qemudNodeDeviceReset (virNodeDevicePtr dev)
     if (!pci)
         return -1;
 
-    qemuDriverLock(driver);
+    qemuDriverLockRO(driver);
 
     if (pciResetDevice(dev->conn, pci, driver->activePciHostdevs) < 0)
         goto out;
-- 
1.6.2.5

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]