mem-prealloc: reduce large guest start-up and migration time.
PATCH v3 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.
Changed in v3:
- limit number of threads spawned based on min(sysconf(_SC_NPROCESSORS_ONLN), 16, smp_cpus)
- implement memset thread specific siglongjmp in SIGBUS signal_handler.
--- backends/hostmem.c | 4 +-- exec.c | 2 +- include/qemu/osdep.h | 3 +- util/oslib-posix.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++----- util/oslib-win32.c | 3 +- 5 files changed, 95 insertions(+), 13 deletions(-)
backends/hostmem.c +2 -2
@@ -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
@@ -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
@@ -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 +88 -8
121 lines not on this page · raw message
util/oslib-win32.c +2 -1
@@ -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
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 429
Quoted 4 lines · Jitendra Kolhe
if (sigsetjmp(sigjump, 1)) {util/oslib-posix.c +1
-2-error_setg (errp,"os_mem_prealloc: Insufficient free host memory " -"pages available to allocate guest RAM\n" ); + memset_thread_failed = true;
This sigsetjmp is not needed, no one can siglongjmp to it.
The SIG_UNBLOCK/SIG_BLOCK in the main thread is not necessary, either.
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 435
Quoted 11 lines
} else {util/oslib-posix.c +2
-5-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);
... and now touch_all_pages can just return memset_thread_failed, which simplifies the code a bit.
Paolo
Quoted 5 lines
util/oslib-posix.c +4
-0+ } +if (memset_thread_failed) { +error_setg (errp,"os_mem_prealloc: Insufficient free host memory " +"pages available to allocate guest RAM" ); }
Quoted 24 lines · Paolo Bonzini
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 429
Quoted 4 lines · Jitendra Kolhe
if (sigsetjmp(sigjump, 1)) {util/oslib-posix.c +1
-2-error_setg (errp,"os_mem_prealloc: Insufficient free host memory " -"pages available to allocate guest RAM\n" ); + memset_thread_failed = true;This sigsetjmp is not needed, no one can siglongjmp to it.
The SIG_UNBLOCK/SIG_BLOCK in the main thread is not necessary, either.
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 435
Quoted 11 lines
} else {util/oslib-posix.c +2
-5-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);
Quoted 3 lines
... and now
touch_all_pagescan just returnmemset_thread_failed, which simplifies the code a bit.
sure, will make change accordingly.
- Jitendra
Quoted 7 lines
Paolo
Quoted 5 lines
util/oslib-posix.c +4
-0+ } +if (memset_thread_failed) { +error_setg (errp,"os_mem_prealloc: Insufficient free host memory " +"pages available to allocate guest RAM" ); }