mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
conf: Refactor virDomainNumaDefCPUParseXML
Rewrite the function to save a few local variables and reorder the code to make more sense. Additionally the ncells_max member of the virCPUDef structure is used only for tracking allocation when parsing the numa definition, which can be avoided by switching to VIR_ALLOC_N as the array is not resized after initial allocation.
This commit is contained in:
@@ -154,7 +154,6 @@ virCPUDefCopy(const virCPUDef *cpu)
|
|||||||
if (cpu->ncells) {
|
if (cpu->ncells) {
|
||||||
if (VIR_ALLOC_N(copy->cells, cpu->ncells) < 0)
|
if (VIR_ALLOC_N(copy->cells, cpu->ncells) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
copy->ncells_max = copy->ncells = cpu->ncells;
|
|
||||||
|
|
||||||
for (i = 0; i < cpu->ncells; i++) {
|
for (i = 0; i < cpu->ncells; i++) {
|
||||||
copy->cells[i].mem = cpu->cells[i].mem;
|
copy->cells[i].mem = cpu->cells[i].mem;
|
||||||
|
|||||||
@@ -127,7 +127,6 @@ struct _virCPUDef {
|
|||||||
size_t nfeatures_max;
|
size_t nfeatures_max;
|
||||||
virCPUFeatureDefPtr features;
|
virCPUFeatureDefPtr features;
|
||||||
size_t ncells;
|
size_t ncells;
|
||||||
size_t ncells_max;
|
|
||||||
virCellDefPtr cells;
|
virCellDefPtr cells;
|
||||||
unsigned int cells_cpus;
|
unsigned int cells_cpus;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -688,45 +688,36 @@ virDomainNumaDefCPUParseXML(virCPUDefPtr def,
|
|||||||
{
|
{
|
||||||
xmlNodePtr *nodes = NULL;
|
xmlNodePtr *nodes = NULL;
|
||||||
xmlNodePtr oldNode = ctxt->node;
|
xmlNodePtr oldNode = ctxt->node;
|
||||||
|
char *tmp = NULL;
|
||||||
int n;
|
int n;
|
||||||
size_t i;
|
size_t i;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
if (virXPathNode("/domain/cpu/numa[1]", ctxt)) {
|
/* check if NUMA definition is present */
|
||||||
VIR_FREE(nodes);
|
if (!virXPathNode("/domain/cpu/numa[1]", ctxt))
|
||||||
|
return 0;
|
||||||
|
|
||||||
n = virXPathNodeSet("/domain/cpu/numa[1]/cell", ctxt, &nodes);
|
if ((n = virXPathNodeSet("/domain/cpu/numa[1]/cell", ctxt, &nodes)) <= 0) {
|
||||||
if (n <= 0) {
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
||||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
_("NUMA topology defined without NUMA cells"));
|
||||||
_("NUMA topology defined without NUMA cells"));
|
goto cleanup;
|
||||||
goto error;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (VIR_RESIZE_N(def->cells, def->ncells_max,
|
if (VIR_ALLOC_N(def->cells, n) < 0)
|
||||||
def->ncells, n) < 0)
|
goto cleanup;
|
||||||
goto error;
|
def->ncells = n;
|
||||||
|
|
||||||
def->ncells = n;
|
for (i = 0; i < n; i++) {
|
||||||
|
int rc, ncpus = 0;
|
||||||
|
unsigned int cur_cell = i;
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
/* cells are in order of parsing or explicitly numbered */
|
||||||
char *cpus, *memAccessStr;
|
if ((tmp = virXMLPropString(nodes[i], "id"))) {
|
||||||
int rc, ncpus = 0;
|
if (virStrToLong_uip(tmp, NULL, 10, &cur_cell) < 0) {
|
||||||
unsigned int cur_cell;
|
virReportError(VIR_ERR_XML_ERROR,
|
||||||
char *tmp = NULL;
|
_("Invalid 'id' attribute in NUMA cell: '%s'"),
|
||||||
|
tmp);
|
||||||
tmp = virXMLPropString(nodes[i], "id");
|
goto cleanup;
|
||||||
if (!tmp) {
|
|
||||||
cur_cell = i;
|
|
||||||
} else {
|
|
||||||
rc = virStrToLong_ui(tmp, NULL, 10, &cur_cell);
|
|
||||||
if (rc == -1) {
|
|
||||||
virReportError(VIR_ERR_XML_ERROR,
|
|
||||||
_("Invalid 'id' attribute in NUMA cell: %s"),
|
|
||||||
tmp);
|
|
||||||
VIR_FREE(tmp);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
VIR_FREE(tmp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cur_cell >= n) {
|
if (cur_cell >= n) {
|
||||||
@@ -734,60 +725,57 @@ virDomainNumaDefCPUParseXML(virCPUDefPtr def,
|
|||||||
_("Exactly one 'cell' element per guest "
|
_("Exactly one 'cell' element per guest "
|
||||||
"NUMA cell allowed, non-contiguous ranges or "
|
"NUMA cell allowed, non-contiguous ranges or "
|
||||||
"ranges not starting from 0 are not allowed"));
|
"ranges not starting from 0 are not allowed"));
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (def->cells[cur_cell].cpustr) {
|
|
||||||
virReportError(VIR_ERR_XML_ERROR,
|
|
||||||
_("Duplicate NUMA cell info for cell id '%u'"),
|
|
||||||
cur_cell);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
cpus = virXMLPropString(nodes[i], "cpus");
|
|
||||||
if (!cpus) {
|
|
||||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
||||||
_("Missing 'cpus' attribute in NUMA cell"));
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
def->cells[cur_cell].cpustr = cpus;
|
|
||||||
|
|
||||||
ncpus = virBitmapParse(cpus, 0, &def->cells[cur_cell].cpumask,
|
|
||||||
VIR_DOMAIN_CPUMASK_LEN);
|
|
||||||
if (ncpus <= 0)
|
|
||||||
goto error;
|
|
||||||
def->cells_cpus += ncpus;
|
|
||||||
|
|
||||||
ctxt->node = nodes[i];
|
|
||||||
if (virDomainParseMemory("./@memory", "./@unit", ctxt,
|
|
||||||
&def->cells[cur_cell].mem, true, false) < 0)
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
memAccessStr = virXMLPropString(nodes[i], "memAccess");
|
|
||||||
if (memAccessStr) {
|
|
||||||
rc = virMemAccessTypeFromString(memAccessStr);
|
|
||||||
|
|
||||||
if (rc <= 0) {
|
|
||||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
||||||
_("Invalid 'memAccess' attribute "
|
|
||||||
"value '%s'"),
|
|
||||||
memAccessStr);
|
|
||||||
VIR_FREE(memAccessStr);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
def->cells[cur_cell].memAccess = rc;
|
|
||||||
|
|
||||||
VIR_FREE(memAccessStr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
VIR_FREE(tmp);
|
||||||
|
|
||||||
|
if (def->cells[cur_cell].cpumask) {
|
||||||
|
virReportError(VIR_ERR_XML_ERROR,
|
||||||
|
_("Duplicate NUMA cell info for cell id '%u'"),
|
||||||
|
cur_cell);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(tmp = virXMLPropString(nodes[i], "cpus"))) {
|
||||||
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
||||||
|
_("Missing 'cpus' attribute in NUMA cell"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ncpus = virBitmapParse(tmp, 0, &def->cells[cur_cell].cpumask,
|
||||||
|
VIR_DOMAIN_CPUMASK_LEN);
|
||||||
|
|
||||||
|
if (ncpus <= 0)
|
||||||
|
goto cleanup;
|
||||||
|
def->cells_cpus += ncpus;
|
||||||
|
|
||||||
|
def->cells[cur_cell].cpustr = tmp;
|
||||||
|
tmp = NULL;
|
||||||
|
|
||||||
|
ctxt->node = nodes[i];
|
||||||
|
if (virDomainParseMemory("./@memory", "./@unit", ctxt,
|
||||||
|
&def->cells[cur_cell].mem, true, false) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if ((tmp = virXMLPropString(nodes[i], "memAccess"))) {
|
||||||
|
if ((rc = virMemAccessTypeFromString(tmp)) <= 0) {
|
||||||
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||||
|
_("Invalid 'memAccess' attribute value '%s'"),
|
||||||
|
tmp);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
def->cells[cur_cell].memAccess = rc;
|
||||||
|
VIR_FREE(tmp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
error:
|
|
||||||
cleanup:
|
cleanup:
|
||||||
ctxt->node = oldNode;
|
ctxt->node = oldNode;
|
||||||
VIR_FREE(nodes);
|
VIR_FREE(nodes);
|
||||||
|
VIR_FREE(tmp);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -243,7 +243,6 @@ virCapsPtr testQemuCapsInit(void)
|
|||||||
ARRAY_CARDINALITY(host_cpu_features), /* nfeatures_max */
|
ARRAY_CARDINALITY(host_cpu_features), /* nfeatures_max */
|
||||||
host_cpu_features, /* features */
|
host_cpu_features, /* features */
|
||||||
0, /* ncells */
|
0, /* ncells */
|
||||||
0, /* ncells_max */
|
|
||||||
NULL, /* cells */
|
NULL, /* cells */
|
||||||
0, /* cells_cpus */
|
0, /* cells_cpus */
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user