tools/libxc: use superpages
v6 cover tools/libxc: use superpages
Using superpages on the receiving dom0 will avoid performance regressions.
Olaf
v6:
handle freeing of partly populated superpages correctly more DPRINTFs
v5:
send correct version, rebase was not fully finished
v4:
restore trailing "_bit" in bitmap function names keep track of gaps between previous and current batch split alloc functionality in x86_hvm_allocate_pfn
v3:
clear pointer in xc_sr_bitmap_free
some coding style changes
use getdomaininfo.max_pages to avoid Over-allocation check trim bitmap function names, drop trailing "_bit" add some comments
v2:
split into individual commits
Olaf Hering (3):
tools/libxc: move SUPERPAGE macros to common header tools/libxc: add API for bitmap access for restore tools/libxc: use superpages during restore of HVM guest
tools/libxc/xc_dom_x86.c | 5 - tools/libxc/xc_private.h | 5 + tools/libxc/xc_sr_common.c | 41 +++++ tools/libxc/xc_sr_common.h | 93 ++++++++++-- tools/libxc/xc_sr_restore.c | 141 ++---------------- tools/libxc/xc_sr_restore_x86_hvm.c | 288 ++++++++++++++++++++++++++++++++++++ tools/libxc/xc_sr_restore_x86_pv.c | 72 ++++++++- 7 files changed, 497 insertions(+), 148 deletions(-)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v6 1/3 tools/libxc: move SUPERPAGE macros to common header
The macros SUPERPAGE_2MB_SHIFT and SUPERPAGE_1GB_SHIFT will be used by other code in libxc. Move the macros to a header file.
--- tools/libxc/xc_dom_x86.c | 5 ----- tools/libxc/xc_private.h | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-)
tools/libxc/xc_dom_x86.c +0 -5
@@ -43,11 +43,6 @@#define SUPERPAGE_BATCH_SIZE 512 -#define SUPERPAGE_2MB_SHIFT 9 -#define SUPERPAGE_2MB_NR_PFNS (1UL << SUPERPAGE_2MB_SHIFT) -#define SUPERPAGE_1GB_SHIFT 18 -#define SUPERPAGE_1GB_NR_PFNS (1UL << SUPERPAGE_1GB_SHIFT) -#define X86_CR0_PE 0x01#define X86_CR0_ET 0x10
tools/libxc/xc_private.h +5 -0
@@ -66,6 +66,11 @@ struct iovec {#define DECLARE_FLASK_OP struct xen_flask_op op#define DECLARE_PLATFORM_OP struct xen_platform_op platform_op +#define SUPERPAGE_2MB_SHIFT 9 +#define SUPERPAGE_2MB_NR_PFNS (1UL << SUPERPAGE_2MB_SHIFT) +#define SUPERPAGE_1GB_SHIFT 18 +#define SUPERPAGE_1GB_NR_PFNS (1UL << SUPERPAGE_1GB_SHIFT) +#undef PAGE_SHIFT#undef PAGE_SIZE#undef PAGE_MASK
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v6 2/3 tools/libxc: add API for bitmap access for restore
In reply to Olaf HeringExtend API for managing bitmaps. Each bitmap is now represented by a generic struct xc_sr_bitmap.
Switch the existing populated_pfns to this API.
--- tools/libxc/xc_sr_common.c | 41 +++++++++++++++++++++++++++ tools/libxc/xc_sr_common.h | 68 +++++++++++++++++++++++++++++++++++++++++++-- tools/libxc/xc_sr_restore.c | 66 ++----------------------------------------- 3 files changed, 110 insertions(+), 65 deletions(-)
tools/libxc/xc_sr_common.c +41 -0
@@ -155,6 +155,47 @@ static void __attribute__((unused)) build_assertions(void)BUILD_BUG_ON (sizeof (struct xc_sr_rec_hvm_params ) !=8 ); } +/* +* Expand the tracking structures as needed. +* To avoid realloc()ing too excessively, the size increased to the nearest power +* of two large enough to contain the required number of bits. +*/ +bool _xc_sr_bitmap_resize (struct xc_sr_bitmap *bm,unsigned long bits) +{ +if (bits > bm->bits) + { +size_t new_max; +size_t old_sz, new_sz; +void *p; + +/* Round up to the nearest power of two larger than bit, less 1. */ + new_max = bits; + new_max |= new_max >>1 ; + new_max |= new_max >>2 ; + new_max |= new_max >>4 ; + new_max |= new_max >>8 ; + new_max |= new_max >>16 ; +#ifdef __x86_64__ + new_max |= new_max >>32 ; +#endif + + old_sz =bitmap_size (bm->bits +1 ); + new_sz =bitmap_size (new_max +1 ); + p =realloc (bm->p, new_sz); +if (!p) +return false; + +if (bm->p) +memset (p + old_sz,0 , new_sz - old_sz); +else +memset (p,0 , new_sz); + + bm->p = p; + bm->bits = new_max; + } +return true; +} +/* * Local variables: * mode: C
tools/libxc/xc_sr_common.h +66 -2
86 lines not on this page · raw message
tools/libxc/xc_sr_restore.c +3 -63
84 lines not on this page · raw message
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v6 3/3 tools/libxc: use superpages during restore of HVM guest
In reply to Olaf HeringDuring creating of a HVM domU meminit_hvm() tries to map superpages. After save/restore or migration this mapping is lost, everything is allocated in single pages. This causes a performance degradition after migration.
Add neccessary code to preallocate a superpage for the chunk of pfns that is received. In case a pfn was not populated on the sending side it must be freed on the receiving side to avoid over-allocation.
The existing code for x86_pv is moved unmodified into its own file.
--- tools/libxc/xc_sr_common.h | 25 +++- tools/libxc/xc_sr_restore.c | 75 +--------- tools/libxc/xc_sr_restore_x86_hvm.c | 288 ++++++++++++++++++++++++++++++++++++ tools/libxc/xc_sr_restore_x86_pv.c | 72 ++++++++- 4 files changed, 382 insertions(+), 78 deletions(-)
tools/libxc/xc_sr_common.h +17 -8
@@ -139,6 +139,16 @@ struct xc_sr_restore_ops*/ int (*setup)(struct xc_sr_context *ctx); +/** +* Populate PFNs +* +* Given a set of pfns, obtain memory from Xen to fill the physmap for the +* unpopulated subset. +*/ +int (*populate_pfns)(struct xc_sr_context *ctx,unsigned count, +const xen_pfn_t *original_pfns,const uint32_t *types); + +/** * Process an individual record from the stream. The caller shall take * care of processing common records (e.g. END, PAGE_DATA).
@@ -224,6 +234,8 @@ struct xc_sr_contextint send_back_fd;unsigned long p2m_size; +unsigned long max_pages; +unsigned long tot_pages;xc_hypercall_buffer_t dirty_bitmap_hbuf;/* From Image Header. */
@@ -336,6 +348,11 @@ struct xc_sr_context/* HVM context blob. */ void *context;size_t contextsz; + +/* Bitmap of currently allocated PFNs during restore. */ +struct xc_sr_bitmap attempted_1g; +struct xc_sr_bitmap attempted_2m; +struct xc_sr_bitmap allocated_pfns; } restore; }; } x86_hvm;
@@ -455,14 +472,6 @@ static inline int write_record(struct xc_sr_context *ctx,*/ int read_record (struct xc_sr_context *ctx,int fd,struct xc_sr_record *rec); -/* -* This would ideally be private in restore.c, but is needed by -* x86_pv_localise_page() if we receive pagetables frames ahead of the -* contents of the frames they point at. -*/ -int populate_pfns (struct xc_sr_context *ctx,unsigned count, -const xen_pfn_t *original_pfns,const uint32_t *types); -#endif /* * Local variables:
tools/libxc/xc_sr_restore.c +6 -69
94 lines not on this page · raw message
tools/libxc/xc_sr_restore_x86_hvm.c +288 -0
317 lines not on this page · raw message
tools/libxc/xc_sr_restore_x86_pv.c +71 -1
90 lines not on this page · raw message
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/3] tools/libxc: use superpages during restore of HVM guest
Review
Quoted 8 lines · Olaf Hering
(quoted diff) +7
-0+static int x86_hvm_populate_pfns(struct xc_sr_context *ctx, unsigned count, + /* + * Scan the entire superpage because several batches will fit into + * a superpage, and it is unknown which pfn triggered the allocation. + */ + order = SUPERPAGE_1GB_SHIFT; + pfn = min_pfn = (min_pfn >> order) << order;
Scanning an entire superpage again and again looked expensive, but with the debug change below it turned out that the loop which peeks at each single bit in populated_pfns is likely not a bootleneck.
Migrating a domU with a simple workload that touches pages to mark them dirty will set the min_pfn/max_pfn to a large range anyway after the first iteration. This large range may also happen with an idle domU. A small domU takes 78 seconds to migrate, and just the freeing part takes 1.4 seconds. Similar for a large domain, the loop takes 1% of the time.
78 seconds, 1.4 seconds, 2119 calls (8GB, 12*512M memdirty)
695 seconds, 7.6 seconds, 18076 calls (72GB, 12*5G memdirty)Olaf
track time spent if decrease_reservation is needed
tools/libxc/xc_sr_common.h +3 -0
@@ -353,6 +353,9 @@ struct xc_sr_contextstruct xc_sr_bitmap attempted_1g;struct xc_sr_bitmap attempted_2m;struct xc_sr_bitmap allocated_pfns; + +unsigned long tv_nsec; +unsigned long iterations; } restore; }; } x86_hvm;
tools/libxc/xc_sr_restore.c +1 -0
@@ -769,6 +769,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom, { ctx.restore.ops = restore_ops_x86_hvm;if (restore (&ctx) ) + ;return -1 ; } else
tools/libxc/xc_sr_restore_x86_hvm.c +40 -1
79 lines not on this page · raw message
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/3] tools/libxc: use superpages during restore of HVM guest
In reply to Olaf HeringOn Sat, Aug 26, 2017 at 12:33:32PM +0200, Olaf Hering wrote:
[...]
Quoted 24 lines
tools/libxc/xc_sr_restore_x86_hvm.c +23
-0+static int x86_hvm_populate_pfns (struct xc_sr_context *ctx,unsigned count, +const xen_pfn_t *original_pfns, +const uint32_t *types) +{ +xc_interface *xch = ctx->xch; +xen_pfn_t pfn, min_pfn = original_pfns[0 ], max_pfn = original_pfns[0 ]; +unsigned i, freed =0 , order; +int rc =-1 ; + +for ( i =0 ; i < count; ++i ) + { +if ( original_pfns[i] < min_pfn ) + min_pfn = original_pfns[i]; +if ( original_pfns[i] > max_pfn ) + max_pfn = original_pfns[i]; + } +DPRINTF ("batch of %u pfns between %" PRI_xen_pfn" %" PRI_xen_pfn"\n" , + count, min_pfn, max_pfn); + +for ( i =0 ; i < count; ++i ) + { + if ( (types[i] != XEN_DOMCTL_PFINFO_XTAB && + types[i] != XEN_DOMCTL_PFINFO_BROKEN) &&
tools/libxc/xc_sr_restore_x86_hvm.c static int x86_hvm_stream_complete(struct xc_sr_context *ctx) · line 475
Quoted 10 lines
tools/libxc/xc_sr_restore_x86_hvm.c +10
-0+ !pfn_is_populated(ctx, original_pfns[i]) ) + { + rc =x86_hvm_allocate_pfn (ctx, original_pfns[i]); +if ( rc ) + goto err; + rc =pfn_set_populated (ctx, original_pfns[i]); +if ( rc ) + goto err; + } + }
As far as I can tell the algorithm in the patch can't handle:
1. First pfn in a batch points to start of second 1G address space
2. Second pfn in a batch points to a page in the middle of first 1G
3. Guest can only use 1G ram
This is a valid scenario in post-copy migration algorithm.
Please correct me if I'm wrong.
Quoted 31 lines
tools/libxc/xc_sr_restore_x86_hvm.c +31
-0+ +/* +* Scan the entire superpage because several batches will fit into +* a superpage, and it is unknown which pfn triggered the allocation. +*/ + order =SUPERPAGE_1GB_SHIFT ; + pfn = min_pfn = (min_pfn >> order) << order; + +while ( pfn <= max_pfn ) + { +struct xc_sr_bitmap *bm; + bm = &ctx->x86_hvm.restore.allocated_pfns; +if ( !xc_sr_bitmap_resize (bm, pfn) ) + { +PERROR ("Failed to realloc allocated_pfns %" PRI_xen_pfn, pfn); + goto err; + } +if ( !pfn_is_populated (ctx, pfn) && +xc_sr_test_and_clear_bit (pfn, bm) ) { +xen_pfn_t p = pfn; + rc =xc_domain_decrease_reservation_exact (xch, ctx->domid,1 ,0 , &p); +if ( rc ) + { +PERROR ("Failed to release pfn %" PRI_xen_pfn, pfn); + goto err; + } + ctx->restore.tot_pages--; + freed++; + } + pfn++; + }
tools/libxc/xc_sr_restore_x86_hvm.c static int x86_hvm_stream_complete(struct xc_sr_context *ctx) · line 516
Quoted 10 lines
tools/libxc/xc_sr_restore_x86_hvm.c +10
-0+if ( freed ) +DPRINTF ("freed %u between %" PRI_xen_pfn" %" PRI_xen_pfn"\n" , + freed, min_pfn, max_pfn); + + rc =0 ; + + err: +return rc; +} +
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/3] tools/libxc: use superpages during restore of HVM guest
Quoted 5 lines · Wei Liu
As far as I can tell the algorithm in the patch can't handle:
1. First pfn in a batch points to start of second 1G address space
2. Second pfn in a batch points to a page in the middle of first 1G
3. Guest can only use 1G ram
In which way does it not handle it? Over-allocation is supposed to be handled by the "ctx->restore.tot_pages + sp->count > ctx->restore.max_pages" checks. Do you mean the second 1G is allocated, then max_pages is reached, and allocation in other areas is not possible anymore?
Can this actually happen with the available senders? If not, this is again the missing memory map.
Olaf
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/3] tools/libxc: use superpages during restore of HVM guest
Quoted 8 lines · Olaf Hering
Quoted 5 lines · Wei Liu
As far as I can tell the algorithm in the patch can't handle:
1. First pfn in a batch points to start of second 1G address space
2. Second pfn in a batch points to a page in the middle of first 1G
3. Guest can only use 1G ram
Quoted 5 lines
In which way does it not handle it? Over-allocation is supposed to be handled by the "
ctx->restore.tot_pages+sp->count>ctx->restore.max_pages" checks. Do you mean the second 1G is allocated, thenmax_pagesis reached, and allocation in other areas is not possible anymore?
Yes.
Quoted 3 lines
Can this actually happen with the available senders? If not, this is again the missing memory map.
Probably not now, but as said, you shouldn't rely on the structure of the stream unless it is stated in the spec.
And it can definitely happen with post-copy algorithm.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/3] tools/libxc: use superpages during restore of HVM guest
Quoted 4 lines · Wei Liu
Quoted 2 lines
Can this actually happen with the available senders? If not, this is again the missing memory map.
Probably not now, but as said, you shouldn't rely on the structure of the stream unless it is stated in the spec.
Well, what can happen with todays implementation on the sender side is the case of a ballooned guest with enough holes within a batch. These will trigger 1G allocations before the releasing of memory happens. To solve this, the releasing of memory has to happen more often, probably after crossing each 2M boundary.
Olaf
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel