Skip to contentSkip to conversation

tools/libxc: Batch memory allocations for PV guests

PATCH Superseded Ross Lagerwall 2 participants 2 messages Jun 10, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libxc: Batch memory allocations for PV guests

The current code for allocating memory for PV guests batches the hypercalls to allocate memory by allocating 1024*1024 extents of order 0 at a time. To make this faster, first try allocating single extents of order 9 (2 MiB) before falling back to the order 0 allocating if the order 9 allocation fails.

On my test machine this reduced the time to start a 128 GiB PV guest by about 60 seconds.

---
 tools/libxc/xc_dom_x86.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
tools/libxc/xc_dom_x86.c +20 -2
index 783f749..a674d39 100644
@@ -761,7 +761,7 @@ int arch_setup_meminit(struct xc_dom_image *dom)
 {
     int rc;
     xen_pfn_t pfn, allocsz, mfn, total, pfn_base;
-    int i, j;
+    int i, j, k;
 
     rc = x86_compat(dom->xch, dom->guest_domid, dom->guest_type);
     if ( rc )
@@ -881,7 +881,25 @@ int arch_setup_meminit(struct xc_dom_image *dom)
             for ( pfn = pfn_base; pfn < pfn_base+pages; pfn++ )
                 dom->p2m_host[pfn] = pfn;
 
-            for ( j = 0; j < pages; j += allocsz )
+            for ( j = 0; j < pages; )
+            {
+                if ( (pages - j) < SUPERPAGE_NR_PFNS )
+                    break;
+
+                mfn = dom->p2m_host[pfn_base + j];
+                rc = xc_domain_populate_physmap(dom->xch, dom->guest_domid, 1,
+                         SUPERPAGE_PFN_SHIFT, memflags, &mfn);
+
+                if ( rc != 1 )
+                    break;
+
+                /* Expand the returned mfn into the p2m array */
+                for ( k = 0; k < SUPERPAGE_NR_PFNS; k++, j++ )
+                    dom->p2m_host[pfn_base + j] = mfn + k;
+            }
+
+            rc = 0;
+            for ( ; j < pages; j += allocsz )
             {
                 allocsz = pages - j;
                 if ( allocsz > 1024*1024 )
Signature · 1 line
2.1.0
#1 lore raw
Quoted 35 lines · Ross Lagerwall

The current code for allocating memory for PV guests batches the hypercalls to allocate memory by allocating 1024*1024 extents of order 0 at a time. To make this faster, first try allocating single extents of order 9 (2 MiB) before falling back to the order 0 allocating if the order 9 allocation fails.

On my test machine this reduced the time to start a 128 GiB PV guest by about 60 seconds.

---
 tools/libxc/xc_dom_x86.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
tools/libxc/xc_dom_x86.c +3 -2
index 783f749..a674d39 100644
@@ -761,7 +761,7 @@ int arch_setup_meminit(struct xc_dom_image *dom)
 {
     int rc;
     xen_pfn_t pfn, allocsz, mfn, total, pfn_base;
-    int i, j;
+    int i, j, k;
 
     rc = x86_compat(dom->xch, dom->guest_domid, dom->guest_type);
     if ( rc )
@@ -881,7 +881,25 @@ int arch_setup_meminit(struct xc_dom_image *dom)
             for ( pfn = pfn_base; pfn < pfn_base+pages; pfn++ )
                 dom->p2m_host[pfn] = pfn;
 
-            for ( j = 0; j < pages; j += allocsz )
+            for ( j = 0; j < pages; )
+            {

tools/libxc/xc_dom_x86.c int arch_setup_meminit(struct xc_dom_image *dom) · line 895

Quoted 10 lines
tools/libxc/xc_dom_x86.c +10 -0
+                if ( (pages - j) < SUPERPAGE_NR_PFNS )
+                    break;
+
+                mfn = dom->p2m_host[pfn_base + j];
+                rc = xc_domain_populate_physmap(dom->xch, dom->guest_domid, 1,
+                         SUPERPAGE_PFN_SHIFT, memflags, &mfn);
+
+                if ( rc != 1 )
+                    break;
+

If you want to optimise you might as well consider batching to reduce the number of hypercall made. See dom->superpages path.

Wei.

Quoted 13 lines
tools/libxc/xc_dom_x86.c +6 -0
+                for ( k = 0; k < SUPERPAGE_NR_PFNS; k++, j++ )
+                    dom->p2m_host[pfn_base + j] = mfn + k;
+            }
+
+            rc = 0;
+            for ( ; j < pages; j += allocsz )
             {
 
 
                 allocsz = pages - j;
                 if ( allocsz > 1024*1024 )
Signature · 1 line
2.1.0