Skip to contentSkip to conversation

Functional and performance tests for migration

PATCH New Edwin Török 1 participant 3 messages Mar 30

lore
Share & tools
lore
#0 lore raw

cover Functional and performance tests for migration

This is a test that can be run in Dom0, which constructs a minimal guest, fills the memory with a deterministic pattern, xc_domains_save/xc_domain_restore on 2 threads, and checks the pattern at the end. On mismatch hexdumps of the portions that do not match are printed.
Although the test is very simple it has already been very useful during the development of 2 migration optimization patch sets (foreign copy and migration compression, to be posted separately).

Of course the test is not a guarantee that migration works correctly, there are lots of aspects that are not tested (interaction with device emulators, more complicated memory layouts, the memory layout changing during migration, etc.), but is a lot easier to debug when it goes wrong than a real guest crashing (quite often even if you run 'memtest' in the guest, it'll either crash while executing without printing where the mismatch is -- if you corrupted the actual program, or continue undetected, requiring many migration iterations to catch the bug).

It can also be used as a low-level performance test by specifying the REPEAT environment variable.

Example usage from the tests/migration directory when working on optimizations in libxenguest:

```
make -j$(nproc) -O -C ../../libs/guest && make -j$(nproc) -O && scp ../../libs/guest/libxenguest.so.4.22.0 ../../libs/guest/libxenguest.so.4.22 test-migration root@$HOST: && ssh root@$HOST env REPEAT=20 LD_LIBRARY_PATH=/root ./test-migration 2>/dev/null | grep 'Performance.*xc_domain_restore'
```

The output can be post-processed with cut -f2 -d: |cut -f1 -ds, and then compared with ministat for example.

Currently this patchset depends on another that refactors the unit test Makefiles. Once I've updated that patchset I'll send a rebased version of this one.

Edwin Török (2):
tools/tests/migration: introduce migration functional and performance
tests
tools/lib/call: cache up to 4 pages in hypercall bounce buffers

 tools/libs/call/buffer.c               |  28 +-
 tools/libs/call/core.c                 |   3 +-
 tools/libs/call/private.h              |   8 +-
 tools/tests/Makefile                   |   1 +
 tools/tests/common/guests.c            |   1 +
 tools/tests/migration/.gitignore       |   1 +
 tools/tests/migration/Makefile         |  70 +++
 tools/tests/migration/memory.c         |   6 +
 tools/tests/migration/memory.h         |  92 ++++
 tools/tests/migration/patterns.c       |  76 +++
 tools/tests/migration/patterns.h       |  22 +
 tools/tests/migration/test-migration.c | 713 +++++++++++++++++++++++++
 tools/tests/migration/xen_patterns.c   |  92 ++++
 tools/tests/migration/xen_patterns.h   |  14 +
 14 files changed, 1111 insertions(+), 16 deletions(-)
 create mode 100644 tools/tests/migration/.gitignore
 create mode 100644 tools/tests/migration/Makefile
 create mode 100644 tools/tests/migration/memory.c
 create mode 100644 tools/tests/migration/memory.h
 create mode 100644 tools/tests/migration/patterns.c
 create mode 100644 tools/tests/migration/patterns.h
 create mode 100644 tools/tests/migration/test-migration.c
 create mode 100644 tools/tests/migration/xen_patterns.c
 create mode 100644 tools/tests/migration/xen_patterns.h
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 tools/tests/migration: introduce migration functional and performance tests

Create a minimal guest that is localhost-migratable via xc_domain_save/xc_domain_restore.
This only exercises the basics of a migration (no device emulators, no page dirtying), but has already been useful at detecting bugs in an upcoming migration improvement patches.

Fill the memory with a deterministic pattern (either the page's own address, or a random value constructed from a fixed seed). Regenerate the same pattern after migration (one page at a time), and check whether it matches the expected pattern.

On a mismatch it prints a hexdump, and this can be useful to identify whether the entire memory is corrupted, or whether the data is present, but at a constant offset, and whether the entire memory is corrupted, or just some particular pages.

To construct a somewhat more realistic scenario it also:
* inserts a gap of some non-streamable pages, by marking them as MMIO
* tests explicitly aligned userspace buffers
* tests explicitly unaligned userspace buffers (at a constant offset from an aligned one)

Each of these scenarios has been useful in finding and fixing bugs in prototype optimization patches.

To distinguish bugs in memory mapping/copying from bugs in the migration streaming code, it contains a separate test for foreign mapping, using the same patterns test.
The code is generic, to allow introducing other forms of mapping/copying in the future (e.g. foreign copy)

---
 tools/tests/Makefile                   |   1 +
 tools/tests/common/guests.c            |   1 +
 tools/tests/migration/.gitignore       |   1 +
 tools/tests/migration/Makefile         |  70 +++
 tools/tests/migration/memory.c         |   6 +
 tools/tests/migration/memory.h         |  92 ++++
 tools/tests/migration/patterns.c       |  76 +++
 tools/tests/migration/patterns.h       |  22 +
 tools/tests/migration/test-migration.c | 713 +++++++++++++++++++++++++
 tools/tests/migration/xen_patterns.c   |  92 ++++
 tools/tests/migration/xen_patterns.h   |  14 +
 11 files changed, 1088 insertions(+)
 create mode 100644 tools/tests/migration/.gitignore
 create mode 100644 tools/tests/migration/Makefile
 create mode 100644 tools/tests/migration/memory.c
 create mode 100644 tools/tests/migration/memory.h
 create mode 100644 tools/tests/migration/patterns.c
 create mode 100644 tools/tests/migration/patterns.h
 create mode 100644 tools/tests/migration/test-migration.c
 create mode 100644 tools/tests/migration/xen_patterns.c
 create mode 100644 tools/tests/migration/xen_patterns.h
tools/tests/Makefile +1 -0
index 6477a4386d..d67b4e415f 100644
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += domid
 SUBDIRS-y += mem-claim
+SUBDIRS-y += migration
 SUBDIRS-y += paging-mempool
 SUBDIRS-y += pdx
 SUBDIRS-y += rangeset
tools/tests/common/guests.c +1 -0
index a46ff2f68d..20acd9129e 100644
@@ -53,6 +53,7 @@ void test_guest(struct xen_domctl_createdomain *c)
 
 void test_guests(void)
 {
+    xen_has_pv = 0;
     if ( xen_has_pv )
     {
         struct xen_domctl_createdomain c = {
tools/tests/migration/.gitignore new file +1 -0
new file mode 100644
index 0000000000..619b2b4578
@@ -0,0 +1 @@
+test-migration
tools/tests/migration/Makefile new file +70 -0

70 lines not on this page · raw message

tools/tests/migration/memory.c new file +6 -0
new file mode 100644
index 0000000000..e29a32af5d
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include "memory.h"
+
+#include <stddef.h>
+#include <stdint.h>
+
tools/tests/migration/memory.h new file +92 -0

92 lines not on this page · raw message

tools/tests/migration/patterns.c new file +76 -0

76 lines not on this page · raw message

tools/tests/migration/patterns.h new file +22 -0
new file mode 100644
index 0000000000..e7187c3775
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+
+enum pattern_kind {
+    PATTERN_ADDR,
+    PATTERN_RANDOM
+};
+
+void pattern_fill(size_t n, char dest[n], enum pattern_kind kind,
+                  uint64_t seed);
+
+void pattern_dirty(size_t n, char dest[n], size_t iteration);
+
+int pattern_cmp(size_t n, const char src[n], char tmp[n],
+                enum pattern_kind kind, uint64_t seed, size_t iteration);
+
+void pattern_hexdump(size_t n, const char src[n], FILE *out);
+
+void pattern_hexdump_diff(size_t n, const char a[n], const char b[n],
+                          FILE *out);
tools/tests/migration/test-migration.c new file +713 -0

713 lines not on this page · raw message

tools/tests/migration/xen_patterns.c new file +92 -0

92 lines not on this page · raw message

tools/tests/migration/xen_patterns.h new file +14 -0
new file mode 100644
index 0000000000..1470235f07
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include "patterns.h"
+
+#include <xenctrl.h>
+
+void xen_pattern_fill(size_t n, char dest[n], enum pattern_kind kind,
+                      domid_t domid, size_t nr_pages,
+                      const xen_pfn_t pfns[nr_pages]);
+
+void xen_pattern_check(size_t n, const char src[n], enum pattern_kind kind,
+                       domid_t domid, size_t nr_pages,
+                       const xen_pfn_t pfns[nr_pages], size_t iteration);
+
+void xen_dirty_pages(size_t n, char dest[n], size_t iteration);
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 tools/lib/call: cache up to 4 pages in hypercall bounce buffers

In reply to Edwin Török

During migration there are a lot of mmap/munmap calls, because xc_get_pfn_type_batch exceeds the default hypercall bounce buffer cache size, and needs to allocate every time it is called.

munmap is slow, especially in a PV Dom0 (takes an emulation fault), so is best avoided.

Eventually it'd be good if the memory pool from xmalloc_tlsf.c was reused here, but for now make it handle the commonly encountered sizes (so far up to 4 pages).

---
 tools/libs/call/buffer.c  | 28 ++++++++++++++++------------
 tools/libs/call/core.c    |  3 ++-
 tools/libs/call/private.h |  8 +++++---
 3 files changed, 23 insertions(+), 16 deletions(-)
tools/libs/call/buffer.c +16 -12
index 2579b8c719..5601ee83ba 100644
@@ -56,13 +56,13 @@ static void *cache_alloc(xencall_handle *xcall, size_t nr_pages)
     if ( xcall->buffer_current_allocations > xcall->buffer_maximum_allocations )
         xcall->buffer_maximum_allocations = xcall->buffer_current_allocations;
 
-    if ( nr_pages > 1 )
+    if ( nr_pages > ARRAY_SIZE(xcall->buffer_cache) )
     {
         xcall->buffer_cache_toobig++;
     }
-    else if ( xcall->buffer_cache_nr > 0 )
+    else if ( xcall->buffer_cache_nr[nr_pages-1] > 0 )
     {
-        p = xcall->buffer_cache[--xcall->buffer_cache_nr];
+        p = xcall->buffer_cache[nr_pages-1][--xcall->buffer_cache_nr[nr_pages-1]];
         xcall->buffer_cache_hits++;
     }
     else
@@ -84,10 +84,10 @@ static int cache_free(xencall_handle *xcall, void *p, size_t nr_pages)
     xcall->buffer_total_releases++;
     xcall->buffer_current_allocations--;
 
-    if ( nr_pages == 1 &&
-         xcall->buffer_cache_nr < BUFFER_CACHE_SIZE )
+    if ( nr_pages && nr_pages < ARRAY_SIZE(xcall->buffer_cache) &&
+         xcall->buffer_cache_nr[nr_pages-1] < BUFFER_CACHE_SIZE )
     {
-        xcall->buffer_cache[xcall->buffer_cache_nr++] = p;
+        xcall->buffer_cache[nr_pages-1][xcall->buffer_cache_nr[nr_pages-1]++] = p;
         rc = 1;
     }
 
@@ -108,17 +108,21 @@ void buffer_release_cache(xencall_handle *xcall)
     DBGPRINTF("current allocations:%d maximum allocations:%d",
               xcall->buffer_current_allocations,
               xcall->buffer_maximum_allocations);
-    DBGPRINTF("cache current size:%d",
-              xcall->buffer_cache_nr);
+    for ( unsigned i = 0; i < ARRAY_SIZE(xcall->buffer_cache_nr); ++i) {
+        DBGPRINTF("cache current size[%u pages]:%d", i+1,
+                xcall->buffer_cache_nr[i]);
+    }
     DBGPRINTF("cache hits:%d misses:%d toobig:%d",
               xcall->buffer_cache_hits,
               xcall->buffer_cache_misses,
               xcall->buffer_cache_toobig);
 
-    while ( xcall->buffer_cache_nr > 0 )
-    {
-        p = xcall->buffer_cache[--xcall->buffer_cache_nr];
-        osdep_free_pages(xcall, p, 1);
+    for ( unsigned i = 0; i < ARRAY_SIZE(xcall->buffer_cache_nr); ++i) {
+        while ( xcall->buffer_cache_nr[i] > 0 )
+        {
+            p = xcall->buffer_cache[i][--xcall->buffer_cache_nr[i]];
+            osdep_free_pages(xcall, p, 1);
+        }
     }
 
     cache_unlock(xcall);
tools/libs/call/core.c +2 -1
index 02c4f8e1ae..dd8877c1a0 100644
@@ -14,6 +14,7 @@
  */
 
 #include <stdlib.h>
+#include <string.h>
 
 #include "private.h"
 
@@ -44,7 +45,7 @@ xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
     xentoolcore__register_active_handle(&xcall->tc_ah);
 
     xcall->flags = open_flags;
-    xcall->buffer_cache_nr = 0;
+    memset(xcall->buffer_cache_nr, 0, sizeof(xcall->buffer_cache_nr));
 
     xcall->buffer_total_allocations = 0;
     xcall->buffer_total_releases = 0;
tools/libs/call/private.h +5 -3
index 9c3aa432ef..7aca526abe 100644
@@ -31,13 +31,15 @@ struct xencall_handle {
     Xentoolcore__Active_Handle tc_ah;
 
     /*
-     * A simple cache of unused, single page, hypercall buffers
+     * A simple cache of unused, small, hypercall buffers
+     * buffer_cache[i]'s size is (i-1) pages
      *
      * Protected by a global lock.
      */
 #define BUFFER_CACHE_SIZE 4
-    int buffer_cache_nr;
-    void *buffer_cache[BUFFER_CACHE_SIZE];
+#define BUFFER_CACHE_NRPAGES 4
+    int buffer_cache_nr[BUFFER_CACHE_NRPAGES];
+    void *buffer_cache[BUFFER_CACHE_NRPAGES][BUFFER_CACHE_SIZE];
 
     /*
      * Hypercall buffer statistics. All protected by the global
Signature · 1 line
2.47.3