From: Frediano Ziglio <frediano.ziglio@cloud.com>
To: xen-devel@lists.xenproject.org
Cc: "Frediano Ziglio" <frediano.ziglio@cloud.com>,
"Anthony PERARD" <anthony.perard@vates.tech>,
"Juergen Gross" <jgross@suse.com>,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Andrew Cooper" <andrew.cooper3@citrix.com>
Subject: [PATCH] Optimise restore memory allocation
Date: Wed, 27 Aug 2025 13:33:04 +0100 [thread overview]
Message-ID: <20250827123309.39699-1-frediano.ziglio@cloud.com> (raw)
Try to allocate larger order pages.
With some test memory program stressing TLB (many small random
memory accesses) you can get 15% performance improves.
On the first memory iteration the sender is currently sending
memory in 4mb aligned chunks which allows the receiver to
allocate most pages as 2mb superpages instead of single 4kb pages.
Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
---
tools/libs/guest/xg_sr_restore.c | 39 ++++++++++++++++++++++++++++----
1 file changed, 35 insertions(+), 4 deletions(-)
diff --git a/tools/libs/guest/xg_sr_restore.c b/tools/libs/guest/xg_sr_restore.c
index 06231ca826..8dcb1b19c5 100644
--- a/tools/libs/guest/xg_sr_restore.c
+++ b/tools/libs/guest/xg_sr_restore.c
@@ -129,6 +129,8 @@ static int pfn_set_populated(struct xc_sr_context *ctx, xen_pfn_t pfn)
return 0;
}
+#define IS_POWER_OF_2(n) (((n) & ((n) - 1)) == 0)
+
/*
* Given a set of pfns, obtain memory from Xen to fill the physmap for the
* unpopulated subset. If types is NULL, no page type checking is performed
@@ -141,6 +143,7 @@ int populate_pfns(struct xc_sr_context *ctx, unsigned int count,
xen_pfn_t *mfns = malloc(count * sizeof(*mfns)),
*pfns = malloc(count * sizeof(*pfns));
unsigned int i, nr_pfns = 0;
+ bool contiguous = true;
int rc = -1;
if ( !mfns || !pfns )
@@ -159,18 +162,46 @@ int populate_pfns(struct xc_sr_context *ctx, unsigned int count,
if ( rc )
goto err;
pfns[nr_pfns] = mfns[nr_pfns] = original_pfns[i];
+ if ( pfns[nr_pfns] != pfns[0] + nr_pfns )
+ contiguous = false;
++nr_pfns;
}
}
if ( nr_pfns )
{
- rc = xc_domain_populate_physmap_exact(
- xch, ctx->domid, nr_pfns, 0, 0, mfns);
+ /* try optimizing using larger order */
+ rc = -1;
+ /*
+ * The "nr_pfns <= (1 << 18)" check is mainly for paranoia, it should
+ * never happen, the sender would have to send a really large packet.
+ */
+ if ( contiguous && nr_pfns <= (1 << 18) &&
+ IS_POWER_OF_2(nr_pfns) && (pfns[0] & (nr_pfns - 1)) == 0 )
+ {
+ const unsigned int extent_order = __builtin_ffs(nr_pfns) - 1;
+
+ rc = xc_domain_populate_physmap_exact(
+ xch, ctx->domid, 1, extent_order, 0, mfns);
+ if ( rc )
+ mfns[0] = pfns[0];
+ else
+ {
+ for ( i = 1; i < nr_pfns; ++i )
+ mfns[i] = mfns[0] + i;
+ }
+ }
+
+ /* if using larger order fails fall back to single pages */
if ( rc )
{
- PERROR("Failed to populate physmap");
- goto err;
+ rc = xc_domain_populate_physmap_exact(
+ xch, ctx->domid, nr_pfns, 0, 0, mfns);
+ if ( rc )
+ {
+ PERROR("Failed to populate physmap");
+ goto err;
+ }
}
for ( i = 0; i < nr_pfns; ++i )
--
2.43.0
next reply other threads:[~2025-08-27 12:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-27 12:33 Frediano Ziglio [this message]
2025-08-27 17:23 ` [PATCH] Optimise restore memory allocation Andrew Cooper
2025-08-28 13:30 ` Oleksii Kurochko
2025-08-29 9:34 ` Andrew Cooper
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250827123309.39699-1-frediano.ziglio@cloud.com \
--to=frediano.ziglio@cloud.com \
--cc=andrew.cooper3@citrix.com \
--cc=anthony.perard@vates.tech \
--cc=jgross@suse.com \
--cc=roger.pau@citrix.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.