From: Jedrzej Wasiukiewicz Date: Thu, 14 May 2026 14:41:04 +0000 (+0200) Subject: conf: add energytune to domain XML X-Git-Tag: v12.4.0-rc1~79 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=615a7bb898f66d32bb375c57a214fe7334188d4e;p=thirdparty%2Flibvirt.git conf: add energytune to domain XML The new XML element is under following earlier pattern for resctrl features (cachetune, memorytune). Energytune doesn't currently support the "tuning" part, only monitoring. I added it as energytune for consistency with cache and memory features, keeping all resctrl handling under cputune. This also makes sense with current resctrl architecture - all monitoring groups are part of an allocation group. Changes: - Added parsing to domain_conf.c - Added schema definition in domaincommon.rng - Documented the element in formatdomain.rst - Added energytune test Signed-off-by: Jedrzej Wasiukiewicz Signed-off-by: Christopher M. Cantalupo Reviewed-by: Michal Privoznik --- diff --git a/docs/formatdomain.rst b/docs/formatdomain.rst index 078cd7aa84..db1ca5637a 100644 --- a/docs/formatdomain.rst +++ b/docs/formatdomain.rst @@ -900,6 +900,9 @@ CPU Tuning + + + ... @@ -1084,6 +1087,23 @@ CPU Tuning responsible for making sure the value makes sense on their system and configuration. +``energytune`` :since:`Since 12.4.0` + Optional ``energytune`` element allows to monitor energy consumption using the + resctrl filesystem on the host. Whether or not is this supported can be + gathered from capabilities where number of monitors and available features are + reported. The required attribute ``vcpus`` specifies to which allocation group + this monitor belongs. A vCPU can only be member of one allocation group and monitor + group. The ``vcpus`` specified by ``energytune`` can be identical to those + specified by ``cachetune`` or ``memorytune``. However they are not allowed to + overlap each other. Supported subelements are: + + ``monitor`` + The optional element ``monitor`` creates the energy monitor for + this allocation group and has the following required attribute: + + ``vcpus`` + vCPU list the monitor applies to. + Memory Allocation ----------------- diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index d73bac5cc5..2d3e646bcb 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -19467,6 +19467,57 @@ virDomainMemorytuneDefParse(virDomainDef *def, } +static int +virDomainEnergytuneDefParse(virDomainDef *def, + xmlXPathContextPtr ctxt, + xmlNodePtr node, + unsigned int flags) +{ + VIR_XPATH_NODE_AUTORESTORE(ctxt) + virDomainResctrlDef *resctrl = NULL; + virDomainResctrlDef *newresctrl = NULL; + g_autoptr(virBitmap) vcpus = NULL; + g_autoptr(virResctrlAlloc) alloc = NULL; + size_t nmons; + int ret = -1; + + ctxt->node = node; + + if (virDomainResctrlParseVcpus(def, node, &vcpus) < 0) + return -1; + + if (virBitmapIsAllClear(vcpus)) + return 0; + + if (virDomainResctrlVcpuMatch(def, vcpus, &resctrl) < 0) + return -1; + + if (resctrl) { + alloc = virObjectRef(resctrl->alloc); + } else { + if (!(alloc = virResctrlAllocNew())) + return -1; + if (!(newresctrl = virDomainResctrlNew(node, alloc, vcpus, flags))) + return -1; + resctrl = newresctrl; + } + + nmons = resctrl->nmonitors; + if (virDomainResctrlMonDefParse(def, ctxt, node, + VIR_RESCTRL_MONITOR_TYPE_ENERGY, + resctrl) < 0) + goto cleanup; + + if (newresctrl && resctrl->nmonitors > nmons) + VIR_APPEND_ELEMENT(def->resctrls, def->nresctrls, newresctrl); + + ret = 0; + cleanup: + virDomainResctrlDefFree(newresctrl); + return ret; +} + + static int virDomainDefTunablesParse(virDomainDef *def, xmlXPathContextPtr ctxt, @@ -19671,6 +19722,15 @@ virDomainDefTunablesParse(virDomainDef *def, } VIR_FREE(nodes); + if ((n = virXPathNodeSet("./cputune/energytune", ctxt, &nodes)) < 0) + return -1; + + for (i = 0; i < n; i++) { + if (virDomainEnergytuneDefParse(def, ctxt, nodes[i], flags) < 0) + return -1; + } + VIR_FREE(nodes); + return 0; } @@ -28721,6 +28781,42 @@ virDomainMemorytuneDefFormat(virBuffer *buf, return 0; } + +static int +virDomainEnergytuneDefFormat(virBuffer *buf, + virDomainResctrlDef *resctrl, + unsigned int flags) +{ + g_auto(virBuffer) childrenBuf = VIR_BUFFER_INIT_CHILD(buf); + g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER; + g_autofree char *vcpus = NULL; + size_t i; + + for (i = 0; i < resctrl->nmonitors; i++) { + if (virDomainResctrlMonDefFormatHelper(resctrl->monitors[i], + VIR_RESCTRL_MONITOR_TYPE_ENERGY, + &childrenBuf) < 0) + return -1; + } + + if (!virBufferUse(&childrenBuf)) + return 0; + + vcpus = virBitmapFormat(resctrl->vcpus); + virBufferAsprintf(&attrBuf, " vcpus='%s'", vcpus); + + if (!(flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE)) { + const char *alloc_id = virResctrlAllocGetID(resctrl->alloc); + if (!alloc_id) + return -1; + + virBufferAsprintf(&attrBuf, " id='%s'", alloc_id); + } + + virXMLFormatElement(buf, "energytune", &attrBuf, &childrenBuf); + return 0; +} + static int virDomainCputuneDefFormat(virBuffer *buf, virDomainDef *def, @@ -28821,6 +28917,9 @@ virDomainCputuneDefFormat(virBuffer *buf, for (i = 0; i < def->nresctrls; i++) virDomainMemorytuneDefFormat(&childrenBuf, def->resctrls[i], flags); + for (i = 0; i < def->nresctrls; i++) + virDomainEnergytuneDefFormat(&childrenBuf, def->resctrls[i], flags); + virXMLFormatElement(buf, "cputune", NULL, &childrenBuf); return 0; diff --git a/src/conf/schemas/domaincommon.rng b/src/conf/schemas/domaincommon.rng index 8c03e14d37..eb365a83b5 100644 --- a/src/conf/schemas/domaincommon.rng +++ b/src/conf/schemas/domaincommon.rng @@ -1293,6 +1293,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/tests/genericxml2xmlindata/energytune.xml b/tests/genericxml2xmlindata/energytune.xml new file mode 100644 index 0000000000..4ee4bedb68 --- /dev/null +++ b/tests/genericxml2xmlindata/energytune.xml @@ -0,0 +1,32 @@ + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 219136 + 219136 + 4 + + + + + + + + + + hvm + + + + destroy + restart + destroy + + /usr/bin/qemu-system-i386 + + + + + + + + diff --git a/tests/genericxml2xmltest.c b/tests/genericxml2xmltest.c index 6be694cac5..169c71efa3 100644 --- a/tests/genericxml2xmltest.c +++ b/tests/genericxml2xmltest.c @@ -210,6 +210,7 @@ mymain(void) DO_TEST("cachetune-small"); DO_TEST("cachetune-cdp"); DO_TEST("cachetune"); + DO_TEST("energytune"); DO_TEST_DIFFERENT("cachetune-extra-tunes"); DO_TEST_FAIL_INACTIVE("cachetune-colliding-allocs"); DO_TEST_FAIL_INACTIVE("cachetune-colliding-tunes");