render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
404507
From 5fb3ec71e0e67f1f1c0157c9c9038c5136820799 Mon Sep 17 00:00:00 2001
404507
Message-Id: <5fb3ec71e0e67f1f1c0157c9c9038c5136820799@dist-git>
404507
From: Martin Kletzander <mkletzan@redhat.com>
404507
Date: Wed, 31 Jan 2018 16:32:29 +0100
404507
Subject: [PATCH] resctrl: Add functions to work with resctrl allocations
404507
404507
With this commit we finally have a way to read and manipulate basic resctrl
404507
settings.  Locking is done only on exposed functions that read/write from/to
404507
resctrlfs.  Not in functions that are exposed in virresctrlpriv.h as those are
404507
only supposed to be used from tests.
404507
404507
More information about how resctrl works:
404507
404507
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/x86/intel_rdt_ui.txt
404507
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
(cherry picked from commit a64c761c275c61517360cf1f5af5461fc5b93138)
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
404507
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
404507
404507
Conflicts:
404507
404507
 - whitespace due to 3e7db8d3e8538bcd5deb1b111fb1233fc18831aa
404507
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
---
404507
 src/Makefile.am           |    2 +-
404507
 src/libvirt_private.syms  |   10 +
404507
 src/util/virresctrl.c     | 1174 ++++++++++++++++++++++++++++++++++++++++++++-
404507
 src/util/virresctrl.h     |   49 ++
404507
 src/util/virresctrlpriv.h |   27 ++
404507
 5 files changed, 1260 insertions(+), 2 deletions(-)
404507
 create mode 100644 src/util/virresctrlpriv.h
404507
404507
diff --git a/src/Makefile.am b/src/Makefile.am
404507
index b74856ba78..6b24a47afe 100644
404507
--- a/src/Makefile.am
404507
+++ b/src/Makefile.am
404507
@@ -167,7 +167,7 @@ UTIL_SOURCES =							\
404507
 		util/virprocess.c util/virprocess.h		\
404507
 		util/virqemu.c util/virqemu.h			\
404507
 		util/virrandom.h util/virrandom.c		\
404507
-		util/virresctrl.h util/virresctrl.c		\
404507
+		util/virresctrl.h util/virresctrl.c util/virresctrlpriv.h \
404507
 		util/virrotatingfile.h util/virrotatingfile.c   \
404507
 		util/virscsi.c util/virscsi.h			\
404507
 		util/virscsihost.c util/virscsihost.h		\
404507
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
404507
index 02bad6c0ce..ee75e09767 100644
404507
--- a/src/libvirt_private.syms
404507
+++ b/src/libvirt_private.syms
404507
@@ -2547,6 +2547,16 @@ virRandomInt;
404507
 # util/virresctrl.h
404507
 virCacheTypeFromString;
404507
 virCacheTypeToString;
404507
+virResctrlAllocAddPID;
404507
+virResctrlAllocCreate;
404507
+virResctrlAllocForeachSize;
404507
+virResctrlAllocFormat;
404507
+virResctrlAllocGetID;
404507
+virResctrlAllocGetUnused;
404507
+virResctrlAllocNew;
404507
+virResctrlAllocRemove;
404507
+virResctrlAllocSetID;
404507
+virResctrlAllocSetSize;
404507
 virResctrlGetInfo;
404507
 virResctrlInfoGetCache;
404507
 virResctrlInfoNew;
404507
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
404507
index d439e7792c..e252aefe31 100644
404507
--- a/src/util/virresctrl.c
404507
+++ b/src/util/virresctrl.c
404507
@@ -18,8 +18,12 @@
404507
 
404507
 #include <config.h>
404507
 
404507
-#include "virresctrl.h"
404507
+#include <sys/file.h>
404507
+#include <sys/types.h>
404507
+#include <sys/stat.h>
404507
+#include <fcntl.h>
404507
 
404507
+#include "virresctrlpriv.h"
404507
 #include "c-ctype.h"
404507
 #include "count-one-bits.h"
404507
 #include "viralloc.h"
404507
@@ -152,6 +156,193 @@ virResctrlInfoNew(void)
404507
 }
404507
 
404507
 
404507
+/* Alloc-related definitions and AllocClass-related functions */
404507
+
404507
+/*
404507
+ * virResctrlAlloc represents one allocation (in XML under cputune/cachetune and
404507
+ * consequently a directory under /sys/fs/resctrl).  Since it can have multiple
404507
+ * parts of multiple caches allocated it is represented as bunch of nested
404507
+ * sparse arrays (by sparse I mean array of pointers so that each might be NULL
404507
+ * in case there is no allocation for that particular one (level, cache, ...)).
404507
+ *
404507
+ * Since one allocation can be made for caches on different levels, the first
404507
+ * nested sparse array is of types virResctrlAllocPerLevel.  For example if you
404507
+ * have allocation for level 3 cache, there will be three NULL pointers and then
404507
+ * allocated pointer to virResctrlAllocPerLevel.  That way you can access it by
404507
+ * `alloc[level]` as O(1) is desired instead of crawling through normal arrays
404507
+ * or lists in three nested loops.  The code uses a lot of direct accesses.
404507
+ *
404507
+ * Each virResctrlAllocPerLevel can have allocations for different cache
404507
+ * allocation types.  You can allocate instruction cache (VIR_CACHE_TYPE_CODE),
404507
+ * data cache (VIR_CACHE_TYPE_DATA) or unified cache (VIR_CACHE_TYPE_BOTH).
404507
+ * Those allocations are kept in sparse array of virResctrlAllocPerType pointers.
404507
+ *
404507
+ * For each virResctrlAllocPerType users can request some size of the cache to
404507
+ * be allocated.  That's what the sparse array `sizes` is for.  Non-NULL
404507
+ * pointers represent requested size allocations.  The array is indexed by host
404507
+ * cache id (gotten from `/sys/devices/system/cpu/cpuX/cache/indexY/id`).  Users
404507
+ * can see this information e.g. in the output of `virsh capabilities` (for that
404507
+ * information there's the other struct, namely `virResctrlInfo`).
404507
+ *
404507
+ * When allocation is being created we need to find unused part of the cache for
404507
+ * all of them.  While doing that we store the bitmask in a sparse array of
404507
+ * virBitmaps named `masks` indexed the same way as `sizes`.  The upper bounds
404507
+ * of the sparse arrays are stored in nmasks or nsizes, respectively.
404507
+ */
404507
+typedef struct _virResctrlAllocPerType virResctrlAllocPerType;
404507
+typedef virResctrlAllocPerType *virResctrlAllocPerTypePtr;
404507
+struct _virResctrlAllocPerType {
404507
+    /* There could be bool saying whether this is set or not, but since everything
404507
+     * in virResctrlAlloc (and most of libvirt) goes with pointer arrays we would
404507
+     * have to have one more level of allocation anyway, so this stays faithful to
404507
+     * the concept */
404507
+    unsigned long long **sizes;
404507
+    size_t nsizes;
404507
+
404507
+    /* Mask for each cache */
404507
+    virBitmapPtr *masks;
404507
+    size_t nmasks;
404507
+};
404507
+
404507
+typedef struct _virResctrlAllocPerLevel virResctrlAllocPerLevel;
404507
+typedef virResctrlAllocPerLevel *virResctrlAllocPerLevelPtr;
404507
+struct _virResctrlAllocPerLevel {
404507
+    virResctrlAllocPerTypePtr *types; /* Indexed with enum virCacheType */
404507
+    /* There is no `ntypes` member variable as it is always allocated for
404507
+     * VIR_CACHE_TYPE_LAST number of items */
404507
+};
404507
+
404507
+struct _virResctrlAlloc {
404507
+    virObject parent;
404507
+
404507
+    virResctrlAllocPerLevelPtr *levels;
404507
+    size_t nlevels;
404507
+
404507
+    /* The identifier (any unique string for now) */
404507
+    char *id;
404507
+    /* libvirt-generated path in /sys/fs/resctrl for this particular
404507
+     * allocation */
404507
+    char *path;
404507
+};
404507
+
404507
+static virClassPtr virResctrlAllocClass;
404507
+
404507
+static void
404507
+virResctrlAllocDispose(void *obj)
404507
+{
404507
+    size_t i = 0;
404507
+    size_t j = 0;
404507
+    size_t k = 0;
404507
+
404507
+    virResctrlAllocPtr resctrl = obj;
404507
+
404507
+    for (i = 0; i < resctrl->nlevels; i++) {
404507
+        virResctrlAllocPerLevelPtr level = resctrl->levels[i];
404507
+
404507
+        if (!level)
404507
+            continue;
404507
+
404507
+        for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
404507
+            virResctrlAllocPerTypePtr type = level->types[j];
404507
+
404507
+            if (!type)
404507
+                continue;
404507
+
404507
+            for (k = 0; k < type->nsizes; k++)
404507
+                VIR_FREE(type->sizes[k]);
404507
+
404507
+            for (k = 0; k < type->nmasks; k++)
404507
+                virBitmapFree(type->masks[k]);
404507
+
404507
+            VIR_FREE(type->sizes);
404507
+            VIR_FREE(type->masks);
404507
+            VIR_FREE(type);
404507
+        }
404507
+        VIR_FREE(level->types);
404507
+        VIR_FREE(level);
404507
+    }
404507
+
404507
+    VIR_FREE(resctrl->id);
404507
+    VIR_FREE(resctrl->path);
404507
+    VIR_FREE(resctrl->levels);
404507
+}
404507
+
404507
+
404507
+static int
404507
+virResctrlAllocOnceInit(void)
404507
+{
404507
+    if (!(virResctrlAllocClass = virClassNew(virClassForObject(),
404507
+                                             "virResctrlAlloc",
404507
+                                             sizeof(virResctrlAlloc),
404507
+                                             virResctrlAllocDispose)))
404507
+        return -1;
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
+VIR_ONCE_GLOBAL_INIT(virResctrlAlloc)
404507
+
404507
+
404507
+virResctrlAllocPtr
404507
+virResctrlAllocNew(void)
404507
+{
404507
+    if (virResctrlAllocInitialize() < 0)
404507
+        return NULL;
404507
+
404507
+    return virObjectNew(virResctrlAllocClass);
404507
+}
404507
+
404507
+
404507
+/* Common functions */
404507
+static int
404507
+virResctrlLockInternal(int op)
404507
+{
404507
+    int fd = open(SYSFS_RESCTRL_PATH, O_DIRECTORY | O_CLOEXEC);
404507
+
404507
+    if (fd < 0) {
404507
+        virReportSystemError(errno, "%s", _("Cannot open resctrlfs"));
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (flock(fd, op) < 0) {
404507
+        virReportSystemError(errno, "%s", _("Cannot lock resctrlfs"));
404507
+        VIR_FORCE_CLOSE(fd);
404507
+        return -1;
404507
+    }
404507
+
404507
+    return fd;
404507
+}
404507
+
404507
+
404507
+static inline int
404507
+virResctrlLockWrite(void)
404507
+{
404507
+    return virResctrlLockInternal(LOCK_EX);
404507
+}
404507
+
404507
+
404507
+static int
404507
+virResctrlUnlock(int fd)
404507
+{
404507
+    if (fd == -1)
404507
+        return 0;
404507
+
404507
+    /* The lock gets unlocked by closing the fd, which we need to do anyway in
404507
+     * order to clean up properly */
404507
+    if (VIR_CLOSE(fd) < 0) {
404507
+        virReportSystemError(errno, "%s", _("Cannot close resctrlfs"));
404507
+
404507
+        /* Trying to save the already broken */
404507
+        if (flock(fd, LOCK_UN) < 0)
404507
+            virReportSystemError(errno, "%s", _("Cannot unlock resctrlfs"));
404507
+        return -1;
404507
+    }
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
 /* Info-related functions */
404507
 static bool
404507
 virResctrlInfoIsEmpty(virResctrlInfoPtr resctrl)
404507
@@ -367,3 +558,984 @@ virResctrlInfoGetCache(virResctrlInfoPtr resctrl,
404507
     VIR_FREE(*controls);
404507
     goto cleanup;
404507
 }
404507
+
404507
+
404507
+/* Alloc-related functions */
404507
+bool
404507
+virResctrlAllocIsEmpty(virResctrlAllocPtr resctrl)
404507
+{
404507
+    size_t i = 0;
404507
+    size_t j = 0;
404507
+    size_t k = 0;
404507
+
404507
+    if (!resctrl)
404507
+        return true;
404507
+
404507
+    for (i = 0; i < resctrl->nlevels; i++) {
404507
+        virResctrlAllocPerLevelPtr a_level = resctrl->levels[i];
404507
+
404507
+        if (!a_level)
404507
+            continue;
404507
+
404507
+        for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
404507
+            virResctrlAllocPerTypePtr a_type = a_level->types[j];
404507
+
404507
+            if (!a_type)
404507
+                continue;
404507
+
404507
+            for (k = 0; k < a_type->nsizes; k++) {
404507
+                if (a_type->sizes[k])
404507
+                    return false;
404507
+            }
404507
+
404507
+            for (k = 0; k < a_type->nmasks; k++) {
404507
+                if (a_type->masks[k])
404507
+                    return false;
404507
+            }
404507
+        }
404507
+    }
404507
+
404507
+    return true;
404507
+}
404507
+
404507
+
404507
+static virResctrlAllocPerTypePtr
404507
+virResctrlAllocGetType(virResctrlAllocPtr resctrl,
404507
+                       unsigned int level,
404507
+                       virCacheType type)
404507
+{
404507
+    virResctrlAllocPerLevelPtr a_level = NULL;
404507
+
404507
+    if (resctrl->nlevels <= level &&
404507
+        VIR_EXPAND_N(resctrl->levels, resctrl->nlevels, level - resctrl->nlevels + 1) < 0)
404507
+        return NULL;
404507
+
404507
+    if (!resctrl->levels[level]) {
404507
+        virResctrlAllocPerTypePtr *types = NULL;
404507
+
404507
+        if (VIR_ALLOC_N(types, VIR_CACHE_TYPE_LAST) < 0)
404507
+            return NULL;
404507
+
404507
+        if (VIR_ALLOC(resctrl->levels[level]) < 0) {
404507
+            VIR_FREE(types);
404507
+            return NULL;
404507
+        }
404507
+        resctrl->levels[level]->types = types;
404507
+    }
404507
+
404507
+    a_level = resctrl->levels[level];
404507
+
404507
+    if (!a_level->types[type] && VIR_ALLOC(a_level->types[type]) < 0)
404507
+        return NULL;
404507
+
404507
+    return a_level->types[type];
404507
+}
404507
+
404507
+
404507
+static int
404507
+virResctrlAllocUpdateMask(virResctrlAllocPtr resctrl,
404507
+                          unsigned int level,
404507
+                          virCacheType type,
404507
+                          unsigned int cache,
404507
+                          virBitmapPtr mask)
404507
+{
404507
+    virResctrlAllocPerTypePtr a_type = virResctrlAllocGetType(resctrl, level, type);
404507
+
404507
+    if (!a_type)
404507
+        return -1;
404507
+
404507
+    if (a_type->nmasks <= cache &&
404507
+        VIR_EXPAND_N(a_type->masks, a_type->nmasks,
404507
+                     cache - a_type->nmasks + 1) < 0)
404507
+        return -1;
404507
+
404507
+    if (!a_type->masks[cache]) {
404507
+        a_type->masks[cache] = virBitmapNew(virBitmapSize(mask));
404507
+
404507
+        if (!a_type->masks[cache])
404507
+            return -1;
404507
+    }
404507
+
404507
+    return virBitmapCopy(a_type->masks[cache], mask);
404507
+}
404507
+
404507
+
404507
+static int
404507
+virResctrlAllocUpdateSize(virResctrlAllocPtr resctrl,
404507
+                          unsigned int level,
404507
+                          virCacheType type,
404507
+                          unsigned int cache,
404507
+                          unsigned long long size)
404507
+{
404507
+    virResctrlAllocPerTypePtr a_type = virResctrlAllocGetType(resctrl, level, type);
404507
+
404507
+    if (!a_type)
404507
+        return -1;
404507
+
404507
+    if (a_type->nsizes <= cache &&
404507
+        VIR_EXPAND_N(a_type->sizes, a_type->nsizes,
404507
+                     cache - a_type->nsizes + 1) < 0)
404507
+        return -1;
404507
+
404507
+    if (!a_type->sizes[cache] && VIR_ALLOC(a_type->sizes[cache]) < 0)
404507
+        return -1;
404507
+
404507
+    *(a_type->sizes[cache]) = size;
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
+/*
404507
+ * Check if there is an allocation for this level/type/cache already.  Called
404507
+ * before updating the structure.  VIR_CACHE_TYPE_BOTH collides with any type,
404507
+ * the other types collide with itself.  This code basically checks if either:
404507
+ * `alloc[level]->types[type]->sizes[cache]`
404507
+ * or
404507
+ * `alloc[level]->types[VIR_CACHE_TYPE_BOTH]->sizes[cache]`
404507
+ * is non-NULL.  All the fuzz around it is checking for NULL pointers along
404507
+ * the way.
404507
+ */
404507
+static bool
404507
+virResctrlAllocCheckCollision(virResctrlAllocPtr alloc,
404507
+                              unsigned int level,
404507
+                              virCacheType type,
404507
+                              unsigned int cache)
404507
+{
404507
+    virResctrlAllocPerLevelPtr a_level = NULL;
404507
+    virResctrlAllocPerTypePtr a_type = NULL;
404507
+
404507
+    if (!alloc)
404507
+        return false;
404507
+
404507
+    if (alloc->nlevels <= level)
404507
+        return false;
404507
+
404507
+    a_level = alloc->levels[level];
404507
+
404507
+    if (!a_level)
404507
+        return false;
404507
+
404507
+    a_type = a_level->types[VIR_CACHE_TYPE_BOTH];
404507
+
404507
+    /* If there is an allocation for type 'both', there can be no other
404507
+     * allocation for the same cache */
404507
+    if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
404507
+        return true;
404507
+
404507
+    if (type == VIR_CACHE_TYPE_BOTH) {
404507
+        a_type = a_level->types[VIR_CACHE_TYPE_CODE];
404507
+
404507
+        if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
404507
+            return true;
404507
+
404507
+        a_type = a_level->types[VIR_CACHE_TYPE_DATA];
404507
+
404507
+        if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
404507
+            return true;
404507
+    } else {
404507
+        a_type = a_level->types[type];
404507
+
404507
+        if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
404507
+            return true;
404507
+    }
404507
+
404507
+    return false;
404507
+}
404507
+
404507
+
404507
+int
404507
+virResctrlAllocSetSize(virResctrlAllocPtr resctrl,
404507
+                       unsigned int level,
404507
+                       virCacheType type,
404507
+                       unsigned int cache,
404507
+                       unsigned long long size)
404507
+{
404507
+    if (virResctrlAllocCheckCollision(resctrl, level, type, cache)) {
404507
+        virReportError(VIR_ERR_XML_ERROR,
404507
+                       _("Colliding cache allocations for cache "
404507
+                         "level '%u' id '%u', type '%s'"),
404507
+                       level, cache, virCacheTypeToString(type));
404507
+        return -1;
404507
+    }
404507
+
404507
+    return virResctrlAllocUpdateSize(resctrl, level, type, cache, size);
404507
+}
404507
+
404507
+
404507
+int
404507
+virResctrlAllocForeachSize(virResctrlAllocPtr resctrl,
404507
+                           virResctrlAllocForeachSizeCallback cb,
404507
+                           void *opaque)
404507
+{
404507
+    int ret = 0;
404507
+    unsigned int level = 0;
404507
+    unsigned int type = 0;
404507
+    unsigned int cache = 0;
404507
+
404507
+    if (!resctrl)
404507
+        return 0;
404507
+
404507
+    for (level = 0; level < resctrl->nlevels; level++) {
404507
+        virResctrlAllocPerLevelPtr a_level = resctrl->levels[level];
404507
+
404507
+        if (!a_level)
404507
+            continue;
404507
+
404507
+        for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) {
404507
+            virResctrlAllocPerTypePtr a_type = a_level->types[type];
404507
+
404507
+            if (!a_type)
404507
+                continue;
404507
+
404507
+            for (cache = 0; cache < a_type->nsizes; cache++) {
404507
+                unsigned long long *size = a_type->sizes[cache];
404507
+
404507
+                if (!size)
404507
+                    continue;
404507
+
404507
+                ret = cb(level, type, cache, *size, opaque);
404507
+                if (ret < 0)
404507
+                    return ret;
404507
+            }
404507
+        }
404507
+    }
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
+int
404507
+virResctrlAllocSetID(virResctrlAllocPtr alloc,
404507
+                     const char *id)
404507
+{
404507
+    if (!id) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
404507
+                       _("Resctrl allocation 'id' cannot be NULL"));
404507
+        return -1;
404507
+    }
404507
+
404507
+    return VIR_STRDUP(alloc->id, id);
404507
+}
404507
+
404507
+
404507
+const char *
404507
+virResctrlAllocGetID(virResctrlAllocPtr alloc)
404507
+{
404507
+    return alloc->id;
404507
+}
404507
+
404507
+
404507
+char *
404507
+virResctrlAllocFormat(virResctrlAllocPtr resctrl)
404507
+{
404507
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
404507
+    unsigned int level = 0;
404507
+    unsigned int type = 0;
404507
+    unsigned int cache = 0;
404507
+
404507
+    if (!resctrl)
404507
+        return NULL;
404507
+
404507
+    for (level = 0; level < resctrl->nlevels; level++) {
404507
+        virResctrlAllocPerLevelPtr a_level = resctrl->levels[level];
404507
+
404507
+        if (!a_level)
404507
+            continue;
404507
+
404507
+        for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) {
404507
+            virResctrlAllocPerTypePtr a_type = a_level->types[type];
404507
+
404507
+            if (!a_type)
404507
+                continue;
404507
+
404507
+            virBufferAsprintf(&buf, "L%u%s:", level, virResctrlTypeToString(type));
404507
+
404507
+            for (cache = 0; cache < a_type->nmasks; cache++) {
404507
+                virBitmapPtr mask = a_type->masks[cache];
404507
+                char *mask_str = NULL;
404507
+
404507
+                if (!mask)
404507
+                    continue;
404507
+
404507
+                mask_str = virBitmapToString(mask, false, true);
404507
+                if (!mask_str) {
404507
+                    virBufferFreeAndReset(&buf;;
404507
+                    return NULL;
404507
+                }
404507
+
404507
+                virBufferAsprintf(&buf, "%u=%s;", cache, mask_str);
404507
+                VIR_FREE(mask_str);
404507
+            }
404507
+
404507
+            virBufferTrim(&buf, ";", 1);
404507
+            virBufferAddChar(&buf, '\n');
404507
+        }
404507
+    }
404507
+
404507
+    virBufferCheckError(&buf;;
404507
+    return virBufferContentAndReset(&buf;;
404507
+}
404507
+
404507
+
404507
+static int
404507
+virResctrlAllocParseProcessCache(virResctrlInfoPtr resctrl,
404507
+                                 virResctrlAllocPtr alloc,
404507
+                                 unsigned int level,
404507
+                                 virCacheType type,
404507
+                                 char *cache)
404507
+{
404507
+    char *tmp = strchr(cache, '=');
404507
+    unsigned int cache_id = 0;
404507
+    virBitmapPtr mask = NULL;
404507
+    int ret = -1;
404507
+
404507
+    if (!tmp)
404507
+        return 0;
404507
+
404507
+    *tmp = '\0';
404507
+    tmp++;
404507
+
404507
+    if (virStrToLong_uip(cache, NULL, 10, &cache_id) < 0) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR,
404507
+                       _("Invalid cache id '%s'"), cache);
404507
+        return -1;
404507
+    }
404507
+
404507
+    mask = virBitmapNewString(tmp);
404507
+    if (!mask)
404507
+        return -1;
404507
+
404507
+    virBitmapShrink(mask, resctrl->levels[level]->types[type]->bits);
404507
+
404507
+    if (virResctrlAllocUpdateMask(alloc, level, type, cache_id, mask) < 0)
404507
+        goto cleanup;
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virBitmapFree(mask);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+static int
404507
+virResctrlAllocParseProcessLine(virResctrlInfoPtr resctrl,
404507
+                                virResctrlAllocPtr alloc,
404507
+                                char *line)
404507
+{
404507
+    char **caches = NULL;
404507
+    char *tmp = NULL;
404507
+    unsigned int level = 0;
404507
+    int type = -1;
404507
+    size_t ncaches = 0;
404507
+    size_t i = 0;
404507
+    int ret = -1;
404507
+
404507
+    /* For no reason there can be spaces */
404507
+    virSkipSpaces((const char **) &line);
404507
+
404507
+    /* Skip lines that don't concern caches, e.g. MB: etc. */
404507
+    if (line[0] != 'L')
404507
+        return 0;
404507
+
404507
+    /* And lines that we can't parse too */
404507
+    tmp = strchr(line, ':');
404507
+    if (!tmp)
404507
+        return 0;
404507
+
404507
+    *tmp = '\0';
404507
+    tmp++;
404507
+
404507
+    if (virStrToLong_uip(line + 1, &line, 10, &level) < 0) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR,
404507
+                       _("Cannot parse resctrl schema level '%s'"),
404507
+                       line + 1);
404507
+        return -1;
404507
+    }
404507
+
404507
+    type = virResctrlTypeFromString(line);
404507
+    if (type < 0) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR,
404507
+                       _("Cannot parse resctrl schema level '%s'"),
404507
+                       line + 1);
404507
+        return -1;
404507
+    }
404507
+
404507
+    caches = virStringSplitCount(tmp, ";", 0, &ncaches);
404507
+    if (!caches)
404507
+        return 0;
404507
+
404507
+    for (i = 0; i < ncaches; i++) {
404507
+        if (virResctrlAllocParseProcessCache(resctrl, alloc, level, type, caches[i]) < 0)
404507
+            goto cleanup;
404507
+    }
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virStringListFree(caches);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+static int
404507
+virResctrlAllocParse(virResctrlInfoPtr resctrl,
404507
+                     virResctrlAllocPtr alloc,
404507
+                     const char *schemata)
404507
+{
404507
+    char **lines = NULL;
404507
+    size_t nlines = 0;
404507
+    size_t i = 0;
404507
+    int ret = -1;
404507
+
404507
+    lines = virStringSplitCount(schemata, "\n", 0, &nlines);
404507
+    for (i = 0; i < nlines; i++) {
404507
+        if (virResctrlAllocParseProcessLine(resctrl, alloc, lines[i]) < 0)
404507
+            goto cleanup;
404507
+    }
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virStringListFree(lines);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+static void
404507
+virResctrlAllocSubtractPerType(virResctrlAllocPerTypePtr dst,
404507
+                               virResctrlAllocPerTypePtr src)
404507
+{
404507
+    size_t i = 0;
404507
+
404507
+    if (!dst || !src)
404507
+        return;
404507
+
404507
+    for (i = 0; i < dst->nmasks && i < src->nmasks; i++) {
404507
+        if (dst->masks[i] && src->masks[i])
404507
+            virBitmapSubtract(dst->masks[i], src->masks[i]);
404507
+    }
404507
+}
404507
+
404507
+
404507
+static void
404507
+virResctrlAllocSubtract(virResctrlAllocPtr dst,
404507
+                        virResctrlAllocPtr src)
404507
+{
404507
+    size_t i = 0;
404507
+    size_t j = 0;
404507
+
404507
+    if (!src)
404507
+        return;
404507
+
404507
+    for (i = 0; i < dst->nlevels && i < src->nlevels; i++) {
404507
+        if (dst->levels[i] && src->levels[i]) {
404507
+            for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
404507
+                virResctrlAllocSubtractPerType(dst->levels[i]->types[j],
404507
+                                               src->levels[i]->types[j]);
404507
+            }
404507
+        }
404507
+    }
404507
+}
404507
+
404507
+
404507
+static virResctrlAllocPtr
404507
+virResctrlAllocNewFromInfo(virResctrlInfoPtr info)
404507
+{
404507
+    size_t i = 0;
404507
+    size_t j = 0;
404507
+    size_t k = 0;
404507
+    virResctrlAllocPtr ret = virResctrlAllocNew();
404507
+    virBitmapPtr mask = NULL;
404507
+
404507
+    if (!ret)
404507
+        return NULL;
404507
+
404507
+    for (i = 0; i < info->nlevels; i++) {
404507
+        virResctrlInfoPerLevelPtr i_level = info->levels[i];
404507
+
404507
+        if (!i_level)
404507
+            continue;
404507
+
404507
+        for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
404507
+            virResctrlInfoPerTypePtr i_type = i_level->types[j];
404507
+
404507
+            if (!i_type)
404507
+                continue;
404507
+
404507
+            virBitmapFree(mask);
404507
+            mask = virBitmapNew(i_type->bits);
404507
+            if (!mask)
404507
+                goto error;
404507
+            virBitmapSetAll(mask);
404507
+
404507
+            for (k = 0; k <= i_type->max_cache_id; k++) {
404507
+                if (virResctrlAllocUpdateMask(ret, i, j, k, mask) < 0)
404507
+                    goto error;
404507
+            }
404507
+        }
404507
+    }
404507
+
404507
+ cleanup:
404507
+    virBitmapFree(mask);
404507
+    return ret;
404507
+ error:
404507
+    virObjectUnref(ret);
404507
+    ret = NULL;
404507
+    goto cleanup;
404507
+}
404507
+
404507
+/*
404507
+ * This function creates an allocation that represents all unused parts of all
404507
+ * caches in the system.  It uses virResctrlInfo for creating a new full
404507
+ * allocation with all bits set (using virResctrlAllocNewFromInfo()) and then
404507
+ * scans for all allocations under /sys/fs/resctrl and subtracts each one of
404507
+ * them from it.  That way it can then return an allocation with only bit set
404507
+ * being those that are not mentioned in any other allocation.  It is used for
404507
+ * two things, a) calculating the masks when creating allocations and b) from
404507
+ * tests.
404507
+ */
404507
+virResctrlAllocPtr
404507
+virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
404507
+{
404507
+    virResctrlAllocPtr ret = NULL;
404507
+    virResctrlAllocPtr alloc = NULL;
404507
+    struct dirent *ent = NULL;
404507
+    DIR *dirp = NULL;
404507
+    char *schemata = NULL;
404507
+    int rv = -1;
404507
+
404507
+    if (virResctrlInfoIsEmpty(resctrl)) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                       _("Resource control is not supported on this host"));
404507
+        return NULL;
404507
+    }
404507
+
404507
+    ret = virResctrlAllocNewFromInfo(resctrl);
404507
+    if (!ret)
404507
+        return NULL;
404507
+
404507
+    if (virFileReadValueString(&schemata,
404507
+                               SYSFS_RESCTRL_PATH
404507
+                               "/schemata") < 0) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
404507
+                       _("Could not read schemata file for the default group"));
404507
+        goto error;
404507
+    }
404507
+
404507
+    alloc = virResctrlAllocNew();
404507
+    if (!alloc)
404507
+        goto error;
404507
+
404507
+    if (virResctrlAllocParse(resctrl, alloc, schemata) < 0)
404507
+        goto error;
404507
+
404507
+    virResctrlAllocSubtract(ret, alloc);
404507
+
404507
+    if (virDirOpen(&dirp, SYSFS_RESCTRL_PATH) < 0)
404507
+        goto error;
404507
+
404507
+    while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH)) > 0) {
404507
+        if (ent->d_type != DT_DIR)
404507
+            continue;
404507
+
404507
+        if (STREQ(ent->d_name, "info"))
404507
+            continue;
404507
+
404507
+        VIR_FREE(schemata);
404507
+        rv = virFileReadValueString(&schemata,
404507
+                                    SYSFS_RESCTRL_PATH
404507
+                                    "/%s/schemata",
404507
+                                    ent->d_name);
404507
+        if (rv == -2)
404507
+            continue;
404507
+
404507
+        if (rv < 0) {
404507
+            virReportError(VIR_ERR_INTERNAL_ERROR,
404507
+                           _("Could not read schemata file for group %s"),
404507
+                           ent->d_name);
404507
+            goto error;
404507
+        }
404507
+
404507
+        virObjectUnref(alloc);
404507
+        alloc = virResctrlAllocNew();
404507
+        if (!alloc)
404507
+            goto error;
404507
+
404507
+        if (virResctrlAllocParse(resctrl, alloc, schemata) < 0)
404507
+            goto error;
404507
+
404507
+        virResctrlAllocSubtract(ret, alloc);
404507
+    }
404507
+    if (rv < 0)
404507
+        goto error;
404507
+
404507
+ cleanup:
404507
+    virObjectUnref(alloc);
404507
+    VIR_DIR_CLOSE(dirp);
404507
+    VIR_FREE(schemata);
404507
+    return ret;
404507
+
404507
+ error:
404507
+    virObjectUnref(ret);
404507
+    ret = NULL;
404507
+    goto cleanup;
404507
+}
404507
+
404507
+
404507
+
404507
+static int
404507
+virResctrlAllocSetMask(virResctrlAllocPerTypePtr a_type,
404507
+                       unsigned int cache,
404507
+                       virBitmapPtr mask)
404507
+{
404507
+    if (a_type->nmasks <= cache &&
404507
+        VIR_EXPAND_N(a_type->masks, a_type->nmasks,
404507
+                     cache - a_type->nmasks + 1) < 0)
404507
+        return -1;
404507
+
404507
+    a_type->masks[cache] = mask;
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
+/*
404507
+ * Given the information about requested allocation type `a_type`, the host
404507
+ * cache for a particular type `i_type` and unused bits in the system `f_type`
404507
+ * this function tries to find the smallest free space in which the allocation
404507
+ * for cache id `cache` would fit.  We're looking for the smallest place in
404507
+ * order to minimize fragmentation and maximize the possibility of succeeding.
404507
+ */
404507
+static int
404507
+virResctrlAllocFindUnused(virResctrlAllocPerTypePtr a_type,
404507
+                          virResctrlInfoPerTypePtr i_type,
404507
+                          virResctrlAllocPerTypePtr f_type,
404507
+                          unsigned int level,
404507
+                          unsigned int type,
404507
+                          unsigned int cache)
404507
+{
404507
+    unsigned long long *size = a_type->sizes[cache];
404507
+    virBitmapPtr a_mask = NULL;
404507
+    virBitmapPtr f_mask = NULL;
404507
+    unsigned long long need_bits;
404507
+    size_t i = 0;
404507
+    ssize_t pos = -1;
404507
+    ssize_t last_bits = 0;
404507
+    ssize_t last_pos = -1;
404507
+
404507
+    /* If there is no reservation requested we need to set all bits.  That's due
404507
+     * to weird interface of the resctrl sysfs.  It's also the reason why we
404507
+     * cannot reserve the whole cache in one allocation. */
404507
+    if (!size) {
404507
+        a_mask = virBitmapNew(i_type->bits);
404507
+        if (!a_mask)
404507
+            return -1;
404507
+
404507
+        virBitmapSetAll(a_mask);
404507
+
404507
+        if (virResctrlAllocSetMask(a_type, cache, a_mask) < 0) {
404507
+            virBitmapFree(a_mask);
404507
+            return -1;
404507
+        }
404507
+
404507
+        return 0;
404507
+    }
404507
+
404507
+    if (cache >= f_type->nmasks) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("Cache with id %u does not exists for level %d"),
404507
+                       cache, level);
404507
+        return -1;
404507
+    }
404507
+
404507
+    f_mask = f_type->masks[cache];
404507
+    if (!f_mask) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("Cache level %d id %u does not support tuning for "
404507
+                         "scope type '%s'"),
404507
+                       level, cache, virCacheTypeToString(type));
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (*size == i_type->size) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("Cache allocation for the whole cache is not "
404507
+                         "possible, specify size smaller than %llu"),
404507
+                       i_type->size);
404507
+        return -1;
404507
+    }
404507
+
404507
+    need_bits = *size / i_type->control.granularity;
404507
+
404507
+    if (*size % i_type->control.granularity) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("Cache allocation of size %llu is not "
404507
+                         "divisible by granularity %llu"),
404507
+                       *size, i_type->control.granularity);
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (need_bits < i_type->min_cbm_bits) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("Cache allocation of size %llu is smaller "
404507
+                         "than the minimum allowed allocation %llu"),
404507
+                       *size,
404507
+                       i_type->control.granularity * i_type->min_cbm_bits);
404507
+        return -1;
404507
+    }
404507
+
404507
+    while ((pos = virBitmapNextSetBit(f_mask, pos)) >= 0) {
404507
+        ssize_t pos_clear = virBitmapNextClearBit(f_mask, pos);
404507
+        ssize_t bits;
404507
+
404507
+        if (pos_clear < 0)
404507
+            pos_clear = virBitmapSize(f_mask);
404507
+
404507
+        bits = pos_clear - pos;
404507
+
404507
+        /* Not enough bits, move on and skip all of them */
404507
+        if (bits < need_bits) {
404507
+            pos = pos_clear;
404507
+            continue;
404507
+        }
404507
+
404507
+        /* This fits perfectly */
404507
+        if (bits == need_bits) {
404507
+            last_pos = pos;
404507
+            break;
404507
+        }
404507
+
404507
+        /* Remember the smaller region if we already found on before */
404507
+        if (last_pos < 0 || (last_bits && bits < last_bits)) {
404507
+            last_bits = bits;
404507
+            last_pos = pos;
404507
+        }
404507
+
404507
+        pos = pos_clear;
404507
+    }
404507
+
404507
+    if (last_pos < 0) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("Not enough room for allocation of "
404507
+                         "%llu bytes for level %u cache %u "
404507
+                         "scope type '%s'"),
404507
+                       *size, level, cache,
404507
+                       virCacheTypeToString(type));
404507
+        return -1;
404507
+    }
404507
+
404507
+    a_mask = virBitmapNew(i_type->bits);
404507
+    if (!a_mask)
404507
+        return -1;
404507
+
404507
+    for (i = last_pos; i < last_pos + need_bits; i++) {
404507
+        ignore_value(virBitmapSetBit(a_mask, i));
404507
+        ignore_value(virBitmapClearBit(f_mask, i));
404507
+    }
404507
+
404507
+    if (virResctrlAllocSetMask(a_type, cache, a_mask) < 0) {
404507
+        virBitmapFree(a_mask);
404507
+        return -1;
404507
+    }
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
+/*
404507
+ * This function is called when creating an allocation in the system.  What it
404507
+ * does is that it gets all the unused bits using virResctrlAllocGetUnused() and
404507
+ * then tries to find a proper space for every requested allocation effectively
404507
+ * transforming `sizes` into `masks`.
404507
+ */
404507
+static int
404507
+virResctrlAllocMasksAssign(virResctrlInfoPtr resctrl,
404507
+                           virResctrlAllocPtr alloc)
404507
+{
404507
+    int ret = -1;
404507
+    unsigned int level = 0;
404507
+    virResctrlAllocPtr alloc_free = NULL;
404507
+
404507
+    alloc_free = virResctrlAllocGetUnused(resctrl);
404507
+    if (!alloc_free)
404507
+        return -1;
404507
+
404507
+    for (level = 0; level < alloc->nlevels; level++) {
404507
+        virResctrlAllocPerLevelPtr a_level = alloc->levels[level];
404507
+        virResctrlAllocPerLevelPtr f_level = NULL;
404507
+        unsigned int type = 0;
404507
+
404507
+        if (!a_level)
404507
+            continue;
404507
+
404507
+        if (level < alloc_free->nlevels)
404507
+            f_level = alloc_free->levels[level];
404507
+
404507
+        if (!f_level) {
404507
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                           _("Cache level %d does not support tuning"),
404507
+                           level);
404507
+            goto cleanup;
404507
+        }
404507
+
404507
+        for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) {
404507
+            virResctrlAllocPerTypePtr a_type = a_level->types[type];
404507
+            virResctrlAllocPerTypePtr f_type = f_level->types[type];
404507
+            unsigned int cache = 0;
404507
+
404507
+            if (!a_type)
404507
+                continue;
404507
+
404507
+            if (!f_type) {
404507
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                               _("Cache level %d does not support tuning for "
404507
+                                 "scope type '%s'"),
404507
+                               level, virCacheTypeToString(type));
404507
+                goto cleanup;
404507
+            }
404507
+
404507
+            for (cache = 0; cache < a_type->nsizes; cache++) {
404507
+                virResctrlInfoPerLevelPtr i_level = resctrl->levels[level];
404507
+                virResctrlInfoPerTypePtr i_type = i_level->types[type];
404507
+
404507
+                if (virResctrlAllocFindUnused(a_type, i_type, f_type, level, type, cache) < 0)
404507
+                    goto cleanup;
404507
+            }
404507
+        }
404507
+    }
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virObjectUnref(alloc_free);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+/* This checks if the directory for the alloc exists.  If not it tries to create
404507
+ * it and apply appropriate alloc settings. */
404507
+int
404507
+virResctrlAllocCreate(virResctrlInfoPtr resctrl,
404507
+                      virResctrlAllocPtr alloc,
404507
+                      const char *machinename)
404507
+{
404507
+    char *schemata_path = NULL;
404507
+    char *alloc_str = NULL;
404507
+    int ret = -1;
404507
+    int lockfd = -1;
404507
+
404507
+    if (!alloc)
404507
+        return 0;
404507
+
404507
+    if (!resctrl) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                       _("Resource control is not supported on this host"));
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (!alloc->id) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
404507
+                       _("Resctrl Allocation ID must be set before creation"));
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (!alloc->path &&
404507
+        virAsprintf(&alloc->path, "%s/%s-%s",
404507
+                    SYSFS_RESCTRL_PATH, machinename, alloc->id) < 0)
404507
+        return -1;
404507
+
404507
+    if (virFileExists(alloc->path)) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR,
404507
+                       _("Path '%s' for resctrl allocation exists"),
404507
+                       alloc->path);
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    lockfd = virResctrlLockWrite();
404507
+    if (lockfd < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (virResctrlAllocMasksAssign(resctrl, alloc) < 0)
404507
+        goto cleanup;
404507
+
404507
+    alloc_str = virResctrlAllocFormat(alloc);
404507
+    if (!alloc_str)
404507
+        goto cleanup;
404507
+
404507
+    if (virAsprintf(&schemata_path, "%s/schemata", alloc->path) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (virFileMakePath(alloc->path) < 0) {
404507
+        virReportSystemError(errno,
404507
+                             _("Cannot create resctrl directory '%s'"),
404507
+                             alloc->path);
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    VIR_DEBUG("Writing resctrl schemata '%s' into '%s'", alloc_str, schemata_path);
404507
+    if (virFileWriteStr(schemata_path, alloc_str, 0) < 0) {
404507
+        rmdir(alloc->path);
404507
+        virReportSystemError(errno,
404507
+                             _("Cannot write into schemata file '%s'"),
404507
+                             schemata_path);
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virResctrlUnlock(lockfd);
404507
+    VIR_FREE(alloc_str);
404507
+    VIR_FREE(schemata_path);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+int
404507
+virResctrlAllocAddPID(virResctrlAllocPtr alloc,
404507
+                      pid_t pid)
404507
+{
404507
+    char *tasks = NULL;
404507
+    char *pidstr = NULL;
404507
+    int ret = 0;
404507
+
404507
+    if (!alloc->path) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
404507
+                       _("Cannot add pid to non-existing resctrl allocation"));
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (virAsprintf(&tasks, "%s/tasks", alloc->path) < 0)
404507
+        return -1;
404507
+
404507
+    if (virAsprintf(&pidstr, "%lld", (long long int) pid) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (virFileWriteStr(tasks, pidstr, 0) < 0) {
404507
+        virReportSystemError(errno,
404507
+                             _("Cannot write pid in tasks file '%s'"),
404507
+                             tasks);
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    VIR_FREE(tasks);
404507
+    VIR_FREE(pidstr);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+int
404507
+virResctrlAllocRemove(virResctrlAllocPtr alloc)
404507
+{
404507
+    int ret = 0;
404507
+
404507
+    if (!alloc->path)
404507
+        return 0;
404507
+
404507
+    VIR_DEBUG("Removing resctrl allocation %s", alloc->path);
404507
+    if (rmdir(alloc->path) != 0 && errno != ENOENT) {
404507
+        ret = -errno;
404507
+        VIR_ERROR(_("Unable to remove %s (%d)"), alloc->path, errno);
404507
+    }
404507
+
404507
+    return ret;
404507
+}
404507
diff --git a/src/util/virresctrl.h b/src/util/virresctrl.h
404507
index a81ff687de..63a954feaa 100644
404507
--- a/src/util/virresctrl.h
404507
+++ b/src/util/virresctrl.h
404507
@@ -65,4 +65,53 @@ virResctrlInfoGetCache(virResctrlInfoPtr resctrl,
404507
                        size_t *ncontrols,
404507
                        virResctrlInfoPerCachePtr **controls);
404507
 
404507
+/* Alloc-related things */
404507
+typedef struct _virResctrlAlloc virResctrlAlloc;
404507
+typedef virResctrlAlloc *virResctrlAllocPtr;
404507
+
404507
+typedef int virResctrlAllocForeachSizeCallback(unsigned int level,
404507
+                                               virCacheType type,
404507
+                                               unsigned int cache,
404507
+                                               unsigned long long size,
404507
+                                               void *opaque);
404507
+
404507
+virResctrlAllocPtr
404507
+virResctrlAllocNew(void);
404507
+
404507
+bool
404507
+virResctrlAllocIsEmpty(virResctrlAllocPtr resctrl);
404507
+
404507
+int
404507
+virResctrlAllocSetSize(virResctrlAllocPtr resctrl,
404507
+                       unsigned int level,
404507
+                       virCacheType type,
404507
+                       unsigned int cache,
404507
+                       unsigned long long size);
404507
+
404507
+int
404507
+virResctrlAllocForeachSize(virResctrlAllocPtr resctrl,
404507
+                           virResctrlAllocForeachSizeCallback cb,
404507
+                           void *opaque);
404507
+
404507
+int
404507
+virResctrlAllocSetID(virResctrlAllocPtr alloc,
404507
+                     const char *id);
404507
+const char *
404507
+virResctrlAllocGetID(virResctrlAllocPtr alloc);
404507
+
404507
+char *
404507
+virResctrlAllocFormat(virResctrlAllocPtr alloc);
404507
+
404507
+int
404507
+virResctrlAllocCreate(virResctrlInfoPtr r_info,
404507
+                      virResctrlAllocPtr alloc,
404507
+                      const char *machinename);
404507
+
404507
+int
404507
+virResctrlAllocAddPID(virResctrlAllocPtr alloc,
404507
+                      pid_t pid);
404507
+
404507
+int
404507
+virResctrlAllocRemove(virResctrlAllocPtr alloc);
404507
+
404507
 #endif /*  __VIR_RESCTRL_H__ */
404507
diff --git a/src/util/virresctrlpriv.h b/src/util/virresctrlpriv.h
404507
new file mode 100644
404507
index 0000000000..72b115afd1
404507
--- /dev/null
404507
+++ b/src/util/virresctrlpriv.h
404507
@@ -0,0 +1,27 @@
404507
+/*
404507
+ * virresctrlpriv.h:
404507
+ *
404507
+ * This library is free software; you can redistribute it and/or
404507
+ * modify it under the terms of the GNU Lesser General Public
404507
+ * License as published by the Free Software Foundation; either
404507
+ * version 2.1 of the License, or (at your option) any later version.
404507
+ *
404507
+ * This library is distributed in the hope that it will be useful,
404507
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
404507
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
404507
+ * Lesser General Public License for more details.
404507
+ *
404507
+ * You should have received a copy of the GNU Lesser General Public
404507
+ * License along with this library.  If not, see
404507
+ * <http://www.gnu.org/licenses/>.
404507
+ */
404507
+
404507
+#ifndef __VIR_RESCTRL_PRIV_H__
404507
+# define __VIR_RESCTRL_PRIV_H__
404507
+
404507
+# include "virresctrl.h"
404507
+
404507
+virResctrlAllocPtr
404507
+virResctrlAllocGetUnused(virResctrlInfoPtr resctrl);
404507
+
404507
+#endif /* __VIR_RESCTRL_PRIV_H__ */
404507
-- 
404507
2.16.1
404507