[PATCH 05/26] hypervisor: add support for delay interval during autostart

Daniel P. Berrangé posted 26 patches 1 week, 5 days ago
[PATCH 05/26] hypervisor: add support for delay interval during autostart
Posted by Daniel P. Berrangé 1 week, 5 days ago
This delay can reduce the CPU/IO load storm when autostarting many
guests.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 src/hypervisor/domain_driver.c | 20 +++++++++++++++++---
 src/hypervisor/domain_driver.h |  1 +
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/hypervisor/domain_driver.c b/src/hypervisor/domain_driver.c
index c5b082fd00..44c8fad4d5 100644
--- a/src/hypervisor/domain_driver.c
+++ b/src/hypervisor/domain_driver.c
@@ -656,11 +656,16 @@ virDomainDriverGetIOThreadsConfig(virDomainDef *targetDef,
     return ret;
 }
 
+typedef struct _virDomainDriverAutoStartState {
+    virDomainDriverAutoStartConfig *cfg;
+    bool first;
+} virDomainDriverAutoStartState;
+
 static int
 virDomainDriverAutoStartOne(virDomainObj *vm,
                             void *opaque)
 {
-    virDomainDriverAutoStartConfig *cfg = opaque;
+    virDomainDriverAutoStartState *state = opaque;
 
     virObjectLock(vm);
     virObjectRef(vm);
@@ -670,7 +675,15 @@ virDomainDriverAutoStartOne(virDomainObj *vm,
 
     if (vm->autostart && !virDomainObjIsActive(vm)) {
         virResetLastError();
-        cfg->callback(vm, cfg->opaque);
+        if (state->cfg->delayMS) {
+            if (!state->first) {
+                g_usleep(state->cfg->delayMS * 1000ull);
+            } else {
+                state->first = false;
+            }
+        }
+
+        state->cfg->callback(vm, state->cfg->opaque);
     }
 
     virDomainObjEndAPI(&vm);
@@ -682,6 +695,7 @@ virDomainDriverAutoStartOne(virDomainObj *vm,
 void virDomainDriverAutoStart(virDomainObjList *domains,
                               virDomainDriverAutoStartConfig *cfg)
 {
+    virDomainDriverAutoStartState state = { .cfg = cfg, .first = true };
     bool autostart;
     VIR_DEBUG("Run autostart stateDir=%s", cfg->stateDir);
     if (virDriverShouldAutostart(cfg->stateDir, &autostart) < 0 ||
@@ -690,5 +704,5 @@ void virDomainDriverAutoStart(virDomainObjList *domains,
         return;
     }
 
-    virDomainObjListForEach(domains, false, virDomainDriverAutoStartOne, cfg);
+    virDomainObjListForEach(domains, false, virDomainDriverAutoStartOne, &state);
 }
diff --git a/src/hypervisor/domain_driver.h b/src/hypervisor/domain_driver.h
index c27ed9155e..d695e26f90 100644
--- a/src/hypervisor/domain_driver.h
+++ b/src/hypervisor/domain_driver.h
@@ -84,6 +84,7 @@ typedef struct _virDomainDriverAutoStartConfig {
     char *stateDir;
     virDomainDriverAutoStartCallback callback;
     void *opaque;
+    int delayMS; /* milliseconds between starting each guest */
 } virDomainDriverAutoStartConfig;
 
 void virDomainDriverAutoStart(virDomainObjList *domains,
-- 
2.47.1