提交 089c81b0 编写于 作者: M Martin Kletzander

util: Reorder parts of virresctrl

Move description of the purpose of the file before any definition.

One empty line between related enum definitions.

All typedefs before all structs.  This is exception from the usual, but not the
only one, we already have something similar for some other structs.  This way we
can move contents between structs and reorder some parts nicely without moving
all definitions of one type before another one just so it's defined.

Define all classes in one place.

Have one initialization function for all classes in the file.
Signed-off-by: NMartin Kletzander <mkletzan@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
上级 657ddeff
...@@ -37,21 +37,22 @@ ...@@ -37,21 +37,22 @@
VIR_LOG_INIT("util.virresctrl") VIR_LOG_INIT("util.virresctrl")
/* Common definitions */
#define SYSFS_RESCTRL_PATH "/sys/fs/resctrl"
/* Resctrl is short for Resource Control. It might be implemented for various /* Resctrl is short for Resource Control. It might be implemented for various
* resources, but at the time of this writing this is only supported for cache * resources, but at the time of this writing this is only supported for cache
* allocation technology (aka CAT). Hence the reson for leaving 'Cache' out of * allocation technology (aka CAT). Hence the reson for leaving 'Cache' out of
* all the structure and function names for now (can be added later if needed. * all the structure and function names for now (can be added later if needed.
*/ */
/* Common definitions */
#define SYSFS_RESCTRL_PATH "/sys/fs/resctrl"
/* Our naming for cache types and scopes */ /* Our naming for cache types and scopes */
VIR_ENUM_IMPL(virCache, VIR_CACHE_TYPE_LAST, VIR_ENUM_IMPL(virCache, VIR_CACHE_TYPE_LAST,
"both", "both",
"code", "code",
"data") "data")
/* /*
* This is the same enum, but for the resctrl naming * This is the same enum, but for the resctrl naming
* of the type (L<level><type>) * of the type (L<level><type>)
...@@ -63,9 +64,28 @@ VIR_ENUM_IMPL(virResctrl, VIR_CACHE_TYPE_LAST, ...@@ -63,9 +64,28 @@ VIR_ENUM_IMPL(virResctrl, VIR_CACHE_TYPE_LAST,
"DATA") "DATA")
/* Info-related definitions and InfoClass-related functions */ /* 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. */
typedef struct _virResctrlInfoPerType virResctrlInfoPerType; typedef struct _virResctrlInfoPerType virResctrlInfoPerType;
typedef virResctrlInfoPerType *virResctrlInfoPerTypePtr; typedef virResctrlInfoPerType *virResctrlInfoPerTypePtr;
typedef struct _virResctrlInfoPerLevel virResctrlInfoPerLevel;
typedef virResctrlInfoPerLevel *virResctrlInfoPerLevelPtr;
typedef struct _virResctrlAllocPerType virResctrlAllocPerType;
typedef virResctrlAllocPerType *virResctrlAllocPerTypePtr;
typedef struct _virResctrlAllocPerLevel virResctrlAllocPerLevel;
typedef virResctrlAllocPerLevel *virResctrlAllocPerLevelPtr;
/* Class definitions and initializations */
static virClassPtr virResctrlInfoClass;
static virClassPtr virResctrlAllocClass;
/* virResctrlInfo */
struct _virResctrlInfoPerType { struct _virResctrlInfoPerType {
/* Kernel-provided information */ /* Kernel-provided information */
char *cbm_mask; char *cbm_mask;
...@@ -86,8 +106,6 @@ struct _virResctrlInfoPerType { ...@@ -86,8 +106,6 @@ struct _virResctrlInfoPerType {
virResctrlInfoPerCache control; virResctrlInfoPerCache control;
}; };
typedef struct _virResctrlInfoPerLevel virResctrlInfoPerLevel;
typedef virResctrlInfoPerLevel *virResctrlInfoPerLevelPtr;
struct _virResctrlInfoPerLevel { struct _virResctrlInfoPerLevel {
virResctrlInfoPerTypePtr *types; virResctrlInfoPerTypePtr *types;
}; };
...@@ -99,7 +117,6 @@ struct _virResctrlInfo { ...@@ -99,7 +117,6 @@ struct _virResctrlInfo {
size_t nlevels; size_t nlevels;
}; };
static virClassPtr virResctrlInfoClass;
static void static void
virResctrlInfoDispose(void *obj) virResctrlInfoDispose(void *obj)
...@@ -130,30 +147,7 @@ virResctrlInfoDispose(void *obj) ...@@ -130,30 +147,7 @@ virResctrlInfoDispose(void *obj)
} }
static int /* virResctrlAlloc */
virResctrlInfoOnceInit(void)
{
if (!VIR_CLASS_NEW(virResctrlInfo, virClassForObject()))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virResctrlInfo)
virResctrlInfoPtr
virResctrlInfoNew(void)
{
if (virResctrlInfoInitialize() < 0)
return NULL;
return virObjectNew(virResctrlInfoClass);
}
/* Alloc-related definitions and AllocClass-related functions */
/* /*
* virResctrlAlloc represents one allocation (in XML under cputune/cachetune and * virResctrlAlloc represents one allocation (in XML under cputune/cachetune and
...@@ -186,8 +180,6 @@ virResctrlInfoNew(void) ...@@ -186,8 +180,6 @@ virResctrlInfoNew(void)
* virBitmaps named `masks` indexed the same way as `sizes`. The upper bounds * virBitmaps named `masks` indexed the same way as `sizes`. The upper bounds
* of the sparse arrays are stored in nmasks or nsizes, respectively. * of the sparse arrays are stored in nmasks or nsizes, respectively.
*/ */
typedef struct _virResctrlAllocPerType virResctrlAllocPerType;
typedef virResctrlAllocPerType *virResctrlAllocPerTypePtr;
struct _virResctrlAllocPerType { struct _virResctrlAllocPerType {
/* There could be bool saying whether this is set or not, but since everything /* 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 * in virResctrlAlloc (and most of libvirt) goes with pointer arrays we would
...@@ -201,8 +193,6 @@ struct _virResctrlAllocPerType { ...@@ -201,8 +193,6 @@ struct _virResctrlAllocPerType {
size_t nmasks; size_t nmasks;
}; };
typedef struct _virResctrlAllocPerLevel virResctrlAllocPerLevel;
typedef virResctrlAllocPerLevel *virResctrlAllocPerLevelPtr;
struct _virResctrlAllocPerLevel { struct _virResctrlAllocPerLevel {
virResctrlAllocPerTypePtr *types; /* Indexed with enum virCacheType */ virResctrlAllocPerTypePtr *types; /* Indexed with enum virCacheType */
/* There is no `ntypes` member variable as it is always allocated for /* There is no `ntypes` member variable as it is always allocated for
...@@ -222,7 +212,6 @@ struct _virResctrlAlloc { ...@@ -222,7 +212,6 @@ struct _virResctrlAlloc {
char *path; char *path;
}; };
static virClassPtr virResctrlAllocClass;
static void static void
virResctrlAllocDispose(void *obj) virResctrlAllocDispose(void *obj)
...@@ -265,27 +254,20 @@ virResctrlAllocDispose(void *obj) ...@@ -265,27 +254,20 @@ virResctrlAllocDispose(void *obj)
} }
/* Global initialization for classes */
static int static int
virResctrlAllocOnceInit(void) virResctrlOnceInit(void)
{ {
if (!VIR_CLASS_NEW(virResctrlInfo, virClassForObject()))
return -1;
if (!VIR_CLASS_NEW(virResctrlAlloc, virClassForObject())) if (!VIR_CLASS_NEW(virResctrlAlloc, virClassForObject()))
return -1; return -1;
return 0; return 0;
} }
VIR_ONCE_GLOBAL_INIT(virResctrl)
VIR_ONCE_GLOBAL_INIT(virResctrlAlloc)
virResctrlAllocPtr
virResctrlAllocNew(void)
{
if (virResctrlAllocInitialize() < 0)
return NULL;
return virObjectNew(virResctrlAllocClass);
}
/* Common functions */ /* Common functions */
...@@ -331,32 +313,7 @@ virResctrlUnlock(int fd) ...@@ -331,32 +313,7 @@ virResctrlUnlock(int fd)
} }
/* Info-related functions */ /* virResctrlInfo-related definitions */
static bool
virResctrlInfoIsEmpty(virResctrlInfoPtr resctrl)
{
size_t i = 0;
size_t j = 0;
if (!resctrl)
return true;
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;
}
int int
virResctrlGetInfo(virResctrlInfoPtr resctrl) virResctrlGetInfo(virResctrlInfoPtr resctrl)
{ {
...@@ -488,6 +445,41 @@ virResctrlGetInfo(virResctrlInfoPtr resctrl) ...@@ -488,6 +445,41 @@ virResctrlGetInfo(virResctrlInfoPtr resctrl)
} }
virResctrlInfoPtr
virResctrlInfoNew(void)
{
if (virResctrlInitialize() < 0)
return NULL;
return virObjectNew(virResctrlInfoClass);
}
static bool
virResctrlInfoIsEmpty(virResctrlInfoPtr resctrl)
{
size_t i = 0;
size_t j = 0;
if (!resctrl)
return true;
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;
}
int int
virResctrlInfoGetCache(virResctrlInfoPtr resctrl, virResctrlInfoGetCache(virResctrlInfoPtr resctrl,
unsigned int level, unsigned int level,
...@@ -552,7 +544,17 @@ virResctrlInfoGetCache(virResctrlInfoPtr resctrl, ...@@ -552,7 +544,17 @@ virResctrlInfoGetCache(virResctrlInfoPtr resctrl,
} }
/* Alloc-related functions */ /* virResctrlAlloc-related definitions */
virResctrlAllocPtr
virResctrlAllocNew(void)
{
if (virResctrlInitialize() < 0)
return NULL;
return virObjectNew(virResctrlAllocClass);
}
bool bool
virResctrlAllocIsEmpty(virResctrlAllocPtr alloc) virResctrlAllocIsEmpty(virResctrlAllocPtr alloc)
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册