[libvirt] [PATCHv3 15/43] snapshot: cache qemu-img location

Eric Blake eblake at redhat.com
Wed Aug 24 15:22:32 UTC 2011


As more clients start to want to know this information, doing
a PATH stat walk and malloc for every client adds up.

* src/qemu/qemu_conf.h (qemud_driver): Add member.
* src/qemu/qemu_driver.c (qemudShutdown): Cleanup.
(qemuFindQemuImgBinary): Add an argument, and cache result.
(qemuDomainSnapshotDiscard, qemuDomainSnapshotCreateInactive)
(qemuDomainSnapshotRevertInactive, qemuDomainSnapshotCreateXML)
(qemuDomainRevertToSnapshot): Update callers.
---
 src/qemu/qemu_conf.h   |    1 +
 src/qemu/qemu_driver.c |   42 +++++++++++++++++++++---------------------
 2 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index 0a60d32..5469a63 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -82,6 +82,7 @@ struct qemud_driver {
     char *cacheDir;
     char *saveDir;
     char *snapshotDir;
+    char *qemuImgBinary;
     unsigned int vncAutoUnixSocket : 1;
     unsigned int vncTLS : 1;
     unsigned int vncTLSx509verify : 1;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 90b1d91..5a18309 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -774,6 +774,7 @@ qemudShutdown(void) {
     VIR_FREE(qemu_driver->cacheDir);
     VIR_FREE(qemu_driver->saveDir);
     VIR_FREE(qemu_driver->snapshotDir);
+    VIR_FREE(qemu_driver->qemuImgBinary);
     VIR_FREE(qemu_driver->autoDumpPath);
     VIR_FREE(qemu_driver->vncTLSx509certdir);
     VIR_FREE(qemu_driver->vncListen);
@@ -1581,19 +1582,19 @@ cleanup:


 /* Locate an appropriate 'qemu-img' binary.  */
-static char *
-qemuFindQemuImgBinary(void)
+static const char *
+qemuFindQemuImgBinary(struct qemud_driver *driver)
 {
-    char *ret;
-
-    ret = virFindFileInPath("kvm-img");
-    if (ret == NULL)
-        ret = virFindFileInPath("qemu-img");
-    if (ret == NULL)
-        qemuReportError(VIR_ERR_INTERNAL_ERROR,
-                        "%s", _("unable to find kvm-img or qemu-img"));
+    if (!driver->qemuImgBinary) {
+        driver->qemuImgBinary = virFindFileInPath("kvm-img");
+        if (!driver->qemuImgBinary)
+            driver->qemuImgBinary = virFindFileInPath("qemu-img");
+        if (!driver->qemuImgBinary)
+            qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                            "%s", _("unable to find kvm-img or qemu-img"));
+    }

-    return ret;
+    return driver->qemuImgBinary;
 }

 static int
@@ -1667,7 +1668,7 @@ qemuDomainSnapshotDiscard(struct qemud_driver *driver,

     if (!metadata_only) {
         if (!virDomainObjIsActive(vm)) {
-            qemuimgarg[0] = qemuFindQemuImgBinary();
+            qemuimgarg[0] = qemuFindQemuImgBinary(driver);
             if (qemuimgarg[0] == NULL)
                 /* qemuFindQemuImgBinary set the error */
                 goto cleanup;
@@ -1739,7 +1740,6 @@ qemuDomainSnapshotDiscard(struct qemud_driver *driver,

 cleanup:
     VIR_FREE(snapFile);
-    VIR_FREE(qemuimgarg[0]);

     return ret;
 }
@@ -8494,14 +8494,15 @@ static int qemuDomainSnapshotIsAllowed(virDomainObjPtr vm)

 /* The domain is expected to be locked and inactive. */
 static int
-qemuDomainSnapshotCreateInactive(virDomainObjPtr vm,
+qemuDomainSnapshotCreateInactive(struct qemud_driver *driver,
+                                 virDomainObjPtr vm,
                                  virDomainSnapshotObjPtr snap)
 {
     const char *qemuimgarg[] = { NULL, "snapshot", "-c", NULL, NULL, NULL };
     int ret = -1;
     int i;

-    qemuimgarg[0] = qemuFindQemuImgBinary();
+    qemuimgarg[0] = qemuFindQemuImgBinary(driver);
     if (qemuimgarg[0] == NULL) {
         /* qemuFindQemuImgBinary set the error */
         goto cleanup;
@@ -8534,7 +8535,6 @@ qemuDomainSnapshotCreateInactive(virDomainObjPtr vm,
     ret = 0;

 cleanup:
-    VIR_FREE(qemuimgarg[0]);
     return ret;
 }

@@ -8650,7 +8650,7 @@ static virDomainSnapshotPtr qemuDomainSnapshotCreateXML(virDomainPtr domain,

     /* actually do the snapshot */
     if (!virDomainObjIsActive(vm)) {
-        if (qemuDomainSnapshotCreateInactive(vm, snap) < 0)
+        if (qemuDomainSnapshotCreateInactive(driver, vm, snap) < 0)
             goto cleanup;
     } else {
         if (qemuDomainSnapshotCreateActive(domain->conn, driver,
@@ -8883,14 +8883,15 @@ cleanup:

 /* The domain is expected to be locked and inactive. */
 static int
-qemuDomainSnapshotRevertInactive(virDomainObjPtr vm,
+qemuDomainSnapshotRevertInactive(struct qemud_driver *driver,
+                                 virDomainObjPtr vm,
                                  virDomainSnapshotObjPtr snap)
 {
     const char *qemuimgarg[] = { NULL, "snapshot", "-a", NULL, NULL, NULL };
     int ret = -1;
     int i;

-    qemuimgarg[0] = qemuFindQemuImgBinary();
+    qemuimgarg[0] = qemuFindQemuImgBinary(driver);
     if (qemuimgarg[0] == NULL) {
         /* qemuFindQemuImgBinary set the error */
         goto cleanup;
@@ -8923,7 +8924,6 @@ qemuDomainSnapshotRevertInactive(virDomainObjPtr vm,
     ret = 0;

 cleanup:
-    VIR_FREE(qemuimgarg[0]);
     return ret;
 }

@@ -9063,7 +9063,7 @@ static int qemuDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
             }
         }

-        if (qemuDomainSnapshotRevertInactive(vm, snap) < 0)
+        if (qemuDomainSnapshotRevertInactive(driver, vm, snap) < 0)
             goto endjob;
     }

-- 
1.7.4.4




More information about the libvir-list mailing list