From 5d8ef8ad670d52558bcfdc0e1ee3e76fb33cd5d1 Mon Sep 17 00:00:00 2001 From: Luyao Huang Date: Tue, 23 Jun 2015 21:24:25 +0800 Subject: [PATCH] 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 and . This will avoid the more and more bigger element check during we introduce new elements in and in the future. Signed-off-by: Luyao Huang --- src/conf/domain_conf.c | 168 ++++++++++++++++------------------------- 1 file changed, 65 insertions(+), 103 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 183e66c399..3c0c27b6f5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -20979,24 +20979,6 @@ virDomainHubDefFormat(virBufferPtr buf, return 0; } -/* - * Return true if no specified in domain XML - * (I.e. all vcpus inherit the cpuset from "cpuset" of - * ). 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 virDomainResourceDefFormat(virBufferPtr buf, @@ -21127,8 +21109,8 @@ virDomainDefFormatInternal(virDomainDefPtr def, const char *type = NULL; int n; size_t i; - bool blkio = false; - bool cputune = false; + virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; + int indent; virCheckFlags(VIR_DOMAIN_DEF_FORMAT_COMMON_FLAGS | VIR_DOMAIN_DEF_FORMAT_STATUS | @@ -21204,62 +21186,47 @@ virDomainDefFormatInternal(virDomainDefPtr def, virBufferAsprintf(buf, "%llu\n", def->mem.cur_balloon); - /* add blkiotune only if there are any */ - if (def->blkio.weight) { - blkio = true; - } else { - for (n = 0; n < def->blkio.ndevices; n++) { - if (def->blkio.devices[n].weight || - def->blkio.devices[n].riops || - def->blkio.devices[n].wiops || - def->blkio.devices[n].rbps || - def->blkio.devices[n].wbps) { - blkio = true; - break; - } - } + /* start format blkiotune */ + indent = virBufferGetIndent(buf, false); + virBufferAdjustIndent(&childrenBuf, indent + 2); + if (def->blkio.weight) + virBufferAsprintf(&childrenBuf, "%u\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 && + !dev->rbps && !dev->wbps) + continue; + virBufferAddLit(&childrenBuf, "\n"); + virBufferAdjustIndent(&childrenBuf, 2); + virBufferEscapeString(&childrenBuf, "%s\n", + dev->path); + if (dev->weight) + virBufferAsprintf(&childrenBuf, "%u\n", + dev->weight); + if (dev->riops) + virBufferAsprintf(&childrenBuf, "%u\n", + dev->riops); + if (dev->wiops) + virBufferAsprintf(&childrenBuf, "%u\n", + dev->wiops); + if (dev->rbps) + virBufferAsprintf(&childrenBuf, "%llu\n", + dev->rbps); + if (dev->wbps) + virBufferAsprintf(&childrenBuf, "%llu\n", + dev->wbps); + virBufferAdjustIndent(&childrenBuf, -2); + virBufferAddLit(&childrenBuf, "\n"); } - - if (blkio) { + if (virBufferUse(&childrenBuf)) { virBufferAddLit(buf, "\n"); - virBufferAdjustIndent(buf, 2); - - if (def->blkio.weight) - virBufferAsprintf(buf, "%u\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 && - !dev->rbps && !dev->wbps) - continue; - virBufferAddLit(buf, "\n"); - virBufferAdjustIndent(buf, 2); - virBufferEscapeString(buf, "%s\n", - dev->path); - if (dev->weight) - virBufferAsprintf(buf, "%u\n", - dev->weight); - if (dev->riops) - virBufferAsprintf(buf, "%u\n", - dev->riops); - if (dev->wiops) - virBufferAsprintf(buf, "%u\n", - dev->wiops); - if (dev->rbps) - virBufferAsprintf(buf, "%llu\n", - dev->rbps); - if (dev->wbps) - virBufferAsprintf(buf, "%llu\n", - dev->wbps); - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - } - - virBufferAdjustIndent(buf, -2); + virBufferAddBuffer(buf, &childrenBuf); virBufferAddLit(buf, "\n"); } + virBufferFreeAndReset(&childrenBuf); /* add memtune only if there are any */ if (virMemoryLimitIsSet(def->mem.hard_limit) || @@ -21338,35 +21305,26 @@ virDomainDefFormatInternal(virDomainDefPtr def, } } - if (def->cputune.sharesSpecified || - (def->cputune.nvcpupin && !virDomainIsAllVcpupinInherited(def)) || - def->cputune.period || def->cputune.quota || - def->cputune.emulatorpin || - def->cputune.emulator_period || def->cputune.emulator_quota || - virDomainIOThreadIDArrayHasPin(def) || - def->cputune.vcpusched || def->cputune.iothreadsched) { - virBufferAddLit(buf, "\n"); - cputune = true; - } - - virBufferAdjustIndent(buf, 2); + /* start format cputune */ + indent = virBufferGetIndent(buf, false); + virBufferAdjustIndent(&childrenBuf, indent + 2); if (def->cputune.sharesSpecified) - virBufferAsprintf(buf, "%lu\n", + virBufferAsprintf(&childrenBuf, "%lu\n", def->cputune.shares); if (def->cputune.period) - virBufferAsprintf(buf, "%llu\n", + virBufferAsprintf(&childrenBuf, "%llu\n", def->cputune.period); if (def->cputune.quota) - virBufferAsprintf(buf, "%lld\n", + virBufferAsprintf(&childrenBuf, "%lld\n", def->cputune.quota); if (def->cputune.emulator_period) - virBufferAsprintf(buf, "%llu" + virBufferAsprintf(&childrenBuf, "%llu" "\n", def->cputune.emulator_period); if (def->cputune.emulator_quota) - virBufferAsprintf(buf, "%lld" + virBufferAsprintf(&childrenBuf, "%lld" "\n", def->cputune.emulator_quota); @@ -21376,24 +21334,24 @@ virDomainDefFormatInternal(virDomainDefPtr def, if (virBitmapEqual(def->cpumask, def->cputune.vcpupin[i]->cpumask)) continue; - virBufferAsprintf(buf, "cputune.vcpupin[i]->id); if (!(cpumask = virBitmapFormat(def->cputune.vcpupin[i]->cpumask))) goto error; - virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask); + virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask); VIR_FREE(cpumask); } if (def->cputune.emulatorpin) { char *cpumask; - virBufferAddLit(buf, "cputune.emulatorpin))) goto error; - virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask); + virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask); VIR_FREE(cpumask); } @@ -21404,13 +21362,13 @@ virDomainDefFormatInternal(virDomainDefPtr def, if (!def->iothreadids[i]->cpumask) continue; - virBufferAsprintf(buf, "iothreadids[i]->iothread_id); if (!(cpumask = virBitmapFormat(def->iothreadids[i]->cpumask))) goto error; - virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask); + virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask); VIR_FREE(cpumask); } @@ -21420,13 +21378,13 @@ virDomainDefFormatInternal(virDomainDefPtr def, if (!(ids = virBitmapFormat(sp->ids))) goto error; - virBufferAsprintf(buf, "policy)); VIR_FREE(ids); if (sp->priority) - virBufferAsprintf(buf, " priority='%d'", sp->priority); - virBufferAddLit(buf, "/>\n"); + virBufferAsprintf(&childrenBuf, " priority='%d'", sp->priority); + virBufferAddLit(&childrenBuf, "/>\n"); } for (i = 0; i < def->cputune.niothreadsched; i++) { @@ -21435,18 +21393,21 @@ virDomainDefFormatInternal(virDomainDefPtr def, if (!(ids = virBitmapFormat(sp->ids))) goto error; - virBufferAsprintf(buf, "policy)); VIR_FREE(ids); if (sp->priority) - virBufferAsprintf(buf, " priority='%d'", sp->priority); - virBufferAddLit(buf, "/>\n"); + virBufferAsprintf(&childrenBuf, " priority='%d'", sp->priority); + virBufferAddLit(&childrenBuf, "/>\n"); } - virBufferAdjustIndent(buf, -2); - if (cputune) + if (virBufferUse(&childrenBuf)) { + virBufferAddLit(buf, "\n"); + virBufferAddBuffer(buf, &childrenBuf); virBufferAddLit(buf, "\n"); + } + virBufferFreeAndReset(&childrenBuf); if (virDomainNumatuneFormatXML(buf, def->numa) < 0) goto error; @@ -22020,6 +21981,7 @@ virDomainDefFormatInternal(virDomainDefPtr def, error: virBufferFreeAndReset(buf); + virBufferFreeAndReset(&childrenBuf); return -1; }