The switch contains considerable amount of changes:
virQEMUCapsRememberCached() is removed because this is now handled
by virFileCacheSave().
virQEMUCapsInitCached() is removed because this is now handled by
virFileCacheLoad().
virQEMUCapsNewForBinary() is split into two functions,
virQEMUCapsNewData() which creates new data if there is nothing
cached and virQEMUCapsLoadFile() which loads the cached data.
This is now handled by virFileCacheNewData().
virQEMUCapsCacheValidate() is removed because this is now handled by
virFileCacheValidate().
virQEMUCapsCacheFree() is removed because it's no longer required.
Add virCapsPtr into virQEMUCapsCachePriv because for each call of
virFileCacheLookup*() we need to use current virCapsPtr.
Signed-off-by: Pavel Hrdina <phrdina(a)redhat.com>
---
src/qemu/qemu_capabilities.c | 322 ++++++++++++-------------------------------
src/qemu/qemu_capabilities.h | 17 +--
src/qemu/qemu_capspriv.h | 8 +-
src/qemu/qemu_conf.h | 3 +-
src/qemu/qemu_driver.c | 2 +-
tests/testutilsqemu.c | 9 +-
tests/testutilsqemu.h | 3 +-
7 files changed, 102 insertions(+), 262 deletions(-)
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index e190cfa8b1..c3e09616de 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -29,6 +29,7 @@
#include "virlog.h"
#include "virerror.h"
#include "virfile.h"
+#include "virfilecache.h"
#include "virpidfile.h"
#include "virprocess.h"
#include "cpu/cpu.h"
@@ -953,7 +954,7 @@ virQEMUCapsFindBinaryForArch(virArch hostarch,
static int
virQEMUCapsInitGuest(virCapsPtr caps,
- virQEMUCapsCachePtr cache,
+ virFileCachePtr cache,
virArch hostarch,
virArch guestarch)
{
@@ -1170,7 +1171,8 @@ virQEMUCapsProbeHostCPUForEmulator(virCapsPtr caps,
}
-virCapsPtr virQEMUCapsInit(virQEMUCapsCachePtr cache)
+virCapsPtr
+virQEMUCapsInit(virFileCachePtr cache)
{
virCapsPtr caps;
size_t i;
@@ -2163,7 +2165,7 @@ virQEMUCapsExtractDeviceStr(const char *qemu,
int virQEMUCapsGetDefaultVersion(virCapsPtr caps,
- virQEMUCapsCachePtr capsCache,
+ virFileCachePtr capsCache,
unsigned int *version)
{
virQEMUCapsPtr qemucaps;
@@ -3766,8 +3768,10 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps,
static void
-virQEMUCapsCachePrivFree(virQEMUCapsCachePrivPtr priv)
+virQEMUCapsCachePrivFree(void *privData)
{
+ virQEMUCapsCachePrivPtr priv = privData;
+
VIR_FREE(priv->libDir);
VIR_FREE(priv);
}
@@ -4196,8 +4200,11 @@ virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps)
static int
-virQEMUCapsSaveCache(virQEMUCapsPtr qemuCaps, const char *filename)
+virQEMUCapsSaveFile(void *data,
+ const char *filename,
+ void *privData ATTRIBUTE_UNUSED)
{
+ virQEMUCapsPtr qemuCaps = data;
char *xml = NULL;
int ret = -1;
@@ -4221,48 +4228,13 @@ virQEMUCapsSaveCache(virQEMUCapsPtr qemuCaps, const char
*filename)
return ret;
}
-static int
-virQEMUCapsRememberCached(virQEMUCapsPtr qemuCaps, const char *cacheDir)
-{
- char *capsdir = NULL;
- char *capsfile = NULL;
- int ret = -1;
- char *binaryhash = NULL;
-
- if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0)
- goto cleanup;
-
- if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256,
- qemuCaps->binary,
- &binaryhash) < 0)
- goto cleanup;
-
- if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0)
- goto cleanup;
-
- if (virFileMakePath(capsdir) < 0) {
- virReportSystemError(errno,
- _("Unable to create directory '%s'"),
- capsdir);
- goto cleanup;
- }
-
- if (virQEMUCapsSaveCache(qemuCaps, capsfile) < 0)
- goto cleanup;
-
- ret = 0;
- cleanup:
- VIR_FREE(binaryhash);
- VIR_FREE(capsfile);
- VIR_FREE(capsdir);
- return ret;
-}
-
static bool
-virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps,
- virQEMUCapsCachePrivPtr priv)
+virQEMUCapsIsValid(void *data,
+ void *privData)
{
+ virQEMUCapsPtr qemuCaps = data;
+ virQEMUCapsCachePrivPtr priv = privData;
bool kvmUsable;
struct stat sb;
@@ -4321,87 +4293,6 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps,
}
-static int
-virQEMUCapsInitCached(virCapsPtr caps,
- virQEMUCapsPtr *qemuCaps,
- const char *binary,
- const char *cacheDir,
- virQEMUCapsCachePrivPtr priv)
-{
- char *capsdir = NULL;
- char *capsfile = NULL;
- int ret = -1;
- char *binaryhash = NULL;
- struct stat sb;
- virQEMUCapsPtr qemuCapsNew = NULL;
-
- if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0)
- goto cleanup;
-
- if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, binary, &binaryhash) < 0)
- goto cleanup;
-
- if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0)
- goto cleanup;
-
- if (virFileMakePath(capsdir) < 0) {
- virReportSystemError(errno,
- _("Unable to create directory '%s'"),
- capsdir);
- goto cleanup;
- }
-
- if (stat(capsfile, &sb) < 0) {
- if (errno == ENOENT) {
- VIR_DEBUG("No cached capabilities '%s' for '%s'",
- capsfile, binary);
- ret = 0;
- goto cleanup;
- }
- virReportSystemError(errno,
- _("Unable to access cache '%s' for
'%s'"),
- capsfile, binary);
- goto cleanup;
- }
-
- if (!(qemuCapsNew = virQEMUCapsNew()))
- goto cleanup;
-
- if (VIR_STRDUP(qemuCapsNew->binary, binary) < 0)
- goto discard;
-
- if (virQEMUCapsLoadCache(caps, qemuCapsNew, capsfile) < 0) {
- VIR_WARN("Failed to load cached caps from '%s' for '%s':
%s",
- capsfile, qemuCapsNew->binary, virGetLastErrorMessage());
- virResetLastError();
- goto discard;
- }
-
- if (!virQEMUCapsIsValid(qemuCapsNew, priv))
- goto discard;
-
- VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=%d",
- capsfile, qemuCapsNew->binary,
- (long long)qemuCapsNew->ctime, qemuCapsNew->usedQMP);
-
- ret = 1;
- *qemuCaps = qemuCapsNew;
- cleanup:
- VIR_FREE(binaryhash);
- VIR_FREE(capsfile);
- VIR_FREE(capsdir);
- return ret;
-
- discard:
- VIR_DEBUG("Dropping cached capabilities '%s' for '%s'",
- capsfile, qemuCapsNew->binary);
- ignore_value(unlink(capsfile));
- virObjectUnref(qemuCapsNew);
- ret = 0;
- goto cleanup;
-}
-
-
#define QEMU_SYSTEM_PREFIX "qemu-system-"
static int
@@ -5280,36 +5171,45 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps,
goto cleanup;
}
-static virQEMUCapsPtr
-virQEMUCapsNewForBinary(virCapsPtr caps,
- const char *binary,
- const char *cacheDir,
- virQEMUCapsCachePrivPtr priv)
+static void *
+virQEMUCapsNewData(const char *binary,
+ void *privData)
{
- int rv;
- virQEMUCapsPtr qemuCaps = NULL;
+ virQEMUCapsCachePrivPtr priv = privData;
- if ((rv = virQEMUCapsInitCached(caps, &qemuCaps, binary, cacheDir, priv)) <
0)
- goto error;
+ return virQEMUCapsNewForBinaryInternal(priv->caps,
+ binary,
+ priv->libDir,
+ priv->runUid,
+ priv->runGid,
+ false);
+}
+
+
+static void *
+virQEMUCapsLoadFile(const char *filename,
+ const char *binary,
+ void *privData)
+{
+ virQEMUCapsPtr qemuCaps = virQEMUCapsNew();
+ virQEMUCapsCachePrivPtr priv = privData;
- if (rv == 0) {
- if (!(qemuCaps = virQEMUCapsNewForBinaryInternal(caps, binary,
- priv->libDir,
- priv->runUid,
- priv->runGid,
- false))) {
- goto error;
- }
+ if (!qemuCaps)
+ return NULL;
- if (virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0)
- goto error;
- }
+ if (VIR_STRDUP(qemuCaps->binary, binary) < 0)
+ goto error;
+
+ if (virQEMUCapsLoadCache(priv->caps, qemuCaps, filename) < 0)
+ goto error;
+ cleanup:
return qemuCaps;
error:
virObjectUnref(qemuCaps);
- return NULL;
+ qemuCaps = NULL;
+ goto cleanup;
}
@@ -5351,90 +5251,63 @@ virQEMUCapsFilterByMachineType(virQEMUCapsPtr qemuCaps,
}
-virQEMUCapsCachePtr
+virFileCacheHandlers qemuCapsCacheHandlers = {
+ .isValid = virQEMUCapsIsValid,
+ .newData = virQEMUCapsNewData,
+ .loadFile = virQEMUCapsLoadFile,
+ .saveFile = virQEMUCapsSaveFile,
+ .privFree = virQEMUCapsCachePrivFree,
+};
+
+
+virFileCachePtr
virQEMUCapsCacheNew(const char *libDir,
const char *cacheDir,
uid_t runUid,
gid_t runGid)
{
- virQEMUCapsCachePtr cache;
+ char *capsCacheDir = NULL;
+ virFileCachePtr cache = NULL;
+ virQEMUCapsCachePrivPtr priv = NULL;
- if (VIR_ALLOC(cache) < 0)
- 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, virObjectFreeHashData)))
+ if (virAsprintf(&capsCacheDir, "%s/capabilities", cacheDir) < 0)
goto error;
- if (VIR_STRDUP(cache->cacheDir, cacheDir) < 0)
+
+ if (!(cache = virFileCacheNew(capsCacheDir, qemuCapsCacheHandlers)))
goto error;
- if (VIR_ALLOC(cache->priv) < 0)
+ if (VIR_ALLOC(priv) < 0)
goto error;
+ virFileCacheSetPriv(cache, priv);
- if (VIR_STRDUP(cache->priv->libDir, libDir) < 0)
+ if (VIR_STRDUP(priv->libDir, libDir) < 0)
goto error;
- cache->priv->runUid = runUid;
- cache->priv->runGid = runGid;
+ priv->runUid = runUid;
+ priv->runGid = runGid;
+ cleanup:
+ VIR_FREE(capsCacheDir);
return cache;
error:
- virQEMUCapsCacheFree(cache);
- return NULL;
-}
-
-
-static void ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
-virQEMUCapsCacheValidate(virQEMUCapsCachePtr cache,
- const char *binary,
- virCapsPtr caps,
- virQEMUCapsPtr *qemuCaps)
-{
- if (*qemuCaps &&
- !virQEMUCapsIsValid(*qemuCaps, cache->priv)) {
- VIR_DEBUG("Cached capabilities %p no longer valid for %s",
- *qemuCaps, binary);
- virHashRemoveEntry(cache->binaries, binary);
- *qemuCaps = NULL;
- }
-
- if (!*qemuCaps) {
- VIR_DEBUG("Creating capabilities for %s", binary);
- *qemuCaps = virQEMUCapsNewForBinary(caps, binary,
- cache->cacheDir,
- cache->priv);
- if (*qemuCaps) {
- VIR_DEBUG("Caching capabilities %p for %s", *qemuCaps, binary);
- if (virHashAddEntry(cache->binaries, binary, *qemuCaps) < 0) {
- virObjectUnref(*qemuCaps);
- *qemuCaps = NULL;
- }
- }
- }
+ virObjectUnref(cache);
+ cache = NULL;
+ goto cleanup;
}
virQEMUCapsPtr
virQEMUCapsCacheLookup(virCapsPtr caps,
- virQEMUCapsCachePtr cache,
+ virFileCachePtr cache,
const char *binary)
{
virQEMUCapsPtr ret = NULL;
+ virQEMUCapsCachePrivPtr priv = virFileCacheGetPriv(cache);
- virMutexLock(&cache->lock);
-
- ret = virHashLookup(cache->binaries, binary);
- virQEMUCapsCacheValidate(cache, binary, caps, &ret);
- virObjectRef(ret);
-
- virMutexUnlock(&cache->lock);
+ priv->caps = caps;
+ ret = virFileCacheLookup(cache, binary);
+ priv->caps = NULL;
VIR_DEBUG("Returning caps %p for %s", ret, binary);
return ret;
@@ -5443,7 +5316,7 @@ virQEMUCapsCacheLookup(virCapsPtr caps,
virQEMUCapsPtr
virQEMUCapsCacheLookupCopy(virCapsPtr caps,
- virQEMUCapsCachePtr cache,
+ virFileCachePtr cache,
const char *binary,
const char *machineType)
{
@@ -5478,64 +5351,39 @@ virQEMUCapsCompareArch(const void *payload,
virQEMUCapsPtr
virQEMUCapsCacheLookupByArch(virCapsPtr caps,
- virQEMUCapsCachePtr cache,
+ virFileCachePtr cache,
virArch arch)
{
virQEMUCapsPtr ret = NULL;
+ virQEMUCapsCachePrivPtr priv = virFileCacheGetPriv(cache);
virArch target;
struct virQEMUCapsSearchData data = { .arch = arch };
- virMutexLock(&cache->lock);
- ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data, NULL);
+ priv->caps = caps;
+ ret = virFileCacheLookupByFunc(cache, virQEMUCapsCompareArch, &data);
if (!ret) {
/* If the first attempt at finding capabilities has failed, try
* again using the QEMU target as lookup key instead */
target = virQEMUCapsFindTarget(virArchFromHost(), data.arch);
if (target != data.arch) {
data.arch = target;
- ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch,
- &data, NULL);
+ ret = virFileCacheLookupByFunc(cache, virQEMUCapsCompareArch, &data);
}
}
+ priv->caps = NULL;
- if (ret) {
- char *binary;
-
- if (VIR_STRDUP(binary, ret->binary) < 0) {
- ret = NULL;
- } else {
- virQEMUCapsCacheValidate(cache, binary, caps, &ret);
- VIR_FREE(binary);
- }
- } else {
+ if (!ret) {
virReportError(VIR_ERR_INVALID_ARG,
_("unable to find any emulator to serve '%s' "
"architecture"), virArchToString(arch));
}
- virObjectRef(ret);
- virMutexUnlock(&cache->lock);
-
VIR_DEBUG("Returning caps %p for arch %s", ret, virArchToString(arch));
return ret;
}
-void
-virQEMUCapsCacheFree(virQEMUCapsCachePtr cache)
-{
- if (!cache)
- return;
-
- virQEMUCapsCachePrivFree(cache->priv);
- VIR_FREE(cache->cacheDir);
- virHashFree(cache->binaries);
- virMutexDestroy(&cache->lock);
- VIR_FREE(cache);
-}
-
-
bool
virQEMUCapsSupportsChardev(const virDomainDef *def,
virQEMUCapsPtr qemuCaps,
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 7f5382df62..77b519d939 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -30,6 +30,7 @@
# include "qemu_monitor.h"
# include "domain_capabilities.h"
# include "virfirmware.h"
+# include "virfilecache.h"
/*
* Internal flags to keep track of qemu command line capabilities
@@ -423,9 +424,6 @@ typedef enum {
typedef struct _virQEMUCaps virQEMUCaps;
typedef virQEMUCaps *virQEMUCapsPtr;
-typedef struct _virQEMUCapsCache virQEMUCapsCache;
-typedef virQEMUCapsCache *virQEMUCapsCachePtr;
-
virQEMUCapsPtr virQEMUCapsNew(void);
void virQEMUCapsSet(virQEMUCapsPtr qemuCaps,
@@ -497,25 +495,24 @@ int virQEMUCapsGetMachineTypesCaps(virQEMUCapsPtr qemuCaps,
void virQEMUCapsFilterByMachineType(virQEMUCapsPtr qemuCaps,
const char *machineType);
-virQEMUCapsCachePtr virQEMUCapsCacheNew(const char *libDir,
+virFileCachePtr virQEMUCapsCacheNew(const char *libDir,
const char *cacheDir,
uid_t uid, gid_t gid);
virQEMUCapsPtr virQEMUCapsCacheLookup(virCapsPtr caps,
- virQEMUCapsCachePtr cache,
+ virFileCachePtr cache,
const char *binary);
virQEMUCapsPtr virQEMUCapsCacheLookupCopy(virCapsPtr caps,
- virQEMUCapsCachePtr cache,
+ virFileCachePtr cache,
const char *binary,
const char *machineType);
virQEMUCapsPtr virQEMUCapsCacheLookupByArch(virCapsPtr caps,
- virQEMUCapsCachePtr cache,
+ virFileCachePtr cache,
virArch arch);
-void virQEMUCapsCacheFree(virQEMUCapsCachePtr cache);
-virCapsPtr virQEMUCapsInit(virQEMUCapsCachePtr cache);
+virCapsPtr virQEMUCapsInit(virFileCachePtr cache);
int virQEMUCapsGetDefaultVersion(virCapsPtr caps,
- virQEMUCapsCachePtr capsCache,
+ virFileCachePtr capsCache,
unsigned int *version);
VIR_ENUM_DECL(virQEMUCaps);
diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h
index 61e7c34d12..77e41d753b 100644
--- a/src/qemu/qemu_capspriv.h
+++ b/src/qemu/qemu_capspriv.h
@@ -29,6 +29,7 @@
# define __QEMU_CAPSPRIV_H__
struct _virQEMUCapsCachePriv {
+ virCapsPtr caps;
char *libDir;
uid_t runUid;
gid_t runGid;
@@ -36,13 +37,6 @@ struct _virQEMUCapsCachePriv {
typedef struct _virQEMUCapsCachePriv virQEMUCapsCachePriv;
typedef virQEMUCapsCachePriv *virQEMUCapsCachePrivPtr;
-struct _virQEMUCapsCache {
- virMutex lock;
- virHashTablePtr binaries;
- char *cacheDir;
- virQEMUCapsCachePrivPtr priv;
-};
-
virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps);
virQEMUCapsPtr
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index 1407eef4d4..3013f24ed4 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -48,6 +48,7 @@
# include "virclosecallbacks.h"
# include "virhostdev.h"
# include "virfile.h"
+# include "virfilecache.h"
# include "virfirmware.h"
# ifdef CPU_SETSIZE /* Linux */
@@ -245,7 +246,7 @@ struct _virQEMUDriver {
virDomainXMLOptionPtr xmlopt;
/* Immutable pointer, self-locking APIs */
- virQEMUCapsCachePtr qemuCapsCache;
+ virFileCachePtr qemuCapsCache;
/* Immutable pointer, self-locking APIs */
virObjectEventStatePtr domainEventState;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index cdb727b554..fd705645c4 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1078,7 +1078,7 @@ qemuStateCleanup(void)
virObjectUnref(qemu_driver->hostdevMgr);
virHashFree(qemu_driver->sharedDevices);
virObjectUnref(qemu_driver->caps);
- virQEMUCapsCacheFree(qemu_driver->qemuCapsCache);
+ virObjectUnref(qemu_driver->qemuCapsCache);
virObjectUnref(qemu_driver->domains);
virObjectUnref(qemu_driver->remotePorts);
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index 88e11ba14a..710d12dc4c 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -11,6 +11,7 @@
# define __QEMU_CAPSPRIV_H_ALLOW__
# include "qemu/qemu_capspriv.h"
# include "virstring.h"
+# include "virfilecache.h"
# define VIR_FROM_THIS VIR_FROM_QEMU
@@ -587,14 +588,14 @@ void qemuTestDriverFree(virQEMUDriver *driver)
virFileDeleteTree(driver->config->stateDir);
virFileDeleteTree(driver->config->configDir);
}
- virQEMUCapsCacheFree(driver->qemuCapsCache);
+ virObjectUnref(driver->qemuCapsCache);
virObjectUnref(driver->xmlopt);
virObjectUnref(driver->caps);
virObjectUnref(driver->config);
virObjectUnref(driver->securityManager);
}
-int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache,
+int qemuTestCapsCacheInsert(virFileCachePtr cache,
virQEMUCapsPtr caps)
{
size_t i;
@@ -609,9 +610,7 @@ int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache,
for (i = 0; i < ARRAY_CARDINALITY(QEMUBinList); i++) {
virObjectRef(tmpCaps);
- if (virHashUpdateEntry(cache->binaries,
- QEMUBinList[i],
- tmpCaps) < 0) {
+ if (virFileCacheInsertData(cache, QEMUBinList[i], tmpCaps) < 0) {
virObjectUnref(tmpCaps);
return -1;
}
diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h
index 05e5651ae8..f29c6e5d62 100644
--- a/tests/testutilsqemu.h
+++ b/tests/testutilsqemu.h
@@ -1,6 +1,7 @@
#ifdef WITH_QEMU
# include "capabilities.h"
+# include "virfilecache.h"
# include "domain_conf.h"
# include "qemu/qemu_capabilities.h"
# include "qemu/qemu_conf.h"
@@ -30,7 +31,7 @@ void qemuTestSetHostCPU(virCapsPtr caps,
int qemuTestDriverInit(virQEMUDriver *driver);
void qemuTestDriverFree(virQEMUDriver *driver);
-int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache,
+int qemuTestCapsCacheInsert(virFileCachePtr cache,
virQEMUCapsPtr caps);
int testQemuCapsSetGIC(virQEMUCapsPtr qemuCaps,
--
2.13.2