A whole-process resctrl group (allocation or monitor declared without
vcpus) covers the entire emulator process rather than a set of vCPU
threads. Assign the emulator PID to such groups during the pre-exec
handshake in qemuProcessResctrlCreate, so every thread the QEMU process
later spawns - vCPUs, iothreads, workers... - inherits
the group.

virResctrlDeterminePath now treats a NULL id as a whole-process group,
resolving to the bare machine name with no id suffix.

Signed-off-by: Jedrzej Wasiukiewicz <[email protected]>
---
 src/qemu/qemu_process.c | 40 +++++++++++++++++++++++++++++++++++++---
 src/util/virresctrl.c   | 10 ++++------
 2 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index b2506edce0..8907f4f0ec 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -2909,18 +2909,33 @@ qemuProcessResctrlCreate(virQEMUDriver *driver,
 
     for (i = 0; i < vm->def->nresctrls; i++) {
         size_t j = 0;
+        virDomainResctrlDef *resctrl = vm->def->resctrls[i];
+
         if (virResctrlAllocCreate(caps->host.resctrl,
-                                  vm->def->resctrls[i]->alloc,
+                                  resctrl->alloc,
                                   priv->machineName) < 0)
             return -1;
 
-        for (j = 0; j < vm->def->resctrls[i]->nmonitors; j++) {
+        /* A whole-process group covers every emulator thread.
+         * Assign the emulator PID now, while still in the pre-exec handshake
+         * window, so the resctrl group is inherited by every thread the QEMU
+         * process subsequently spawns (vCPUs, iothreads, workers etc.). */
+        if (resctrl->whole_process &&
+            !virResctrlAllocIsEmpty(resctrl->alloc) &&
+            virResctrlAllocAddPID(resctrl->alloc, vm->pid) < 0)
+            return -1;
+
+        for (j = 0; j < resctrl->nmonitors; j++) {
             virDomainResctrlMonDef *mon = NULL;
 
-            mon = vm->def->resctrls[i]->monitors[j];
+            mon = resctrl->monitors[j];
             if (virResctrlMonitorCreate(mon->instance,
                                         priv->machineName) < 0)
                 return -1;
+
+            if (mon->whole_process &&
+                virResctrlMonitorAddPID(mon->instance, vm->pid) < 0)
+                return -1;
         }
     }
 
@@ -6265,6 +6280,25 @@ qemuProcessSetupVcpu(virDomainObj *vm,
         size_t j = 0;
         virDomainResctrlDef *ct = vm->def->resctrls[i];
 
+        /* A whole-process allocation covers every thread: its control group is
+         * assigned the emulator PID once at startup and inherited by every
+         * thread, so per-vCPU threads need no allocation assignment here.
+         * Per-vCPU monitors underneath it, however, still need each vCPU's PID
+         * to carve out their own mon_group. */
+        if (ct->whole_process) {
+            for (j = 0; j < ct->nmonitors; j++) {
+                mon = ct->monitors[j];
+
+                if (virBitmapIsBitSet(mon->vcpus, vcpuid)) {
+                    if (virResctrlMonitorAddPID(mon->instance, vcpupid) < 0)
+                        return -1;
+                    break;
+                }
+            }
+
+            continue;
+        }
+
         if (virBitmapIsBitSet(ct->vcpus, vcpuid)) {
             if (virResctrlAllocAddPID(ct->alloc, vcpupid) < 0)
                 return -1;
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index 66df44fb58..d4728d2e7c 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -2287,12 +2287,10 @@ virResctrlDeterminePath(const char *parentpath,
                         const char *prefix,
                         const char *id)
 {
-    if (!id) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Resctrl ID must be set before determining resctrl 
parentpath='%1$s' prefix='%2$s'"),
-                       parentpath, prefix);
-        return NULL;
-    }
+    /* A NULL id denotes a whole-process group, which uses the bare machine
+     * name with no id suffix. */
+    if (!id)
+        return g_strdup_printf("%s/%s", parentpath, prefix);
 
     return g_strdup_printf("%s/%s-%s", parentpath, prefix, id);
 }
-- 
2.43.0

---------------------------------------------------------------------
Intel Technology Poland sp. z o.o.
ul. Slowackiego 173 | 80-298 Gdansk | Sad Rejonowy Gdansk Polnoc | VII Wydzial 
Gospodarczy Krajowego Rejestru Sadowego - KRS 101882 | NIP 957-07-52-316 | 
Kapital zakladowy 200.000 PLN.
Spolka oswiadcza, ze posiada status duzego przedsiebiorcy w rozumieniu ustawy z 
dnia 8 marca 2013 r. o przeciwdzialaniu nadmiernym opoznieniom w transakcjach 
handlowych.

Ta wiadomosc wraz z zalacznikami jest przeznaczona dla okreslonego adresata i 
moze zawierac informacje poufne. W razie przypadkowego otrzymania tej 
wiadomosci, prosimy o powiadomienie nadawcy oraz trwale jej usuniecie; 
jakiekolwiek przegladanie lub rozpowszechnianie jest zabronione.
This e-mail and any attachments may contain confidential material for the sole 
use of the intended recipient(s). If you are not the intended recipient, please 
contact the sender and delete all copies; any review or distribution by others 
is strictly prohibited.

Reply via email to