提交 5d8ef8ad 编写于 作者: L Luyao Huang 提交者: Martin Kletzander

conf: improve the way we format blkiotune and cputune

Just refactor existing code to use a child buf instead of
check all element before format <blkiotune> and <cputune>.
This will avoid the more and more bigger element check during
we introduce new elements in <blkiotune> and <cputune> in the
future.
Signed-off-by: NLuyao Huang <lhuang@redhat.com>
上级 4902d170
...@@ -20979,24 +20979,6 @@ virDomainHubDefFormat(virBufferPtr buf, ...@@ -20979,24 +20979,6 @@ virDomainHubDefFormat(virBufferPtr buf,
return 0; return 0;
} }
/*
* Return true if no <vcpupin> specified in domain XML
* (I.e. all vcpus inherit the cpuset from "cpuset" of
* <vcpu>). Or false otherwise.
*/
static bool
virDomainIsAllVcpupinInherited(virDomainDefPtr def)
{
size_t i;
for (i = 0; i < def->cputune.nvcpupin; i++) {
if (!virBitmapEqual(def->cputune.vcpupin[i]->cpumask, def->cpumask))
return false;
}
return true;
}
static void static void
virDomainResourceDefFormat(virBufferPtr buf, virDomainResourceDefFormat(virBufferPtr buf,
...@@ -21127,8 +21109,8 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -21127,8 +21109,8 @@ virDomainDefFormatInternal(virDomainDefPtr def,
const char *type = NULL; const char *type = NULL;
int n; int n;
size_t i; size_t i;
bool blkio = false; virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
bool cputune = false; int indent;
virCheckFlags(VIR_DOMAIN_DEF_FORMAT_COMMON_FLAGS | virCheckFlags(VIR_DOMAIN_DEF_FORMAT_COMMON_FLAGS |
VIR_DOMAIN_DEF_FORMAT_STATUS | VIR_DOMAIN_DEF_FORMAT_STATUS |
...@@ -21204,62 +21186,47 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -21204,62 +21186,47 @@ virDomainDefFormatInternal(virDomainDefPtr def,
virBufferAsprintf(buf, "<currentMemory unit='KiB'>%llu</currentMemory>\n", virBufferAsprintf(buf, "<currentMemory unit='KiB'>%llu</currentMemory>\n",
def->mem.cur_balloon); def->mem.cur_balloon);
/* add blkiotune only if there are any */ /* start format blkiotune */
if (def->blkio.weight) { indent = virBufferGetIndent(buf, false);
blkio = true; virBufferAdjustIndent(&childrenBuf, indent + 2);
} else { if (def->blkio.weight)
for (n = 0; n < def->blkio.ndevices; n++) { virBufferAsprintf(&childrenBuf, "<weight>%u</weight>\n",
if (def->blkio.devices[n].weight || def->blkio.weight);
def->blkio.devices[n].riops ||
def->blkio.devices[n].wiops ||
def->blkio.devices[n].rbps ||
def->blkio.devices[n].wbps) {
blkio = true;
break;
}
}
}
if (blkio) {
virBufferAddLit(buf, "<blkiotune>\n");
virBufferAdjustIndent(buf, 2);
if (def->blkio.weight)
virBufferAsprintf(buf, "<weight>%u</weight>\n",
def->blkio.weight);
for (n = 0; n < def->blkio.ndevices; n++) {
virBlkioDevicePtr dev = &def->blkio.devices[n];
if (!dev->weight && !dev->riops && !dev->wiops && for (n = 0; n < def->blkio.ndevices; n++) {
!dev->rbps && !dev->wbps) virBlkioDevicePtr dev = &def->blkio.devices[n];
continue;
virBufferAddLit(buf, "<device>\n");
virBufferAdjustIndent(buf, 2);
virBufferEscapeString(buf, "<path>%s</path>\n",
dev->path);
if (dev->weight)
virBufferAsprintf(buf, "<weight>%u</weight>\n",
dev->weight);
if (dev->riops)
virBufferAsprintf(buf, "<read_iops_sec>%u</read_iops_sec>\n",
dev->riops);
if (dev->wiops)
virBufferAsprintf(buf, "<write_iops_sec>%u</write_iops_sec>\n",
dev->wiops);
if (dev->rbps)
virBufferAsprintf(buf, "<read_bytes_sec>%llu</read_bytes_sec>\n",
dev->rbps);
if (dev->wbps)
virBufferAsprintf(buf, "<write_bytes_sec>%llu</write_bytes_sec>\n",
dev->wbps);
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</device>\n");
}
virBufferAdjustIndent(buf, -2); if (!dev->weight && !dev->riops && !dev->wiops &&
!dev->rbps && !dev->wbps)
continue;
virBufferAddLit(&childrenBuf, "<device>\n");
virBufferAdjustIndent(&childrenBuf, 2);
virBufferEscapeString(&childrenBuf, "<path>%s</path>\n",
dev->path);
if (dev->weight)
virBufferAsprintf(&childrenBuf, "<weight>%u</weight>\n",
dev->weight);
if (dev->riops)
virBufferAsprintf(&childrenBuf, "<read_iops_sec>%u</read_iops_sec>\n",
dev->riops);
if (dev->wiops)
virBufferAsprintf(&childrenBuf, "<write_iops_sec>%u</write_iops_sec>\n",
dev->wiops);
if (dev->rbps)
virBufferAsprintf(&childrenBuf, "<read_bytes_sec>%llu</read_bytes_sec>\n",
dev->rbps);
if (dev->wbps)
virBufferAsprintf(&childrenBuf, "<write_bytes_sec>%llu</write_bytes_sec>\n",
dev->wbps);
virBufferAdjustIndent(&childrenBuf, -2);
virBufferAddLit(&childrenBuf, "</device>\n");
}
if (virBufferUse(&childrenBuf)) {
virBufferAddLit(buf, "<blkiotune>\n");
virBufferAddBuffer(buf, &childrenBuf);
virBufferAddLit(buf, "</blkiotune>\n"); virBufferAddLit(buf, "</blkiotune>\n");
} }
virBufferFreeAndReset(&childrenBuf);
/* add memtune only if there are any */ /* add memtune only if there are any */
if (virMemoryLimitIsSet(def->mem.hard_limit) || if (virMemoryLimitIsSet(def->mem.hard_limit) ||
...@@ -21338,35 +21305,26 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -21338,35 +21305,26 @@ virDomainDefFormatInternal(virDomainDefPtr def,
} }
} }
if (def->cputune.sharesSpecified || /* start format cputune */
(def->cputune.nvcpupin && !virDomainIsAllVcpupinInherited(def)) || indent = virBufferGetIndent(buf, false);
def->cputune.period || def->cputune.quota || virBufferAdjustIndent(&childrenBuf, indent + 2);
def->cputune.emulatorpin ||
def->cputune.emulator_period || def->cputune.emulator_quota ||
virDomainIOThreadIDArrayHasPin(def) ||
def->cputune.vcpusched || def->cputune.iothreadsched) {
virBufferAddLit(buf, "<cputune>\n");
cputune = true;
}
virBufferAdjustIndent(buf, 2);
if (def->cputune.sharesSpecified) if (def->cputune.sharesSpecified)
virBufferAsprintf(buf, "<shares>%lu</shares>\n", virBufferAsprintf(&childrenBuf, "<shares>%lu</shares>\n",
def->cputune.shares); def->cputune.shares);
if (def->cputune.period) if (def->cputune.period)
virBufferAsprintf(buf, "<period>%llu</period>\n", virBufferAsprintf(&childrenBuf, "<period>%llu</period>\n",
def->cputune.period); def->cputune.period);
if (def->cputune.quota) if (def->cputune.quota)
virBufferAsprintf(buf, "<quota>%lld</quota>\n", virBufferAsprintf(&childrenBuf, "<quota>%lld</quota>\n",
def->cputune.quota); def->cputune.quota);
if (def->cputune.emulator_period) if (def->cputune.emulator_period)
virBufferAsprintf(buf, "<emulator_period>%llu" virBufferAsprintf(&childrenBuf, "<emulator_period>%llu"
"</emulator_period>\n", "</emulator_period>\n",
def->cputune.emulator_period); def->cputune.emulator_period);
if (def->cputune.emulator_quota) if (def->cputune.emulator_quota)
virBufferAsprintf(buf, "<emulator_quota>%lld" virBufferAsprintf(&childrenBuf, "<emulator_quota>%lld"
"</emulator_quota>\n", "</emulator_quota>\n",
def->cputune.emulator_quota); def->cputune.emulator_quota);
...@@ -21376,24 +21334,24 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -21376,24 +21334,24 @@ virDomainDefFormatInternal(virDomainDefPtr def,
if (virBitmapEqual(def->cpumask, def->cputune.vcpupin[i]->cpumask)) if (virBitmapEqual(def->cpumask, def->cputune.vcpupin[i]->cpumask))
continue; continue;
virBufferAsprintf(buf, "<vcpupin vcpu='%u' ", virBufferAsprintf(&childrenBuf, "<vcpupin vcpu='%u' ",
def->cputune.vcpupin[i]->id); def->cputune.vcpupin[i]->id);
if (!(cpumask = virBitmapFormat(def->cputune.vcpupin[i]->cpumask))) if (!(cpumask = virBitmapFormat(def->cputune.vcpupin[i]->cpumask)))
goto error; goto error;
virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask); virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask);
VIR_FREE(cpumask); VIR_FREE(cpumask);
} }
if (def->cputune.emulatorpin) { if (def->cputune.emulatorpin) {
char *cpumask; char *cpumask;
virBufferAddLit(buf, "<emulatorpin "); virBufferAddLit(&childrenBuf, "<emulatorpin ");
if (!(cpumask = virBitmapFormat(def->cputune.emulatorpin))) if (!(cpumask = virBitmapFormat(def->cputune.emulatorpin)))
goto error; goto error;
virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask); virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask);
VIR_FREE(cpumask); VIR_FREE(cpumask);
} }
...@@ -21404,13 +21362,13 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -21404,13 +21362,13 @@ virDomainDefFormatInternal(virDomainDefPtr def,
if (!def->iothreadids[i]->cpumask) if (!def->iothreadids[i]->cpumask)
continue; continue;
virBufferAsprintf(buf, "<iothreadpin iothread='%u' ", virBufferAsprintf(&childrenBuf, "<iothreadpin iothread='%u' ",
def->iothreadids[i]->iothread_id); def->iothreadids[i]->iothread_id);
if (!(cpumask = virBitmapFormat(def->iothreadids[i]->cpumask))) if (!(cpumask = virBitmapFormat(def->iothreadids[i]->cpumask)))
goto error; goto error;
virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask); virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask);
VIR_FREE(cpumask); VIR_FREE(cpumask);
} }
...@@ -21420,13 +21378,13 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -21420,13 +21378,13 @@ virDomainDefFormatInternal(virDomainDefPtr def,
if (!(ids = virBitmapFormat(sp->ids))) if (!(ids = virBitmapFormat(sp->ids)))
goto error; goto error;
virBufferAsprintf(buf, "<vcpusched vcpus='%s' scheduler='%s'", virBufferAsprintf(&childrenBuf, "<vcpusched vcpus='%s' scheduler='%s'",
ids, virProcessSchedPolicyTypeToString(sp->policy)); ids, virProcessSchedPolicyTypeToString(sp->policy));
VIR_FREE(ids); VIR_FREE(ids);
if (sp->priority) if (sp->priority)
virBufferAsprintf(buf, " priority='%d'", sp->priority); virBufferAsprintf(&childrenBuf, " priority='%d'", sp->priority);
virBufferAddLit(buf, "/>\n"); virBufferAddLit(&childrenBuf, "/>\n");
} }
for (i = 0; i < def->cputune.niothreadsched; i++) { for (i = 0; i < def->cputune.niothreadsched; i++) {
...@@ -21435,18 +21393,21 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -21435,18 +21393,21 @@ virDomainDefFormatInternal(virDomainDefPtr def,
if (!(ids = virBitmapFormat(sp->ids))) if (!(ids = virBitmapFormat(sp->ids)))
goto error; goto error;
virBufferAsprintf(buf, "<iothreadsched iothreads='%s' scheduler='%s'", virBufferAsprintf(&childrenBuf, "<iothreadsched iothreads='%s' scheduler='%s'",
ids, virProcessSchedPolicyTypeToString(sp->policy)); ids, virProcessSchedPolicyTypeToString(sp->policy));
VIR_FREE(ids); VIR_FREE(ids);
if (sp->priority) if (sp->priority)
virBufferAsprintf(buf, " priority='%d'", sp->priority); virBufferAsprintf(&childrenBuf, " priority='%d'", sp->priority);
virBufferAddLit(buf, "/>\n"); virBufferAddLit(&childrenBuf, "/>\n");
} }
virBufferAdjustIndent(buf, -2); if (virBufferUse(&childrenBuf)) {
if (cputune) virBufferAddLit(buf, "<cputune>\n");
virBufferAddBuffer(buf, &childrenBuf);
virBufferAddLit(buf, "</cputune>\n"); virBufferAddLit(buf, "</cputune>\n");
}
virBufferFreeAndReset(&childrenBuf);
if (virDomainNumatuneFormatXML(buf, def->numa) < 0) if (virDomainNumatuneFormatXML(buf, def->numa) < 0)
goto error; goto error;
...@@ -22020,6 +21981,7 @@ virDomainDefFormatInternal(virDomainDefPtr def, ...@@ -22020,6 +21981,7 @@ virDomainDefFormatInternal(virDomainDefPtr def,
error: error:
virBufferFreeAndReset(buf); virBufferFreeAndReset(buf);
virBufferFreeAndReset(&childrenBuf);
return -1; return -1;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册