2017-07-25 14:03:11 +00:00
|
|
|
/*
|
|
|
|
* virresctrl.c:
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2020-02-16 21:59:28 +00:00
|
|
|
#include <unistd.h>
|
2017-07-25 14:03:11 +00:00
|
|
|
|
2018-12-13 14:53:50 +00:00
|
|
|
#define LIBVIRT_VIRRESCTRLPRIV_H_ALLOW
|
2017-10-16 15:17:32 +00:00
|
|
|
#include "virresctrlpriv.h"
|
2017-07-25 14:03:11 +00:00
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virlog.h"
|
2017-11-20 10:12:06 +00:00
|
|
|
#include "virobject.h"
|
2017-07-25 14:03:11 +00:00
|
|
|
#include "virstring.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_RESCTRL
|
|
|
|
|
2019-01-20 16:32:42 +00:00
|
|
|
VIR_LOG_INIT("util.virresctrl");
|
2017-07-25 14:03:11 +00:00
|
|
|
|
2017-11-20 10:12:06 +00:00
|
|
|
|
2017-07-25 14:03:11 +00:00
|
|
|
/* Resctrl is short for Resource Control. It might be implemented for various
|
2018-11-12 13:31:33 +00:00
|
|
|
* resources. Currently this supports cache allocation technology (aka CAT),
|
|
|
|
* memory bandwidth allocation (aka MBA) and cache monitoring technology (aka
|
|
|
|
* CMT). More resources technologies may be added in the future.
|
2017-07-25 14:03:11 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
|
|
|
|
/* Common definitions */
|
|
|
|
#define SYSFS_RESCTRL_PATH "/sys/fs/resctrl"
|
|
|
|
|
|
|
|
|
2018-02-08 14:55:09 +00:00
|
|
|
/* Following are three different enum implementations for the same enum. Each
|
|
|
|
* one of them helps translating to/from strings for different interfaces. The
|
|
|
|
* delimiter must be VIR_CACHE_TYPE_LAST for all of them in order to stay
|
|
|
|
* consistent in between all of them. */
|
|
|
|
|
|
|
|
/* Cache name mapping for Linux kernel naming. */
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virCacheKernel,
|
|
|
|
VIR_CACHE_TYPE_LAST,
|
2018-02-08 14:55:09 +00:00
|
|
|
"Unified",
|
|
|
|
"Instruction",
|
2019-01-20 16:30:15 +00:00
|
|
|
"Data",
|
|
|
|
);
|
2018-02-08 14:55:09 +00:00
|
|
|
|
|
|
|
/* Cache name mapping for our XML naming. */
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virCache,
|
|
|
|
VIR_CACHE_TYPE_LAST,
|
2017-07-25 14:03:11 +00:00
|
|
|
"both",
|
|
|
|
"code",
|
2019-01-20 16:30:15 +00:00
|
|
|
"data",
|
|
|
|
);
|
2018-02-08 14:55:09 +00:00
|
|
|
|
|
|
|
/* Cache name mapping for resctrl interface naming. */
|
2019-01-20 16:04:56 +00:00
|
|
|
VIR_ENUM_DECL(virResctrl);
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virResctrl,
|
|
|
|
VIR_CACHE_TYPE_LAST,
|
2017-08-02 11:33:04 +00:00
|
|
|
"",
|
|
|
|
"CODE",
|
2019-01-20 16:30:15 +00:00
|
|
|
"DATA",
|
|
|
|
);
|
2017-08-02 11:33:04 +00:00
|
|
|
|
2018-09-20 10:10:50 +00:00
|
|
|
/* Monitor feature name prefix mapping for monitor naming */
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virResctrlMonitorPrefix,
|
|
|
|
VIR_RESCTRL_MONITOR_TYPE_LAST,
|
2018-09-20 10:10:50 +00:00
|
|
|
"__unsupported__",
|
|
|
|
"llc_",
|
2019-01-20 16:30:15 +00:00
|
|
|
"mbm_",
|
|
|
|
);
|
2018-09-20 10:10:50 +00:00
|
|
|
|
2017-11-20 10:12:06 +00:00
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
/* All private typedefs so that they exist for all later definitions. This way
|
|
|
|
* structs can be included in one or another without reorganizing the code every
|
|
|
|
* time. */
|
2017-11-20 10:12:06 +00:00
|
|
|
typedef struct _virResctrlInfoPerType virResctrlInfoPerType;
|
|
|
|
typedef virResctrlInfoPerType *virResctrlInfoPerTypePtr;
|
2018-02-08 14:21:57 +00:00
|
|
|
|
|
|
|
typedef struct _virResctrlInfoPerLevel virResctrlInfoPerLevel;
|
|
|
|
typedef virResctrlInfoPerLevel *virResctrlInfoPerLevelPtr;
|
|
|
|
|
2018-07-30 03:12:28 +00:00
|
|
|
typedef struct _virResctrlInfoMemBW virResctrlInfoMemBW;
|
|
|
|
typedef virResctrlInfoMemBW *virResctrlInfoMemBWPtr;
|
|
|
|
|
2018-09-20 10:10:47 +00:00
|
|
|
typedef struct _virResctrlInfoMongrp virResctrlInfoMongrp;
|
|
|
|
typedef virResctrlInfoMongrp *virResctrlInfoMongrpPtr;
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
typedef struct _virResctrlAllocPerType virResctrlAllocPerType;
|
|
|
|
typedef virResctrlAllocPerType *virResctrlAllocPerTypePtr;
|
|
|
|
|
|
|
|
typedef struct _virResctrlAllocPerLevel virResctrlAllocPerLevel;
|
|
|
|
typedef virResctrlAllocPerLevel *virResctrlAllocPerLevelPtr;
|
|
|
|
|
2018-07-30 03:12:30 +00:00
|
|
|
typedef struct _virResctrlAllocMemBW virResctrlAllocMemBW;
|
|
|
|
typedef virResctrlAllocMemBW *virResctrlAllocMemBWPtr;
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
|
|
|
|
/* Class definitions and initializations */
|
|
|
|
static virClassPtr virResctrlInfoClass;
|
|
|
|
static virClassPtr virResctrlAllocClass;
|
2018-11-12 13:31:33 +00:00
|
|
|
static virClassPtr virResctrlMonitorClass;
|
2018-02-08 14:21:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* virResctrlInfo */
|
2017-11-20 10:12:06 +00:00
|
|
|
struct _virResctrlInfoPerType {
|
|
|
|
/* Kernel-provided information */
|
|
|
|
unsigned int min_cbm_bits;
|
|
|
|
|
|
|
|
/* Our computed information from the above */
|
|
|
|
unsigned int bits;
|
|
|
|
unsigned int max_cache_id;
|
|
|
|
|
|
|
|
/* In order to be self-sufficient we need size information per cache.
|
2018-01-27 18:01:36 +00:00
|
|
|
* Funnily enough, one of the outcomes of the resctrl design is that it
|
2017-11-20 10:12:06 +00:00
|
|
|
* does not account for different sizes per cache on the same level. So
|
|
|
|
* for the sake of easiness, let's copy that, for now. */
|
|
|
|
unsigned long long size;
|
|
|
|
|
|
|
|
/* Information that we will return upon request (this is public struct) as
|
|
|
|
* until now all the above is internal to this module */
|
|
|
|
virResctrlInfoPerCache control;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virResctrlInfoPerLevel {
|
|
|
|
virResctrlInfoPerTypePtr *types;
|
|
|
|
};
|
|
|
|
|
2018-07-30 03:12:28 +00:00
|
|
|
/* Information about memory bandwidth allocation */
|
|
|
|
struct _virResctrlInfoMemBW {
|
|
|
|
/* minimum memory bandwidth allowed */
|
|
|
|
unsigned int min_bandwidth;
|
|
|
|
/* bandwidth granularity */
|
|
|
|
unsigned int bandwidth_granularity;
|
|
|
|
/* Maximum number of simultaneous allocations */
|
|
|
|
unsigned int max_allocation;
|
|
|
|
/* level number of last level cache */
|
|
|
|
unsigned int last_level_cache;
|
|
|
|
/* max id of last level cache, this is used to track
|
|
|
|
* how many last level cache available in host system,
|
|
|
|
* the number of memory bandwidth allocation controller
|
|
|
|
* is identical with last level cache. */
|
|
|
|
unsigned int max_id;
|
|
|
|
};
|
|
|
|
|
2018-09-20 10:10:47 +00:00
|
|
|
struct _virResctrlInfoMongrp {
|
|
|
|
/* Maximum number of simultaneous monitors */
|
|
|
|
unsigned int max_monitor;
|
|
|
|
/* null-terminal string list for monitor features */
|
|
|
|
char **features;
|
|
|
|
/* Number of monitor features */
|
|
|
|
size_t nfeatures;
|
|
|
|
|
|
|
|
/* Last level cache related information */
|
|
|
|
|
|
|
|
/* This adjustable value affects the final reuse of resources used by
|
|
|
|
* monitor. After the action of removing a monitor, the kernel may not
|
|
|
|
* release all hardware resources that monitor used immediately if the
|
|
|
|
* cache occupancy value associated with 'removed' monitor is above this
|
|
|
|
* threshold. Once the cache occupancy is below this threshold, the
|
|
|
|
* underlying hardware resource will be reclaimed and be put into the
|
|
|
|
* resource pool for next reusing.*/
|
|
|
|
unsigned int cache_reuse_threshold;
|
|
|
|
/* The cache 'level' that has the monitor capability */
|
|
|
|
unsigned int cache_level;
|
|
|
|
};
|
|
|
|
|
2017-11-20 10:12:06 +00:00
|
|
|
struct _virResctrlInfo {
|
|
|
|
virObject parent;
|
|
|
|
|
|
|
|
virResctrlInfoPerLevelPtr *levels;
|
|
|
|
size_t nlevels;
|
2018-07-30 03:12:28 +00:00
|
|
|
|
|
|
|
virResctrlInfoMemBWPtr membw_info;
|
2018-09-20 10:10:47 +00:00
|
|
|
|
|
|
|
virResctrlInfoMongrpPtr monitor_info;
|
2017-11-20 10:12:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virResctrlInfoDispose(void *obj)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
|
|
|
|
|
|
|
virResctrlInfoPtr resctrl = obj;
|
|
|
|
|
|
|
|
for (i = 0; i < resctrl->nlevels; i++) {
|
|
|
|
virResctrlInfoPerLevelPtr level = resctrl->levels[i];
|
|
|
|
|
|
|
|
if (!level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (level->types) {
|
2018-02-08 15:02:04 +00:00
|
|
|
for (j = 0; j < VIR_CACHE_TYPE_LAST; j++)
|
2017-11-20 10:12:06 +00:00
|
|
|
VIR_FREE(level->types[j]);
|
|
|
|
}
|
|
|
|
VIR_FREE(level->types);
|
|
|
|
VIR_FREE(level);
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:10:47 +00:00
|
|
|
if (resctrl->monitor_info)
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(resctrl->monitor_info->features);
|
2018-09-20 10:10:47 +00:00
|
|
|
|
2018-07-30 03:12:28 +00:00
|
|
|
VIR_FREE(resctrl->membw_info);
|
2017-11-20 10:12:06 +00:00
|
|
|
VIR_FREE(resctrl->levels);
|
2018-09-20 10:10:47 +00:00
|
|
|
VIR_FREE(resctrl->monitor_info);
|
2017-11-20 10:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-20 10:10:50 +00:00
|
|
|
void
|
|
|
|
virResctrlInfoMonFree(virResctrlInfoMonPtr mon)
|
|
|
|
{
|
|
|
|
if (!mon)
|
|
|
|
return;
|
|
|
|
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(mon->features);
|
2018-09-20 10:10:50 +00:00
|
|
|
VIR_FREE(mon);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-24 20:30:04 +00:00
|
|
|
/* virResctrlAlloc and virResctrlMonitor */
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
/*
|
2018-11-12 13:31:33 +00:00
|
|
|
* virResctrlAlloc and virResctrlMonitor are representing a resource control
|
|
|
|
* group (in XML under cputune/cachetune and consequently a directory under
|
|
|
|
* /sys/fs/resctrl). virResctrlAlloc is the data structure for resource
|
|
|
|
* allocation, while the virResctrlMonitor represents the resource monitoring
|
|
|
|
* part.
|
|
|
|
*
|
|
|
|
* virResctrlAlloc represents one allocation. Since it can have multiple
|
2017-10-16 15:17:32 +00:00
|
|
|
* parts of multiple caches allocated it is represented as bunch of nested
|
|
|
|
* sparse arrays (by sparse I mean array of pointers so that each might be NULL
|
2018-07-30 03:12:30 +00:00
|
|
|
* in case there is no allocation for that particular cache allocation (level,
|
|
|
|
* cache, ...) or memory allocation for particular node).
|
|
|
|
*
|
2018-11-12 13:31:32 +00:00
|
|
|
* Allocation corresponding to root directory, /sys/fs/sysctrl/, defines the
|
|
|
|
* default resource allocating policy, which is created immediately after
|
|
|
|
* mounting, and owns all the tasks and cpus in the system. Cache or memory
|
|
|
|
* bandwidth resource will be shared for tasks in this allocation.
|
|
|
|
*
|
2018-07-30 03:12:30 +00:00
|
|
|
* =====Cache allocation technology (CAT)=====
|
2017-10-16 15:17:32 +00:00
|
|
|
*
|
|
|
|
* Since one allocation can be made for caches on different levels, the first
|
|
|
|
* nested sparse array is of types virResctrlAllocPerLevel. For example if you
|
|
|
|
* have allocation for level 3 cache, there will be three NULL pointers and then
|
|
|
|
* allocated pointer to virResctrlAllocPerLevel. That way you can access it by
|
|
|
|
* `alloc[level]` as O(1) is desired instead of crawling through normal arrays
|
|
|
|
* or lists in three nested loops. The code uses a lot of direct accesses.
|
|
|
|
*
|
|
|
|
* Each virResctrlAllocPerLevel can have allocations for different cache
|
|
|
|
* allocation types. You can allocate instruction cache (VIR_CACHE_TYPE_CODE),
|
|
|
|
* data cache (VIR_CACHE_TYPE_DATA) or unified cache (VIR_CACHE_TYPE_BOTH).
|
|
|
|
* Those allocations are kept in sparse array of virResctrlAllocPerType pointers.
|
|
|
|
*
|
|
|
|
* For each virResctrlAllocPerType users can request some size of the cache to
|
|
|
|
* be allocated. That's what the sparse array `sizes` is for. Non-NULL
|
|
|
|
* pointers represent requested size allocations. The array is indexed by host
|
|
|
|
* cache id (gotten from `/sys/devices/system/cpu/cpuX/cache/indexY/id`). Users
|
|
|
|
* can see this information e.g. in the output of `virsh capabilities` (for that
|
|
|
|
* information there's the other struct, namely `virResctrlInfo`).
|
|
|
|
*
|
|
|
|
* When allocation is being created we need to find unused part of the cache for
|
|
|
|
* all of them. While doing that we store the bitmask in a sparse array of
|
|
|
|
* virBitmaps named `masks` indexed the same way as `sizes`. The upper bounds
|
|
|
|
* of the sparse arrays are stored in nmasks or nsizes, respectively.
|
2018-10-17 08:27:36 +00:00
|
|
|
*
|
2018-07-30 03:12:30 +00:00
|
|
|
* =====Memory Bandwidth allocation technology (MBA)=====
|
|
|
|
*
|
|
|
|
* The memory bandwidth allocation support in virResctrlAlloc works in the
|
|
|
|
* same fashion as CAT. However, memory bandwidth controller doesn't have a
|
|
|
|
* hierarchy organization as cache, each node have one memory bandwidth
|
|
|
|
* controller to memory bandwidth distribution. The number of memory bandwidth
|
|
|
|
* controller is identical with number of last level cache. So MBA also employs
|
|
|
|
* a sparse array to represent whether a memory bandwidth allocation happens
|
|
|
|
* on corresponding node. The available memory controller number is collected
|
|
|
|
* in 'virResctrlInfo'.
|
2018-11-12 13:31:33 +00:00
|
|
|
*
|
|
|
|
* =====Cache monitoring technology (CMT)=====
|
|
|
|
*
|
|
|
|
* Cache monitoring technology is used to perceive how many cache the process
|
|
|
|
* is using actually. virResctrlMonitor represents the resource control
|
|
|
|
* monitoring group, it is supported to monitor resource utilization
|
|
|
|
* information on granularity of vcpu.
|
|
|
|
*
|
|
|
|
* From a hardware perspective, cache monitoring technology (CMT), memory
|
|
|
|
* bandwidth technology (MBM), as well as the CAT and MBA, are all orthogonal
|
|
|
|
* features. The monitor will be created under the scope of default resctrl
|
|
|
|
* group if no specific CAT or MBA entries are provided for the guest."
|
2017-10-16 15:17:32 +00:00
|
|
|
*/
|
|
|
|
struct _virResctrlAllocPerType {
|
|
|
|
/* There could be bool saying whether this is set or not, but since everything
|
|
|
|
* in virResctrlAlloc (and most of libvirt) goes with pointer arrays we would
|
|
|
|
* have to have one more level of allocation anyway, so this stays faithful to
|
|
|
|
* the concept */
|
|
|
|
unsigned long long **sizes;
|
|
|
|
size_t nsizes;
|
|
|
|
|
|
|
|
/* Mask for each cache */
|
|
|
|
virBitmapPtr *masks;
|
|
|
|
size_t nmasks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virResctrlAllocPerLevel {
|
|
|
|
virResctrlAllocPerTypePtr *types; /* Indexed with enum virCacheType */
|
|
|
|
/* There is no `ntypes` member variable as it is always allocated for
|
|
|
|
* VIR_CACHE_TYPE_LAST number of items */
|
|
|
|
};
|
|
|
|
|
2018-07-30 03:12:30 +00:00
|
|
|
/*
|
|
|
|
* virResctrlAllocMemBW represents one memory bandwidth allocation.
|
|
|
|
* Since it can have several last level caches in a NUMA system, it is
|
|
|
|
* also represented as a nested sparse arrays as virRestrlAllocPerLevel.
|
|
|
|
*/
|
|
|
|
struct _virResctrlAllocMemBW {
|
|
|
|
unsigned int **bandwidths;
|
|
|
|
size_t nbandwidths;
|
|
|
|
};
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
struct _virResctrlAlloc {
|
|
|
|
virObject parent;
|
|
|
|
|
|
|
|
virResctrlAllocPerLevelPtr *levels;
|
|
|
|
size_t nlevels;
|
|
|
|
|
2018-07-30 03:12:30 +00:00
|
|
|
virResctrlAllocMemBWPtr mem_bw;
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
/* The identifier (any unique string for now) */
|
|
|
|
char *id;
|
|
|
|
/* libvirt-generated path in /sys/fs/resctrl for this particular
|
|
|
|
* allocation */
|
|
|
|
char *path;
|
|
|
|
};
|
|
|
|
|
2018-11-12 13:31:33 +00:00
|
|
|
/*
|
|
|
|
* virResctrlMonitor is the data structure for resctrl monitor. Resctrl
|
|
|
|
* monitor represents a resctrl monitoring group, which can be used to
|
|
|
|
* monitor the resource utilization information for either cache or
|
|
|
|
* memory bandwidth.
|
|
|
|
*/
|
|
|
|
struct _virResctrlMonitor {
|
|
|
|
virObject parent;
|
|
|
|
|
|
|
|
/* Each virResctrlMonitor is associated with one specific allocation,
|
|
|
|
* either the root directory allocation under /sys/fs/resctrl or a
|
|
|
|
* specific allocation defined under the root directory.
|
|
|
|
* This pointer points to the allocation this monitor is associated with.
|
|
|
|
*/
|
|
|
|
virResctrlAllocPtr alloc;
|
|
|
|
/* The monitor identifier. For a monitor has the same @path name as its
|
|
|
|
* @alloc, the @id will be set to the same value as it is in @alloc->id.
|
|
|
|
*/
|
|
|
|
char *id;
|
|
|
|
/* libvirt-generated path in /sys/fs/resctrl for this particular
|
|
|
|
* monitor */
|
|
|
|
char *path;
|
|
|
|
};
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
virResctrlAllocDispose(void *obj)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
|
|
|
size_t k = 0;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
virResctrlAllocPtr alloc = obj;
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
for (i = 0; i < alloc->nlevels; i++) {
|
|
|
|
virResctrlAllocPerLevelPtr level = alloc->levels[i];
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (!level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
|
|
|
|
virResctrlAllocPerTypePtr type = level->types[j];
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (k = 0; k < type->nsizes; k++)
|
|
|
|
VIR_FREE(type->sizes[k]);
|
|
|
|
|
|
|
|
for (k = 0; k < type->nmasks; k++)
|
|
|
|
virBitmapFree(type->masks[k]);
|
|
|
|
|
|
|
|
VIR_FREE(type->sizes);
|
|
|
|
VIR_FREE(type->masks);
|
|
|
|
VIR_FREE(type);
|
|
|
|
}
|
|
|
|
VIR_FREE(level->types);
|
|
|
|
VIR_FREE(level);
|
|
|
|
}
|
|
|
|
|
2018-07-30 03:12:30 +00:00
|
|
|
if (alloc->mem_bw) {
|
|
|
|
virResctrlAllocMemBWPtr mem_bw = alloc->mem_bw;
|
|
|
|
for (i = 0; i < mem_bw->nbandwidths; i++)
|
|
|
|
VIR_FREE(mem_bw->bandwidths[i]);
|
2019-04-11 15:13:16 +00:00
|
|
|
VIR_FREE(alloc->mem_bw->bandwidths);
|
2018-07-30 03:12:30 +00:00
|
|
|
VIR_FREE(alloc->mem_bw);
|
|
|
|
}
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
VIR_FREE(alloc->id);
|
|
|
|
VIR_FREE(alloc->path);
|
|
|
|
VIR_FREE(alloc->levels);
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 13:31:33 +00:00
|
|
|
static void
|
|
|
|
virResctrlMonitorDispose(void *obj)
|
|
|
|
{
|
|
|
|
virResctrlMonitorPtr monitor = obj;
|
|
|
|
|
|
|
|
virObjectUnref(monitor->alloc);
|
|
|
|
VIR_FREE(monitor->id);
|
|
|
|
VIR_FREE(monitor->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
/* Global initialization for classes */
|
2017-10-16 15:17:32 +00:00
|
|
|
static int
|
2018-02-08 14:21:57 +00:00
|
|
|
virResctrlOnceInit(void)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
2018-02-08 14:21:57 +00:00
|
|
|
if (!VIR_CLASS_NEW(virResctrlInfo, virClassForObject()))
|
|
|
|
return -1;
|
|
|
|
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virResctrlAlloc, virClassForObject()))
|
2017-10-16 15:17:32 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-11-12 13:31:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virResctrlMonitor, virClassForObject()))
|
|
|
|
return -1;
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virResctrl);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Common functions */
|
2020-07-29 10:46:12 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
static int
|
2020-07-29 11:02:01 +00:00
|
|
|
virResctrlLock(void)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
2020-07-09 07:27:41 +00:00
|
|
|
int fd = open(SYSFS_RESCTRL_PATH, O_RDONLY | O_CLOEXEC);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (fd < 0) {
|
2018-01-27 18:01:36 +00:00
|
|
|
virReportSystemError(errno, "%s", _("Cannot open resctrl"));
|
2017-10-16 15:17:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:46:12 +00:00
|
|
|
if (flock(fd, LOCK_EX) < 0) {
|
2018-01-27 18:01:36 +00:00
|
|
|
virReportSystemError(errno, "%s", _("Cannot lock resctrl"));
|
2017-10-16 15:17:32 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virResctrlUnlock(int fd)
|
|
|
|
{
|
|
|
|
if (fd == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* The lock gets unlocked by closing the fd, which we need to do anyway in
|
|
|
|
* order to clean up properly */
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
2018-01-27 18:01:36 +00:00
|
|
|
virReportSystemError(errno, "%s", _("Cannot close resctrl"));
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
/* Trying to save the already broken */
|
2020-07-29 10:46:12 +00:00
|
|
|
if (flock(fd, LOCK_UN) < 0)
|
2018-01-27 18:01:36 +00:00
|
|
|
virReportSystemError(errno, "%s", _("Cannot unlock resctrl"));
|
2018-02-08 14:09:02 +00:00
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:46:12 +00:00
|
|
|
#else /* WIN32 */
|
|
|
|
|
|
|
|
static int
|
2020-07-29 11:02:01 +00:00
|
|
|
virResctrlLock(void)
|
2020-07-29 10:46:12 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("resctrl locking is not supported "
|
|
|
|
"on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virResctrlUnlock(int fd G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("resctrl locking is not supported "
|
|
|
|
"on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WIN32 */
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
/* virResctrlInfo-related definitions */
|
2018-02-08 14:47:46 +00:00
|
|
|
static int
|
2018-07-30 03:12:26 +00:00
|
|
|
virResctrlGetCacheInfo(virResctrlInfoPtr resctrl,
|
|
|
|
DIR *dirp)
|
2017-11-20 10:12:06 +00:00
|
|
|
{
|
|
|
|
char *endptr = NULL;
|
|
|
|
char *tmp_str = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
int rv = -1;
|
|
|
|
int type = 0;
|
|
|
|
struct dirent *ent = NULL;
|
|
|
|
unsigned int level = 0;
|
2018-02-08 15:02:04 +00:00
|
|
|
virBitmapPtr tmp_map = NULL;
|
2017-11-20 10:12:06 +00:00
|
|
|
virResctrlInfoPerLevelPtr i_level = NULL;
|
|
|
|
virResctrlInfoPerTypePtr i_type = NULL;
|
|
|
|
|
|
|
|
while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH "/info")) > 0) {
|
2018-01-24 14:47:31 +00:00
|
|
|
VIR_DEBUG("Parsing info type '%s'", ent->d_name);
|
2017-11-20 10:12:06 +00:00
|
|
|
if (ent->d_name[0] != 'L')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (virStrToLong_uip(ent->d_name + 1, &endptr, 10, &level) < 0) {
|
2018-01-24 14:47:31 +00:00
|
|
|
VIR_DEBUG("Cannot parse resctrl cache info level '%s'", ent->d_name + 1);
|
|
|
|
continue;
|
2017-11-20 10:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type = virResctrlTypeFromString(endptr);
|
|
|
|
if (type < 0) {
|
2018-01-24 14:47:31 +00:00
|
|
|
VIR_DEBUG("Cannot parse resctrl cache info type '%s'", endptr);
|
|
|
|
continue;
|
2017-11-20 10:12:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
i_type = g_new0(virResctrlInfoPerType, 1);
|
2017-11-20 10:12:06 +00:00
|
|
|
i_type->control.scope = type;
|
|
|
|
|
|
|
|
rv = virFileReadValueUint(&i_type->control.max_allocation,
|
|
|
|
SYSFS_RESCTRL_PATH "/info/%s/num_closids",
|
|
|
|
ent->d_name);
|
2018-01-29 13:46:45 +00:00
|
|
|
if (rv == -2) {
|
|
|
|
/* The file doesn't exist, so it's unusable for us,
|
|
|
|
* but we can scan further */
|
|
|
|
VIR_WARN("The path '" SYSFS_RESCTRL_PATH "/info/%s/num_closids' "
|
|
|
|
"does not exist",
|
|
|
|
ent->d_name);
|
|
|
|
} else if (rv < 0) {
|
|
|
|
/* Other failures are fatal, so just quit */
|
2017-11-20 10:12:06 +00:00
|
|
|
goto cleanup;
|
2018-01-29 13:46:45 +00:00
|
|
|
}
|
2017-11-20 10:12:06 +00:00
|
|
|
|
2018-02-08 15:02:04 +00:00
|
|
|
rv = virFileReadValueString(&tmp_str,
|
2017-11-20 10:12:06 +00:00
|
|
|
SYSFS_RESCTRL_PATH
|
|
|
|
"/info/%s/cbm_mask",
|
|
|
|
ent->d_name);
|
2018-01-29 13:46:45 +00:00
|
|
|
if (rv == -2) {
|
|
|
|
/* If the previous file exists, so should this one. Hence -2 is
|
|
|
|
* fatal in this case as well (errors out in next condition) - the
|
|
|
|
* kernel interface might've changed too much or something else is
|
|
|
|
* wrong. */
|
2017-11-20 10:12:06 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot get cbm_mask from resctrl cache info"));
|
2018-01-29 13:46:45 +00:00
|
|
|
}
|
2017-11-20 10:12:06 +00:00
|
|
|
if (rv < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-02-08 15:02:04 +00:00
|
|
|
virStringTrimOptionalNewline(tmp_str);
|
|
|
|
|
|
|
|
tmp_map = virBitmapNewString(tmp_str);
|
|
|
|
VIR_FREE(tmp_str);
|
|
|
|
if (!tmp_map) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot parse cbm_mask from resctrl cache info"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
i_type->bits = virBitmapCountBits(tmp_map);
|
|
|
|
virBitmapFree(tmp_map);
|
|
|
|
tmp_map = NULL;
|
2017-11-20 10:12:06 +00:00
|
|
|
|
|
|
|
rv = virFileReadValueUint(&i_type->min_cbm_bits,
|
|
|
|
SYSFS_RESCTRL_PATH "/info/%s/min_cbm_bits",
|
|
|
|
ent->d_name);
|
|
|
|
if (rv == -2)
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot get min_cbm_bits from resctrl cache info"));
|
|
|
|
if (rv < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (resctrl->nlevels <= level &&
|
|
|
|
VIR_EXPAND_N(resctrl->levels, resctrl->nlevels,
|
|
|
|
level - resctrl->nlevels + 1) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-01-24 14:47:31 +00:00
|
|
|
if (!resctrl->levels[level]) {
|
|
|
|
virResctrlInfoPerTypePtr *types = NULL;
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
types = g_new0(virResctrlInfoPerTypePtr, VIR_CACHE_TYPE_LAST);
|
2018-01-24 14:47:31 +00:00
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
resctrl->levels[level] = g_new0(virResctrlInfoPerLevel, 1);
|
2018-01-24 14:47:31 +00:00
|
|
|
resctrl->levels[level]->types = types;
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:12:06 +00:00
|
|
|
i_level = resctrl->levels[level];
|
|
|
|
|
|
|
|
if (i_level->types[type]) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2018-01-27 18:01:36 +00:00
|
|
|
_("Duplicate cache type in resctrl for level %u"),
|
2017-11-20 10:12:06 +00:00
|
|
|
level);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
i_level->types[type] = g_steal_pointer(&i_type);
|
2017-11-20 10:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(i_type);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:28 +00:00
|
|
|
static int
|
|
|
|
virResctrlGetMemoryBandwidthInfo(virResctrlInfoPtr resctrl)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int rv = -1;
|
|
|
|
virResctrlInfoMemBWPtr i_membw = NULL;
|
|
|
|
|
|
|
|
/* query memory bandwidth allocation info */
|
2020-10-05 17:06:36 +00:00
|
|
|
i_membw = g_new0(virResctrlInfoMemBW, 1);
|
2018-07-30 03:12:28 +00:00
|
|
|
rv = virFileReadValueUint(&i_membw->bandwidth_granularity,
|
|
|
|
SYSFS_RESCTRL_PATH "/info/MB/bandwidth_gran");
|
|
|
|
if (rv == -2) {
|
|
|
|
/* The file doesn't exist, so it's unusable for us,
|
|
|
|
* probably memory bandwidth allocation unsupported */
|
|
|
|
VIR_INFO("The path '" SYSFS_RESCTRL_PATH "/info/MB/bandwidth_gran'"
|
|
|
|
"does not exist");
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
} else if (rv < 0) {
|
|
|
|
/* Other failures are fatal, so just quit */
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = virFileReadValueUint(&i_membw->min_bandwidth,
|
|
|
|
SYSFS_RESCTRL_PATH "/info/MB/min_bandwidth");
|
|
|
|
if (rv == -2) {
|
|
|
|
/* If the previous file exists, so should this one. Hence -2 is
|
|
|
|
* fatal in this case (errors out in next condition) - the kernel
|
|
|
|
* interface might've changed too much or something else is wrong. */
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot get min bandwidth from resctrl memory info"));
|
|
|
|
}
|
|
|
|
if (rv < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
rv = virFileReadValueUint(&i_membw->max_allocation,
|
|
|
|
SYSFS_RESCTRL_PATH "/info/MB/num_closids");
|
|
|
|
if (rv == -2) {
|
|
|
|
/* Similar reasoning to min_bandwidth above. */
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot get max allocation from resctrl memory info"));
|
|
|
|
}
|
|
|
|
if (rv < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
resctrl->membw_info = g_steal_pointer(&i_membw);
|
2018-07-30 03:12:28 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(i_membw);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-20 10:10:47 +00:00
|
|
|
/*
|
|
|
|
* Retrieve monitor capability from the resource control file system.
|
|
|
|
*
|
|
|
|
* The monitor capability is exposed through "SYSFS_RESCTRL_PATH/info/L3_MON"
|
|
|
|
* directory under the resource control file system. The monitor capability is
|
|
|
|
* parsed by reading the interface files and stored in the structure
|
|
|
|
* 'virResctrlInfoMongrp'.
|
|
|
|
*
|
|
|
|
* Not all host supports the resource monitor, leave the pointer
|
|
|
|
* @resctrl->monitor_info empty if not supported.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virResctrlGetMonitorInfo(virResctrlInfoPtr resctrl)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int rv = -1;
|
|
|
|
char *featurestr = NULL;
|
|
|
|
char **features = NULL;
|
|
|
|
size_t nfeatures = 0;
|
|
|
|
virResctrlInfoMongrpPtr info_monitor = NULL;
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
info_monitor = g_new0(virResctrlInfoMongrp, 1);
|
2018-09-20 10:10:47 +00:00
|
|
|
|
|
|
|
/* For now, monitor only exists in level 3 cache */
|
|
|
|
info_monitor->cache_level = 3;
|
|
|
|
|
|
|
|
rv = virFileReadValueUint(&info_monitor->max_monitor,
|
|
|
|
SYSFS_RESCTRL_PATH "/info/L3_MON/num_rmids");
|
|
|
|
if (rv == -2) {
|
|
|
|
/* The file doesn't exist, so it's unusable for us, probably resource
|
|
|
|
* monitor unsupported */
|
|
|
|
VIR_INFO("The file '" SYSFS_RESCTRL_PATH "/info/L3_MON/num_rmids' "
|
|
|
|
"does not exist");
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
} else if (rv < 0) {
|
|
|
|
/* Other failures are fatal, so just quit */
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = virFileReadValueUint(&info_monitor->cache_reuse_threshold,
|
|
|
|
SYSFS_RESCTRL_PATH
|
|
|
|
"/info/L3_MON/max_threshold_occupancy");
|
|
|
|
if (rv == -2) {
|
|
|
|
/* If CMT is not supported, then 'max_threshold_occupancy' file
|
|
|
|
* will not exist. */
|
|
|
|
VIR_DEBUG("File '" SYSFS_RESCTRL_PATH
|
|
|
|
"/info/L3_MON/max_threshold_occupancy' does not exist");
|
|
|
|
} else if (rv < 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = virFileReadValueString(&featurestr,
|
|
|
|
SYSFS_RESCTRL_PATH
|
|
|
|
"/info/L3_MON/mon_features");
|
|
|
|
if (rv == -2)
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot get mon_features from resctrl"));
|
|
|
|
if (rv < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!*featurestr) {
|
|
|
|
/* If no feature found in "/info/L3_MON/mon_features",
|
|
|
|
* some error happens */
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2018-12-04 17:08:14 +00:00
|
|
|
_("Got empty feature list from resctrl"));
|
2018-09-20 10:10:47 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
features = virStringSplitCount(featurestr, "\n", 0, &nfeatures);
|
2018-09-20 18:57:22 +00:00
|
|
|
VIR_DEBUG("Resctrl supported %zd monitoring features", nfeatures);
|
2018-09-20 10:10:47 +00:00
|
|
|
|
|
|
|
info_monitor->nfeatures = nfeatures;
|
2019-10-16 11:43:52 +00:00
|
|
|
info_monitor->features = g_steal_pointer(&features);
|
|
|
|
resctrl->monitor_info = g_steal_pointer(&info_monitor);
|
2018-09-20 10:10:47 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(featurestr);
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(features);
|
2018-09-20 10:10:47 +00:00
|
|
|
VIR_FREE(info_monitor);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:26 +00:00
|
|
|
static int
|
|
|
|
virResctrlGetInfo(virResctrlInfoPtr resctrl)
|
|
|
|
{
|
|
|
|
DIR *dirp = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
ret = virDirOpenIfExists(&dirp, SYSFS_RESCTRL_PATH "/info");
|
|
|
|
if (ret <= 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-07-30 03:12:28 +00:00
|
|
|
ret = virResctrlGetMemoryBandwidthInfo(resctrl);
|
|
|
|
if (ret < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-07-30 03:12:26 +00:00
|
|
|
ret = virResctrlGetCacheInfo(resctrl, dirp);
|
|
|
|
if (ret < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-09-20 10:10:47 +00:00
|
|
|
ret = virResctrlGetMonitorInfo(resctrl);
|
|
|
|
if (ret < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-07-30 03:12:26 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
VIR_DIR_CLOSE(dirp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
virResctrlInfoPtr
|
|
|
|
virResctrlInfoNew(void)
|
|
|
|
{
|
2018-02-08 14:47:46 +00:00
|
|
|
virResctrlInfoPtr ret = NULL;
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
if (virResctrlInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2018-02-08 14:47:46 +00:00
|
|
|
ret = virObjectNew(virResctrlInfoClass);
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virResctrlGetInfo(ret) < 0) {
|
|
|
|
virObjectUnref(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-02-08 14:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
virResctrlInfoIsEmpty(virResctrlInfoPtr resctrl)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
|
|
|
|
|
|
|
if (!resctrl)
|
|
|
|
return true;
|
|
|
|
|
2018-07-30 03:12:28 +00:00
|
|
|
if (resctrl->membw_info)
|
|
|
|
return false;
|
|
|
|
|
2018-09-20 10:10:47 +00:00
|
|
|
if (resctrl->monitor_info)
|
|
|
|
return false;
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
for (i = 0; i < resctrl->nlevels; i++) {
|
|
|
|
virResctrlInfoPerLevelPtr i_level = resctrl->levels[i];
|
|
|
|
|
|
|
|
if (!i_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
|
|
|
|
if (i_level->types[j])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:41 +00:00
|
|
|
int
|
|
|
|
virResctrlInfoGetMemoryBandwidth(virResctrlInfoPtr resctrl,
|
|
|
|
unsigned int level,
|
|
|
|
virResctrlInfoMemBWPerNodePtr control)
|
|
|
|
{
|
|
|
|
virResctrlInfoMemBWPtr membw_info = resctrl->membw_info;
|
|
|
|
|
|
|
|
if (!membw_info)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (membw_info->last_level_cache != level)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
control->granularity = membw_info->bandwidth_granularity;
|
|
|
|
control->min = membw_info->min_bandwidth;
|
|
|
|
control->max_allocation = membw_info->max_allocation;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-20 10:12:06 +00:00
|
|
|
int
|
|
|
|
virResctrlInfoGetCache(virResctrlInfoPtr resctrl,
|
|
|
|
unsigned int level,
|
|
|
|
unsigned long long size,
|
|
|
|
size_t *ncontrols,
|
|
|
|
virResctrlInfoPerCachePtr **controls)
|
|
|
|
{
|
|
|
|
virResctrlInfoPerLevelPtr i_level = NULL;
|
|
|
|
virResctrlInfoPerTypePtr i_type = NULL;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if (virResctrlInfoIsEmpty(resctrl))
|
|
|
|
return 0;
|
|
|
|
|
2018-07-30 03:12:29 +00:00
|
|
|
/* Let's take the opportunity to update the number of last level
|
|
|
|
* cache. This number of memory bandwidth controller is same with
|
|
|
|
* last level cache */
|
|
|
|
if (resctrl->membw_info) {
|
|
|
|
virResctrlInfoMemBWPtr membw_info = resctrl->membw_info;
|
|
|
|
|
|
|
|
if (level > membw_info->last_level_cache) {
|
|
|
|
membw_info->last_level_cache = level;
|
|
|
|
membw_info->max_id = 0;
|
|
|
|
} else if (membw_info->last_level_cache == level) {
|
|
|
|
membw_info->max_id++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 10:12:06 +00:00
|
|
|
if (level >= resctrl->nlevels)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
i_level = resctrl->levels[level];
|
|
|
|
if (!i_level)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < VIR_CACHE_TYPE_LAST; i++) {
|
|
|
|
i_type = i_level->types[i];
|
|
|
|
if (!i_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Let's take the opportunity to update our internal information about
|
|
|
|
* the cache size */
|
|
|
|
if (!i_type->size) {
|
|
|
|
i_type->size = size;
|
|
|
|
i_type->control.granularity = size / i_type->bits;
|
|
|
|
if (i_type->min_cbm_bits != 1)
|
|
|
|
i_type->control.min = i_type->min_cbm_bits * i_type->control.granularity;
|
|
|
|
} else {
|
|
|
|
if (i_type->size != size) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("level %u cache size %llu does not match "
|
|
|
|
"expected size %llu"),
|
2018-09-19 08:38:14 +00:00
|
|
|
level, i_type->size, size);
|
2017-11-20 10:12:06 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
i_type->max_cache_id++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_EXPAND_N(*controls, *ncontrols, 1) < 0)
|
|
|
|
goto error;
|
2020-10-05 17:06:36 +00:00
|
|
|
(*controls)[*ncontrols - 1] = g_new0(virResctrlInfoPerCache, 1);
|
2017-11-20 10:12:06 +00:00
|
|
|
memcpy((*controls)[*ncontrols - 1], &i_type->control, sizeof(i_type->control));
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2017-11-20 10:12:06 +00:00
|
|
|
error:
|
|
|
|
while (*ncontrols)
|
|
|
|
VIR_FREE((*controls)[--*ncontrols]);
|
|
|
|
VIR_FREE(*controls);
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2017-11-20 10:12:06 +00:00
|
|
|
}
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
|
2018-09-20 10:10:50 +00:00
|
|
|
/* virResctrlInfoGetMonitorPrefix
|
|
|
|
*
|
|
|
|
* @resctrl: Pointer to virResctrlInfo
|
|
|
|
* @prefix: Monitor prefix name for monitor looking for.
|
|
|
|
* @monitor: Returns the capability information for target monitor if the
|
|
|
|
* monitor with @prefex is supported by host.
|
|
|
|
*
|
|
|
|
* Return monitor capability information for @prefix through @monitor.
|
|
|
|
* If monitor with @prefix is not supported in system, @monitor will be
|
|
|
|
* cleared to NULL.
|
|
|
|
*
|
|
|
|
* Returns 0 if @monitor is created or monitor type with @prefix is not
|
|
|
|
* supported by host, -1 on failure with error message set.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virResctrlInfoGetMonitorPrefix(virResctrlInfoPtr resctrl,
|
|
|
|
const char *prefix,
|
|
|
|
virResctrlInfoMonPtr *monitor)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
virResctrlInfoMongrpPtr mongrp_info = NULL;
|
|
|
|
virResctrlInfoMonPtr mon = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!prefix) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Empty prefix name for resctrl monitor"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virResctrlInfoIsEmpty(resctrl))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mongrp_info = resctrl->monitor_info;
|
|
|
|
|
|
|
|
if (!mongrp_info) {
|
|
|
|
VIR_INFO("Monitor is not supported in host");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < VIR_RESCTRL_MONITOR_TYPE_LAST; i++) {
|
|
|
|
if (STREQ(prefix, virResctrlMonitorPrefixTypeToString(i))) {
|
2020-10-05 17:06:36 +00:00
|
|
|
mon = g_new0(virResctrlInfoMon, 1);
|
2018-09-20 10:10:50 +00:00
|
|
|
mon->type = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mon) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Bad prefix name '%s' for resctrl monitor"),
|
|
|
|
prefix);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mon->max_monitor = mongrp_info->max_monitor;
|
|
|
|
|
|
|
|
if (mon->type == VIR_RESCTRL_MONITOR_TYPE_CACHE) {
|
|
|
|
mon->cache_reuse_threshold = mongrp_info->cache_reuse_threshold;
|
|
|
|
mon->cache_level = mongrp_info->cache_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < mongrp_info->nfeatures; i++) {
|
|
|
|
if (STRPREFIX(mongrp_info->features[i], prefix)) {
|
|
|
|
if (virStringListAdd(&mon->features,
|
|
|
|
mongrp_info->features[i]) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
mon->nfeatures++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
/* In case *monitor is pointed to some monitor, clean it. */
|
|
|
|
virResctrlInfoMonFree(*monitor);
|
|
|
|
|
|
|
|
if (mon->nfeatures == 0) {
|
|
|
|
/* No feature found for current monitor, means host does not support
|
|
|
|
* monitor type with @prefix name.
|
|
|
|
* Telling caller this monitor is supported by hardware specification,
|
|
|
|
* but not supported by this host. */
|
|
|
|
VIR_INFO("No resctrl monitor features using prefix '%s' found", prefix);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*monitor = g_steal_pointer(&mon);
|
2018-09-20 10:10:50 +00:00
|
|
|
cleanup:
|
|
|
|
virResctrlInfoMonFree(mon);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-08 14:21:57 +00:00
|
|
|
/* virResctrlAlloc-related definitions */
|
|
|
|
virResctrlAllocPtr
|
|
|
|
virResctrlAllocNew(void)
|
|
|
|
{
|
|
|
|
if (virResctrlInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virObjectNew(virResctrlAllocClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
bool
|
2018-02-01 13:57:40 +00:00
|
|
|
virResctrlAllocIsEmpty(virResctrlAllocPtr alloc)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
|
|
|
size_t k = 0;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
if (!alloc)
|
2017-10-16 15:17:32 +00:00
|
|
|
return true;
|
|
|
|
|
2018-07-30 03:12:30 +00:00
|
|
|
if (alloc->mem_bw)
|
|
|
|
return false;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
for (i = 0; i < alloc->nlevels; i++) {
|
|
|
|
virResctrlAllocPerLevelPtr a_level = alloc->levels[i];
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (!a_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
|
|
|
|
virResctrlAllocPerTypePtr a_type = a_level->types[j];
|
|
|
|
|
|
|
|
if (!a_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (k = 0; k < a_type->nsizes; k++) {
|
|
|
|
if (a_type->sizes[k])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k = 0; k < a_type->nmasks; k++) {
|
|
|
|
if (a_type->masks[k])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virResctrlAllocPerTypePtr
|
2018-02-01 13:57:40 +00:00
|
|
|
virResctrlAllocGetType(virResctrlAllocPtr alloc,
|
2017-10-16 15:17:32 +00:00
|
|
|
unsigned int level,
|
|
|
|
virCacheType type)
|
|
|
|
{
|
|
|
|
virResctrlAllocPerLevelPtr a_level = NULL;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
if (alloc->nlevels <= level &&
|
|
|
|
VIR_EXPAND_N(alloc->levels, alloc->nlevels, level - alloc->nlevels + 1) < 0)
|
2017-10-16 15:17:32 +00:00
|
|
|
return NULL;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
if (!alloc->levels[level]) {
|
2017-10-16 15:17:32 +00:00
|
|
|
virResctrlAllocPerTypePtr *types = NULL;
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
types = g_new0(virResctrlAllocPerTypePtr, VIR_CACHE_TYPE_LAST);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
alloc->levels[level] = g_new0(virResctrlAllocPerLevel, 1);
|
2018-02-01 13:57:40 +00:00
|
|
|
alloc->levels[level]->types = types;
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
a_level = alloc->levels[level];
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
if (!a_level->types[type])
|
|
|
|
a_level->types[type] = g_new0(virResctrlAllocPerType, 1);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
return a_level->types[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2018-02-01 13:57:40 +00:00
|
|
|
virResctrlAllocUpdateMask(virResctrlAllocPtr alloc,
|
2017-10-16 15:17:32 +00:00
|
|
|
unsigned int level,
|
|
|
|
virCacheType type,
|
|
|
|
unsigned int cache,
|
|
|
|
virBitmapPtr mask)
|
|
|
|
{
|
2018-02-01 13:57:40 +00:00
|
|
|
virResctrlAllocPerTypePtr a_type = virResctrlAllocGetType(alloc, level, type);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (!a_type)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (a_type->nmasks <= cache &&
|
|
|
|
VIR_EXPAND_N(a_type->masks, a_type->nmasks,
|
|
|
|
cache - a_type->nmasks + 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2020-10-01 15:42:11 +00:00
|
|
|
if (!a_type->masks[cache])
|
2020-10-02 08:26:34 +00:00
|
|
|
a_type->masks[cache] = virBitmapNewCopy(mask);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2020-10-02 08:26:34 +00:00
|
|
|
return 0;
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2018-02-01 13:57:40 +00:00
|
|
|
virResctrlAllocUpdateSize(virResctrlAllocPtr alloc,
|
2017-10-16 15:17:32 +00:00
|
|
|
unsigned int level,
|
|
|
|
virCacheType type,
|
|
|
|
unsigned int cache,
|
|
|
|
unsigned long long size)
|
|
|
|
{
|
2018-02-01 13:57:40 +00:00
|
|
|
virResctrlAllocPerTypePtr a_type = virResctrlAllocGetType(alloc, level, type);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (!a_type)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (a_type->nsizes <= cache &&
|
|
|
|
VIR_EXPAND_N(a_type->sizes, a_type->nsizes,
|
|
|
|
cache - a_type->nsizes + 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
if (!a_type->sizes[cache])
|
|
|
|
a_type->sizes[cache] = g_new0(unsigned long long, 1);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
*(a_type->sizes[cache]) = size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if there is an allocation for this level/type/cache already. Called
|
|
|
|
* before updating the structure. VIR_CACHE_TYPE_BOTH collides with any type,
|
|
|
|
* the other types collide with itself. This code basically checks if either:
|
|
|
|
* `alloc[level]->types[type]->sizes[cache]`
|
|
|
|
* or
|
|
|
|
* `alloc[level]->types[VIR_CACHE_TYPE_BOTH]->sizes[cache]`
|
|
|
|
* is non-NULL. All the fuzz around it is checking for NULL pointers along
|
|
|
|
* the way.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
virResctrlAllocCheckCollision(virResctrlAllocPtr alloc,
|
|
|
|
unsigned int level,
|
|
|
|
virCacheType type,
|
|
|
|
unsigned int cache)
|
|
|
|
{
|
|
|
|
virResctrlAllocPerLevelPtr a_level = NULL;
|
|
|
|
virResctrlAllocPerTypePtr a_type = NULL;
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (alloc->nlevels <= level)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
a_level = alloc->levels[level];
|
|
|
|
|
|
|
|
if (!a_level)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
a_type = a_level->types[VIR_CACHE_TYPE_BOTH];
|
|
|
|
|
|
|
|
/* If there is an allocation for type 'both', there can be no other
|
|
|
|
* allocation for the same cache */
|
|
|
|
if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (type == VIR_CACHE_TYPE_BOTH) {
|
|
|
|
a_type = a_level->types[VIR_CACHE_TYPE_CODE];
|
|
|
|
|
|
|
|
if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
|
|
|
|
return true;
|
|
|
|
|
|
|
|
a_type = a_level->types[VIR_CACHE_TYPE_DATA];
|
|
|
|
|
|
|
|
if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
a_type = a_level->types[type];
|
|
|
|
|
|
|
|
if (a_type && a_type->nsizes > cache && a_type->sizes[cache])
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2018-07-30 03:12:25 +00:00
|
|
|
virResctrlAllocSetCacheSize(virResctrlAllocPtr alloc,
|
|
|
|
unsigned int level,
|
|
|
|
virCacheType type,
|
|
|
|
unsigned int cache,
|
|
|
|
unsigned long long size)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
2018-02-01 13:57:40 +00:00
|
|
|
if (virResctrlAllocCheckCollision(alloc, level, type, cache)) {
|
2017-10-16 15:17:32 +00:00
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Colliding cache allocations for cache "
|
|
|
|
"level '%u' id '%u', type '%s'"),
|
|
|
|
level, cache, virCacheTypeToString(type));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
return virResctrlAllocUpdateSize(alloc, level, type, cache, size);
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2018-07-30 03:12:25 +00:00
|
|
|
virResctrlAllocForeachCache(virResctrlAllocPtr alloc,
|
|
|
|
virResctrlAllocForeachCacheCallback cb,
|
|
|
|
void *opaque)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned int level = 0;
|
|
|
|
unsigned int type = 0;
|
|
|
|
unsigned int cache = 0;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
if (!alloc)
|
2017-10-16 15:17:32 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
for (level = 0; level < alloc->nlevels; level++) {
|
|
|
|
virResctrlAllocPerLevelPtr a_level = alloc->levels[level];
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (!a_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) {
|
|
|
|
virResctrlAllocPerTypePtr a_type = a_level->types[type];
|
|
|
|
|
|
|
|
if (!a_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (cache = 0; cache < a_type->nsizes; cache++) {
|
|
|
|
unsigned long long *size = a_type->sizes[cache];
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = cb(level, type, cache, *size, opaque);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:34 +00:00
|
|
|
/* virResctrlAllocSetMemoryBandwidth
|
|
|
|
* @alloc: Pointer to an active allocation
|
|
|
|
* @id: node id of MBA to be set
|
|
|
|
* @memory_bandwidth: new memory bandwidth value
|
|
|
|
*
|
|
|
|
* Set the @memory_bandwidth for the node @id entry in the @alloc.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure with error message set.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virResctrlAllocSetMemoryBandwidth(virResctrlAllocPtr alloc,
|
|
|
|
unsigned int id,
|
|
|
|
unsigned int memory_bandwidth)
|
|
|
|
{
|
|
|
|
virResctrlAllocMemBWPtr mem_bw = alloc->mem_bw;
|
|
|
|
|
|
|
|
if (memory_bandwidth > 100) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("Memory Bandwidth value exceeding 100 is invalid."));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mem_bw) {
|
2020-10-05 17:06:36 +00:00
|
|
|
mem_bw = g_new0(virResctrlAllocMemBW, 1);
|
2018-07-30 03:12:34 +00:00
|
|
|
alloc->mem_bw = mem_bw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mem_bw->nbandwidths <= id &&
|
|
|
|
VIR_EXPAND_N(mem_bw->bandwidths, mem_bw->nbandwidths,
|
|
|
|
id - mem_bw->nbandwidths + 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (mem_bw->bandwidths[id]) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Memory Bandwidth already defined for node %u"),
|
|
|
|
id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
mem_bw->bandwidths[id] = g_new0(unsigned int, 1);
|
2018-07-30 03:12:34 +00:00
|
|
|
*(mem_bw->bandwidths[id]) = memory_bandwidth;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:33 +00:00
|
|
|
/* virResctrlAllocForeachMemory
|
|
|
|
* @alloc: Pointer to an active allocation
|
|
|
|
* @cb: Callback function
|
|
|
|
* @opaque: Opaque data to be passed to @cb
|
|
|
|
*
|
|
|
|
* If available, traverse the defined memory bandwidth allocations and
|
|
|
|
* call the @cb function.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 and immediate failure if the @cb has any failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virResctrlAllocForeachMemory(virResctrlAllocPtr alloc,
|
|
|
|
virResctrlAllocForeachMemoryCallback cb,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
virResctrlAllocMemBWPtr mem_bw;
|
|
|
|
|
|
|
|
if (!alloc || !alloc->mem_bw)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mem_bw = alloc->mem_bw;
|
|
|
|
for (i = 0; i < mem_bw->nbandwidths; i++) {
|
|
|
|
if (mem_bw->bandwidths[i]) {
|
|
|
|
if (cb(i, *mem_bw->bandwidths[i], opaque) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 13:31:40 +00:00
|
|
|
static int
|
|
|
|
virResctrlSetID(char **resctrlid,
|
|
|
|
const char *id)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
|
|
|
if (!id) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2018-11-12 13:31:40 +00:00
|
|
|
_("New resctrl 'id' cannot be NULL"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*resctrlid) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Attempt to overwrite resctrlid='%s' with id='%s'"),
|
|
|
|
*resctrlid, id);
|
2017-10-16 15:17:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-20 10:55:05 +00:00
|
|
|
*resctrlid = g_strdup(id);
|
|
|
|
return 0;
|
2018-11-12 13:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virResctrlAllocSetID(virResctrlAllocPtr alloc,
|
|
|
|
const char *id)
|
|
|
|
{
|
|
|
|
return virResctrlSetID(&alloc->id, id);
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
virResctrlAllocGetID(virResctrlAllocPtr alloc)
|
|
|
|
{
|
|
|
|
return alloc->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:31 +00:00
|
|
|
/* Format the Memory Bandwidth Allocation line that will be found in
|
|
|
|
* the schemata files. The line should be start with "MB:" and be
|
|
|
|
* followed by "id=value" pairs separated by a semi-colon such as:
|
|
|
|
*
|
|
|
|
* MB:0=100;1=100
|
|
|
|
*
|
2020-07-09 04:42:21 +00:00
|
|
|
* which indicates node id 0 has 100 percent bandwidth and node id 1
|
2018-07-30 03:12:31 +00:00
|
|
|
* has 100 percent bandwidth. A trailing semi-colon is not formatted.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virResctrlAllocMemoryBandwidthFormat(virResctrlAllocPtr alloc,
|
|
|
|
virBufferPtr buf)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!alloc->mem_bw)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "MB:");
|
|
|
|
|
|
|
|
for (i = 0; i < alloc->mem_bw->nbandwidths; i++) {
|
|
|
|
if (alloc->mem_bw->bandwidths[i]) {
|
|
|
|
virBufferAsprintf(buf, "%zd=%u;", i,
|
|
|
|
*(alloc->mem_bw->bandwidths[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 19:26:38 +00:00
|
|
|
virBufferTrim(buf, ";");
|
2018-07-30 03:12:31 +00:00
|
|
|
virBufferAddChar(buf, '\n');
|
2019-10-24 12:34:44 +00:00
|
|
|
return 0;
|
2018-07-30 03:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virResctrlAllocParseProcessMemoryBandwidth(virResctrlInfoPtr resctrl,
|
|
|
|
virResctrlAllocPtr alloc,
|
|
|
|
char *mem_bw)
|
|
|
|
{
|
|
|
|
unsigned int bandwidth;
|
|
|
|
unsigned int id;
|
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
tmp = strchr(mem_bw, '=');
|
|
|
|
if (!tmp)
|
|
|
|
return 0;
|
|
|
|
*tmp = '\0';
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
if (virStrToLong_uip(mem_bw, NULL, 10, &id) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid node id %u "), id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (virStrToLong_uip(tmp, NULL, 10, &bandwidth) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid bandwidth %u"), bandwidth);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (bandwidth < resctrl->membw_info->min_bandwidth ||
|
|
|
|
id > resctrl->membw_info->max_id) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Missing or inconsistent resctrl info for "
|
|
|
|
"memory bandwidth node '%u'"), id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (alloc->mem_bw->nbandwidths <= id &&
|
|
|
|
VIR_EXPAND_N(alloc->mem_bw->bandwidths, alloc->mem_bw->nbandwidths,
|
|
|
|
id - alloc->mem_bw->nbandwidths + 1) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-10-05 17:06:36 +00:00
|
|
|
if (!alloc->mem_bw->bandwidths[id])
|
|
|
|
alloc->mem_bw->bandwidths[id] = g_new0(unsigned int, 1);
|
2018-07-30 03:12:31 +00:00
|
|
|
|
|
|
|
*(alloc->mem_bw->bandwidths[id]) = bandwidth;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse a schemata formatted MB: entry. Format details are described in
|
|
|
|
* virResctrlAllocMemoryBandwidthFormat.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virResctrlAllocParseMemoryBandwidthLine(virResctrlInfoPtr resctrl,
|
|
|
|
virResctrlAllocPtr alloc,
|
|
|
|
char *line)
|
|
|
|
{
|
|
|
|
char **mbs = NULL;
|
|
|
|
char *tmp = NULL;
|
|
|
|
size_t nmbs = 0;
|
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* For no reason there can be spaces */
|
|
|
|
virSkipSpaces((const char **) &line);
|
|
|
|
|
|
|
|
if (STRNEQLEN(line, "MB", 2))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!resctrl || !resctrl->membw_info ||
|
|
|
|
!resctrl->membw_info->min_bandwidth ||
|
|
|
|
!resctrl->membw_info->bandwidth_granularity) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing or inconsistent resctrl info for "
|
|
|
|
"memory bandwidth allocation"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
if (!alloc->mem_bw)
|
|
|
|
alloc->mem_bw = g_new0(virResctrlAllocMemBW, 1);
|
2018-07-30 03:12:31 +00:00
|
|
|
|
|
|
|
tmp = strchr(line, ':');
|
|
|
|
if (!tmp)
|
|
|
|
return 0;
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
mbs = virStringSplitCount(tmp, ";", 0, &nmbs);
|
|
|
|
for (i = 0; i < nmbs; i++) {
|
|
|
|
if (virResctrlAllocParseProcessMemoryBandwidth(resctrl, alloc, mbs[i]) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(mbs);
|
2018-07-30 03:12:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:27 +00:00
|
|
|
static int
|
|
|
|
virResctrlAllocFormatCache(virResctrlAllocPtr alloc,
|
|
|
|
virBufferPtr buf)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
|
|
|
unsigned int level = 0;
|
|
|
|
unsigned int type = 0;
|
|
|
|
unsigned int cache = 0;
|
|
|
|
|
2018-02-01 13:57:40 +00:00
|
|
|
for (level = 0; level < alloc->nlevels; level++) {
|
|
|
|
virResctrlAllocPerLevelPtr a_level = alloc->levels[level];
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (!a_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) {
|
|
|
|
virResctrlAllocPerTypePtr a_type = a_level->types[type];
|
|
|
|
|
|
|
|
if (!a_type)
|
|
|
|
continue;
|
|
|
|
|
2018-07-30 03:12:27 +00:00
|
|
|
virBufferAsprintf(buf, "L%u%s:", level, virResctrlTypeToString(type));
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
for (cache = 0; cache < a_type->nmasks; cache++) {
|
|
|
|
virBitmapPtr mask = a_type->masks[cache];
|
|
|
|
char *mask_str = NULL;
|
|
|
|
|
|
|
|
if (!mask)
|
|
|
|
continue;
|
|
|
|
|
2020-10-01 13:22:24 +00:00
|
|
|
mask_str = virBitmapToString(mask);
|
2018-07-30 03:12:27 +00:00
|
|
|
if (!mask_str)
|
|
|
|
return -1;
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2018-07-30 03:12:27 +00:00
|
|
|
virBufferAsprintf(buf, "%u=%s;", cache, mask_str);
|
2017-10-16 15:17:32 +00:00
|
|
|
VIR_FREE(mask_str);
|
|
|
|
}
|
|
|
|
|
2020-02-02 19:26:38 +00:00
|
|
|
virBufferTrim(buf, ";");
|
2018-07-30 03:12:27 +00:00
|
|
|
virBufferAddChar(buf, '\n');
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 12:34:44 +00:00
|
|
|
return 0;
|
2018-07-30 03:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
virResctrlAllocFormat(virResctrlAllocPtr alloc)
|
|
|
|
{
|
2020-07-03 02:30:20 +00:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2018-07-30 03:12:27 +00:00
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
return NULL;
|
|
|
|
|
2020-07-03 02:30:20 +00:00
|
|
|
if (virResctrlAllocFormatCache(alloc, &buf) < 0)
|
2018-07-30 03:12:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-07-03 02:30:20 +00:00
|
|
|
if (virResctrlAllocMemoryBandwidthFormat(alloc, &buf) < 0)
|
2018-07-30 03:12:31 +00:00
|
|
|
return NULL;
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virResctrlAllocParseProcessCache(virResctrlInfoPtr resctrl,
|
|
|
|
virResctrlAllocPtr alloc,
|
|
|
|
unsigned int level,
|
|
|
|
virCacheType type,
|
|
|
|
char *cache)
|
|
|
|
{
|
|
|
|
char *tmp = strchr(cache, '=');
|
|
|
|
unsigned int cache_id = 0;
|
|
|
|
virBitmapPtr mask = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*tmp = '\0';
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
if (virStrToLong_uip(cache, NULL, 10, &cache_id) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid cache id '%s'"), cache);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = virBitmapNewString(tmp);
|
|
|
|
if (!mask)
|
|
|
|
return -1;
|
|
|
|
|
2018-02-02 14:05:22 +00:00
|
|
|
if (!resctrl ||
|
|
|
|
level >= resctrl->nlevels ||
|
|
|
|
!resctrl->levels[level] ||
|
|
|
|
!resctrl->levels[level]->types[type]) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Missing or inconsistent resctrl info for "
|
2018-02-08 13:50:42 +00:00
|
|
|
"level '%u' type '%s'"),
|
2018-02-02 14:05:22 +00:00
|
|
|
level, virCacheTypeToString(type));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-02-05 12:50:44 +00:00
|
|
|
virBitmapShrink(mask, resctrl->levels[level]->types[type]->bits);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (virResctrlAllocUpdateMask(alloc, level, type, cache_id, mask) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virBitmapFree(mask);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2018-07-30 03:12:25 +00:00
|
|
|
virResctrlAllocParseCacheLine(virResctrlInfoPtr resctrl,
|
|
|
|
virResctrlAllocPtr alloc,
|
|
|
|
char *line)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
|
|
|
char **caches = NULL;
|
|
|
|
char *tmp = NULL;
|
|
|
|
unsigned int level = 0;
|
|
|
|
int type = -1;
|
|
|
|
size_t ncaches = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* For no reason there can be spaces */
|
|
|
|
virSkipSpaces((const char **) &line);
|
|
|
|
|
|
|
|
/* Skip lines that don't concern caches, e.g. MB: etc. */
|
|
|
|
if (line[0] != 'L')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* And lines that we can't parse too */
|
|
|
|
tmp = strchr(line, ':');
|
|
|
|
if (!tmp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*tmp = '\0';
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
if (virStrToLong_uip(line + 1, &line, 10, &level) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot parse resctrl schema level '%s'"),
|
|
|
|
line + 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = virResctrlTypeFromString(line);
|
|
|
|
if (type < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot parse resctrl schema level '%s'"),
|
|
|
|
line + 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
caches = virStringSplitCount(tmp, ";", 0, &ncaches);
|
|
|
|
if (!caches)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ncaches; i++) {
|
|
|
|
if (virResctrlAllocParseProcessCache(resctrl, alloc, level, type, caches[i]) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(caches);
|
2017-10-16 15:17:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virResctrlAllocParse(virResctrlInfoPtr resctrl,
|
|
|
|
virResctrlAllocPtr alloc,
|
|
|
|
const char *schemata)
|
|
|
|
{
|
|
|
|
char **lines = NULL;
|
|
|
|
size_t nlines = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
lines = virStringSplitCount(schemata, "\n", 0, &nlines);
|
|
|
|
for (i = 0; i < nlines; i++) {
|
2018-07-30 03:12:25 +00:00
|
|
|
if (virResctrlAllocParseCacheLine(resctrl, alloc, lines[i]) < 0)
|
2017-10-16 15:17:32 +00:00
|
|
|
goto cleanup;
|
2018-07-30 03:12:31 +00:00
|
|
|
if (virResctrlAllocParseMemoryBandwidthLine(resctrl, alloc, lines[i]) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(lines);
|
2017-10-16 15:17:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-25 16:03:43 +00:00
|
|
|
static int
|
|
|
|
virResctrlAllocGetGroup(virResctrlInfoPtr resctrl,
|
|
|
|
const char *groupname,
|
|
|
|
virResctrlAllocPtr *alloc)
|
|
|
|
{
|
|
|
|
char *schemata = NULL;
|
|
|
|
int rv = virFileReadValueString(&schemata,
|
2018-09-19 08:38:14 +00:00
|
|
|
SYSFS_RESCTRL_PATH "/%s/schemata",
|
|
|
|
groupname);
|
2018-01-25 16:03:43 +00:00
|
|
|
|
|
|
|
*alloc = NULL;
|
|
|
|
|
|
|
|
if (rv < 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
*alloc = virResctrlAllocNew();
|
|
|
|
if (!*alloc)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virResctrlAllocParse(resctrl, *alloc, schemata) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
VIR_FREE(schemata);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
VIR_FREE(schemata);
|
|
|
|
virObjectUnref(*alloc);
|
|
|
|
*alloc = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virResctrlAllocPtr
|
|
|
|
virResctrlAllocGetDefault(virResctrlInfoPtr resctrl)
|
|
|
|
{
|
|
|
|
virResctrlAllocPtr ret = NULL;
|
|
|
|
int rv = virResctrlAllocGetGroup(resctrl, ".", &ret);
|
|
|
|
|
|
|
|
if (rv == -2) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Could not read schemata file for the default group"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
static void
|
|
|
|
virResctrlAllocSubtractPerType(virResctrlAllocPerTypePtr dst,
|
|
|
|
virResctrlAllocPerTypePtr src)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if (!dst || !src)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < dst->nmasks && i < src->nmasks; i++) {
|
|
|
|
if (dst->masks[i] && src->masks[i])
|
|
|
|
virBitmapSubtract(dst->masks[i], src->masks[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virResctrlAllocSubtract(virResctrlAllocPtr dst,
|
|
|
|
virResctrlAllocPtr src)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
|
|
|
|
|
|
|
if (!src)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < dst->nlevels && i < src->nlevels; i++) {
|
|
|
|
if (dst->levels[i] && src->levels[i]) {
|
|
|
|
for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
|
|
|
|
virResctrlAllocSubtractPerType(dst->levels[i]->types[j],
|
|
|
|
src->levels[i]->types[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virResctrlAllocPtr
|
|
|
|
virResctrlAllocNewFromInfo(virResctrlInfoPtr info)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
|
|
|
size_t k = 0;
|
|
|
|
virResctrlAllocPtr ret = virResctrlAllocNew();
|
|
|
|
virBitmapPtr mask = NULL;
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < info->nlevels; i++) {
|
|
|
|
virResctrlInfoPerLevelPtr i_level = info->levels[i];
|
|
|
|
|
|
|
|
if (!i_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = 0; j < VIR_CACHE_TYPE_LAST; j++) {
|
|
|
|
virResctrlInfoPerTypePtr i_type = i_level->types[j];
|
|
|
|
|
|
|
|
if (!i_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
virBitmapFree(mask);
|
|
|
|
mask = virBitmapNew(i_type->bits);
|
|
|
|
virBitmapSetAll(mask);
|
|
|
|
|
|
|
|
for (k = 0; k <= i_type->max_cache_id; k++) {
|
|
|
|
if (virResctrlAllocUpdateMask(ret, i, j, k, mask) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:47:04 +00:00
|
|
|
/* set default free memory bandwidth to 100% */
|
2018-07-30 03:12:30 +00:00
|
|
|
if (info->membw_info) {
|
2020-10-05 17:06:36 +00:00
|
|
|
ret->mem_bw = g_new0(virResctrlAllocMemBW, 1);
|
2018-07-30 03:12:30 +00:00
|
|
|
|
|
|
|
if (VIR_EXPAND_N(ret->mem_bw->bandwidths, ret->mem_bw->nbandwidths,
|
|
|
|
info->membw_info->max_id + 1) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
for (i = 0; i < ret->mem_bw->nbandwidths; i++) {
|
2020-10-05 17:06:36 +00:00
|
|
|
ret->mem_bw->bandwidths[i] = g_new0(unsigned int, 1);
|
2018-07-30 03:12:30 +00:00
|
|
|
*(ret->mem_bw->bandwidths[i]) = 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
cleanup:
|
|
|
|
virBitmapFree(mask);
|
|
|
|
return ret;
|
|
|
|
error:
|
|
|
|
virObjectUnref(ret);
|
|
|
|
ret = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-03-11 09:23:10 +00:00
|
|
|
* This function creates an allocation that represents all unused parts of all
|
|
|
|
* caches in the system. It uses virResctrlInfo for creating a new full
|
|
|
|
* allocation with all bits set (using virResctrlAllocNewFromInfo()) and then
|
|
|
|
* scans for all allocations under /sys/fs/resctrl and subtracts each one of
|
|
|
|
* them from it. That way it can then return an allocation with only bit set
|
|
|
|
* being those that are not mentioned in any other allocation. It is used for
|
|
|
|
* two things, a) calculating the masks when creating allocations and b) from
|
|
|
|
* tests.
|
|
|
|
*
|
|
|
|
* MBA (Memory Bandwidth Allocation) is not taken into account as it is a
|
|
|
|
* limiting setting, not an allocating one. The way it works is also vastly
|
|
|
|
* different from CAT.
|
2017-10-16 15:17:32 +00:00
|
|
|
*/
|
|
|
|
virResctrlAllocPtr
|
|
|
|
virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
|
|
|
|
{
|
|
|
|
virResctrlAllocPtr ret = NULL;
|
|
|
|
virResctrlAllocPtr alloc = NULL;
|
|
|
|
struct dirent *ent = NULL;
|
|
|
|
DIR *dirp = NULL;
|
|
|
|
int rv = -1;
|
|
|
|
|
|
|
|
if (virResctrlInfoIsEmpty(resctrl)) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Resource control is not supported on this host"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = virResctrlAllocNewFromInfo(resctrl);
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
2018-01-25 16:03:43 +00:00
|
|
|
alloc = virResctrlAllocGetDefault(resctrl);
|
2017-10-16 15:17:32 +00:00
|
|
|
if (!alloc)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
virResctrlAllocSubtract(ret, alloc);
|
2018-01-25 16:03:43 +00:00
|
|
|
virObjectUnref(alloc);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (virDirOpen(&dirp, SYSFS_RESCTRL_PATH) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH)) > 0) {
|
|
|
|
if (STREQ(ent->d_name, "info"))
|
|
|
|
continue;
|
|
|
|
|
2018-01-25 16:03:43 +00:00
|
|
|
rv = virResctrlAllocGetGroup(resctrl, ent->d_name, &alloc);
|
2017-10-16 15:17:32 +00:00
|
|
|
if (rv == -2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not read schemata file for group %s"),
|
|
|
|
ent->d_name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
virResctrlAllocSubtract(ret, alloc);
|
2018-01-25 16:03:43 +00:00
|
|
|
virObjectUnref(alloc);
|
|
|
|
alloc = NULL;
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
if (rv < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnref(alloc);
|
|
|
|
VIR_DIR_CLOSE(dirp);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virObjectUnref(ret);
|
|
|
|
ret = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given the information about requested allocation type `a_type`, the host
|
|
|
|
* cache for a particular type `i_type` and unused bits in the system `f_type`
|
|
|
|
* this function tries to find the smallest free space in which the allocation
|
|
|
|
* for cache id `cache` would fit. We're looking for the smallest place in
|
|
|
|
* order to minimize fragmentation and maximize the possibility of succeeding.
|
2018-01-30 14:06:53 +00:00
|
|
|
*
|
|
|
|
* Per-cache allocation for the @level, @type and @cache must already be
|
|
|
|
* allocated for @alloc (does not have to exist though).
|
2017-10-16 15:17:32 +00:00
|
|
|
*/
|
|
|
|
static int
|
2018-01-30 14:06:53 +00:00
|
|
|
virResctrlAllocFindUnused(virResctrlAllocPtr alloc,
|
2017-10-16 15:17:32 +00:00
|
|
|
virResctrlInfoPerTypePtr i_type,
|
|
|
|
virResctrlAllocPerTypePtr f_type,
|
|
|
|
unsigned int level,
|
|
|
|
unsigned int type,
|
|
|
|
unsigned int cache)
|
|
|
|
{
|
2018-01-30 14:06:53 +00:00
|
|
|
unsigned long long *size = alloc->levels[level]->types[type]->sizes[cache];
|
2017-10-16 15:17:32 +00:00
|
|
|
virBitmapPtr a_mask = NULL;
|
|
|
|
virBitmapPtr f_mask = NULL;
|
|
|
|
unsigned long long need_bits;
|
|
|
|
size_t i = 0;
|
|
|
|
ssize_t pos = -1;
|
|
|
|
ssize_t last_bits = 0;
|
|
|
|
ssize_t last_pos = -1;
|
2018-01-30 14:06:53 +00:00
|
|
|
int ret = -1;
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2018-01-26 17:14:49 +00:00
|
|
|
if (!size)
|
2017-10-16 15:17:32 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cache >= f_type->nmasks) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Cache with id %u does not exists for level %d"),
|
|
|
|
cache, level);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
f_mask = f_type->masks[cache];
|
|
|
|
if (!f_mask) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Cache level %d id %u does not support tuning for "
|
|
|
|
"scope type '%s'"),
|
|
|
|
level, cache, virCacheTypeToString(type));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*size == i_type->size) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Cache allocation for the whole cache is not "
|
|
|
|
"possible, specify size smaller than %llu"),
|
|
|
|
i_type->size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
need_bits = *size / i_type->control.granularity;
|
|
|
|
|
|
|
|
if (*size % i_type->control.granularity) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Cache allocation of size %llu is not "
|
|
|
|
"divisible by granularity %llu"),
|
|
|
|
*size, i_type->control.granularity);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_bits < i_type->min_cbm_bits) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Cache allocation of size %llu is smaller "
|
|
|
|
"than the minimum allowed allocation %llu"),
|
|
|
|
*size,
|
|
|
|
i_type->control.granularity * i_type->min_cbm_bits);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pos = virBitmapNextSetBit(f_mask, pos)) >= 0) {
|
|
|
|
ssize_t pos_clear = virBitmapNextClearBit(f_mask, pos);
|
|
|
|
ssize_t bits;
|
|
|
|
|
|
|
|
if (pos_clear < 0)
|
|
|
|
pos_clear = virBitmapSize(f_mask);
|
|
|
|
|
|
|
|
bits = pos_clear - pos;
|
|
|
|
|
|
|
|
/* Not enough bits, move on and skip all of them */
|
|
|
|
if (bits < need_bits) {
|
|
|
|
pos = pos_clear;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This fits perfectly */
|
|
|
|
if (bits == need_bits) {
|
|
|
|
last_pos = pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember the smaller region if we already found on before */
|
|
|
|
if (last_pos < 0 || (last_bits && bits < last_bits)) {
|
|
|
|
last_bits = bits;
|
|
|
|
last_pos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = pos_clear;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_pos < 0) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Not enough room for allocation of "
|
|
|
|
"%llu bytes for level %u cache %u "
|
|
|
|
"scope type '%s'"),
|
|
|
|
*size, level, cache,
|
|
|
|
virCacheTypeToString(type));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
a_mask = virBitmapNew(i_type->bits);
|
|
|
|
|
2018-01-30 14:06:53 +00:00
|
|
|
for (i = last_pos; i < last_pos + need_bits; i++)
|
2017-10-16 15:17:32 +00:00
|
|
|
ignore_value(virBitmapSetBit(a_mask, i));
|
|
|
|
|
2018-01-30 14:06:53 +00:00
|
|
|
if (virResctrlAllocUpdateMask(alloc, level, type, cache, a_mask) < 0)
|
|
|
|
goto cleanup;
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2018-01-30 14:06:53 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virBitmapFree(a_mask);
|
|
|
|
return ret;
|
2017-10-16 15:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 03:12:32 +00:00
|
|
|
static int
|
|
|
|
virResctrlAllocMemoryBandwidth(virResctrlInfoPtr resctrl,
|
2019-03-11 09:23:10 +00:00
|
|
|
virResctrlAllocPtr alloc)
|
2018-07-30 03:12:32 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
virResctrlAllocMemBWPtr mem_bw_alloc = alloc->mem_bw;
|
|
|
|
virResctrlInfoMemBWPtr mem_bw_info = resctrl->membw_info;
|
|
|
|
|
|
|
|
if (!mem_bw_alloc)
|
|
|
|
return 0;
|
|
|
|
|
2019-02-27 14:03:10 +00:00
|
|
|
if (!mem_bw_info) {
|
2018-07-30 03:12:32 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("RDT Memory Bandwidth allocation unsupported"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < mem_bw_alloc->nbandwidths; i++) {
|
|
|
|
if (!mem_bw_alloc->bandwidths[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*(mem_bw_alloc->bandwidths[i]) % mem_bw_info->bandwidth_granularity) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Memory Bandwidth allocation of size "
|
|
|
|
"%u is not divisible by granularity %u"),
|
|
|
|
*(mem_bw_alloc->bandwidths[i]),
|
|
|
|
mem_bw_info->bandwidth_granularity);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*(mem_bw_alloc->bandwidths[i]) < mem_bw_info->min_bandwidth) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Memory Bandwidth allocation of size "
|
|
|
|
"%u is smaller than the minimum "
|
|
|
|
"allowed allocation %u"),
|
|
|
|
*(mem_bw_alloc->bandwidths[i]),
|
|
|
|
mem_bw_info->min_bandwidth);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (i > mem_bw_info->max_id) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("bandwidth controller id %zd does not "
|
|
|
|
"exist, max controller id %u"),
|
|
|
|
i, mem_bw_info->max_id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-11 10:13:32 +00:00
|
|
|
static int
|
|
|
|
virResctrlAllocCopyMemBW(virResctrlAllocPtr dst,
|
|
|
|
virResctrlAllocPtr src)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
virResctrlAllocMemBWPtr dst_bw = NULL;
|
|
|
|
virResctrlAllocMemBWPtr src_bw = src->mem_bw;
|
|
|
|
|
|
|
|
if (!src->mem_bw)
|
|
|
|
return 0;
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
if (!dst->mem_bw)
|
|
|
|
dst->mem_bw = g_new0(virResctrlAllocMemBW, 1);
|
2019-03-11 10:13:32 +00:00
|
|
|
|
|
|
|
dst_bw = dst->mem_bw;
|
|
|
|
|
|
|
|
if (src_bw->nbandwidths > dst_bw->nbandwidths &&
|
|
|
|
VIR_EXPAND_N(dst_bw->bandwidths, dst_bw->nbandwidths,
|
|
|
|
src_bw->nbandwidths - dst_bw->nbandwidths) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < src_bw->nbandwidths; i++) {
|
2019-03-12 08:53:48 +00:00
|
|
|
if (dst_bw->bandwidths[i])
|
2019-03-11 10:13:32 +00:00
|
|
|
continue;
|
2020-10-05 17:06:36 +00:00
|
|
|
dst_bw->bandwidths[i] = g_new0(unsigned int, 1);
|
2019-03-11 10:13:32 +00:00
|
|
|
*dst_bw->bandwidths[i] = *src_bw->bandwidths[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-26 17:14:49 +00:00
|
|
|
static int
|
|
|
|
virResctrlAllocCopyMasks(virResctrlAllocPtr dst,
|
|
|
|
virResctrlAllocPtr src)
|
|
|
|
{
|
|
|
|
unsigned int level = 0;
|
|
|
|
|
|
|
|
for (level = 0; level < src->nlevels; level++) {
|
|
|
|
virResctrlAllocPerLevelPtr s_level = src->levels[level];
|
|
|
|
unsigned int type = 0;
|
|
|
|
|
|
|
|
if (!s_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) {
|
|
|
|
virResctrlAllocPerTypePtr s_type = s_level->types[type];
|
|
|
|
virResctrlAllocPerTypePtr d_type = NULL;
|
|
|
|
unsigned int cache = 0;
|
|
|
|
|
|
|
|
if (!s_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
d_type = virResctrlAllocGetType(dst, level, type);
|
|
|
|
if (!d_type)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (cache = 0; cache < s_type->nmasks; cache++) {
|
|
|
|
virBitmapPtr mask = s_type->masks[cache];
|
|
|
|
|
|
|
|
if (mask && virResctrlAllocUpdateMask(dst, level, type, cache, mask) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
/*
|
2018-07-30 03:12:32 +00:00
|
|
|
* This function is called when creating an allocation in the system.
|
|
|
|
* What it does is that it gets all the unused resources using
|
|
|
|
* virResctrlAllocGetUnused and then tries to find a proper space for
|
|
|
|
* every requested allocation effectively transforming `sizes` into `masks`.
|
2017-10-16 15:17:32 +00:00
|
|
|
*/
|
|
|
|
static int
|
2018-07-30 03:12:25 +00:00
|
|
|
virResctrlAllocAssign(virResctrlInfoPtr resctrl,
|
|
|
|
virResctrlAllocPtr alloc)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
unsigned int level = 0;
|
|
|
|
virResctrlAllocPtr alloc_free = NULL;
|
2018-01-26 17:14:49 +00:00
|
|
|
virResctrlAllocPtr alloc_default = NULL;
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
alloc_free = virResctrlAllocGetUnused(resctrl);
|
|
|
|
if (!alloc_free)
|
|
|
|
return -1;
|
|
|
|
|
2018-01-26 17:14:49 +00:00
|
|
|
alloc_default = virResctrlAllocGetDefault(resctrl);
|
|
|
|
if (!alloc_default)
|
2018-02-01 13:19:42 +00:00
|
|
|
goto cleanup;
|
2018-01-26 17:14:49 +00:00
|
|
|
|
2019-03-11 09:23:10 +00:00
|
|
|
if (virResctrlAllocMemoryBandwidth(resctrl, alloc) < 0)
|
2018-07-30 03:12:32 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-01-26 17:14:49 +00:00
|
|
|
if (virResctrlAllocCopyMasks(alloc, alloc_default) < 0)
|
2018-02-01 13:19:42 +00:00
|
|
|
goto cleanup;
|
2018-01-26 17:14:49 +00:00
|
|
|
|
2019-03-11 10:13:32 +00:00
|
|
|
if (virResctrlAllocCopyMemBW(alloc, alloc_default) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
for (level = 0; level < alloc->nlevels; level++) {
|
|
|
|
virResctrlAllocPerLevelPtr a_level = alloc->levels[level];
|
|
|
|
virResctrlAllocPerLevelPtr f_level = NULL;
|
|
|
|
unsigned int type = 0;
|
|
|
|
|
|
|
|
if (!a_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (level < alloc_free->nlevels)
|
|
|
|
f_level = alloc_free->levels[level];
|
|
|
|
|
|
|
|
if (!f_level) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Cache level %d does not support tuning"),
|
|
|
|
level);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) {
|
|
|
|
virResctrlAllocPerTypePtr a_type = a_level->types[type];
|
|
|
|
virResctrlAllocPerTypePtr f_type = f_level->types[type];
|
|
|
|
unsigned int cache = 0;
|
|
|
|
|
|
|
|
if (!a_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!f_type) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Cache level %d does not support tuning for "
|
|
|
|
"scope type '%s'"),
|
|
|
|
level, virCacheTypeToString(type));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (cache = 0; cache < a_type->nsizes; cache++) {
|
|
|
|
virResctrlInfoPerLevelPtr i_level = resctrl->levels[level];
|
|
|
|
virResctrlInfoPerTypePtr i_type = i_level->types[type];
|
|
|
|
|
2018-01-30 14:06:53 +00:00
|
|
|
if (virResctrlAllocFindUnused(alloc, i_type, f_type, level, type, cache) < 0)
|
2017-10-16 15:17:32 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnref(alloc_free);
|
2018-01-26 17:14:49 +00:00
|
|
|
virObjectUnref(alloc_default);
|
2017-10-16 15:17:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 13:31:34 +00:00
|
|
|
static char *
|
|
|
|
virResctrlDeterminePath(const char *parentpath,
|
|
|
|
const char *prefix,
|
|
|
|
const char *id)
|
|
|
|
{
|
|
|
|
if (!id) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Resctrl ID must be set before determining resctrl "
|
|
|
|
"parentpath='%s' prefix='%s'"), parentpath, prefix);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-04 15:03:42 +00:00
|
|
|
return g_strdup_printf("%s/%s-%s", parentpath, prefix, id);
|
2018-11-12 13:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-30 22:57:42 +00:00
|
|
|
int
|
|
|
|
virResctrlAllocDeterminePath(virResctrlAllocPtr alloc,
|
|
|
|
const char *machinename)
|
|
|
|
{
|
2018-11-12 13:31:34 +00:00
|
|
|
if (alloc->path) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Resctrl allocation path is already set to '%s'"),
|
|
|
|
alloc->path);
|
2018-01-30 22:57:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-12 13:31:32 +00:00
|
|
|
/* If the allocation is empty, then the path will be SYSFS_RESCTRL_PATH */
|
|
|
|
if (virResctrlAllocIsEmpty(alloc)) {
|
2019-10-20 11:49:46 +00:00
|
|
|
alloc->path = g_strdup(SYSFS_RESCTRL_PATH);
|
2018-11-12 13:31:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-12 13:31:34 +00:00
|
|
|
alloc->path = virResctrlDeterminePath(SYSFS_RESCTRL_PATH,
|
|
|
|
machinename, alloc->id);
|
|
|
|
|
|
|
|
if (!alloc->path)
|
2018-01-30 22:57:42 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 13:31:38 +00:00
|
|
|
/* This function creates a resctrl directory in resource control file system,
|
|
|
|
* and the directory path is specified by @path. */
|
|
|
|
static int
|
|
|
|
virResctrlCreateGroupPath(const char *path)
|
|
|
|
{
|
|
|
|
/* Directory exists, return */
|
|
|
|
if (virFileExists(path))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (virFileMakePath(path) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot create resctrl directory '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
/* This checks if the directory for the alloc exists. If not it tries to create
|
|
|
|
* it and apply appropriate alloc settings. */
|
|
|
|
int
|
|
|
|
virResctrlAllocCreate(virResctrlInfoPtr resctrl,
|
|
|
|
virResctrlAllocPtr alloc,
|
|
|
|
const char *machinename)
|
|
|
|
{
|
|
|
|
char *schemata_path = NULL;
|
|
|
|
char *alloc_str = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
int lockfd = -1;
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
return 0;
|
|
|
|
|
2018-01-27 18:00:54 +00:00
|
|
|
if (virResctrlInfoIsEmpty(resctrl)) {
|
2017-10-16 15:17:32 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Resource control is not supported on this host"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-30 22:57:42 +00:00
|
|
|
if (virResctrlAllocDeterminePath(alloc, machinename) < 0)
|
2017-10-16 15:17:32 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-11-12 13:31:32 +00:00
|
|
|
/* If using the system/default path for the allocation, then we're done */
|
|
|
|
if (STREQ(alloc->path, SYSFS_RESCTRL_PATH))
|
|
|
|
return 0;
|
|
|
|
|
2020-07-29 11:02:01 +00:00
|
|
|
lockfd = virResctrlLock();
|
2017-10-16 15:17:32 +00:00
|
|
|
if (lockfd < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-07-30 03:12:25 +00:00
|
|
|
if (virResctrlAllocAssign(resctrl, alloc) < 0)
|
2017-10-16 15:17:32 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-12 13:31:38 +00:00
|
|
|
if (virResctrlCreateGroupPath(alloc->path) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
alloc_str = virResctrlAllocFormat(alloc);
|
|
|
|
if (!alloc_str)
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
schemata_path = g_strdup_printf("%s/schemata", alloc->path);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Writing resctrl schemata '%s' into '%s'", alloc_str, schemata_path);
|
|
|
|
if (virFileWriteStr(schemata_path, alloc_str, 0) < 0) {
|
|
|
|
rmdir(alloc->path);
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot write into schemata file '%s'"),
|
|
|
|
schemata_path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virResctrlUnlock(lockfd);
|
|
|
|
VIR_FREE(alloc_str);
|
|
|
|
VIR_FREE(schemata_path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 13:31:36 +00:00
|
|
|
static int
|
|
|
|
virResctrlAddPID(const char *path,
|
|
|
|
pid_t pid)
|
2017-10-16 15:17:32 +00:00
|
|
|
{
|
|
|
|
char *tasks = NULL;
|
|
|
|
char *pidstr = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-11-12 13:31:36 +00:00
|
|
|
if (!path) {
|
2017-10-16 15:17:32 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2018-11-12 13:31:36 +00:00
|
|
|
_("Cannot add pid to non-existing resctrl group"));
|
2017-10-16 15:17:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
tasks = g_strdup_printf("%s/tasks", path);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
pidstr = g_strdup_printf("%lld", (long long int)pid);
|
2017-10-16 15:17:32 +00:00
|
|
|
|
|
|
|
if (virFileWriteStr(tasks, pidstr, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot write pid in tasks file '%s'"),
|
|
|
|
tasks);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(tasks);
|
|
|
|
VIR_FREE(pidstr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 13:31:36 +00:00
|
|
|
int
|
|
|
|
virResctrlAllocAddPID(virResctrlAllocPtr alloc,
|
|
|
|
pid_t pid)
|
|
|
|
{
|
|
|
|
/* If the allocation is empty, then it is impossible to add a PID to
|
|
|
|
* allocation due to lacking of its 'tasks' file so just return */
|
|
|
|
if (virResctrlAllocIsEmpty(alloc))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return virResctrlAddPID(alloc->path, pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
int
|
|
|
|
virResctrlAllocRemove(virResctrlAllocPtr alloc)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!alloc->path)
|
|
|
|
return 0;
|
|
|
|
|
2018-11-12 13:31:32 +00:00
|
|
|
/* Do not destroy if path is the system/default path for the allocation */
|
|
|
|
if (STREQ(alloc->path, SYSFS_RESCTRL_PATH))
|
|
|
|
return 0;
|
|
|
|
|
2017-10-16 15:17:32 +00:00
|
|
|
VIR_DEBUG("Removing resctrl allocation %s", alloc->path);
|
|
|
|
if (rmdir(alloc->path) != 0 && errno != ENOENT) {
|
|
|
|
ret = -errno;
|
|
|
|
VIR_ERROR(_("Unable to remove %s (%d)"), alloc->path, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-11-12 13:31:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* virResctrlMonitor-related definitions */
|
|
|
|
|
|
|
|
virResctrlMonitorPtr
|
|
|
|
virResctrlMonitorNew(void)
|
|
|
|
{
|
|
|
|
if (virResctrlInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virObjectNew(virResctrlMonitorClass);
|
|
|
|
}
|
2018-11-12 13:31:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virResctrlMonitorDeterminePath
|
|
|
|
*
|
|
|
|
* @monitor: Pointer to a resctrl monitor
|
|
|
|
* @machinename: Name string of the VM
|
|
|
|
*
|
|
|
|
* Determines the directory path that the underlying resctrl group will be
|
|
|
|
* created with.
|
|
|
|
*
|
|
|
|
* A monitor represents a directory under resource control file system,
|
|
|
|
* its directory path could be the same path as @monitor->alloc, could be a
|
|
|
|
* path of directory under 'mon_groups' of @monitor->alloc, or a path of
|
|
|
|
* directory under '/sys/fs/resctrl/mon_groups' if @monitor->alloc is NULL.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virResctrlMonitorDeterminePath(virResctrlMonitorPtr monitor,
|
|
|
|
const char *machinename)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *parentpath = NULL;
|
2018-11-12 13:31:35 +00:00
|
|
|
|
|
|
|
if (!monitor) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Invalid resctrl monitor"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!monitor->alloc) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing resctrl monitor alloc"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (monitor->path) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Resctrl monitor path is already set to '%s'"),
|
|
|
|
monitor->path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-11 03:31:06 +00:00
|
|
|
if (!virResctrlAllocIsEmpty(monitor->alloc) &&
|
|
|
|
STREQ_NULLABLE(monitor->id, monitor->alloc->id)) {
|
2019-10-20 11:49:46 +00:00
|
|
|
monitor->path = g_strdup(monitor->alloc->path);
|
2018-11-12 13:31:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
parentpath = g_strdup_printf("%s/mon_groups", monitor->alloc->path);
|
2018-11-12 13:31:35 +00:00
|
|
|
|
|
|
|
monitor->path = virResctrlDeterminePath(parentpath, machinename,
|
|
|
|
monitor->id);
|
|
|
|
if (!monitor->path)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-12 13:31:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virResctrlMonitorAddPID(virResctrlMonitorPtr monitor,
|
|
|
|
pid_t pid)
|
|
|
|
{
|
|
|
|
return virResctrlAddPID(monitor->path, pid);
|
|
|
|
}
|
2018-11-12 13:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virResctrlMonitorCreate(virResctrlMonitorPtr monitor,
|
|
|
|
const char *machinename)
|
|
|
|
{
|
|
|
|
int lockfd = -1;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!monitor)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (virResctrlMonitorDeterminePath(monitor, machinename) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2020-07-29 11:02:01 +00:00
|
|
|
lockfd = virResctrlLock();
|
2018-11-12 13:31:39 +00:00
|
|
|
if (lockfd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = virResctrlCreateGroupPath(monitor->path);
|
|
|
|
|
|
|
|
virResctrlUnlock(lockfd);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-11-12 13:31:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virResctrlMonitorSetID(virResctrlMonitorPtr monitor,
|
|
|
|
const char *id)
|
|
|
|
|
|
|
|
{
|
|
|
|
return virResctrlSetID(&monitor->id, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
virResctrlMonitorGetID(virResctrlMonitorPtr monitor)
|
|
|
|
{
|
|
|
|
return monitor->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virResctrlMonitorSetAlloc(virResctrlMonitorPtr monitor,
|
|
|
|
virResctrlAllocPtr alloc)
|
|
|
|
{
|
|
|
|
monitor->alloc = virObjectRef(alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virResctrlMonitorRemove(virResctrlMonitorPtr monitor)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!monitor->path)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (STREQ(monitor->path, monitor->alloc->path))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
VIR_DEBUG("Removing resctrl monitor path=%s", monitor->path);
|
|
|
|
if (rmdir(monitor->path) != 0 && errno != ENOENT) {
|
|
|
|
ret = -errno;
|
|
|
|
VIR_ERROR(_("Unable to remove %s (%d)"), monitor->path, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virResctrlMonitorStatsSorter(const void *a,
|
|
|
|
const void *b)
|
|
|
|
{
|
2019-05-10 15:21:47 +00:00
|
|
|
return (*(virResctrlMonitorStatsPtr *)a)->id
|
|
|
|
- (*(virResctrlMonitorStatsPtr *)b)->id;
|
2018-11-12 13:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virResctrlMonitorGetStats
|
|
|
|
*
|
|
|
|
* @monitor: The monitor that the statistic data will be retrieved from.
|
2019-06-11 03:31:14 +00:00
|
|
|
* @resources: A string list for the monitor feature names.
|
2018-11-26 17:56:14 +00:00
|
|
|
* @stats: Pointer of of virResctrlMonitorStatsPtr array for holding cache or
|
|
|
|
* memory bandwidth usage data.
|
2018-11-12 13:31:41 +00:00
|
|
|
* @nstats: A size_t pointer to hold the returned array length of @stats
|
|
|
|
*
|
|
|
|
* Get cache or memory bandwidth utilization information.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
2019-06-11 03:31:14 +00:00
|
|
|
int
|
2018-11-12 13:31:41 +00:00
|
|
|
virResctrlMonitorGetStats(virResctrlMonitorPtr monitor,
|
2019-06-11 03:31:14 +00:00
|
|
|
const char **resources,
|
2018-11-26 17:56:14 +00:00
|
|
|
virResctrlMonitorStatsPtr **stats,
|
2018-11-12 13:31:41 +00:00
|
|
|
size_t *nstats)
|
|
|
|
{
|
|
|
|
int rv = -1;
|
|
|
|
int ret = -1;
|
2019-06-11 03:31:14 +00:00
|
|
|
size_t i = 0;
|
2020-01-02 10:45:04 +00:00
|
|
|
unsigned long long val = 0;
|
2018-11-12 13:31:41 +00:00
|
|
|
DIR *dirp = NULL;
|
|
|
|
char *datapath = NULL;
|
2018-11-20 02:10:39 +00:00
|
|
|
char *filepath = NULL;
|
2018-11-12 13:31:41 +00:00
|
|
|
struct dirent *ent = NULL;
|
|
|
|
virResctrlMonitorStatsPtr stat = NULL;
|
|
|
|
|
|
|
|
if (!monitor) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Invalid resctrl monitor"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
datapath = g_strdup_printf("%s/mon_data", monitor->path);
|
2018-11-12 13:31:41 +00:00
|
|
|
|
|
|
|
if (virDirOpen(&dirp, datapath) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
*nstats = 0;
|
|
|
|
while (virDirRead(dirp, &ent, datapath) > 0) {
|
|
|
|
char *node_id = NULL;
|
|
|
|
|
2018-11-20 02:10:39 +00:00
|
|
|
VIR_FREE(filepath);
|
|
|
|
|
2018-11-12 13:31:41 +00:00
|
|
|
/* Looking for directory that contains resource utilization
|
|
|
|
* information file. The directory name is arranged in format
|
|
|
|
* "mon_<node_name>_<node_id>". For example, "mon_L3_00" and
|
|
|
|
* "mon_L3_01" are two target directories for a two nodes system
|
|
|
|
* with resource utilization data file for each node respectively.
|
|
|
|
*/
|
2019-10-22 13:26:14 +00:00
|
|
|
filepath = g_strdup_printf("%s/%s", datapath, ent->d_name);
|
2018-11-20 02:10:39 +00:00
|
|
|
|
|
|
|
if (!virFileIsDir(filepath))
|
2018-11-12 13:31:41 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Looking for directory has a prefix 'mon_L' */
|
|
|
|
if (!(node_id = STRSKIP(ent->d_name, "mon_L")))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Looking for directory has another '_' */
|
|
|
|
node_id = strchr(node_id, '_');
|
|
|
|
if (!node_id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Skip the character '_' */
|
|
|
|
if (!(node_id = STRSKIP(node_id, "_")))
|
|
|
|
continue;
|
|
|
|
|
2020-10-05 17:06:36 +00:00
|
|
|
stat = g_new0(virResctrlMonitorStats, 1);
|
2018-11-14 19:01:06 +00:00
|
|
|
|
2018-11-12 13:31:41 +00:00
|
|
|
/* The node ID number should be here, parsing it. */
|
|
|
|
if (virStrToLong_uip(node_id, NULL, 0, &stat->id) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-06-11 03:31:14 +00:00
|
|
|
for (i = 0; resources[i]; i++) {
|
2020-01-02 10:45:04 +00:00
|
|
|
rv = virFileReadValueUllong(&val, "%s/%s/%s", datapath,
|
|
|
|
ent->d_name, resources[i]);
|
2019-06-11 03:31:14 +00:00
|
|
|
if (rv == -2) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("File '%s/%s/%s' does not exist."),
|
|
|
|
datapath, ent->d_name, resources[i]);
|
|
|
|
}
|
|
|
|
if (rv < 0)
|
|
|
|
goto cleanup;
|
2018-11-12 13:31:41 +00:00
|
|
|
|
2019-06-11 03:31:14 +00:00
|
|
|
if (VIR_APPEND_ELEMENT(stat->vals, stat->nvals, val) < 0)
|
|
|
|
goto cleanup;
|
2019-06-11 03:31:13 +00:00
|
|
|
|
2019-06-11 03:31:14 +00:00
|
|
|
if (virStringListAdd(&stat->features, resources[i]) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-06-11 03:31:13 +00:00
|
|
|
|
2018-11-26 17:56:14 +00:00
|
|
|
if (VIR_APPEND_ELEMENT(*stats, *nstats, stat) < 0)
|
2018-11-12 13:31:41 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort in id's ascending order */
|
|
|
|
if (*nstats)
|
2019-05-10 15:21:47 +00:00
|
|
|
qsort(*stats, *nstats, sizeof(**stats), virResctrlMonitorStatsSorter);
|
2018-11-12 13:31:41 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(datapath);
|
2018-11-20 02:10:39 +00:00
|
|
|
VIR_FREE(filepath);
|
2019-06-11 03:31:12 +00:00
|
|
|
virResctrlMonitorStatsFree(stat);
|
2018-11-12 13:31:41 +00:00
|
|
|
VIR_DIR_CLOSE(dirp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-26 17:56:15 +00:00
|
|
|
void
|
2019-06-11 03:31:12 +00:00
|
|
|
virResctrlMonitorStatsFree(virResctrlMonitorStatsPtr stat)
|
2018-11-26 17:56:15 +00:00
|
|
|
{
|
2019-06-11 03:31:12 +00:00
|
|
|
if (!stat)
|
2018-11-26 17:56:15 +00:00
|
|
|
return;
|
|
|
|
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(stat->features);
|
2019-06-11 03:31:13 +00:00
|
|
|
VIR_FREE(stat->vals);
|
2019-06-11 03:31:12 +00:00
|
|
|
VIR_FREE(stat);
|
2018-11-26 17:56:15 +00:00
|
|
|
}
|