Skip to contentSkip to conversation

Initialize backend memory objects in parallel

PATCH Superseded Mark Kanda 3 participants 11 messages Jan 8, 2024 – Jan 17, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mark Kanda

author
#0 lore raw

cover Initialize backend memory objects in parallel

QEMU initializes preallocated backend memory when parsing the corresponding objects from the command line. In certain scenarios, such as memory being preallocated across multiple numa nodes, this approach is not optimal due to the unnecessary serialization.

This series addresses this issue by initializing the backend memory objects in parallel.

Mark Kanda (2):
oslib-posix: refactor memory prealloc threads
oslib-posix: initialize backend memory objects in parallel

 include/qemu/osdep.h |   6 ++
 system/vl.c          |   2 +
 util/oslib-posix.c   | 150 +++++++++++++++++++++++++++++--------------
 util/oslib-win32.c   |   5 ++
 4 files changed, 116 insertions(+), 47 deletions(-)
Signature · 1 line
2.39.3
author
#1 lore raw

PATCH 1/2 oslib-posix: refactor memory prealloc threads

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
 util/oslib-posix.c | 104 +++++++++++++++++++++++++++++----------------
 1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +68 -36
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
 
 struct MemsetThread;
 
+static QLIST_HEAD(, MemsetContext) memset_contexts =
+    QLIST_HEAD_INITIALIZER(memset_contexts);
+
 typedef struct MemsetContext {
     bool all_threads_created;
     bool any_thread_failed;
     struct MemsetThread *threads;
     int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
 } MemsetContext;
 
 struct MemsetThread {
@@ -81,7 +85,7 @@ struct MemsetThread {
 typedef struct MemsetThread MemsetThread;
 
 /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
 struct sigaction sigbus_oldact;
 static QemuMutex sigbus_mutex;
 
@@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
 #endif /* CONFIG_LINUX */
 {
     int i;
+    MemsetContext *context;
 
     if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) {
-            MemsetThread *thread = &sigbus_memset_context->threads[i];
+        QLIST_FOREACH(context, &memset_contexts, next) {
+            for (i = 0; i < context->num_threads; i++) {
+                MemsetThread *thread = &context->threads[i];
 
-            if (qemu_thread_is_self(&thread->pgthread)) {
-                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) {
+                    siglongjmp(thread->env, 1);
+                }
             }
         }
     }
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages,
                            bool use_madv_populate_write)
 {
     static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages, max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
     size_t numpages_per_thread, leftover;
     void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
     char *addr = area;
 
+    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads);
+
     if (g_once_init_enter(&initialized)) {
         qemu_mutex_init(&page_mutex);
         qemu_cond_init(&page_cond);
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages,
 
     if (use_madv_populate_write) {
         /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
             if (qemu_madvise(area, hpagesize * numpages,
                              QEMU_MADV_POPULATE_WRITE)) {
                 return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages,
         touch_fn = do_touch_pages;
     }
 
-    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i < leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i < leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
         if (tc) {
-            thread_context_create_thread(tc, &context.threads[i].pgthread,
+            thread_context_create_thread(tc, &context->threads[i].pgthread,
                                          "touch_pages",
-                                         touch_fn, &context.threads[i],
+                                         touch_fn, &context->threads[i],
                                          QEMU_THREAD_JOINABLE);
         } else {
-            qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+            qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
                                QEMU_THREAD_JOINABLE);
         }
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
     }
 
+    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
     if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;
     }
 
+    return 0;
+}
+
+static int wait_mem_prealloc(void)
+{
+    int i, ret = 0;
+    MemsetContext *context, *next_context;
     qemu_mutex_lock(&page_mutex);
-    context.all_threads_created = true;
+    QLIST_FOREACH(context, &memset_contexts, next) {
+        context->all_threads_created = true;
+    }
     qemu_cond_broadcast(&page_cond);
     qemu_mutex_unlock(&page_mutex);
 
-    for (i = 0; i < context.num_threads; i++) {
-        int tmp = (uintptr_t)qemu_thread_join(&context.threads[i].pgthread);
+    QLIST_FOREACH(context, &memset_contexts, next) {
+        for (i = 0; i < context->num_threads; i++) {
+            int tmp =
+                (uintptr_t)qemu_thread_join(&context->threads[i].pgthread);
 
-        if (tmp) {
-            ret = tmp;
+            if (tmp) {
+                ret = tmp;
+            }
         }
     }
 
-    if (!use_madv_populate_write) {
-        sigbus_memset_context = NULL;
+    if (sigbus_oldact.sa_handler) {
+        /* restore the previous sighandler */
+        if (sigaction(SIGBUS, &sigbus_oldact, NULL)) {
+            /* Terminate QEMU since it can't recover from error */
+            perror("wait_mem_prealloc: failed to reinstall signal handler");
+            exit(1);
+        }
+        memset(&sigbus_oldact, 0, sizeof(sigbus_oldact));
     }
-    g_free(context.threads);
+    sigbus_memset_context = false;
 
+    QLIST_FOREACH_SAFE(context, &memset_contexts, next, next_context) {
+        QLIST_REMOVE(context, next);
+        g_free(context->threads);
+        g_free(context);
+    }
     return ret;
 }
 
@@ -547,11 +580,10 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
     }
 
     if (!use_madv_populate_write) {
-        ret = sigaction(SIGBUS, &sigbus_oldact, NULL);
+        ret = wait_mem_prealloc();
         if (ret) {
-            /* Terminate QEMU since it can't recover from error */
-            perror("qemu_prealloc_mem: failed to reinstall signal handler");
-            exit(1);
+            error_setg_errno(errp, -ret,
+                "qemu_prealloc_mem: failed waiting for memory prealloc");
         }
         qemu_mutex_unlock(&sigbus_mutex);
     }
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

Quoted 124 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
  util/oslib-posix.c | 104 +++++++++++++++++++++++++++++----------------
  1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +30 -22
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
 
  struct MemsetThread;
 
+static QLIST_HEAD(, MemsetContext) memset_contexts =
+    QLIST_HEAD_INITIALIZER(memset_contexts);
+
  typedef struct MemsetContext {
      bool all_threads_created;
      bool any_thread_failed;
      struct MemsetThread *threads;
      int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
  } MemsetContext;
 
  struct MemsetThread {
@@ -81,7 +85,7 @@ struct MemsetThread {
  typedef struct MemsetThread MemsetThread;
 
  /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
  struct sigaction sigbus_oldact;
  static QemuMutex sigbus_mutex;
 
@@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
  #endif /* CONFIG_LINUX */
  {
      int i;
+    MemsetContext *context;
 
      if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) {
-            MemsetThread *thread = &sigbus_memset_context->threads[i];
+        QLIST_FOREACH(context, &memset_contexts, next) {
+            for (i = 0; i < context->num_threads; i++) {
+                MemsetThread *thread = &context->threads[i];
 
-            if (qemu_thread_is_self(&thread->pgthread)) {
-                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) {
+                    siglongjmp(thread->env, 1);
+                }
              }
          }
      }
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages,
                             bool use_madv_populate_write)
  {
      static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages, max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
      size_t numpages_per_thread, leftover;
      void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
      char *addr = area;
 
+    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads);
+
      if (g_once_init_enter(&initialized)) {
          qemu_mutex_init(&page_mutex);
          qemu_cond_init(&page_cond);
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages,
 
      if (use_madv_populate_write) {
          /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
              if (qemu_madvise(area, hpagesize * numpages,
                               QEMU_MADV_POPULATE_WRITE)) {
                  return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages,
          touch_fn = do_touch_pages;
      }
 
-    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i < leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i < leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
          if (tc) {
-            thread_context_create_thread(tc, &context.threads[i].pgthread,
+            thread_context_create_thread(tc, &context->threads[i].pgthread,
                                           "touch_pages",
-                                         touch_fn, &context.threads[i],
+                                         touch_fn, &context->threads[i],
                                           QEMU_THREAD_JOINABLE);
          } else {
-            qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+            qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
                                 QEMU_THREAD_JOINABLE);

util/oslib-posix.c static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 480

Quoted 10 lines
util/oslib-posix.c +4 -2
          }
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
      }
 
+    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
      if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

Signature · 3 lines
Cheers,

David / dhildenb
author
#3 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

Quoted 125 lines · David Hildenbrand
Quoted 124 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
  util/oslib-posix.c | 104 +++++++++++++++++++++++++++++----------------

  1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +0 -0
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
     struct MemsetThread;

  +static QLIST_HEAD(, MemsetContext) memset_contexts = +    QLIST_HEAD_INITIALIZER(memset_contexts); +
  typedef struct MemsetContext {
      bool all_threads_created;
      bool any_thread_failed;
      struct MemsetThread *threads;
      int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
  } MemsetContext;
    struct MemsetThread {

util/oslib-posix.c +1 -1
@@ -81,7 +85,7 @@ struct MemsetThread {
   typedef struct MemsetThread MemsetThread;
     /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
   struct sigaction sigbus_oldact;
   static QemuMutex sigbus_mutex;
   @@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
   #endif /* CONFIG_LINUX */

  {
      int i;
+    MemsetContext *context;
        if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) { -            MemsetThread *thread = &sigbus_memset_context->threads[i]; +        QLIST_FOREACH(context, &memset_contexts, next) {
+            for (i = 0; i < context->num_threads; i++) {
+                MemsetThread *thread = &context->threads[i]; -            if (qemu_thread_is_self(&thread->pgthread)) { -                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) { +                    siglongjmp(thread->env, 1);
+                }
              }
          }
      }

util/oslib-posix.c +2 -4
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t 
 hpagesize, size_t numpages,
                              bool use_madv_populate_write)
   {
       static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages, 
 max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
       size_t numpages_per_thread, leftover;
       void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
       char *addr = area;

  +    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads); +
      if (g_once_init_enter(&initialized)) { qemu_mutex_init(&page_mutex); qemu_cond_init(&page_cond);

util/oslib-posix.c +13 -12
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t 
 hpagesize, size_t numpages,
         if (use_madv_populate_write) {
           /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
               if (qemu_madvise(area, hpagesize * numpages,
                                QEMU_MADV_POPULATE_WRITE)) {
                   return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t 
 hpagesize, size_t numpages,
           touch_fn = do_touch_pages;
       }
   -    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i < 
 leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i < 
 leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
           if (tc) {
-            thread_context_create_thread(tc, 
 &context.threads[i].pgthread,
+            thread_context_create_thread(tc, 
 &context->threads[i].pgthread,
                                            "touch_pages",
-                                         touch_fn, &context.threads[i],
+                                         touch_fn, 
 &context->threads[i],
 QEMU_THREAD_JOINABLE);
           } else {
- qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+ qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
                                  QEMU_THREAD_JOINABLE);
           }
Quoted 9 lines

util/oslib-posix.c static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 480

Quoted 8 lines
util/oslib-posix.c +3 -2
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
       }
   +    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
       if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Thanks David,

Quoted 2 lines

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Ideally, we're hoping to support this with earlier kernels which don't support MADV_POPULATE _WRITE. But, I will check to see if we really need it.

Quoted 2 lines

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

I'm not familiar with the intricacies of virtio-mem, but the basic idea of this series is to *only* allow parallel init during the start up phase (while prealloc_init == false). Once we have parsed all the command line args, we set prealloc_init = true (wait_mem_prealloc_init()) which causes all subsequent calls to qemu_prealloc_mem() to perform initialization synchronously. So, I *think* this covers the virtio-mem use case. Am I missing something?

Thanks/regards,
-Mark

#4 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

Quoted 137 lines · Mark Kanda
Quoted 125 lines · David Hildenbrand
Quoted 124 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
   util/oslib-posix.c | 104 +++++++++++++++++++++++++++++----------------
   1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +4 -1
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
     struct MemsetThread;
   +static QLIST_HEAD(, MemsetContext) memset_contexts =
+    QLIST_HEAD_INITIALIZER(memset_contexts);
+
   typedef struct MemsetContext {
       bool all_threads_created;
       bool any_thread_failed;
       struct MemsetThread *threads;
       int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
   } MemsetContext;
     struct MemsetThread {
@@ -81,7 +85,7 @@ struct MemsetThread {
   typedef struct MemsetThread MemsetThread;
     /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
   struct sigaction sigbus_oldact;
   static QemuMutex sigbus_mutex;
   @@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
   #endif /* CONFIG_LINUX */

  {
      int i;
+    MemsetContext *context;
        if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) { -            MemsetThread *thread = &sigbus_memset_context->threads[i]; +        QLIST_FOREACH(context, &memset_contexts, next) {
+            for (i = 0; i < context->num_threads; i++) {
+                MemsetThread *thread = &context->threads[i];
  -            if (qemu_thread_is_self(&thread->pgthread)) {
-                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) { +                    siglongjmp(thread->env, 1);
+                }
              }
          }
      }

util/oslib-posix.c +17 -16
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
                              bool use_madv_populate_write)
   {
       static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages,
 max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
       size_t numpages_per_thread, leftover;
       void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
       char *addr = area;
   +    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads);
+
       if (g_once_init_enter(&initialized)) {
           qemu_mutex_init(&page_mutex);
           qemu_cond_init(&page_cond);
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
         if (use_madv_populate_write) {
           /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
               if (qemu_madvise(area, hpagesize * numpages,
                                QEMU_MADV_POPULATE_WRITE)) {
                   return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
           touch_fn = do_touch_pages;
       }
   -    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i <
 leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i <
 leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
           if (tc) {
-            thread_context_create_thread(tc,
 &context.threads[i].pgthread,
+            thread_context_create_thread(tc,
 &context->threads[i].pgthread,
                                            "touch_pages",
-                                         touch_fn, &context.threads[i],
+                                         touch_fn,
 &context->threads[i],
 QEMU_THREAD_JOINABLE);
           } else {
- qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+ qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
                                  QEMU_THREAD_JOINABLE);
           }
Quoted 9 lines

util/oslib-posix.c static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 480

Quoted 8 lines
util/oslib-posix.c +3 -2
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
       }
   +    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
       if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Thanks David,

Quoted 7 lines
Quoted 4 lines

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Ideally, we're hoping to support this with earlier kernels which don't support MADV_POPULATE _WRITE. But, I will check to see if we really need it.

That's around since Linux 5.14, so please try simplifying.

Quoted 5 lines
Quoted 4 lines

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

Quoted 7 lines

I'm not familiar with the intricacies of virtio-mem, but the basic idea of this series is to *only* allow parallel init during the start up phase (while prealloc_init == false). Once we have parsed all the command line args, we set prealloc_init = true (wait_mem_prealloc_init()) which causes all subsequent calls to qemu_prealloc_mem() to perform initialization synchronously. So, I *think* this covers the virtio-mem use case. Am I missing something?

Good, so this should likely not affect virtio-mem (which also ends up preallocating memory when loading from a vmstate).

To make this all a bit clearer, what about the following to make this:

* Optimize for MADV_POPULATE_WRITE. If we really need support for !MADV_POPULATE_WRITE, this is better added on top later.
* Pass in via a parameter that the caller requests async handling. "bool async" should be good enough. Then, pass that only from the memory backend call, while QEMU is still initializing (we can find a way to make that work).
* Provide a function that waits for any remaining async os-mem-prealloc activity. That is essentially "wait_mem_prealloc_init", but without the special internal flag handling.

Further, I do wonder if we want to make that behavior configurable. For example, one might want to initialize backends sequentially using 16 threads max, instead of consuming multiple times 16 threads concurrently. Could either be a machine option or a memory backend option (async-prealloc=on). Once hotplugging such backends, we would disable it for now, but could allow it in the future.

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

Quoted 176 lines · David Hildenbrand
Quoted 137 lines · Mark Kanda
Quoted 125 lines · David Hildenbrand
Quoted 124 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
   util/oslib-posix.c | 104 +++++++++++++++++++++++++++++----------------
   1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +4 -1
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
     struct MemsetThread;
   +static QLIST_HEAD(, MemsetContext) memset_contexts =
+    QLIST_HEAD_INITIALIZER(memset_contexts);
+
   typedef struct MemsetContext {
       bool all_threads_created;
       bool any_thread_failed;
       struct MemsetThread *threads;
       int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
   } MemsetContext;
     struct MemsetThread {
@@ -81,7 +85,7 @@ struct MemsetThread {
   typedef struct MemsetThread MemsetThread;
     /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
   struct sigaction sigbus_oldact;
   static QemuMutex sigbus_mutex;
   @@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
   #endif /* CONFIG_LINUX */

  {
      int i;
+    MemsetContext *context;
        if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) { -            MemsetThread *thread = &sigbus_memset_context->threads[i]; +        QLIST_FOREACH(context, &memset_contexts, next) {
+            for (i = 0; i < context->num_threads; i++) {
+                MemsetThread *thread = &context->threads[i];
  -            if (qemu_thread_is_self(&thread->pgthread)) {
-                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) { +                    siglongjmp(thread->env, 1);
+                }
              }
          }
      }

util/oslib-posix.c +17 -16
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
                              bool use_madv_populate_write)
   {
       static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages,
 max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
       size_t numpages_per_thread, leftover;
       void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
       char *addr = area;
   +    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads);
+
       if (g_once_init_enter(&initialized)) {
           qemu_mutex_init(&page_mutex);
           qemu_cond_init(&page_cond);
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
         if (use_madv_populate_write) {
           /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
               if (qemu_madvise(area, hpagesize * numpages,
                                QEMU_MADV_POPULATE_WRITE)) {
                   return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
           touch_fn = do_touch_pages;
       }
   -    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i <
 leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i <
 leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
           if (tc) {
-            thread_context_create_thread(tc,
 &context.threads[i].pgthread,
+            thread_context_create_thread(tc,
 &context->threads[i].pgthread,
                                            "touch_pages",
-                                         touch_fn, &context.threads[i],
+                                         touch_fn,
 &context->threads[i],
 QEMU_THREAD_JOINABLE);
           } else {
- qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+ qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
                                  QEMU_THREAD_JOINABLE);
           }
Quoted 9 lines

util/oslib-posix.c static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 480

Quoted 8 lines
util/oslib-posix.c +3 -2
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
       }
   +    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
       if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Thanks David,

Quoted 7 lines
Quoted 4 lines

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Ideally, we're hoping to support this with earlier kernels which don't support MADV_POPULATE _WRITE. But, I will check to see if we really need it.

That's around since Linux 5.14, so please try simplifying.

Quoted 5 lines
Quoted 4 lines

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

Quoted 7 lines

I'm not familiar with the intricacies of virtio-mem, but the basic idea of this series is to *only* allow parallel init during the start up phase (while prealloc_init == false). Once we have parsed all the command line args, we set prealloc_init = true (wait_mem_prealloc_init()) which causes all subsequent calls to qemu_prealloc_mem() to perform initialization synchronously. So, I *think* this covers the virtio-mem use case. Am I missing something?

Good, so this should likely not affect virtio-mem (which also ends up preallocating memory when loading from a vmstate).

To make this all a bit clearer, what about the following to make this:

* Optimize for MADV_POPULATE_WRITE. If we really need support for !MADV_POPULATE_WRITE, this is better added on top later.
* Pass in via a parameter that the caller requests async handling. "bool async" should be good enough. Then, pass that only from the memory backend call, while QEMU is still initializing (we can find a way to make that work).
* Provide a function that waits for any remaining async os-mem-prealloc activity. That is essentially "wait_mem_prealloc_init", but without the special internal flag handling.

Quoted 3 lines

Further, I do wonder if we want to make that behavior configurable. For example, one might want to initialize backends sequentially using 16 threads max, instead of consuming multiple times 16 threads concurrently.

Seems to me that no matter what parallelisation we use (within mem regions, or across mem regions, or a mix of both), we should never use more threads than there are host CPUs.

Can we have a pool of threads sized per available host CPUs that QEMU can access. Then for each memory backend fire off a set of init jobs that get distributed to "appropriate" threads in the pool. By appropriate I mean threads with the same NUMA affinity as the memory backend. This would give parallelisation both within a single large memory region, as well as across memory regions.

eg 4 host CPUs, 3 memory regions (1GB for 1st numa node, 1GB for 2nd numa node, and 1 GB with no numa affinity). If we spread the init work then we end up with

1st thread gets 500MB to init from 1st memory region, and
250MB to init from 3rd memory region

2st thread gets 500MB to init from 1st memory region, and
250MB to init from 3rd memory region

3st thread gets 500MB to init from 2nd memory region, and
250MB to init from 3rd memory region

4th thread gets 500MB to init from 2nd memory region, and
250MB to init from 3rd memory region

Quoted 4 lines

Could
either be a machine option or a memory backend option (async-prealloc=on). Once hotplugging such backends, we would disable it for now, but could allow it in the future.

IMHO "do the right thing" without user config is preferrable to adding yet more cli options here.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

Quoted 177 lines · Daniel P. Berrangé
Quoted 176 lines · David Hildenbrand
Quoted 137 lines · Mark Kanda
Quoted 125 lines · David Hildenbrand
Quoted 124 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
    util/oslib-posix.c | 104 +++++++++++++++++++++++++++++----------------
    1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +4 -1
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
      struct MemsetThread;
    +static QLIST_HEAD(, MemsetContext) memset_contexts =
+    QLIST_HEAD_INITIALIZER(memset_contexts);
+
    typedef struct MemsetContext {
        bool all_threads_created;
        bool any_thread_failed;
        struct MemsetThread *threads;
        int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
    } MemsetContext;
      struct MemsetThread {
@@ -81,7 +85,7 @@ struct MemsetThread {
    typedef struct MemsetThread MemsetThread;
      /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
    struct sigaction sigbus_oldact;
    static QemuMutex sigbus_mutex;
    @@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
    #endif /* CONFIG_LINUX */

  {
      int i;
+    MemsetContext *context;
        if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) { -            MemsetThread *thread = &sigbus_memset_context->threads[i]; +        QLIST_FOREACH(context, &memset_contexts, next) {
+            for (i = 0; i < context->num_threads; i++) {
+                MemsetThread *thread = &context->threads[i];
  -            if (qemu_thread_is_self(&thread->pgthread)) {
-                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) { +                    siglongjmp(thread->env, 1);
+                }
              }
          }
      }

util/oslib-posix.c +17 -16
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
                               bool use_madv_populate_write)
    {
        static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages,
 max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
        size_t numpages_per_thread, leftover;
        void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
        char *addr = area;
    +    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads);
+
        if (g_once_init_enter(&initialized)) {
            qemu_mutex_init(&page_mutex);
            qemu_cond_init(&page_cond);
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
          if (use_madv_populate_write) {
            /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
                if (qemu_madvise(area, hpagesize * numpages,
                                 QEMU_MADV_POPULATE_WRITE)) {
                    return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
            touch_fn = do_touch_pages;
        }
    -    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i <
 leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i <
 leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
            if (tc) {
-            thread_context_create_thread(tc,
 &context.threads[i].pgthread,
+            thread_context_create_thread(tc,
 &context->threads[i].pgthread,
                                             "touch_pages",
-                                         touch_fn, &context.threads[i],
+                                         touch_fn,
 &context->threads[i],
 QEMU_THREAD_JOINABLE);
            } else {
- qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+ qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
                                   QEMU_THREAD_JOINABLE);
            }
Quoted 9 lines

util/oslib-posix.c static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 480

Quoted 8 lines
util/oslib-posix.c +3 -2
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
        }
    +    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
        if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Thanks David,

Quoted 7 lines
Quoted 4 lines

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Ideally, we're hoping to support this with earlier kernels which don't support MADV_POPULATE _WRITE. But, I will check to see if we really need it.

That's around since Linux 5.14, so please try simplifying.

Quoted 5 lines
Quoted 4 lines

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

Quoted 7 lines

I'm not familiar with the intricacies of virtio-mem, but the basic idea of this series is to *only* allow parallel init during the start up phase (while prealloc_init == false). Once we have parsed all the command line args, we set prealloc_init = true (wait_mem_prealloc_init()) which causes all subsequent calls to qemu_prealloc_mem() to perform initialization synchronously. So, I *think* this covers the virtio-mem use case. Am I missing something?

Good, so this should likely not affect virtio-mem (which also ends up preallocating memory when loading from a vmstate).

To make this all a bit clearer, what about the following to make this:

* Optimize for MADV_POPULATE_WRITE. If we really need support for !MADV_POPULATE_WRITE, this is better added on top later.
* Pass in via a parameter that the caller requests async handling. "bool async" should be good enough. Then, pass that only from the memory backend call, while QEMU is still initializing (we can find a way to make that work).
* Provide a function that waits for any remaining async os-mem-prealloc activity. That is essentially "wait_mem_prealloc_init", but without the special internal flag handling.

Quoted 7 lines
Quoted 3 lines

Further, I do wonder if we want to make that behavior configurable. For example, one might want to initialize backends sequentially using 16 threads max, instead of consuming multiple times 16 threads concurrently.

Seems to me that no matter what parallelisation we use (within mem regions, or across mem regions, or a mix of both), we should never use more threads than there are host CPUs.

Yes. It gets tricky with multipe NUMA nodes, though. And that's what's being requested here.

Quoted 10 lines

Can we have a pool of threads sized per available host CPUs that QEMU can access. Then for each memory backend fire off a set of init jobs that get distributed to "appropriate" threads in the pool. By appropriate I mean threads with the same NUMA affinity as the memory backend. This would give parallelisation both within a single large memory region, as well as across memory regions.

Quoted 3 lines

eg 4 host CPUs, 3 memory regions (1GB for 1st numa node, 1GB for 2nd numa node, and 1 GB with no numa affinity). If we spread the init work then we end up with

I'll note that having a mixture of numa + no numa is not a common config we should try to optimize.

Quoted 4 lines

1st thread gets 500MB to init from 1st memory region, and
250MB to init from 3rd memory region

Quoted 8 lines

2st thread gets 500MB to init from 1st memory region, and
250MB to init from 3rd memory region

3st thread gets 500MB to init from 2nd memory region, and
250MB to init from 3rd memory region

4th thread gets 500MB to init from 2nd memory region, and
250MB to init from 3rd memory region

We do have prealloc contexts that are used to craft new CPUs with the right NUMA affinity. Prealloc contexts are set for memory backends. So extending prealloc context to limit/reuse threads and do the pooling might be possible.

Quoted 8 lines
Quoted 5 lines

Could
either be a machine option or a memory backend option (async-prealloc=on). Once hotplugging such backends, we would disable it for now, but could allow it in the future.

IMHO "do the right thing" without user config is preferrable to adding yet more cli options here.

Sure, if there is an easy way.

Signature · 3 lines
Cheers,

David / dhildenb
author
#7 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

Quoted 228 lines · David Hildenbrand
Quoted 180 lines · Daniel P. Berrangé
Quoted 179 lines · David Hildenbrand
Quoted 140 lines · Mark Kanda
Quoted 128 lines · David Hildenbrand
Quoted 127 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
    util/oslib-posix.c | 104
+++++++++++++++++++++++++++++----------------

    1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +0 -0
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
       struct MemsetThread;

    +static QLIST_HEAD(, MemsetContext) memset_contexts = +    QLIST_HEAD_INITIALIZER(memset_contexts);
+
    typedef struct MemsetContext {
        bool all_threads_created;
        bool any_thread_failed;
        struct MemsetThread *threads;
        int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
    } MemsetContext;
      struct MemsetThread {

util/oslib-posix.c +1 -1
@@ -81,7 +85,7 @@ struct MemsetThread {
     typedef struct MemsetThread MemsetThread;
       /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
     struct sigaction sigbus_oldact;
     static QemuMutex sigbus_mutex;
     @@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
     #endif /* CONFIG_LINUX */

    {
        int i;
+    MemsetContext *context;
          if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) { -            MemsetThread *thread = &sigbus_memset_context->threads[i];
+        QLIST_FOREACH(context, &memset_contexts, next) { +            for (i = 0; i < context->num_threads; i++) { +                MemsetThread *thread = &context->threads[i]; -            if (qemu_thread_is_self(&thread->pgthread)) { -                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) { +                    siglongjmp(thread->env, 1);
+                }
                }
            }
        }

util/oslib-posix.c +2 -4
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
                                bool use_madv_populate_write)
     {
         static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages,
 max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
         size_t numpages_per_thread, leftover;
         void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
         char *addr = area;

    +    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads); +
        if (g_once_init_enter(&initialized)) { qemu_mutex_init(&page_mutex); qemu_cond_init(&page_cond);

util/oslib-posix.c +13 -12
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
           if (use_madv_populate_write) {
             /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
                 if (qemu_madvise(area, hpagesize * numpages,
 QEMU_MADV_POPULATE_WRITE)) {
                     return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
             touch_fn = do_touch_pages;
         }
     -    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i <
 leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i <
 leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
             if (tc) {
-            thread_context_create_thread(tc,
 &context.threads[i].pgthread,
+            thread_context_create_thread(tc,
 &context->threads[i].pgthread,
 "touch_pages",
-                                         touch_fn, 
 &context.threads[i],
+                                         touch_fn,
 &context->threads[i],
 QEMU_THREAD_JOINABLE);
             } else {
- qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+ qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
 QEMU_THREAD_JOINABLE);
             }
Quoted 9 lines

util/oslib-posix.c static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 480

Quoted 8 lines
util/oslib-posix.c +3 -2
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
         }
     +    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
         if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Thanks David,

Quoted 7 lines
Quoted 4 lines

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Ideally, we're hoping to support this with earlier kernels which don't support MADV_POPULATE _WRITE. But, I will check to see if we really need it.

That's around since Linux 5.14, so please try simplifying.

Quoted 5 lines
Quoted 4 lines

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

Quoted 7 lines

I'm not familiar with the intricacies of virtio-mem, but the basic idea of this series is to *only* allow parallel init during the start up phase (while prealloc_init == false). Once we have parsed all the command line args, we set prealloc_init = true (wait_mem_prealloc_init()) which causes all subsequent calls to qemu_prealloc_mem() to perform initialization synchronously. So, I *think* this covers the virtio-mem use case. Am I missing something?

Good, so this should likely not affect virtio-mem (which also ends up preallocating memory when loading from a vmstate).

To make this all a bit clearer, what about the following to make this:

* Optimize for MADV_POPULATE_WRITE. If we really need support for !MADV_POPULATE_WRITE, this is better added on top later.
* Pass in via a parameter that the caller requests async handling. "bool async" should be good enough. Then, pass that only from the memory backend call, while QEMU is still initializing (we can find a way to make that work).
* Provide a function that waits for any remaining async os-mem-prealloc activity. That is essentially "wait_mem_prealloc_init", but without the special internal flag handling.

Quoted 7 lines
Quoted 3 lines

Further, I do wonder if we want to make that behavior configurable. For example, one might want to initialize backends sequentially using 16 threads max, instead of consuming multiple times 16 threads concurrently.

Seems to me that no matter what parallelisation we use (within mem regions, or across mem regions, or a mix of both), we should never use more threads than there are host CPUs.

Yes. It gets tricky with multipe NUMA nodes, though. And that's what's being requested here.

Quoted 10 lines

Can we have a pool of threads sized per available host CPUs that QEMU can access. Then for each memory backend fire off a set of init jobs that get distributed to "appropriate" threads in the pool. By appropriate I mean threads with the same NUMA affinity as the memory backend. This would give parallelisation both within a single large memory region, as well as across memory regions.

Quoted 3 lines

eg 4 host CPUs, 3 memory regions (1GB for 1st numa node, 1GB for 2nd numa node, and 1 GB with no numa affinity). If we spread the init work then we end up with

I'll note that having a mixture of numa + no numa is not a common config we should try to optimize.

Quoted 4 lines

  1st thread gets 500MB to init from 1st memory region, and 250MB to init from 3rd memory region

Quoted 8 lines

  2st thread gets 500MB to init from 1st memory region, and 250MB to init from 3rd memory region

  3st thread gets 500MB to init from 2nd memory region, and 250MB to init from 3rd memory region

  4th thread gets 500MB to init from 2nd memory region, and 250MB to init from 3rd memory region

We do have prealloc contexts that are used to craft new CPUs with the right NUMA affinity. Prealloc contexts are set for memory backends. So extending prealloc context to limit/reuse threads and do the pooling might be possible.

Quoted 6 lines
Quoted 5 lines

Could
either be a machine option or a memory backend option (async-prealloc=on). Once hotplugging such backends, we would disable it for now, but could allow it in the future.

Quoted 5 lines
Quoted 2 lines

IMHO "do the right thing" without user config is preferrable to adding yet more cli options here.

Sure, if there is an easy way.

Thank you David and Daniel for your feedback. I plan to address your suggestions in v2 (including optimizing for MADV_POPULATE_WRITE).

Best regards,
-Mark

author
#8 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

In reply to David Hildenbrand
Quoted 214 lines · David Hildenbrand
Quoted 180 lines · Daniel P. Berrangé
Quoted 179 lines · David Hildenbrand
Quoted 140 lines · Mark Kanda
Quoted 128 lines · David Hildenbrand
Quoted 127 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts (used in a subsequent patch).

---
    util/oslib-posix.c | 104
+++++++++++++++++++++++++++++----------------

    1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +0 -0
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
       struct MemsetThread;

    +static QLIST_HEAD(, MemsetContext) memset_contexts = +    QLIST_HEAD_INITIALIZER(memset_contexts);
+
    typedef struct MemsetContext {
        bool all_threads_created;
        bool any_thread_failed;
        struct MemsetThread *threads;
        int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
    } MemsetContext;
      struct MemsetThread {

util/oslib-posix.c +1 -1
@@ -81,7 +85,7 @@ struct MemsetThread {
     typedef struct MemsetThread MemsetThread;
       /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
     struct sigaction sigbus_oldact;
     static QemuMutex sigbus_mutex;
     @@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
     #endif /* CONFIG_LINUX */

    {
        int i;
+    MemsetContext *context;
          if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) { -            MemsetThread *thread = &sigbus_memset_context->threads[i];
+        QLIST_FOREACH(context, &memset_contexts, next) { +            for (i = 0; i < context->num_threads; i++) { +                MemsetThread *thread = &context->threads[i]; -            if (qemu_thread_is_self(&thread->pgthread)) { -                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) { +                    siglongjmp(thread->env, 1);
+                }
                }
            }
        }

util/oslib-posix.c +2 -4
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
                                bool use_madv_populate_write)
     {
         static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages,
 max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
         size_t numpages_per_thread, leftover;
         void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
         char *addr = area;

    +    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads); +
        if (g_once_init_enter(&initialized)) { qemu_mutex_init(&page_mutex); qemu_cond_init(&page_cond);

util/oslib-posix.c +13 -12
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
           if (use_madv_populate_write) {
             /* Avoid creating a single thread for MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
                 if (qemu_madvise(area, hpagesize * numpages,
 QEMU_MADV_POPULATE_WRITE)) {
                     return -errno;
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
             touch_fn = do_touch_pages;
         }
     -    context.threads = g_new0(MemsetThread, context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i <
 leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i <
 leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
             if (tc) {
-            thread_context_create_thread(tc,
 &context.threads[i].pgthread,
+            thread_context_create_thread(tc,
 &context->threads[i].pgthread,
 "touch_pages",
-                                         touch_fn, 
 &context.threads[i],
+                                         touch_fn,
 &context->threads[i],
 QEMU_THREAD_JOINABLE);
             } else {
- qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+ qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
 QEMU_THREAD_JOINABLE);
             }
Quoted 9 lines

util/oslib-posix.c static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 480

Quoted 8 lines
util/oslib-posix.c +3 -2
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
         }
     +    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
         if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Thanks David,

Quoted 7 lines
Quoted 4 lines

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Ideally, we're hoping to support this with earlier kernels which don't support MADV_POPULATE _WRITE. But, I will check to see if we really need it.

That's around since Linux 5.14, so please try simplifying.

Quoted 5 lines
Quoted 4 lines

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

Quoted 7 lines

I'm not familiar with the intricacies of virtio-mem, but the basic idea of this series is to *only* allow parallel init during the start up phase (while prealloc_init == false). Once we have parsed all the command line args, we set prealloc_init = true (wait_mem_prealloc_init()) which causes all subsequent calls to qemu_prealloc_mem() to perform initialization synchronously. So, I *think* this covers the virtio-mem use case. Am I missing something?

Good, so this should likely not affect virtio-mem (which also ends up preallocating memory when loading from a vmstate).

To make this all a bit clearer, what about the following to make this:

* Optimize for MADV_POPULATE_WRITE. If we really need support for !MADV_POPULATE_WRITE, this is better added on top later.
* Pass in via a parameter that the caller requests async handling. "bool async" should be good enough. Then, pass that only from the memory backend call, while QEMU is still initializing (we can find a way to make that work).
* Provide a function that waits for any remaining async os-mem-prealloc activity. That is essentially "wait_mem_prealloc_init", but without the special internal flag handling.

Quoted 7 lines
Quoted 3 lines

Further, I do wonder if we want to make that behavior configurable. For example, one might want to initialize backends sequentially using 16 threads max, instead of consuming multiple times 16 threads concurrently.

Seems to me that no matter what parallelisation we use (within mem regions, or across mem regions, or a mix of both), we should never use more threads than there are host CPUs.

Yes. It gets tricky with multipe NUMA nodes, though. And that's what's being requested here.

Quoted 10 lines

Can we have a pool of threads sized per available host CPUs that QEMU can access. Then for each memory backend fire off a set of init jobs that get distributed to "appropriate" threads in the pool. By appropriate I mean threads with the same NUMA affinity as the memory backend. This would give parallelisation both within a single large memory region, as well as across memory regions.

Quoted 3 lines

eg 4 host CPUs, 3 memory regions (1GB for 1st numa node, 1GB for 2nd numa node, and 1 GB with no numa affinity). If we spread the init work then we end up with

I'll note that having a mixture of numa + no numa is not a common config we should try to optimize.

Quoted 7 lines

  1st thread gets 500MB to init from 1st memory region, and 250MB to init from 3rd memory region

  2st thread gets 500MB to init from 1st memory region, and 250MB to init from 3rd memory region

Quoted 8 lines
Quoted 5 lines

  3st thread gets 500MB to init from 2nd memory region, and 250MB to init from 3rd memory region

  4th thread gets 500MB to init from 2nd memory region, and 250MB to init from 3rd memory region

We do have prealloc contexts that are used to craft new CPUs with the right NUMA affinity. Prealloc contexts are set for memory backends. So extending prealloc context to limit/reuse threads and do the pooling might be possible.

From a NUMA perspective, I feel prealloc contexts already supports what we want - i.e. we can set the number of prealloc threads and their node affinity per memory region. As such, unless you feel strongly about it, I'm inclined to leave additional enhancements (thread pool) as a future project.

Since multiple NUMA node configs are the primary motivation for parallel init, I do wonder if it should *only* be used in conjunction with prealloc context. Perhaps we should have parallel init only enabled for memory regions with prealloc context?

Thanks/regards,
-Mark

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

Could
either be a machine option or a memory backend option (async-prealloc=on). Once hotplugging such backends, we would disable it for now, but could allow it in the future.

IMHO "do the right thing" without user config is preferrable to adding yet more cli options here.

Sure, if there is an easy way.

#9 lore raw

Re: [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads

Quoted 236 lines · Mark Kanda
Quoted 226 lines · David Hildenbrand
Quoted 186 lines · Daniel P. Berrangé
Quoted 185 lines · David Hildenbrand
Quoted 143 lines · Mark Kanda
Quoted 131 lines · David Hildenbrand
Quoted 130 lines · Mark Kanda

Refactor the memory prealloc threads support:
- Make memset context a global qlist
- Move the memset thread join/cleanup code to a separate routine

This is functionally equivalent and facilitates multiple memset contexts
(used in a subsequent patch).

---
     util/oslib-posix.c | 104
+++++++++++++++++++++++++++++----------------
     1 file changed, 68 insertions(+), 36 deletions(-)
util/oslib-posix.c +4 -1
index e86fd64e09..293297ac6c 100644
@@ -63,11 +63,15 @@
       struct MemsetThread;
     +static QLIST_HEAD(, MemsetContext) memset_contexts =
+    QLIST_HEAD_INITIALIZER(memset_contexts);
+
     typedef struct MemsetContext {
         bool all_threads_created;
         bool any_thread_failed;
         struct MemsetThread *threads;
         int num_threads;
+    QLIST_ENTRY(MemsetContext) next;
     } MemsetContext;
       struct MemsetThread {
@@ -81,7 +85,7 @@ struct MemsetThread {
     typedef struct MemsetThread MemsetThread;
       /* used by sigbus_handler() */
-static MemsetContext *sigbus_memset_context;
+static bool sigbus_memset_context;
     struct sigaction sigbus_oldact;
     static QemuMutex sigbus_mutex;
     @@ -295,13 +299,16 @@ static void sigbus_handler(int signal)
     #endif /* CONFIG_LINUX */

    {
        int i;
+    MemsetContext *context;
          if (sigbus_memset_context) {
-        for (i = 0; i < sigbus_memset_context->num_threads; i++) { -            MemsetThread *thread = &sigbus_memset_context->threads[i];
+        QLIST_FOREACH(context, &memset_contexts, next) { +            for (i = 0; i < context->num_threads; i++) { +                MemsetThread *thread = &context->threads[i];
    -            if (qemu_thread_is_self(&thread->pgthread)) {
-                siglongjmp(thread->env, 1);
+                if (qemu_thread_is_self(&thread->pgthread)) { +                    siglongjmp(thread->env, 1);
+                }
                }
            }
        }

util/oslib-posix.c +4 -4
@@ -417,14 +424,15 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
                                bool use_madv_populate_write)
     {
         static gsize initialized = 0;
-    MemsetContext context = {
-        .num_threads = get_memset_num_threads(hpagesize, numpages,
 max_threads),
-    };
+    MemsetContext *context = g_malloc0(sizeof(MemsetContext));
         size_t numpages_per_thread, leftover;
         void *(*touch_fn)(void *);
-    int ret = 0, i = 0;
+    int i = 0;
         char *addr = area;
     +    context->num_threads =
+        get_memset_num_threads(hpagesize, numpages, max_threads);
+
         if (g_once_init_enter(&initialized)) {
             qemu_mutex_init(&page_mutex);
             qemu_cond_init(&page_cond);
@@ -433,7 +441,7 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
           if (use_madv_populate_write) {
             /* Avoid creating a single thread for

MADV_POPULATE_WRITE */
-        if (context.num_threads == 1) {
+        if (context->num_threads == 1) {
                if (qemu_madvise(area, hpagesize * numpages,
QEMU_MADV_POPULATE_WRITE)) {
                    return -errno;

util/oslib-posix.c +12 -11
@@ -445,49 +453,74 @@ static int touch_all_pages(char *area, size_t
 hpagesize, size_t numpages,
             touch_fn = do_touch_pages;
         }
     -    context.threads = g_new0(MemsetThread,
 context.num_threads);
-    numpages_per_thread = numpages / context.num_threads;
-    leftover = numpages % context.num_threads;
-    for (i = 0; i < context.num_threads; i++) {
-        context.threads[i].addr = addr;
-        context.threads[i].numpages = numpages_per_thread + (i <
 leftover);
-        context.threads[i].hpagesize = hpagesize;
-        context.threads[i].context = &context;
+    context->threads = g_new0(MemsetThread, context->num_threads);
+    numpages_per_thread = numpages / context->num_threads;
+    leftover = numpages % context->num_threads;
+    for (i = 0; i < context->num_threads; i++) {
+        context->threads[i].addr = addr;
+        context->threads[i].numpages = numpages_per_thread + (i <
 leftover);
+        context->threads[i].hpagesize = hpagesize;
+        context->threads[i].context = context;
             if (tc) {
-            thread_context_create_thread(tc,
 &context.threads[i].pgthread,
+            thread_context_create_thread(tc,
 &context->threads[i].pgthread,
 "touch_pages",
-                                         touch_fn,
 &context.threads[i],
+                                         touch_fn,
 &context->threads[i],
 QEMU_THREAD_JOINABLE);
             } else {
- qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
-                               touch_fn, &context.threads[i],
+ qemu_thread_create(&context->threads[i].pgthread, "touch_pages",
+                               touch_fn, &context->threads[i],
 QEMU_THREAD_JOINABLE);
             }
Quoted 9 lines

util/oslib-posix.c static int touch_all_pages(char *area, size_t · line 487

Quoted 8 lines
util/oslib-posix.c +3 -2
-        addr += context.threads[i].numpages * hpagesize;
+        addr += context->threads[i].numpages * hpagesize;
         }
     +    QLIST_INSERT_HEAD(&memset_contexts, context, next);
+
         if (!use_madv_populate_write) {
-        sigbus_memset_context = &context;
+        sigbus_memset_context = true;

Thanks David,

Quoted 8 lines
Quoted 4 lines

Could we just use the sigbus handling alone and support parallel init only when using MADV_POPULATE_WRITE where we don't have to mess with signal handlers?

Ideally, we're hoping to support this with earlier kernels which don't support MADV_POPULATE _WRITE. But, I will check to see if we really need it.

That's around since Linux 5.14, so please try simplifying.

Quoted 5 lines
Quoted 4 lines

Further, how do you changes interact with other callers of qemu_prealloc_mem(), like virtio-mem?

Quoted 8 lines

I'm not familiar with the intricacies of virtio-mem, but the basic idea
of this series is to *only* allow parallel init during the start up phase (while prealloc_init == false). Once we have parsed all the command line args, we set prealloc_init = true (wait_mem_prealloc_init()) which causes all subsequent calls to qemu_prealloc_mem() to perform initialization synchronously. So, I *think* this covers the virtio-mem use case. Am I missing something?

Good, so this should likely not affect virtio-mem (which also ends up preallocating memory when loading from a vmstate).

To make this all a bit clearer, what about the following to make this:

* Optimize for MADV_POPULATE_WRITE. If we really need support for !MADV_POPULATE_WRITE, this is better added on top later.
* Pass in via a parameter that the caller requests async handling. "bool
   async" should be good enough. Then, pass that only from the memory backend call, while QEMU is still initializing (we can find a way to make that work).
* Provide a function that waits for any remaining async os-mem-prealloc activity. That is essentially "wait_mem_prealloc_init", but without the special internal flag handling.

Quoted 8 lines
Quoted 4 lines

Further, I do wonder if we want to make that behavior configurable. For example, one might want to initialize backends sequentially using 16 threads
max, instead of consuming multiple times 16 threads concurrently.

Seems to me that no matter what parallelisation we use (within mem regions, or across mem regions, or a mix of both), we should never use more threads than there are host CPUs.

Yes. It gets tricky with multipe NUMA nodes, though. And that's what's being requested here.

Quoted 10 lines

Can we have a pool of threads sized per available host CPUs that QEMU can access. Then for each memory backend fire off a set of init jobs that get distributed to "appropriate" threads in the pool. By appropriate I mean threads with the same NUMA affinity as the memory backend. This would give parallelisation both within a single large memory region, as well as across memory regions.

Quoted 3 lines

eg 4 host CPUs, 3 memory regions (1GB for 1st numa node, 1GB for 2nd numa node, and 1 GB with no numa affinity). If we spread the init work then we end up with

I'll note that having a mixture of numa + no numa is not a common config we should try to optimize.

Quoted 10 lines

  1st thread gets 500MB to init from 1st memory region, and 250MB to init from 3rd memory region

  2st thread gets 500MB to init from 1st memory region, and 250MB to init from 3rd memory region

  3st thread gets 500MB to init from 2nd memory region, and 250MB to init from 3rd memory region

Quoted 8 lines
Quoted 2 lines

  4th thread gets 500MB to init from 2nd memory region, and 250MB to init from 3rd memory region

We do have prealloc contexts that are used to craft new CPUs with the right NUMA affinity. Prealloc contexts are set for memory backends. So extending prealloc context to limit/reuse threads and do the pooling might be possible.

Quoted 5 lines

From a NUMA perspective, I feel prealloc contexts already supports what
we want - i.e. we can set the number of prealloc threads and their node affinity per memory region. As such, unless you feel strongly about it, I'm inclined to leave additional enhancements (thread pool) as a future project.

I tend to agree. It's uncommon to have many memory backends target the same NUMA node such that you would want to use pooling.

Usually, you'd expect in most vNUMA setups to have, say, 1 memory backend per vNUMA node that is mapped to some NUMA nodes in the parent.

Using prealloc contexts you can make sure that preallocating each individual one gets the right CPU affinity. Setting the prealloc threads you can limit the number of threads used per memory backend.

Quoted 4 lines
Quoted 1 line

Since multiple NUMA node configs are the primary motivation for parallel

init, I do wonder if it should *only* be used in conjunction with prealloc context. Perhaps we should have parallel init only enabled for memory regions with prealloc context?

That sounds reasonable.

Signature · 3 lines
Cheers,

David / dhildenb
author
#10 lore raw

PATCH 2/2 oslib-posix: initialize backend memory objects in parallel

In reply to Mark Kanda

QEMU initializes preallocated backend memory as the objects are parsed from the command line. This is not optimal in some cases (e.g. memory spanning multiple numa nodes) because the memory objects are initialized in series.

Allow the initialization to occur in parallel. The performance increase is significant and scales with the number of objects. On a 2 socket Skylake VM with 128GB and 2 init threads per socket (256GB total), the memory init time decreases from ~27 seconds to ~14 seconds.

---
 include/qemu/osdep.h |  6 ++++++
 system/vl.c          |  2 ++
 util/oslib-posix.c   | 46 +++++++++++++++++++++++++++++++++-----------
 util/oslib-win32.c   |  5 +++++
 4 files changed, 48 insertions(+), 11 deletions(-)
include/qemu/osdep.h +6 -0
index d30ba73eda..57185e6309 100644
@@ -682,6 +682,12 @@ typedef struct ThreadContext ThreadContext;
 void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
                        ThreadContext *tc, Error **errp);
 
+/**
+ * Wait for any outstanding memory prealloc initialization
+ * to complete.
+ */
+void wait_mem_prealloc_init(void);
+
 /**
  * qemu_get_pid_name:
  * @pid: pid of a process
system/vl.c +2 -0
index 6b87bfa32c..9e04acbb2c 100644
@@ -2010,6 +2010,8 @@ static void qemu_create_late_backends(void)
 
     object_option_foreach_add(object_create_late);
 
+    wait_mem_prealloc_init();
+
     if (tpm_init() < 0) {
         exit(1);
     }
util/oslib-posix.c +35 -11

74 lines not on this page · raw message

util/oslib-win32.c +5 -0
index 55b0189dc3..72e050bee1 100644
@@ -276,6 +276,11 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
     }
 }
 
+void wait_mem_prealloc_init(void)
+{
+    /* not supported */
+}
+
 char *qemu_get_pid_name(pid_t pid)
 {
     /* XXX Implement me */
Signature · 1 line
2.39.3