[PATCH 01/23] Add a qemu capabilities cache manager

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Introduce a qemuCapsCachePtr object to provide a global cache
of capabilities for QEMU binaries. The cache auto-populates
on first request for capabilities about a binary, and will
auto-refresh if the binary has changed since a previous cache
was populated

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 src/qemu/qemu_capabilities.c | 99 ++++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_capabilities.h |  9 ++++
 2 files changed, 108 insertions(+)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 43645bf..bb5c670 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -200,6 +200,11 @@ struct _qemuCaps {
     char **machineAliases;
 };
 
+struct _qemuCapsCache {
+    virMutex lock;
+    virHashTablePtr binaries;
+};
+
 
 static virClassPtr qemuCapsClass;
 static void qemuCapsDispose(void *obj);
@@ -1921,6 +1926,10 @@ qemuCapsPtr qemuCapsNewForBinary(const char *binary)
     tmp = strstr(binary, QEMU_SYSTEM_PREFIX);
     if (tmp) {
         tmp += strlen(QEMU_SYSTEM_PREFIX);
+
+        /* For historical compat we uses 'itanium' as arch name */
+        if (STREQ(tmp, "ia64"))
+            tmp = "itanium";
     } else {
         uname_normalize(&ut);
         tmp = ut.machine;
@@ -2029,3 +2038,93 @@ bool qemuCapsIsValid(qemuCapsPtr caps)
 
     return sb.st_mtime == caps->mtime;
 }
+
+
+static void
+qemuCapsHashDataFree(void *payload, const void *key ATTRIBUTE_UNUSED)
+{
+    virObjectUnref(payload);
+}
+
+qemuCapsCachePtr qemuCapsCacheNew(void)
+{
+    qemuCapsCachePtr cache;
+
+    if (VIR_ALLOC(cache) < 0) {
+        virReportOOMError();
+        return NULL;
+    }
+
+    if (virMutexInit(&cache->lock) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Unable to initialize mutex"));
+        VIR_FREE(cache);
+        return NULL;
+    }
+
+    if (!(cache->binaries = virHashCreate(10, qemuCapsHashDataFree)))
+        goto error;
+
+    return cache;
+
+error:
+    qemuCapsCacheFree(cache);
+    return NULL;
+}
+
+
+qemuCapsPtr qemuCapsCacheLookup(qemuCapsCachePtr cache, const char *binary)
+{
+    qemuCapsPtr ret = NULL;
+    virMutexLock(&cache->lock);
+    ret = virHashLookup(cache->binaries, binary);
+    if (ret &&
+        !qemuCapsIsValid(ret)) {
+        VIR_DEBUG("Cached capabilities %p no longer valid for %s",
+                  ret, binary);
+        virHashRemoveEntry(cache->binaries, binary);
+        ret = NULL;
+    }
+    if (!ret) {
+        VIR_DEBUG("Creating capabilities for %s",
+                  binary);
+        ret = qemuCapsNewForBinary(binary);
+        if (ret) {
+            VIR_DEBUG("Caching capabilities %p for %s",
+                      ret, binary);
+            if (virHashAddEntry(cache->binaries, binary, ret) < 0) {
+                virObjectUnref(ret);
+                ret = NULL;
+            }
+        }
+    }
+    VIR_DEBUG("Returning caps %p for %s", ret, binary);
+    virObjectRef(ret);
+    virMutexUnlock(&cache->lock);
+    return ret;
+}
+
+
+qemuCapsPtr qemuCapsCacheLookupCopy(qemuCapsCachePtr cache, const char *binary)
+{
+    qemuCapsPtr caps = qemuCapsCacheLookup(cache, binary);
+    qemuCapsPtr ret;
+
+    if (!caps)
+        return NULL;
+
+    ret = qemuCapsNewCopy(caps);
+    virObjectUnref(caps);
+    return ret;
+}
+
+
+void qemuCapsCacheFree(qemuCapsCachePtr cache)
+{
+    if (!cache)
+        return;
+
+    virHashFree(cache->binaries);
+    virMutexDestroy(&cache->lock);
+    VIR_FREE(cache);
+}
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index c1519ed..cd417e0 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -149,6 +149,9 @@ enum qemuCapsFlags {
 typedef struct _qemuCaps qemuCaps;
 typedef qemuCaps *qemuCapsPtr;
 
+typedef struct _qemuCapsCache qemuCapsCache;
+typedef qemuCapsCache *qemuCapsCachePtr;
+
 qemuCapsPtr qemuCapsNew(void);
 qemuCapsPtr qemuCapsNewCopy(qemuCapsPtr caps);
 qemuCapsPtr qemuCapsNewForBinary(const char *binary);
@@ -178,6 +181,12 @@ const char *qemuCapsGetCanonicalMachine(qemuCapsPtr caps,
 
 bool qemuCapsIsValid(qemuCapsPtr caps);
 
+
+qemuCapsCachePtr qemuCapsCacheNew(void);
+qemuCapsPtr qemuCapsCacheLookup(qemuCapsCachePtr cache, const char *binary);
+qemuCapsPtr qemuCapsCacheLookupCopy(qemuCapsCachePtr cache, const char *binary);
+void qemuCapsCacheFree(qemuCapsCachePtr cache);
+
 virCapsPtr qemuCapsInit(virCapsPtr old_caps);
 
 int qemuCapsProbeMachineTypes(const char *binary,
-- 
1.7.11.4

--
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]