Skip to contentSkip to conversation

mem-prealloc: reduce large guest start-up and migration time.

PATCH Superseded Jitendra Kolhe 3 participants 7 messages Feb 13, 2017 – Feb 14, 2017 Arm STM32

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mem-prealloc: reduce large guest start-up and migration time.

Using "-mem-prealloc" option for a large guest leads to higher guest start-up and migration time. This is because with "-mem-prealloc" option qemu tries to map every guest page (create address translations), and make sure the pages are available during runtime. virsh/libvirt by default, seems to use "-mem-prealloc" option in case the guest is configured to use huge pages. The patch tries to map all guest pages simultaneously by spawning multiple threads. Currently limiting the change to QEMU library functions on POSIX compliant host only, as we are not sure if the problem exists on win32. Below are some stats with "-mem-prealloc" option for guest configured to use huge pages.

------------------------------------------------------------------------
Idle Guest | Start-up time | Migration time
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - single threaded (existing code)
------------------------------------------------------------------------

64 Core - 4TB   | 54m11.796s    | 75m43.843s
64 Core - 1TB   | 8m56.576s     | 14m29.049s
64 Core - 256GB | 2m11.245s     | 3m26.598s
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 8 threads
------------------------------------------------------------------------
64 Core - 4TB   | 5m1.027s      | 34m10.565s
64 Core - 1TB   | 1m10.366s     | 8m28.188s
64 Core - 256GB | 0m19.040s     | 2m10.148s
-----------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 16 threads
-----------------------------------------------------------------------
64 Core - 4TB   | 1m58.970s     | 31m43.400s
64 Core - 1TB   | 0m39.885s     | 7m55.289s
64 Core - 256GB | 0m11.960s     | 2m0.135s

-----------------------------------------------------------------------

Changed in v2:
- modify number of memset threads spawned to min(smp_cpus, 16).
- removed 64GB memory restriction for spawning memset threads.

---
 backends/hostmem.c   |  4 ++--
 exec.c               |  2 +-
 include/qemu/osdep.h |  3 ++-
 util/oslib-posix.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++-----
 util/oslib-win32.c   |  3 ++-
 5 files changed, 69 insertions(+), 11 deletions(-)
backends/hostmem.c +2 -2
index 7f5de70..162c218 100644
@@ -224,7 +224,7 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, &local_err);
+        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             return;
@@ -328,7 +328,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            &local_err);
+                            smp_cpus, &local_err);
             if (local_err) {
                 goto out;
             }
exec.c +1 -1
index 8b9ed73..53afcd2 100644
@@ -1379,7 +1379,7 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     if (mem_prealloc) {
-        os_mem_prealloc(fd, area, memory, errp);
+        os_mem_prealloc(fd, area, memory, smp_cpus, errp);
         if (errp && *errp) {
             goto error;
         }
include/qemu/osdep.h +2 -1
index 56c9e22..fb1d22b 100644
@@ -401,7 +401,8 @@ unsigned long qemu_getauxval(unsigned long type);
 
 void qemu_set_tty_echo(int fd, bool echo);
 
-void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
+void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
+                     Error **errp);
 
 int qemu_read_password(char *buf, int buf_size);
 
util/oslib-posix.c +62 -6

89 lines not on this page · raw message

util/oslib-win32.c +2 -1
index 0b1890f..80e4668 100644
@@ -541,7 +541,8 @@ int getpagesize(void)
     return system_info.dwPageSize;
 }
 
-void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)
+void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
+                     Error **errp)
 {
     int i;
     size_t pagesize = getpagesize();
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 100 lines · On Mon, 13 Feb 2017 14:30:56 +0530 Jitendra Kolhe <[email protected]>…

Using "-mem-prealloc" option for a large guest leads to higher guest start-up and migration time. This is because with "-mem-prealloc" option qemu tries to map every guest page (create address translations), and make sure the pages are available during runtime. virsh/libvirt by default, seems to use "-mem-prealloc" option in case the guest is configured to use huge pages. The patch tries to map all guest pages simultaneously by spawning multiple threads. Currently limiting the change to QEMU library functions on POSIX compliant host only, as we are not sure if the problem exists on win32. Below are some stats with "-mem-prealloc" option for guest configured to use huge pages.

------------------------------------------------------------------------
Idle Guest | Start-up time | Migration time
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - single threaded (existing code)
------------------------------------------------------------------------

64 Core - 4TB   | 54m11.796s    | 75m43.843s
64 Core - 1TB   | 8m56.576s     | 14m29.049s
64 Core - 256GB | 2m11.245s     | 3m26.598s
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 8 threads
------------------------------------------------------------------------
64 Core - 4TB   | 5m1.027s      | 34m10.565s
64 Core - 1TB   | 1m10.366s     | 8m28.188s
64 Core - 256GB | 0m19.040s     | 2m10.148s
-----------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 16 threads
-----------------------------------------------------------------------
64 Core - 4TB   | 1m58.970s     | 31m43.400s
64 Core - 1TB   | 0m39.885s     | 7m55.289s
64 Core - 256GB | 0m11.960s     | 2m0.135s

-----------------------------------------------------------------------

Changed in v2:
- modify number of memset threads spawned to min(smp_cpus, 16).
- removed 64GB memory restriction for spawning memset threads.

---
 backends/hostmem.c   |  4 ++--
 exec.c               |  2 +-
 include/qemu/osdep.h |  3 ++-
 util/oslib-posix.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++-----
 util/oslib-win32.c   |  3 ++-
 5 files changed, 69 insertions(+), 11 deletions(-)
backends/hostmem.c +2 -2
index 7f5de70..162c218 100644
@@ -224,7 +224,7 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, &local_err);
+        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             return;
@@ -328,7 +328,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            &local_err);
+                            smp_cpus, &local_err);
             if (local_err) {
                 goto out;
             }
exec.c +1 -1
index 8b9ed73..53afcd2 100644
@@ -1379,7 +1379,7 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     if (mem_prealloc) {
-        os_mem_prealloc(fd, area, memory, errp);
+        os_mem_prealloc(fd, area, memory, smp_cpus, errp);
         if (errp && *errp) {
             goto error;
         }
include/qemu/osdep.h +2 -1
index 56c9e22..fb1d22b 100644
@@ -401,7 +401,8 @@ unsigned long qemu_getauxval(unsigned long type);
 
 void qemu_set_tty_echo(int fd, bool echo);
 
-void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
+void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
+                     Error **errp);
 
 int qemu_read_password(char *buf, int buf_size);
 
util/oslib-posix.c +0 -0
index f631464..17da029 100644

util/oslib-posix.c line 58

Quoted 4 lines
util/oslib-posix.c +1 -0
@@ -55,6 +55,16 @@
 #include "qemu/error-report.h"
 #endif
 
+#define MAX_MEM_PREALLOC_THREAD_COUNT 16

running with -smp 16 or bigger on host with less than 16 cpus it would be not quite optimal.
Why not to change MAX_MEM_PREALLOC_THREAD_COUNT constant to something like sysconf(_SC_NPROCESSORS_ONLN)

Quoted 101 lines
util/oslib-posix.c +61 -6
+struct PageRange {
+    char *addr;
+    uint64_t numpages;
+    uint64_t hpagesize;
+};
+typedef struct PageRange PageRange;
+
+static PageRange *page_range;
+
 int qemu_get_thread_id(void)
 {
 #if defined(__linux__)
@@ -323,7 +333,56 @@ static void sigbus_handler(int signal)
     siglongjmp(sigjump, 1);
 }
 
-void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)
+static void *do_touch_pages(void *arg)
+{
+    PageRange *range = (PageRange *)arg;
+    char *addr = range->addr;
+    uint64_t numpages = range->numpages;
+    uint64_t hpagesize = range->hpagesize;
+    int i = 0;
+
+    for (i = 0; i < numpages; i++) {
+        memset(addr, 0, 1);
+        addr += hpagesize;
+    }
+    return NULL;
+}
+
+static void touch_all_pages(char *area, size_t hpagesize, size_t numpages,
+                            int smp_cpus)
+{
+    QemuThread page_threads[MAX_MEM_PREALLOC_THREAD_COUNT];
+    uint64_t numpages_per_thread, size_per_thread;
+    char *addr = area;
+    int i = 0;
+    int num_threads = MIN(smp_cpus, MAX_MEM_PREALLOC_THREAD_COUNT);
+
+    page_range = g_new0(PageRange, num_threads);
+    numpages_per_thread = (numpages / num_threads);
+    size_per_thread = (hpagesize * numpages_per_thread);
+    for (i = 0; i < (num_threads - 1); i++) {
+        page_range[i].addr = addr;
+        page_range[i].numpages = numpages_per_thread;
+        page_range[i].hpagesize = hpagesize;
+        qemu_thread_create(page_threads + i, "touch_pages",
+                           do_touch_pages, (page_range + i),
+                           QEMU_THREAD_JOINABLE);
+        addr += size_per_thread;
+        numpages -= numpages_per_thread;
+    }
+    for (i = 0; i < numpages; i++) {
+        memset(addr, 0, 1);
+        addr += hpagesize;
+    }
+    for (i = 0; i < (num_threads - 1); i++) {
+        qemu_thread_join(page_threads + i);
+    }
+    g_free(page_range);
+    page_range = NULL;
+}
+
+void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
+                     Error **errp)
 {
     int ret;
     struct sigaction act, oldact;
@@ -349,14 +408,11 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)
         error_setg(errp, "os_mem_prealloc: Insufficient free host memory "
             "pages available to allocate guest RAM\n");
     } else {
-        int i;
         size_t hpagesize = qemu_fd_getpagesize(fd);
         size_t numpages = DIV_ROUND_UP(memory, hpagesize);
 
-        /* MAP_POPULATE silently ignores failures */
-        for (i = 0; i < numpages; i++) {
-            memset(area + (hpagesize * i), 0, 1);
-        }
+        /* touch pages simultaneously */
+        touch_all_pages(area, hpagesize, numpages, smp_cpus);
     }
 
     ret = sigaction(SIGBUS, &oldact, NULL);
util/oslib-win32.c +2 -1
index 0b1890f..80e4668 100644
@@ -541,7 +541,8 @@ int getpagesize(void)
     return system_info.dwPageSize;
 }
 
-void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)
+void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
+                     Error **errp)
 {
     int i;
     size_t pagesize = getpagesize();
#2 lore raw
Quoted 106 lines · Igor Mammedov
Quoted 103 lines · On Mon, 13 Feb 2017 14:30:56 +0530 Jitendra Kolhe <[email protected]>…

Using "-mem-prealloc" option for a large guest leads to higher guest start-up and migration time. This is because with "-mem-prealloc" option qemu tries to map every guest page (create address translations), and make sure the pages are available during runtime. virsh/libvirt by default, seems to use "-mem-prealloc" option in case the guest is configured to use huge pages. The patch tries to map all guest pages simultaneously by spawning multiple threads. Currently limiting the change to QEMU library functions on POSIX compliant host only, as we are not sure if the problem exists on win32. Below are some stats with "-mem-prealloc" option for guest configured to use huge pages.

------------------------------------------------------------------------
Idle Guest | Start-up time | Migration time
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - single threaded (existing code)
------------------------------------------------------------------------

64 Core - 4TB   | 54m11.796s    | 75m43.843s
64 Core - 1TB   | 8m56.576s     | 14m29.049s
64 Core - 256GB | 2m11.245s     | 3m26.598s
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 8 threads
------------------------------------------------------------------------
64 Core - 4TB   | 5m1.027s      | 34m10.565s
64 Core - 1TB   | 1m10.366s     | 8m28.188s
64 Core - 256GB | 0m19.040s     | 2m10.148s
-----------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 16 threads
-----------------------------------------------------------------------
64 Core - 4TB   | 1m58.970s     | 31m43.400s
64 Core - 1TB   | 0m39.885s     | 7m55.289s
64 Core - 256GB | 0m11.960s     | 2m0.135s

-----------------------------------------------------------------------

Changed in v2:
- modify number of memset threads spawned to min(smp_cpus, 16).
- removed 64GB memory restriction for spawning memset threads.

---
 backends/hostmem.c   |  4 ++--
 exec.c               |  2 +-
 include/qemu/osdep.h |  3 ++-
 util/oslib-posix.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++-----
 util/oslib-win32.c   |  3 ++-
 5 files changed, 69 insertions(+), 11 deletions(-)
backends/hostmem.c +2 -2
index 7f5de70..162c218 100644
@@ -224,7 +224,7 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, &local_err);
+        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             return;
@@ -328,7 +328,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            &local_err);
+                            smp_cpus, &local_err);
             if (local_err) {
                 goto out;
             }
exec.c +1 -1
index 8b9ed73..53afcd2 100644
@@ -1379,7 +1379,7 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     if (mem_prealloc) {
-        os_mem_prealloc(fd, area, memory, errp);
+        os_mem_prealloc(fd, area, memory, smp_cpus, errp);
         if (errp && *errp) {
             goto error;
         }
include/qemu/osdep.h +2 -1
index 56c9e22..fb1d22b 100644
@@ -401,7 +401,8 @@ unsigned long qemu_getauxval(unsigned long type);
 
 void qemu_set_tty_echo(int fd, bool echo);
 
-void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
+void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
+                     Error **errp);
 
 int qemu_read_password(char *buf, int buf_size);
 
util/oslib-posix.c +0 -0
index f631464..17da029 100644
@@ -55,6 +55,16 @@
 #include "qemu/error-report.h"
 #endif
 
Quoted 5 lines

util/oslib-posix.c line 58

Quoted 1 line
util/oslib-posix.c +1 -0
+#define MAX_MEM_PREALLOC_THREAD_COUNT 16

running with -smp 16 or bigger on host with less than 16 cpus it would be not quite optimal.
Why not to change MAX_MEM_PREALLOC_THREAD_COUNT constant to something like sysconf(_SC_NPROCESSORS_ONLN)

The point is to not consume more host resources than would otherwise be consumed by running the guest CPUs. ie, if running a KVM guest with -smp 4 on a 16 CPU host, QEMU should not to consume more than 4 pCPUs worth of resource on the host. Using sysconf would cause the consume to consume all host resources, likely harming other guests workloads.

If the person launching QEMU gives a -smp value that's larger than the host CPUs count, then they've already accepted that they're asking QEMU todo more than the host is really capable of. IOW, I don't think we need to special case memsetting for that, since VCPU execution itself is already going to overcommit the host.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#3 lore raw
Quoted 120 lines · On Mon, 13 Feb 2017 11:23:17 +0000 "Daniel P. Berrange" <[email protected]>…
Quoted 106 lines · Igor Mammedov
Quoted 103 lines · On Mon, 13 Feb 2017 14:30:56 +0530 Jitendra Kolhe <[email protected]>…

Using "-mem-prealloc" option for a large guest leads to higher guest start-up and migration time. This is because with "-mem-prealloc" option qemu tries to map every guest page (create address translations), and make sure the pages are available during runtime. virsh/libvirt by default, seems to use "-mem-prealloc" option in case the guest is configured to use huge pages. The patch tries to map all guest pages simultaneously by spawning multiple threads. Currently limiting the change to QEMU library functions on POSIX compliant host only, as we are not sure if the problem exists on win32. Below are some stats with "-mem-prealloc" option for guest configured to use huge pages.

------------------------------------------------------------------------
Idle Guest | Start-up time | Migration time
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - single threaded (existing code)
------------------------------------------------------------------------

64 Core - 4TB   | 54m11.796s    | 75m43.843s
64 Core - 1TB   | 8m56.576s     | 14m29.049s
64 Core - 256GB | 2m11.245s     | 3m26.598s
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 8 threads
------------------------------------------------------------------------
64 Core - 4TB   | 5m1.027s      | 34m10.565s
64 Core - 1TB   | 1m10.366s     | 8m28.188s
64 Core - 256GB | 0m19.040s     | 2m10.148s
-----------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 16 threads
-----------------------------------------------------------------------
64 Core - 4TB   | 1m58.970s     | 31m43.400s
64 Core - 1TB   | 0m39.885s     | 7m55.289s
64 Core - 256GB | 0m11.960s     | 2m0.135s

-----------------------------------------------------------------------

Changed in v2:
- modify number of memset threads spawned to min(smp_cpus, 16).
- removed 64GB memory restriction for spawning memset threads.

---
 backends/hostmem.c   |  4 ++--
 exec.c               |  2 +-
 include/qemu/osdep.h |  3 ++-
 util/oslib-posix.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++-----
 util/oslib-win32.c   |  3 ++-
 5 files changed, 69 insertions(+), 11 deletions(-)
backends/hostmem.c +2 -2
index 7f5de70..162c218 100644
@@ -224,7 +224,7 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, &local_err);
+        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             return;
@@ -328,7 +328,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            &local_err);
+                            smp_cpus, &local_err);
             if (local_err) {
                 goto out;
             }
exec.c +1 -1
index 8b9ed73..53afcd2 100644
@@ -1379,7 +1379,7 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     if (mem_prealloc) {
-        os_mem_prealloc(fd, area, memory, errp);
+        os_mem_prealloc(fd, area, memory, smp_cpus, errp);
         if (errp && *errp) {
             goto error;
         }
include/qemu/osdep.h +2 -1
index 56c9e22..fb1d22b 100644
@@ -401,7 +401,8 @@ unsigned long qemu_getauxval(unsigned long type);
 
 void qemu_set_tty_echo(int fd, bool echo);
 
-void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
+void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
+                     Error **errp);
 
 int qemu_read_password(char *buf, int buf_size);
 
util/oslib-posix.c +0 -0
index f631464..17da029 100644
@@ -55,6 +55,16 @@
 #include "qemu/error-report.h"
 #endif
 
Quoted 5 lines

util/oslib-posix.c line 58

Quoted 1 line
util/oslib-posix.c +1 -0
+#define MAX_MEM_PREALLOC_THREAD_COUNT 16  

running with -smp 16 or bigger on host with less than 16 cpus it would be not quite optimal.
Why not to change MAX_MEM_PREALLOC_THREAD_COUNT constant to something like sysconf(_SC_NPROCESSORS_ONLN)

The point is to not consume more host resources than would otherwise be consumed by running the guest CPUs. ie, if running a KVM guest with -smp 4 on a 16 CPU host, QEMU should not to consume more than 4 pCPUs worth of resource on the host. Using sysconf would cause the consume to consume all host resources, likely harming other guests workloads.

Quoted 5 lines

If the person launching QEMU gives a -smp value that's larger than the host CPUs count, then they've already accepted that they're asking QEMU todo more than the host is really capable of. IOW, I don't think we need to special case memsetting for that, since VCPU execution itself is already going to overcommit the host.

Doing over commit at preallocate time doesn't make much sense,
if MAX_MEM_PREALLOC_THREAD_COUNT is replaced with sysconf(_SC_NPROCESSORS_ONLN) then QEMU will end up with MIN(-smp, sysconf(_SC_NPROCESSORS_ONLN)) which will put cap on upper value and avoid useless over commit at preallocate time.

Quoted 2 lines

Regards,
Daniel

#4 lore raw
Quoted 128 lines · Igor Mammedov
Quoted 120 lines · On Mon, 13 Feb 2017 11:23:17 +0000 "Daniel P. Berrange" <[email protected]>…
Quoted 106 lines · Igor Mammedov
Quoted 103 lines · On Mon, 13 Feb 2017 14:30:56 +0530 Jitendra Kolhe <[email protected]>…

Using "-mem-prealloc" option for a large guest leads to higher guest start-up and migration time. This is because with "-mem-prealloc" option qemu tries to map every guest page (create address translations), and make sure the pages are available during runtime. virsh/libvirt by default, seems to use "-mem-prealloc" option in case the guest is configured to use huge pages. The patch tries to map all guest pages simultaneously by spawning multiple threads. Currently limiting the change to QEMU library functions on POSIX compliant host only, as we are not sure if the problem exists on win32. Below are some stats with "-mem-prealloc" option for guest configured to use huge pages.

------------------------------------------------------------------------
Idle Guest | Start-up time | Migration time
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - single threaded (existing code)
------------------------------------------------------------------------

64 Core - 4TB   | 54m11.796s    | 75m43.843s
64 Core - 1TB   | 8m56.576s     | 14m29.049s
64 Core - 256GB | 2m11.245s     | 3m26.598s
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 8 threads
------------------------------------------------------------------------
64 Core - 4TB   | 5m1.027s      | 34m10.565s
64 Core - 1TB   | 1m10.366s     | 8m28.188s
64 Core - 256GB | 0m19.040s     | 2m10.148s
-----------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 16 threads
-----------------------------------------------------------------------
64 Core - 4TB   | 1m58.970s     | 31m43.400s
64 Core - 1TB   | 0m39.885s     | 7m55.289s
64 Core - 256GB | 0m11.960s     | 2m0.135s

-----------------------------------------------------------------------

Changed in v2:
- modify number of memset threads spawned to min(smp_cpus, 16).
- removed 64GB memory restriction for spawning memset threads.

---
 backends/hostmem.c   |  4 ++--
 exec.c               |  2 +-
 include/qemu/osdep.h |  3 ++-
 util/oslib-posix.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++-----
 util/oslib-win32.c   |  3 ++-
 5 files changed, 69 insertions(+), 11 deletions(-)
backends/hostmem.c +2 -2
index 7f5de70..162c218 100644
@@ -224,7 +224,7 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, &local_err);
+        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             return;
@@ -328,7 +328,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            &local_err);
+                            smp_cpus, &local_err);
             if (local_err) {
                 goto out;
             }
exec.c +1 -1
index 8b9ed73..53afcd2 100644
@@ -1379,7 +1379,7 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     if (mem_prealloc) {
-        os_mem_prealloc(fd, area, memory, errp);
+        os_mem_prealloc(fd, area, memory, smp_cpus, errp);
         if (errp && *errp) {
             goto error;
         }
include/qemu/osdep.h +2 -1
index 56c9e22..fb1d22b 100644
@@ -401,7 +401,8 @@ unsigned long qemu_getauxval(unsigned long type);
 
 void qemu_set_tty_echo(int fd, bool echo);
 
-void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
+void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
+                     Error **errp);
 
 int qemu_read_password(char *buf, int buf_size);
 
util/oslib-posix.c +0 -0
index f631464..17da029 100644
@@ -55,6 +55,16 @@
 #include "qemu/error-report.h"
 #endif
 
Quoted 5 lines

util/oslib-posix.c line 58

Quoted 1 line
util/oslib-posix.c +1 -0
+#define MAX_MEM_PREALLOC_THREAD_COUNT 16  

running with -smp 16 or bigger on host with less than 16 cpus it would be not quite optimal.
Why not to change MAX_MEM_PREALLOC_THREAD_COUNT constant to something like sysconf(_SC_NPROCESSORS_ONLN)

The point is to not consume more host resources than would otherwise be consumed by running the guest CPUs. ie, if running a KVM guest with -smp 4 on a 16 CPU host, QEMU should not to consume more than 4 pCPUs worth of resource on the host. Using sysconf would cause the consume to consume all host resources, likely harming other guests workloads.

Quoted 5 lines

If the person launching QEMU gives a -smp value that's larger than the host CPUs count, then they've already accepted that they're asking QEMU todo more than the host is really capable of. IOW, I don't think we need to special case memsetting for that, since VCPU execution itself is already going to overcommit the host.

Quoted 6 lines

Doing over commit at preallocate time doesn't make much sense,
if MAX_MEM_PREALLOC_THREAD_COUNT is replaced with sysconf(_SC_NPROCESSORS_ONLN) then QEMU will end up with MIN(-smp, sysconf(_SC_NPROCESSORS_ONLN)) which will put cap on upper value and avoid useless over commit at preallocate time.

I agree, we should consider case where we run with -smp >= 16 which is overcommited on host with < 16 cpus. At the same time we should also be sure that we don't end up spawning to many memset threads. For e.g. I have been running fat guests with -smp > 64 on hosts with 384 cpus.

Thanks,
- Jitendra

Quoted 3 lines
Quoted 2 lines

Regards,
Daniel

#5 lore raw
Quoted 136 lines · Jitendra Kolhe
Quoted 128 lines · Igor Mammedov
Quoted 120 lines · On Mon, 13 Feb 2017 11:23:17 +0000 "Daniel P. Berrange" <[email protected]>…
Quoted 106 lines · Igor Mammedov
Quoted 103 lines · On Mon, 13 Feb 2017 14:30:56 +0530 Jitendra Kolhe <[email protected]>…

Using "-mem-prealloc" option for a large guest leads to higher guest start-up and migration time. This is because with "-mem-prealloc" option qemu tries to map every guest page (create address translations), and make sure the pages are available during runtime. virsh/libvirt by default, seems to use "-mem-prealloc" option in case the guest is configured to use huge pages. The patch tries to map all guest pages simultaneously by spawning multiple threads. Currently limiting the change to QEMU library functions on POSIX compliant host only, as we are not sure if the problem exists on win32. Below are some stats with "-mem-prealloc" option for guest configured to use huge pages.

------------------------------------------------------------------------
Idle Guest | Start-up time | Migration time
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - single threaded (existing code)
------------------------------------------------------------------------

64 Core - 4TB   | 54m11.796s    | 75m43.843s
64 Core - 1TB   | 8m56.576s     | 14m29.049s
64 Core - 256GB | 2m11.245s     | 3m26.598s
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 8 threads
------------------------------------------------------------------------
64 Core - 4TB   | 5m1.027s      | 34m10.565s
64 Core - 1TB   | 1m10.366s     | 8m28.188s
64 Core - 256GB | 0m19.040s     | 2m10.148s
-----------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 16 threads
-----------------------------------------------------------------------
64 Core - 4TB   | 1m58.970s     | 31m43.400s
64 Core - 1TB   | 0m39.885s     | 7m55.289s
64 Core - 256GB | 0m11.960s     | 2m0.135s

-----------------------------------------------------------------------

Changed in v2:
- modify number of memset threads spawned to min(smp_cpus, 16).
- removed 64GB memory restriction for spawning memset threads.

---
 backends/hostmem.c   |  4 ++--
 exec.c               |  2 +-
 include/qemu/osdep.h |  3 ++-
 util/oslib-posix.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++-----
 util/oslib-win32.c   |  3 ++-
 5 files changed, 69 insertions(+), 11 deletions(-)
backends/hostmem.c +2 -2
index 7f5de70..162c218 100644
@@ -224,7 +224,7 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, &local_err);
+        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             return;
@@ -328,7 +328,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            &local_err);
+                            smp_cpus, &local_err);
             if (local_err) {
                 goto out;
             }
exec.c +1 -1
index 8b9ed73..53afcd2 100644
@@ -1379,7 +1379,7 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     if (mem_prealloc) {
-        os_mem_prealloc(fd, area, memory, errp);
+        os_mem_prealloc(fd, area, memory, smp_cpus, errp);
         if (errp && *errp) {
             goto error;
         }
include/qemu/osdep.h +2 -1
index 56c9e22..fb1d22b 100644
@@ -401,7 +401,8 @@ unsigned long qemu_getauxval(unsigned long type);
 
 void qemu_set_tty_echo(int fd, bool echo);
 
-void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
+void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
+                     Error **errp);
 
 int qemu_read_password(char *buf, int buf_size);
 
util/oslib-posix.c +0 -0
index f631464..17da029 100644
@@ -55,6 +55,16 @@
 #include "qemu/error-report.h"
 #endif
 
Quoted 5 lines

util/oslib-posix.c line 58

Quoted 1 line
util/oslib-posix.c +1 -0
+#define MAX_MEM_PREALLOC_THREAD_COUNT 16  

running with -smp 16 or bigger on host with less than 16 cpus it would be not quite optimal.
Why not to change MAX_MEM_PREALLOC_THREAD_COUNT constant to something like sysconf(_SC_NPROCESSORS_ONLN)

The point is to not consume more host resources than would otherwise be consumed by running the guest CPUs. ie, if running a KVM guest with -smp 4 on a 16 CPU host, QEMU should not to consume more than 4 pCPUs worth of resource on the host. Using sysconf would cause the consume to consume all host resources, likely harming other guests workloads.

Quoted 5 lines

If the person launching QEMU gives a -smp value that's larger than the host CPUs count, then they've already accepted that they're asking QEMU todo more than the host is really capable of. IOW, I don't think we need to special case memsetting for that, since VCPU execution itself is already going to overcommit the host.

Quoted 6 lines

Doing over commit at preallocate time doesn't make much sense,
if MAX_MEM_PREALLOC_THREAD_COUNT is replaced with sysconf(_SC_NPROCESSORS_ONLN) then QEMU will end up with MIN(-smp, sysconf(_SC_NPROCESSORS_ONLN)) which will put cap on upper value and avoid useless over commit at preallocate time.

Quoted 6 lines

I agree, we should consider case where we run with -smp >= 16 which is overcommited on host with < 16 cpus. At the same time we should also be sure that we don't end up spawning to many memset threads. For e.g. I have been running fat guests with -smp > 64 on hosts with 384 cpus.

how about putting a cap on MAX_MEM_PREALLOC_THREAD_COUNT to (MIN(sysconf(_SC_NPROCESSORS_ONLN), 16))?
Number of memset threads can be calculated using MIN(smp_cpus, MAX_MEM_PREALLOC_THREAD_COUNT);

Thanks,
- Jitendra

Quoted 7 lines

Thanks,
- Jitendra

Quoted 3 lines
Quoted 2 lines

Regards,
Daniel

#6 lore raw
Quoted 143 lines · On Tue, 14 Feb 2017 11:53:11 +0530 Jitendra Kolhe <[email protected]>…
Quoted 136 lines · Jitendra Kolhe
Quoted 128 lines · Igor Mammedov
Quoted 120 lines · On Mon, 13 Feb 2017 11:23:17 +0000 "Daniel P. Berrange" <[email protected]>…
Quoted 106 lines · Igor Mammedov
Quoted 103 lines · On Mon, 13 Feb 2017 14:30:56 +0530 Jitendra Kolhe <[email protected]>…

Using "-mem-prealloc" option for a large guest leads to higher guest start-up and migration time. This is because with "-mem-prealloc" option qemu tries to map every guest page (create address translations), and make sure the pages are available during runtime. virsh/libvirt by default, seems to use "-mem-prealloc" option in case the guest is configured to use huge pages. The patch tries to map all guest pages simultaneously by spawning multiple threads. Currently limiting the change to QEMU library functions on POSIX compliant host only, as we are not sure if the problem exists on win32. Below are some stats with "-mem-prealloc" option for guest configured to use huge pages.

------------------------------------------------------------------------
Idle Guest | Start-up time | Migration time
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - single threaded (existing code)
------------------------------------------------------------------------

64 Core - 4TB   | 54m11.796s    | 75m43.843s
64 Core - 1TB   | 8m56.576s     | 14m29.049s
64 Core - 256GB | 2m11.245s     | 3m26.598s
------------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 8 threads
------------------------------------------------------------------------
64 Core - 4TB   | 5m1.027s      | 34m10.565s
64 Core - 1TB   | 1m10.366s     | 8m28.188s
64 Core - 256GB | 0m19.040s     | 2m10.148s
-----------------------------------------------------------------------
Guest stats with 2M HugePage usage - map guest pages using 16 threads
-----------------------------------------------------------------------
64 Core - 4TB   | 1m58.970s     | 31m43.400s
64 Core - 1TB   | 0m39.885s     | 7m55.289s
64 Core - 256GB | 0m11.960s     | 2m0.135s

-----------------------------------------------------------------------

Changed in v2:
- modify number of memset threads spawned to min(smp_cpus, 16).
- removed 64GB memory restriction for spawning memset threads.

---
 backends/hostmem.c   |  4 ++--
 exec.c               |  2 +-
 include/qemu/osdep.h |  3 ++-
 util/oslib-posix.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++-----
 util/oslib-win32.c   |  3 ++-
 5 files changed, 69 insertions(+), 11 deletions(-)
backends/hostmem.c +2 -2
index 7f5de70..162c218 100644
@@ -224,7 +224,7 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,
         void *ptr = memory_region_get_ram_ptr(&backend->mr);
         uint64_t sz = memory_region_size(&backend->mr);
 
-        os_mem_prealloc(fd, ptr, sz, &local_err);
+        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             return;
@@ -328,7 +328,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
          */
         if (backend->prealloc) {
             os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
-                            &local_err);
+                            smp_cpus, &local_err);
             if (local_err) {
                 goto out;
             }
exec.c +1 -1
index 8b9ed73..53afcd2 100644
@@ -1379,7 +1379,7 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     if (mem_prealloc) {
-        os_mem_prealloc(fd, area, memory, errp);
+        os_mem_prealloc(fd, area, memory, smp_cpus, errp);
         if (errp && *errp) {
             goto error;
         }
include/qemu/osdep.h +2 -1
index 56c9e22..fb1d22b 100644
@@ -401,7 +401,8 @@ unsigned long qemu_getauxval(unsigned long type);
 
 void qemu_set_tty_echo(int fd, bool echo);
 
-void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
+void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
+                     Error **errp);
 
 int qemu_read_password(char *buf, int buf_size);
 
util/oslib-posix.c +0 -0
index f631464..17da029 100644
@@ -55,6 +55,16 @@
 #include "qemu/error-report.h"
 #endif
 
Quoted 5 lines

util/oslib-posix.c line 58

Quoted 1 line
util/oslib-posix.c +1 -0
+#define MAX_MEM_PREALLOC_THREAD_COUNT 16    

running with -smp 16 or bigger on host with less than 16 cpus it would be not quite optimal.
Why not to change MAX_MEM_PREALLOC_THREAD_COUNT constant to something like sysconf(_SC_NPROCESSORS_ONLN)

The point is to not consume more host resources than would otherwise be consumed by running the guest CPUs. ie, if running a KVM guest with -smp 4 on a 16 CPU host, QEMU should not to consume more than 4 pCPUs worth of resource on the host. Using sysconf would cause the consume to consume all host resources, likely harming other guests workloads.

Quoted 5 lines

If the person launching QEMU gives a -smp value that's larger than the host CPUs count, then they've already accepted that they're asking QEMU todo more than the host is really capable of. IOW, I don't think we need to special case memsetting for that, since VCPU execution itself is already going to overcommit the host.

Quoted 6 lines

Doing over commit at preallocate time doesn't make much sense,
if MAX_MEM_PREALLOC_THREAD_COUNT is replaced with sysconf(_SC_NPROCESSORS_ONLN) then QEMU will end up with MIN(-smp, sysconf(_SC_NPROCESSORS_ONLN)) which will put cap on upper value and avoid useless over commit at preallocate time.

Quoted 6 lines

I agree, we should consider case where we run with -smp >= 16 which is overcommited on host with < 16 cpus. At the same time we should also be sure that we don't end up spawning to many memset threads. For e.g. I have been running fat guests with -smp > 64 on hosts with 384 cpus.

Quoted 4 lines

how about putting a cap on MAX_MEM_PREALLOC_THREAD_COUNT to (MIN(sysconf(_SC_NPROCESSORS_ONLN), 16))?
Number of memset threads can be calculated using MIN(smp_cpus, MAX_MEM_PREALLOC_THREAD_COUNT);

it looks ok to me

Quoted 11 lines

Thanks,
- Jitendra

Quoted 7 lines

Thanks,
- Jitendra

Quoted 3 lines
Quoted 2 lines

Regards,
Daniel