From 0329075733839a51ff9b61606dc720f726d03fb0 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Fri, 11 May 2018 14:48:59 +0200 Subject: [PATCH] conf: Move virDomainMemtune formatting into a separate function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At the same time convert the code to use virXMLFormatElement. Signed-off-by: Michal Privoznik Reviewed-by: Ján Tomko --- src/conf/domain_conf.c | 114 +++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 51 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 5d9148c1b1..6786d81c9b 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -26555,6 +26555,67 @@ virDomainIOMMUDefFormat(virBufferPtr buf, } +static int +virDomainMemtuneFormat(virBufferPtr buf, + const virDomainMemtune *mem) +{ + virBuffer childBuf = VIR_BUFFER_INITIALIZER; + int ret = -1; + + virBufferSetChildIndent(&childBuf, buf); + + if (virMemoryLimitIsSet(mem->hard_limit)) { + virBufferAsprintf(&childBuf, + "%llu\n", + mem->hard_limit); + } + if (virMemoryLimitIsSet(mem->soft_limit)) { + virBufferAsprintf(&childBuf, + "%llu\n", + mem->soft_limit); + } + if (mem->min_guarantee) { + virBufferAsprintf(&childBuf, + "%llu\n", + mem->min_guarantee); + } + if (virMemoryLimitIsSet(mem->swap_hard_limit)) { + virBufferAsprintf(&childBuf, + "%llu\n", + mem->swap_hard_limit); + } + + if (virXMLFormatElement(buf, "memtune", NULL, &childBuf) < 0) + goto cleanup; + + virBufferSetChildIndent(&childBuf, buf); + + if (mem->nhugepages) + virDomainHugepagesFormat(&childBuf, mem->hugepages, mem->nhugepages); + if (mem->nosharepages) + virBufferAddLit(&childBuf, "\n"); + if (mem->locked) + virBufferAddLit(&childBuf, "\n"); + if (mem->source) + virBufferAsprintf(&childBuf, "\n", + virDomainMemorySourceTypeToString(mem->source)); + if (mem->access) + virBufferAsprintf(&childBuf, "\n", + virDomainMemoryAccessTypeToString(mem->access)); + if (mem->allocation) + virBufferAsprintf(&childBuf, "\n", + virDomainMemoryAllocationTypeToString(mem->allocation)); + + if (virXMLFormatElement(buf, "memoryBacking", NULL, &childBuf) < 0) + goto cleanup; + + ret = 0; + cleanup: + virBufferFreeAndReset(&childBuf); + return ret; +} + + /* This internal version appends to an existing buffer * (possibly with auto-indent), rather than flattening * to string. @@ -26693,57 +26754,8 @@ virDomainDefFormatInternal(virDomainDefPtr def, } virBufferFreeAndReset(&childrenBuf); - /* add memtune only if there are any */ - if (virMemoryLimitIsSet(def->mem.hard_limit) || - virMemoryLimitIsSet(def->mem.soft_limit) || - virMemoryLimitIsSet(def->mem.swap_hard_limit) || - def->mem.min_guarantee) { - virBufferAddLit(buf, "\n"); - virBufferAdjustIndent(buf, 2); - if (virMemoryLimitIsSet(def->mem.hard_limit)) { - virBufferAsprintf(buf, "" - "%llu\n", def->mem.hard_limit); - } - if (virMemoryLimitIsSet(def->mem.soft_limit)) { - virBufferAsprintf(buf, "" - "%llu\n", def->mem.soft_limit); - } - if (def->mem.min_guarantee) { - virBufferAsprintf(buf, "" - "%llu\n", def->mem.min_guarantee); - } - if (virMemoryLimitIsSet(def->mem.swap_hard_limit)) { - virBufferAsprintf(buf, "" - "%llu\n", def->mem.swap_hard_limit); - } - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - } - - if (def->mem.nhugepages || def->mem.nosharepages || def->mem.locked - || def->mem.source || def->mem.access || def->mem.allocation) - { - virBufferAddLit(buf, "\n"); - virBufferAdjustIndent(buf, 2); - if (def->mem.nhugepages) - virDomainHugepagesFormat(buf, def->mem.hugepages, def->mem.nhugepages); - if (def->mem.nosharepages) - virBufferAddLit(buf, "\n"); - if (def->mem.locked) - virBufferAddLit(buf, "\n"); - if (def->mem.source) - virBufferAsprintf(buf, "\n", - virDomainMemorySourceTypeToString(def->mem.source)); - if (def->mem.access) - virBufferAsprintf(buf, "\n", - virDomainMemoryAccessTypeToString(def->mem.access)); - if (def->mem.allocation) - virBufferAsprintf(buf, "\n", - virDomainMemoryAllocationTypeToString(def->mem.allocation)); - - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - } + if (virDomainMemtuneFormat(buf, &def->mem) < 0) + goto error; if (virDomainCpuDefFormat(buf, def) < 0) goto error;