]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bhyve: process: factor out rctl(8) code
authorRoman Bogorodskiy <bogorodskiy@gmail.com>
Mon, 18 May 2026 16:33:12 +0000 (18:33 +0200)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Wed, 20 May 2026 16:27:13 +0000 (18:27 +0200)
Factor out all rctl(8) execution code to bhyve_rctl.c.

Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/bhyve/bhyve_process.c
src/bhyve/bhyve_rctl.c
src/bhyve/bhyve_rctl.h

index 7baefeb63b0abcdf4d4526f6f30cfff311c774f4..92665419d431cf99d475dd1be293606437b9afb9 100644 (file)
 #include "bhyve_firmware.h"
 #include "bhyve_monitor.h"
 #include "bhyve_process.h"
+#include "bhyve_rctl.h"
 #include "datatypes.h"
 #include "virerror.h"
 #include "virhook.h"
 #include "virlog.h"
 #include "virfile.h"
 #include "viralloc.h"
-#include "vircommand.h"
 #include "virstring.h"
 #include "virpidfile.h"
 #include "virprocess.h"
@@ -149,32 +149,16 @@ bhyveSetResourceLimits(struct _bhyveConn *driver, virDomainObj *vm)
         return -1;
     }
 
-#define BHYVE_APPLY_RCTL_RULE(field, type, action, format) \
-    do { \
-        if ((field)) { \
-            g_autofree char *rule = NULL; \
-            g_autoptr(virCommand) cmd = virCommandNewArgList("rctl", "-a", NULL); \
-            virCommandAddArgFormat(cmd, "process:%d:" type ":" action "=" format, \
-                                   vm->pid, (field)); \
-            if (virCommandRun(cmd, NULL) < 0) \
-                return -1; \
-         } \
-    } while (0)
-
     if (vm->def->blkio.ndevices == 1) {
         device = &vm->def->blkio.devices[0];
 
-        BHYVE_APPLY_RCTL_RULE(device->riops, "readiops", "throttle", "%u");
-        BHYVE_APPLY_RCTL_RULE(device->wiops, "writeiops", "throttle", "%u");
-        BHYVE_APPLY_RCTL_RULE(device->rbps, "readbps", "throttle", "%llu");
-        BHYVE_APPLY_RCTL_RULE(device->wbps, "writebps", "throttle", "%llu");
+        bhyveRctlSetIoLimits(vm->pid, device);
     }
 
     /* rctl(8) uses bytes for these values and def->mem.* uses kibibytes */
     if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
-        BHYVE_APPLY_RCTL_RULE(vm->def->mem.hard_limit * 1024, "memoryuse", "deny", "%llu");
-
-#undef BHYVE_APPLY_RCTL_RULE
+        if (bhyveRctlSetMemoryHardLimit(vm->pid, vm->def->mem.hard_limit) < 0)
+            return -1;
 
     return 0;
 }
index 69467d2e3471f8f13d805d2153dd09344a3ce07a..c9e8e26cc4fe742f3e015d7f635f0640314ce4d6 100644 (file)
@@ -99,3 +99,37 @@ bhyveRctlGetMemoryHardLimit(pid_t pid, unsigned long long *kb)
 
     return -1;
 }
+
+#define BHYVE_APPLY_RCTL_RULE(pid, field, type, action, format) \
+    do { \
+        if ((field)) { \
+            g_autofree char *rule = NULL; \
+            g_autoptr(virCommand) cmd = virCommandNewArgList("rctl", "-a", NULL); \
+            virCommandAddArgFormat(cmd, "process:%d:" type ":" action "=" format, \
+                                   pid, (field)); \
+            if (virCommandRun(cmd, NULL) < 0) \
+                return -1; \
+         } \
+    } while (0)
+
+int
+bhyveRctlSetMemoryHardLimit(pid_t pid, unsigned long long kb)
+{
+    /* rctl(8) uses bytes for these values and def->mem.* uses kibibytes */
+    BHYVE_APPLY_RCTL_RULE(pid, kb * 1024, "memoryuse", "deny", "%llu");
+
+    return 0;
+}
+
+int
+bhyveRctlSetIoLimits(pid_t pid, const virBlkioDevice *device)
+{
+    BHYVE_APPLY_RCTL_RULE(pid, device->riops, "readiops", "throttle", "%u");
+    BHYVE_APPLY_RCTL_RULE(pid, device->wiops, "writeiops", "throttle", "%u");
+    BHYVE_APPLY_RCTL_RULE(pid, device->rbps, "readbps", "throttle", "%llu");
+    BHYVE_APPLY_RCTL_RULE(pid, device->wbps, "writebps", "throttle", "%llu");
+
+    return 0;
+}
+
+#undef BHYVE_APPLY_RCTL_RULE
index 46f102c38e87a13f00adf48680c9300cb468b1b9..83d4a3201311a78daf24cd16bdcc51c0276b1b33 100644 (file)
 
 #pragma once
 
+#include "domain_conf.h"
+
 int
 bhyveRctlGetMemoryHardLimit(pid_t pid, unsigned long long *kb);
+
+int
+bhyveRctlSetMemoryHardLimit(pid_t pid, unsigned long long kb);
+
+int
+bhyveRctlSetIoLimits(pid_t pid, const virBlkioDevice *device);