Skip to contentSkip to conversation

Xen 4.1 rc1 test report

PATCH In review Zheng, Shaohui 10 participants 32 messages Jan 22, 2011 – Feb 1, 2011

lore
Share & tools
lore
#0 lore raw

Hi, All
Intel QA conducted a full validation for xen 4.1 rc1, it includes VT-x, VT-d, SRIOV, RAS, TXT and xl tools testing. 24 issues were exposed. Refer the bug list, please.

We already assigned 14 bugs to Intel developers (which has an 'Intel' tag in the bug title), most of the rest 10 bugs are related xl command. For the these bugs, need community's help to fix them.

Version information:
Change Set: 22764:75b6287626ee
pvops dom0: 862ef97190f6b54d35c76c93fb2b8fadd7ab7d68 ioemu : 1c304816043c0ffe14d20d6006d6165cb7fddb9b

Bug list:
Vt-d ( 7 bugs)
1. ubuntu PAE SMP guest has network problem with NIC assigned (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1709
2. [VT-d] xen panic on function do_IRQ after many times NIC pass-throu (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1706
3. [VT-D]run guest with NIC assigned will cause system hang sometimes under PAE on Sandy bridge (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1725
4.[vt-d] dom0 igb driver is too old to support 4-port (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1708
5.[VT-d] xen panic when run guest with NIC assigned sometimes (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=24
6.[vt-d] xl command does not response after passthrou IGD card (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1723
7.[vt-d] fail to get IP address after hotplug VF for 300 times (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1722

RAS (1 bug)
1. System hang when running cpu offline (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1654

ACPI (1 bug)
1. System cann't resume after do suspend (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1707

Save/Restore(1 bug)
1. RHEL6 guest fail to do save/restore (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1716

xl command(7 bugs)
1. xl does not check the duplicated configure file and image file (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1711
2. [vt-d] Can not detach the device which was assigned statically (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1717
3. guest shows white screen when boot guest with NIC assigned (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1712
4. memory corruption was reported by "xl" with device pass-throu (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1713
5. [vt-d] fail to passthrou two or more devices to guest (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1710
6. Guest network broken after do SAVE/RESTOR with xl (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1703
7. Too many error information showed when destroy an inexistent guest (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1714

Hypervisor(4 bugs)
1. Only two 1GB-pages be allocated to a 10GBs memory guest (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1721
2. guest with vnif assigned fail to bootup when disable apic (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1692
3. Dom0 crashes on Core2 when dom0_mem is no more than 1972MB (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1726
4. Guest does not disappear after poweroff it (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1720

Performance(1 bug)
1. guest boot very slowly without limit dom0 cpu number on EX (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1719

X2APIC (1 bug)
1. Fail to bootup sandy bridge under PAE with x2apic enabled (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1718

Windows (1 bug)
1. All windows UP guest boot fail
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1704 (Intel)

Thanks & Regards,
Shaohui

#1 lore raw

Re: Xen 4.1 rc1 test report (xl bits)

Quoted 9 lines · Shaohui

Hi, All
Intel QA conducted a full validation for xen 4.1 rc1, it includes VT-x, VT-d, SRIOV, RAS, TXT and xl tools testing. 24 issues were exposed. Refer the bug list, please.

We already assigned 14 bugs to Intel developers (which has an 'Intel' tag in the bug title), most of the rest 10 bugs are related xl command. For the these bugs, need community's help to fix them.

Version information:
Change Set: 22764:75b6287626ee
pvops dom0: 862ef97190f6b54d35c76c93fb2b8fadd7ab7d68 ioemu : 1c304816043c0ffe14d20d6006d6165cb7fddb9b

Thanks.

Quoted 3 lines

xl command(7 bugs)
1. xl does not check the duplicated configure file and image file (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1711

Yes this is kind of known about, we give the user a lot of rope. We had already discussed that it's a bug for two domains to have the same name. A check for this condition would catch the 'oops I ran the same config twice' case.

As for going everywhere and preventing two domains from sharing a disk image it would be a bit more far-reaching and we'd need to think about legit cases where people might use shared r/w disk images with a separate synchronisation protocol...

Quoted 8 lines

2. [vt-d] Can not detach the device which was assigned statically (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1717
3. guest shows white screen when boot guest with NIC assigned (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1712
4. memory corruption was reported by "xl" with device pass-throu (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1713
5. [vt-d] fail to passthrou two or more devices to guest (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1710

We are well aware that pci passthrough is buggy in xl. At the very least, not all the safety checks are done. There are also problems with IRQ's becoming unmapped when one of two pass-through devices which share one or more IRQ's is detached. There are probably other issues too.

The heap corruption in xl looks particularly worrisome I will try find a moment to take a look at that one.

Quoted 2 lines

6. Guest network broken after do SAVE/RESTOR with xl (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1703

Sounds like a real clanger.

Quoted 2 lines

7. Too many error information showed when destroy an inexistent guest (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1714

A three line fix?

Gianni

#2 lore raw

Re: Xen 4.1 rc1 test report (xl bits)

Quoted 5 lines
Quoted 3 lines

7. Too many error information showed when destroy an inexistent guest (Community) http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1714

A three line fix?

Already posted to this list:
http://lists.xensource.com/archives/html/xen-devel/2011-01/msg01452.html

Christoph

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
#3 lore raw

PATCH 1/1 : xl: Check a domain exists before destroying it

Quoted 7 lines · Christoph Egger
Quoted 4 lines
Quoted 2 lines

7. Too many error information showed when destroy an inexistent guest (Community) http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1714

A three line fix?

Already posted to this list:
http://lists.xensource.com/archives/html/xen-devel/2011-01/msg01452.html

This is quite a clever fix but I think Ian Jacksons comments are correct. We should do a libxl_domain_info() and bail early in the destroy path if that fails.

---
xl: Check a domain exists before destroying it

Also fix a mis-formatted error message in xl destroy command.

tools/libxl/libxl.c +11 -0
diff -r b59f04eb8978 tools/libxl/libxl.c
@@ -654,10 +654,21 @@ int libxl_event_get_disk_eject_info(libx
 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
 {
     libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_dominfo dominfo;
     char *dom_path;
     char *vm_path;
     int rc, dm_present;
 
+    rc = libxl_domain_info(ctx, &dominfo, domid);
+    switch(rc) {
+    case 0:
+        break;
+    case ERROR_INVAL:
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "non-existant domain %d", domid);
+    default:
+        return rc;
+    }
+
     if (libxl__domain_is_hvm(ctx, domid)) {
         dm_present = 1;
     } else {
tools/libxl/xl_cmdimpl.c +1 -1
diff -r b59f04eb8978 tools/libxl/xl_cmdimpl.c
@@ -2176,7 +2176,7 @@ static void destroy_domain(const char *p
         exit(-1);
     }
     rc = libxl_domain_destroy(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
+    if (rc) { fprintf(stderr,"destroy failed (rc=%d).\n",rc); exit(-1); }
 }
 
 static void shutdown_domain(const char *p, int wait)
#4 lore raw

PATCH v2 1/1 : xl: Check domain existance when doing domain identifier lookups

Quoted 4 lines · Gianni Tedesco
Quoted 3 lines · Christoph Egger
Quoted 3 lines
Quoted 2 lines

7. Too many error information showed when destroy an inexistent guest (Community) http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1714

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

A three line fix?

Already posted to this list:
http://lists.xensource.com/archives/html/xen-devel/2011-01/msg01452.html

This is quite a clever fix but I think Ian Jacksons comments are correct. We should do a libxl_domain_info() and bail early in the destroy path if that fails.

It occurs to me that the last patch won't fix it for anything but destroy. We should bail with a nice error for any command looking up a domain that doesn't exist and be consistent with name vs. numeric ID.

--
xl: Check domain existance when doing domain identifier lookups

Also fix a mis-formatted error message in xl destroy command.

tools/libxl/xl_cmdimpl.c +17 -4
diff -r b59f04eb8978 tools/libxl/xl_cmdimpl.c
@@ -143,11 +143,24 @@ static int qualifier_to_id(const char *p
 static int domain_qualifier_to_domid(const char *p, uint32_t *domid_r,
                                      int *was_name_r)
 {
-    int was_name;
+    libxl_dominfo dominfo;
+    int was_name, rc;
 
     was_name = qualifier_to_id(p, domid_r);
-    if (was_name_r) *was_name_r = was_name;
-    return was_name ? libxl_name_to_domid(&ctx, p, domid_r) : 0;
+    if (was_name_r)
+        *was_name_r = was_name;
+
+    if ( was_name ) {
+        rc = libxl_name_to_domid(&ctx, p, domid_r);
+        if ( rc )
+            return rc;
+    }
+
+    rc = libxl_domain_info(&ctx, &dominfo, *domid_r);
+    if ( rc )
+        return rc;
+
+    return 0;
 }
 
 static int cpupool_qualifier_to_cpupoolid(const char *p, uint32_t *poolid_r,
@@ -2176,7 +2189,7 @@ static void destroy_domain(const char *p
         exit(-1);
     }
     rc = libxl_domain_destroy(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
+    if (rc) { fprintf(stderr,"destroy failed (rc=%d).\n",rc); exit(-1); }
 }
 
 static void shutdown_domain(const char *p, int wait)
#5 lore raw

Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups

Quoted 29 lines · Gianni Tedesco

It occurs to me that the last patch won't fix it for anything but destroy. We should bail with a nice error for any command looking up a domain that doesn't exist and be consistent with name vs. numeric ID.

--
xl: Check domain existance when doing domain identifier lookups

Also fix a mis-formatted error message in xl destroy command.

diff -r b59f04eb8978 tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c +6 -3
@@ -143,11 +143,24 @@ static int qualifier_to_id(const char *p
 static int domain_qualifier_to_domid(const char *p, uint32_t *domid_r,
                                      int *was_name_r)
 {
-    int was_name;
+    libxl_dominfo dominfo;
+    int was_name, rc;
 
     was_name = qualifier_to_id(p, domid_r);
-    if (was_name_r) *was_name_r = was_name;
-    return was_name ? libxl_name_to_domid(&ctx, p, domid_r) : 0;
+    if (was_name_r)
+        *was_name_r = was_name;
+
+    if ( was_name ) {

tools/libxl/xl_cmdimpl.c static int qualifier_to_id(const char *p · line 163

Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10 -0
+        rc = libxl_name_to_domid(&ctx, p, domid_r);
+        if ( rc )
+            return rc;
+    }
+
+    rc = libxl_domain_info(&ctx, &dominfo, *domid_r);
+    if ( rc )
+        return rc;
+
+    return 0;

is this really needed?
libxl_name_to_domid should have returned error already if the domain doesn't exist.

#6 lore raw

Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups

Quoted 35 lines · Stefano Stabellini
Quoted 34 lines · Gianni Tedesco

It occurs to me that the last patch won't fix it for anything but destroy. We should bail with a nice error for any command looking up a domain that doesn't exist and be consistent with name vs. numeric ID.

--
xl: Check domain existance when doing domain identifier lookups

Also fix a mis-formatted error message in xl destroy command.

diff -r b59f04eb8978 tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c +11 -3
@@ -143,11 +143,24 @@ static int qualifier_to_id(const char *p
 static int domain_qualifier_to_domid(const char *p, uint32_t *domid_r,
                                      int *was_name_r)
 {
-    int was_name;
+    libxl_dominfo dominfo;
+    int was_name, rc;
 
     was_name = qualifier_to_id(p, domid_r);
-    if (was_name_r) *was_name_r = was_name;
-    return was_name ? libxl_name_to_domid(&ctx, p, domid_r) : 0;
+    if (was_name_r)
+        *was_name_r = was_name;
+
+    if ( was_name ) {
+        rc = libxl_name_to_domid(&ctx, p, domid_r);
+        if ( rc )
+            return rc;
+    }
+
Quoted 8 lines

tools/libxl/xl_cmdimpl.c static int qualifier_to_id(const char *p · line 163

Quoted 5 lines
tools/libxl/xl_cmdimpl.c +5 -0
+    rc = libxl_domain_info(&ctx, &dominfo, *domid_r);
+    if ( rc )
+        return rc;
+
+    return 0;

is this really needed?
libxl_name_to_domid should have returned error already if the domain doesn't exist.

Only if was_name is true... If they pass a number then we need to check explicitly. Perhaps it would be better to put that check in an else {} statement though.

Gianni

--
xl: Check domain existance when doing domain identifier lookups

Also fix a mis-formatted error message in xl destroy command.

tools/libxl/xl_cmdimpl.c +17 -4
diff -r b59f04eb8978 tools/libxl/xl_cmdimpl.c
@@ -143,11 +143,24 @@ static int qualifier_to_id(const char *p
 static int domain_qualifier_to_domid(const char *p, uint32_t *domid_r,
                                      int *was_name_r)
 {
-    int was_name;
+    int was_name, rc;
 
     was_name = qualifier_to_id(p, domid_r);
-    if (was_name_r) *was_name_r = was_name;
-    return was_name ? libxl_name_to_domid(&ctx, p, domid_r) : 0;
+    if (was_name_r)
+        *was_name_r = was_name;
+
+    if ( was_name ) {
+        rc = libxl_name_to_domid(&ctx, p, domid_r);
+        if ( rc )
+            return rc;
+    }else{
+        libxl_dominfo dominfo;
+        rc = libxl_domain_info(&ctx, &dominfo, *domid_r);
+        if ( rc )
+            return rc;
+    }
+
+    return 0;
 }
 
 static int cpupool_qualifier_to_cpupoolid(const char *p, uint32_t *poolid_r,
@@ -2176,7 +2189,7 @@ static void destroy_domain(const char *p
         exit(-1);
     }
     rc = libxl_domain_destroy(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
+    if (rc) { fprintf(stderr,"destroy failed (rc=%d).\n",rc); exit(-1); }
 }
 
 static void shutdown_domain(const char *p, int wait)
#7 lore raw

Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups

Quoted 4 lines · Gianni Tedesco

Only if was_name is true... If they pass a number then we need to check explicitly. Perhaps it would be better to put that check in an else {} statement though.

Good point.
Apart from the inconsistent code style:

Quoted 51 lines

--
xl: Check domain existance when doing domain identifier lookups

Also fix a mis-formatted error message in xl destroy command.

diff -r b59f04eb8978 tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c +17 -4
@@ -143,11 +143,24 @@ static int qualifier_to_id(const char *p
 static int domain_qualifier_to_domid(const char *p, uint32_t *domid_r,
                                      int *was_name_r)
 {
-    int was_name;
+    int was_name, rc;
 
     was_name = qualifier_to_id(p, domid_r);
-    if (was_name_r) *was_name_r = was_name;
-    return was_name ? libxl_name_to_domid(&ctx, p, domid_r) : 0;
+    if (was_name_r)
+        *was_name_r = was_name;
+
+    if ( was_name ) {
+        rc = libxl_name_to_domid(&ctx, p, domid_r);
+        if ( rc )
+            return rc;
+    }else{
+        libxl_dominfo dominfo;
+        rc = libxl_domain_info(&ctx, &dominfo, *domid_r);
+        if ( rc )
+            return rc;
+    }
+
+    return 0;
 }
 
 static int cpupool_qualifier_to_cpupoolid(const char *p, uint32_t *poolid_r,
@@ -2176,7 +2189,7 @@ static void destroy_domain(const char *p
         exit(-1);
     }
     rc = libxl_domain_destroy(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
+    if (rc) { fprintf(stderr,"destroy failed (rc=%d).\n",rc); exit(-1); }
 }
 
 static void shutdown_domain(const char *p, int wait)
#8 lore raw

Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups

In reply to Gianni Tedesco

Gianni Tedesco writes ("Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups"):

Quoted 3 lines

Only if was_name is true... If they pass a number then we need to check explicitly. Perhaps it would be better to put that check in an else {} statement though.

I think it's fine to get a whole bunch of errors rather than just one, if you say "xl something <numeric domain>" and the domain doesn't exist.

Ian.

#9 lore raw

Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups

In reply to Gianni Tedesco

Gianni Tedesco writes ("[PATCH, v2]: xl: Check domain existance when doing domain identifier lookups"):

Quoted 3 lines

It occurs to me that the last patch won't fix it for anything but destroy. We should bail with a nice error for any command looking up a domain that doesn't exist and be consistent with name vs. numeric ID.

I think the destroy logic needs to be different because if libxl_domain_info fails for some other reason than ERROR_INVAL (ie "domain does not exist"), destruction it needs to try all of the other destruction steps.

So if you do a general change like this the destroy case probably needs to bypass it.

Ian.

#10 lore raw

Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups

Quoted 5 lines · Ian Jackson

Gianni Tedesco writes ("[PATCH, v2]: xl: Check domain existance when doing domain identifier lookups"):

Quoted 3 lines

It occurs to me that the last patch won't fix it for anything but destroy. We should bail with a nice error for any command looking up a domain that doesn't exist and be consistent with name vs. numeric ID.

Quoted 7 lines

I think the destroy logic needs to be different because if libxl_domain_info fails for some other reason than ERROR_INVAL (ie "domain does not exist"), destruction it needs to try all of the other destruction steps.

So if you do a general change like this the destroy case probably needs to bypass it.

Yes I think you are right, except for that we can make the same argument against more than just destroy. In other words, let's change the logic such that only non-existant domain causes this to fail and other errors are ignored.

--

xl: Check domain existance when doing domain identifier lookups

Also fix a mis-formatted error messages in xl destroy command.

tools/libxl/xl_cmdimpl.c +21 -7
diff -r 5e91e7a6b130 tools/libxl/xl_cmdimpl.c
@@ -143,11 +143,25 @@ static int qualifier_to_id(const char *p
 static int domain_qualifier_to_domid(const char *p, uint32_t *domid_r,
                                      int *was_name_r)
 {
-    int was_name;
+    libxl_dominfo dominfo;
+    int was_name, rc;
 
     was_name = qualifier_to_id(p, domid_r);
-    if (was_name_r) *was_name_r = was_name;
-    return was_name ? libxl_name_to_domid(&ctx, p, domid_r) : 0;
+    if (was_name_r)
+        *was_name_r = was_name;
+
+    if ( was_name ) {
+        rc = libxl_name_to_domid(&ctx, p, domid_r);
+        if ( rc )
+            return rc;
+    }else{
+        rc = libxl_domain_info(&ctx, &dominfo, *domid_r);
+        /* error only if domain does not exist */
+        if ( rc == ERROR_INVAL )
+            return rc;
+    }
+
+    return 0;
 }
 
 static int cpupool_qualifier_to_cpupoolid(const char *p, uint32_t *poolid_r,
@@ -2176,7 +2190,7 @@ static void destroy_domain(const char *p
         exit(-1);
     }
     rc = libxl_domain_destroy(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
+    if (rc) { fprintf(stderr,"destroy failed (rc=%d).\n",rc); exit(-1); }
 }
 
 static void shutdown_domain(const char *p, int wait)
@@ -2185,7 +2199,7 @@ static void shutdown_domain(const char *
 
     find_domain(p);
     rc=libxl_domain_shutdown(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"shutdown failed (rc=%d)\n.",rc);exit(-1); }
+    if (rc) { fprintf(stderr,"shutdown failed (rc=%d)\n",rc);exit(-1); }
 
     if (wait) {
         libxl_waiter waiter;
@@ -2227,7 +2241,7 @@ static void reboot_domain(const char *p)
     int rc;
     find_domain(p);
     rc=libxl_domain_shutdown(&ctx, domid, 1);
-    if (rc) { fprintf(stderr,"reboot failed (rc=%d)\n.",rc);exit(-1); }
+    if (rc) { fprintf(stderr,"reboot failed (rc=%d)\n",rc);exit(-1); }
 }
 
 static void list_domains_details(const libxl_dominfo *info, int nb_domain)
@@ -2669,7 +2683,7 @@ static void core_dump_domain(const char 
     int rc;
     find_domain(domain_spec);
     rc=libxl_domain_core_dump(&ctx, domid, filename);
-    if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n.",rc);exit(-1); }
+    if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n",rc);exit(-1); }
 }
 
 static void migrate_receive(int debug, int daemonize)
#11 lore raw

Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups

Gianni Tedesco writes ("Re: [PATCH, v2]: xl: Check domain existance when doing domain identifier lookups"):

Quoted 4 lines

Yes I think you are right, except for that we can make the same argument against more than just destroy. In other words, let's change the logic such that only non-existant domain causes this to fail and other errors are ignored.

Hrm, yes. Thanks. I have applied your patch.

I corrected some minor code style problems (whitespace placement) in your patch.

Regards,
Ian.

#12 lore raw

Re: [PATCH]: xl: Check a domain exists before destroying it

In reply to Gianni Tedesco

Gianni Tedesco writes ("[PATCH]: xl: Check a domain exists before destroying it"):

Quoted 3 lines

This is quite a clever fix but I think Ian Jacksons comments are correct. We should do a libxl_domain_info() and bail early in the destroy path if that fails.

Thanks, I have applied the libxl part of this patch.

The xl_cmdimpl part:

Quoted 1 line

Also fix a mis-formatted error message in xl destroy command.

misses three other similar mistakes.

$ egrep '\\n.\"' tools/libxl/*.c
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"shutdown failed (rc=%d)\n.",rc);exit(-1); }
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"reboot failed (rc=%d)\n.",rc);exit(-1); }
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n.",rc);exit(-1); }
$ 

Also in general most of the messages from xl don't print full stops. So I suggest the patch below instead.

Ian.

xl: fix up some minor mistakes in error messages

perl -i~ -pe 's/\\n.\"/\\n\"/' tools/libxl/*.c

tools/libxl/xl_cmdimpl.c +4 -4
diff -r 787af706dadc tools/libxl/xl_cmdimpl.c
@@ -2176,7 +2176,7 @@ static void destroy_domain(const char *p
         exit(-1);
     }
     rc = libxl_domain_destroy(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
+    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n",rc); exit(-1); }
 }
 
 static void shutdown_domain(const char *p, int wait)
@@ -2185,7 +2185,7 @@ static void shutdown_domain(const char *
 
     find_domain(p);
     rc=libxl_domain_shutdown(&ctx, domid, 0);
-    if (rc) { fprintf(stderr,"shutdown failed (rc=%d)\n.",rc);exit(-1); }
+    if (rc) { fprintf(stderr,"shutdown failed (rc=%d)\n",rc);exit(-1); }
 
     if (wait) {
         libxl_waiter waiter;
@@ -2227,7 +2227,7 @@ static void reboot_domain(const char *p)
     int rc;
     find_domain(p);
     rc=libxl_domain_shutdown(&ctx, domid, 1);
-    if (rc) { fprintf(stderr,"reboot failed (rc=%d)\n.",rc);exit(-1); }
+    if (rc) { fprintf(stderr,"reboot failed (rc=%d)\n",rc);exit(-1); }
 }
 
 static void list_domains_details(const libxl_dominfo *info, int nb_domain)
@@ -2669,7 +2669,7 @@ static void core_dump_domain(const char 
     int rc;
     find_domain(domain_spec);
     rc=libxl_domain_core_dump(&ctx, domid, filename);
-    if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n.",rc);exit(-1); }
+    if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n",rc);exit(-1); }
 }
 
 static void migrate_receive(int debug, int daemonize)
#13 lore raw

Re: [PATCH]: xl: Check a domain exists before destroying it

Quoted 18 lines · Ian Jackson

Gianni Tedesco writes ("[PATCH]: xl: Check a domain exists before destroying it"):

Quoted 3 lines

This is quite a clever fix but I think Ian Jacksons comments are correct. We should do a libxl_domain_info() and bail early in the destroy path if that fails.

Thanks, I have applied the libxl part of this patch.

The xl_cmdimpl part:

Quoted 1 line

Also fix a mis-formatted error message in xl destroy command.

misses three other similar mistakes.

$ egrep '\\n.\"' tools/libxl/*.c
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n.",rc); exit(-1); }
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"shutdown failed (rc=%d)\n.",rc);exit(-1); }
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"reboot failed (rc=%d)\n.",rc);exit(-1); }
tools/libxl/xl_cmdimpl.c:    if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n.",rc);exit(-1); }
$ 
Quoted 2 lines

Also in general most of the messages from xl don't print full stops. So I suggest the patch below instead.

Good call, I must have barfed my regexp when I searched for the same error...

Gianni

#14 lore raw

Re: [PATCH]: xl: Check a domain exists before destroying it

Gianni Tedesco writes ("Re: [PATCH]: xl: Check a domain exists before destroying it"):

Quoted 6 lines · Ian Jackson
Quoted 2 lines

Also in general most of the messages from xl don't print full stops. So I suggest the patch below instead.

Good call, I must have barfed my regexp when I searched for the same error...

NP. I'll take that as an ack, and have applied my patch.

Thanks,
Ian.

#15 lore raw

Re: Xen 4.1 rc1 test report (xl bits)

In reply to Gianni Tedesco
Quoted 22 lines · Gianni Tedesco
Quoted 9 lines · Shaohui

Hi, All
Intel QA conducted a full validation for xen 4.1 rc1, it includes VT-x, VT-d, SRIOV, RAS, TXT and xl tools testing. 24 issues were exposed. Refer the bug list, please.

We already assigned 14 bugs to Intel developers (which has an 'Intel' tag in the bug title), most of the rest 10 bugs are related xl command. For the these bugs, need community's help to fix them.

Version information:
Change Set: 22764:75b6287626ee
pvops dom0: 862ef97190f6b54d35c76c93fb2b8fadd7ab7d68 ioemu : 1c304816043c0ffe14d20d6006d6165cb7fddb9b

Thanks.

Quoted 3 lines

xl command(7 bugs)
1. xl does not check the duplicated configure file and image file (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1711

Yes this is kind of known about, we give the user a lot of rope. We had already discussed that it's a bug for two domains to have the same name. A check for this condition would catch the 'oops I ran the same config twice' case.

Quoted 4 lines

As for going everywhere and preventing two domains from sharing a disk image it would be a bit more far-reaching and we'd need to think about legit cases where people might use shared r/w disk images with a separate synchronisation protocol...

Yes.
I don't think the duplicated image file problem should be solved at the xl level, however we should at least try to avoid creating domains with an already existing name.

Quoted 9 lines
Quoted 8 lines

2. [vt-d] Can not detach the device which was assigned statically (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1717
3. guest shows white screen when boot guest with NIC assigned (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1712
4. memory corruption was reported by "xl" with device pass-throu (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1713
5. [vt-d] fail to passthrou two or more devices to guest (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1710

Quoted 4 lines

We are well aware that pci passthrough is buggy in xl. At the very least, not all the safety checks are done. There are also problems with IRQ's becoming unmapped when one of two pass-through devices which share one or more IRQ's is detached. There are probably other issues too.

IanC is trying to fix some passthrough problems, hopefully the resulting patch series will make it in time for the 4.1 release.

#17 lore raw

RE: Xen 4.1 rc1 test report (xl bits)

Quoted 14 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Gianni Tedesco
Sent: Tuesday, January 25, 2011 10:05 PM
To: Zheng, Shaohui
Cc: Zhang, Yang Z; Kamala Narasimhan; [email protected]; Ian Jackson; Stefano Stabellini
Subject: Re: [Xen-devel] Xen 4.1 rc1 test report (xl bits)

Quoted 3 lines · Gianni Tedesco
Quoted 2 lines

6. Guest network broken after do SAVE/RESTOR with xl (Community)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1703

Quoted 6 lines
Quoted 1 line

Sounds like a real clanger.

But I cannot repro this, at least with hvm guests.

What is guest OS? Was the guest PV or HVM? What network driver was it using? What is host network setup, the standard xen briding?

It's HVM guest and run rhel5.5-32e inside guest. We use the emulate network. And host use the standard xen bridge.

best regards
yang

Quoted 7 lines

Thanks

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#18 lore raw
In reply to Zheng, Shaohui
Quoted 4 lines

Performance(1 bug)
1. guest boot very slowly without limit dom0 cpu number on EX (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1719

This bug happened 1 year before. Keir has made a fix with c/s 20841, which essentially holds the locked (and hence allocated) hypercall page so that next hypercall can reuse it without doing alloc and mlock again. By doing this, overhead of rschedule IPI as a result of frequent mlock is greatly reduced.

Late in year 2010, libxc introduced a new mechanism called hypercall buffers, as you can refer c/s 22288~22312. Keir's fix is dropped in this new framework. As a result, the bug appears again. Probably the new framework auther can pick up Keir's fix again?

Shan Haitao

#19 lore raw

Thanks haitao's clear explanation. I see that Ian is the author of series c/s 22288~22312. Add Ian into the loop.

Thanks & Regards,
Shaohui

Quoted 24 lines

-----Original Message-----
From: Haitao Shan [mailto:[email protected]]
Sent: Tuesday, January 25, 2011 2:24 PM
To: Zheng, Shaohui; Keir Fraser
Cc: [email protected]
Subject: Re: [Xen-devel] Xen 4.1 rc1 test report

Quoted 4 lines

Performance(1 bug)
1. guest boot very slowly without limit dom0 cpu number on EX (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1719

This bug happened 1 year before. Keir has made a fix with c/s 20841, which essentially holds the locked (and hence allocated) hypercall page so that next hypercall can reuse it without doing alloc and mlock again. By doing this, overhead of rschedule IPI as a result of frequent mlock is greatly reduced.

Late in year 2010, libxc introduced a new mechanism called hypercall buffers, as you can refer c/s 22288~22312. Keir's fix is dropped in this new framework. As a result, the bug appears again. Probably the new framework auther can pick up Keir's fix again?

Shan Haitao

#20 lore raw
In reply to Haitao Shan
Quoted 11 lines · Haitao Shan
Quoted 4 lines

Performance(1 bug)
1. guest boot very slowly without limit dom0 cpu number on EX (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1719

This bug happened 1 year before. Keir has made a fix with c/s 20841, which essentially holds the locked (and hence allocated) hypercall page so that next hypercall can reuse it without doing alloc and mlock again. By doing this, overhead of rschedule IPI as a result of frequent mlock is greatly reduced.

Quoted 4 lines

Late in year 2010, libxc introduced a new mechanism called hypercall buffers, as you can refer c/s 22288~22312. Keir's fix is dropped in this new framework. As a result, the bug appears again. Probably the new framework auther can pick up Keir's fix again?

Ian Campbell, I think (cc'ed).

-- Keir

Quoted 1 line

Shan Haitao

#21 lore raw
In reply to Haitao Shan
Quoted 11 lines · Haitao Shan
Quoted 4 lines

Performance(1 bug)
1. guest boot very slowly without limit dom0 cpu number on EX (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1719

This bug happened 1 year before. Keir has made a fix with c/s 20841, which essentially holds the locked (and hence allocated) hypercall page so that next hypercall can reuse it without doing alloc and mlock again. By doing this, overhead of rschedule IPI as a result of frequent mlock is greatly reduced.

Quoted 4 lines

Late in year 2010, libxc introduced a new mechanism called hypercall buffers, as you can refer c/s 22288~22312. Keir's fix is dropped in this new framework. As a result, the bug appears again. Probably the new framework auther can pick up Keir's fix again?

I think it would make sense to include a low water mark of a small number of pages (perhaps 4 or 8) which instead of being freed are kept and reused in preference to future new allocations. These pages would only finally be released by the xc_interface_close() call.

Is this something which you feel able to make a patch for?

Ian.

#22 lore raw

I think it is basically the same idea as Keir introduced in 20841. I guess this bug would happen on platforms which has large number of physical CPUs, not only on EX system of Intel.
If you can cook the patch, that would be great! Thanks!!

Shan Haitao

2011/1/25 Ian Campbell <[email protected]>:

Quoted 28 lines · Haitao Shan
Quoted 11 lines
Quoted 4 lines

Performance(1 bug)
1. guest boot very slowly without limit dom0 cpu number on EX (Intel)
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1719

This bug happened 1 year before. Keir has made a fix with c/s 20841, which essentially holds the locked (and hence allocated) hypercall page so that next hypercall can reuse it without doing alloc and mlock again. By doing this, overhead of rschedule IPI as a result of frequent mlock is greatly reduced.

Quoted 4 lines

Late in year 2010, libxc introduced a new mechanism called hypercall buffers, as you can refer c/s 22288~22312. Keir's fix is dropped in this new framework. As a result, the bug appears again. Probably the new framework auther can pick up Keir's fix again?

I think it would make sense to include a low water mark of a small number of pages (perhaps 4 or 8) which instead of being freed are kept and reused in preference to future new allocations. These pages would only finally be released by the xc_interface_close() call.

Is this something which you feel able to make a patch for?

Ian.

#23 lore raw

libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

Quoted 4 lines · Haitao Shan

I think it is basically the same idea as Keir introduced in 20841. I guess this bug would happen on platforms which has large number of physical CPUs, not only on EX system of Intel.
If you can cook the patch, that would be great! Thanks!!

Does this help?

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296038761 0
# Node ID 8b8b7e024f9d6f4c2ce1a4efbf38f07eeb460d91
# Parent e4e69622dc95037eab6740f79ecf9c1d05bca529
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be reused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

tools/libxc/xc_hcall_buf.c +102 -3
diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_hcall_buf.c
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <pthread.h>
 
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,11 +29,108 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF
     HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations > xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations = xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr < HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)
 {
-    void *p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
 
-    if (!p)
+    if ( !p )
+        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+
+    if ( !p )
         return NULL;
 
     b->hbuf = p;
@@ -47,7 +145,8 @@ void xc__hypercall_buffer_free_pages(xc_
     if ( b->hbuf == NULL )
         return;
 
-    xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
 }
 
 struct allocation_header {
tools/libxc/xc_private.c +12 -0
diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.c
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
     xch->error_handler   = logger;           xch->error_handler_tofree   = 0;
     xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree = 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
     xch->ops_handle = XC_OSDEP_OPEN_ERROR;
     xch->ops = NULL;
 
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
 static int xc_interface_close_common(xc_interface *xch)
 {
     int rc = 0;
+
+    xc_hypercall_buffer_cache_release(xch);
 
     xtl_logger_destroy(xch->dombuild_logger_tofree);
     xtl_logger_destroy(xch->error_handler_tofree);
tools/libxc/xc_private.h +27 -0
diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.h
@@ -75,6 +75,28 @@ struct xc_interface_core {
     FILE *dombuild_logger_file;
     const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
     xc_osdep_info_t  osdep;
     xc_osdep_ops    *ops; /* backend operations */
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
 #define xc_hypercall_bounce_pre(_xch, _name) xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
 void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t *bounce);
 #define xc_hypercall_bounce_post(_xch, _name) xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
 
 /*
  * Hypercall interfaces.
#24 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

Quoted 7 lines · Ian Campbell
Quoted 4 lines · Haitao Shan

I think it is basically the same idea as Keir introduced in 20841. I guess this bug would happen on platforms which has large number of physical CPUs, not only on EX system of Intel.
If you can cook the patch, that would be great! Thanks!!

Does this help?

On my four way Intel system it is pretty negligible, in a very unscientific experiment the average real time for 3 x "xl create" goes from 0.333 to 0.330 which is in the noise.

Perhaps on a much larger system it helps more?

However looking at the bugzilla report it seems to concern slowness in the HVM BIOS before reaching grub which cannot possibly (AFAIK) be related to libxc or the previous 20841 fix.

The bugzilla report mentions a similar issue fixed by Xiaowei, is there a reference to that fix?

I think this patch is a good idea in its own right but unless there is feedback that this patch helps in this specific case I think we should defer it to 4.2.

(nb, if we do want it for 4.1 then I will resend since there is an xc__hypercall_buffer_cache_release below which needs to becomes xc__blah I didn't rebuild after a last minute change)

Ian.

Quoted 242 lines

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296038761 0
# Node ID 8b8b7e024f9d6f4c2ce1a4efbf38f07eeb460d91
# Parent e4e69622dc95037eab6740f79ecf9c1d05bca529
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be reused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_hcall_buf.c

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +102 -3
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <pthread.h>
 
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,11 +29,108 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF
     HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations > xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations = xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr < HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)
 {
-    void *p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
 
-    if (!p)
+    if ( !p )
+        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+
+    if ( !p )
         return NULL;
 
     b->hbuf = p;
@@ -47,7 +145,8 @@ void xc__hypercall_buffer_free_pages(xc_
     if ( b->hbuf == NULL )
         return;
 
-    xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
 }
 
 struct allocation_header {

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.c

tools/libxc/xc_private.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.c Wed Jan 26 10:22:42 2011 +0000 +12 -0
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
     xch->error_handler   = logger;           xch->error_handler_tofree   = 0;
     xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree = 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
     xch->ops_handle = XC_OSDEP_OPEN_ERROR;
     xch->ops = NULL;
 
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
 static int xc_interface_close_common(xc_interface *xch)
 {
     int rc = 0;
+
+    xc_hypercall_buffer_cache_release(xch);
 
     xtl_logger_destroy(xch->dombuild_logger_tofree);
     xtl_logger_destroy(xch->error_handler_tofree);

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.h

tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +27 -0
@@ -75,6 +75,28 @@ struct xc_interface_core {
     FILE *dombuild_logger_file;
     const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
     xc_osdep_info_t  osdep;
     xc_osdep_ops    *ops; /* backend operations */
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
 #define xc_hypercall_bounce_pre(_xch, _name) xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
 void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t *bounce);
 #define xc_hypercall_bounce_post(_xch, _name) xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
 
 /*
  * Hypercall interfaces.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#25 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

Sorry for late response. I happened to miss this important email. We will do some tests to verify the patch.

I do know there was a similar bug that Xiaowei had done much investigation. I don't know where I can find it in public bugzilla. I can post some internal tracking on this case:
------- Comment #10 From Yang, Xiaowei 2010-01-18 21:58:27 (-) [reply] ------- Hard to say now.

Some update: found the frequent resched IPI is not triggered by process migration. Rather it's caused by one function lru_add_drain_per_cpu() is inserted into each dom0 vcpu's work queue. As most of the vCPUs are in idle, they are notified by a resched IPI to execute the work queue.

There are several places that calls lru_add_drain_per_cpu(): mlock, shm, memory migration. Yet to find which is the culprit.------- Comment #11 From Yang, Xiaowei 2010-01-25 15:11:33 (-) [reply] ------- Should be fixed with c/s 20841 - at least, my test on NHM-EP shows HVM guest
boot time is shortened by half (40s -> 20s) and is as fast as with 2.6.18 dom0. Will have a test on NHM-EX later when grabing the machine.

Shan Haitao

2011/1/27 Ian Campbell <[email protected]>:

Quoted 277 lines · Ian Campbell
Quoted 7 lines · Haitao Shan
Quoted 4 lines

I think it is basically the same idea as Keir introduced in 20841. I guess this bug would happen on platforms which has large number of physical CPUs, not only on EX system of Intel.
If you can cook the patch, that would be great! Thanks!!

Does this help?

On my four way Intel system it is pretty negligible, in a very unscientific experiment the average real time for 3 x "xl create" goes from 0.333 to 0.330 which is in the noise.

Perhaps on a much larger system it helps more?

However looking at the bugzilla report it seems to concern slowness in the HVM BIOS before reaching grub which cannot possibly (AFAIK) be related to libxc or the previous 20841 fix.

The bugzilla report mentions a similar issue fixed by Xiaowei, is there a reference to that fix?

I think this patch is a good idea in its own right but unless there is feedback that this patch helps in this specific case I think we should defer it to 4.2.

(nb, if we do want it for 4.1 then I will resend since there is an xc__hypercall_buffer_cache_release below which needs to becomes xc__blah I didn't rebuild after a last minute change)

Ian.

Quoted 242 lines

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296038761 0
# Node ID 8b8b7e024f9d6f4c2ce1a4efbf38f07eeb460d91
# Parent  e4e69622dc95037eab6740f79ecf9c1d05bca529
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be reused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_hcall_buf.c

tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:22:42 2011 +0000 +1 -0
@@ -18,6 +18,7 @@
 
  #include <stdlib.h>
  #include <string.h>
+#include <pthread.h>
 
  #include "xc_private.h"
  #include "xg_private.h"
@@ -28,11 +29,108 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF

     HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };

tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:22:42 2011 +0000 +94 -0
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations > xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations = xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr < HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+

 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages) {
-    void *p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);

-    if (!p)
+    if ( !p )
+        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages); +
+    if ( !p )
         return NULL;

     b->hbuf = p;

tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:22:42 2011 +0000 +2 -1
@@ -47,7 +145,8 @@ void xc__hypercall_buffer_free_pages(xc_
      if ( b->hbuf == NULL )
          return;
 
-    xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
  }
 
  struct allocation_header {

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.c

tools/libxc/xc_private.c  Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.c  Wed Jan 26 10:22:42 2011 +0000 +12 -0
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
      xch->error_handler   = logger;           xch->error_handler_tofree   = 0;
      xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree = 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
      xch->ops_handle = XC_OSDEP_OPEN_ERROR;
      xch->ops = NULL;
 
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
  static int xc_interface_close_common(xc_interface *xch)
  {
      int rc = 0;
+
+    xc_hypercall_buffer_cache_release(xch);
 
      xtl_logger_destroy(xch->dombuild_logger_tofree);
      xtl_logger_destroy(xch->error_handler_tofree);

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.h

tools/libxc/xc_private.h  Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h  Wed Jan 26 10:22:42 2011 +0000 +22 -0
@@ -75,6 +75,28 @@ struct xc_interface_core {
      FILE *dombuild_logger_file;
      const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
      xc_osdep_info_t  osdep;

     xc_osdep_ops    *ops; /* backend operations */ xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */

tools/libxc/xc_private.h  Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h  Wed Jan 26 10:22:42 2011 +0000 +5 -0
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
  #define xc_hypercall_bounce_pre(_xch, _name) xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
  void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t *bounce);
  #define xc_hypercall_bounce_post(_xch, _name) xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
 
  /*
   * Hypercall interfaces.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#26 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

In reply to Ian Campbell

Hi, Ian,

I can not apply your patch. Is this patch developed against more newer version of Xen than what I can have at hand?

Shan Haitao

2011/1/26 Ian Campbell <[email protected]>:

Quoted 245 lines · Haitao Shan
Quoted 4 lines

I think it is basically the same idea as Keir introduced in 20841. I guess this bug would happen on platforms which has large number of physical CPUs, not only on EX system of Intel.
If you can cook the patch, that would be great! Thanks!!

Does this help?

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296038761 0
# Node ID 8b8b7e024f9d6f4c2ce1a4efbf38f07eeb460d91
# Parent  e4e69622dc95037eab6740f79ecf9c1d05bca529
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be reused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_hcall_buf.c

tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:22:42 2011 +0000 +1 -0
@@ -18,6 +18,7 @@
 
  #include <stdlib.h>
  #include <string.h>
+#include <pthread.h>
 
  #include "xc_private.h"
  #include "xg_private.h"
@@ -28,11 +29,108 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF

    HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };

tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:22:42 2011 +0000 +94 -0
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations > xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations = xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr < HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+

 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages) {
-    void *p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);

-    if (!p)
+    if ( !p )
+        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages); +
+    if ( !p )
        return NULL;

    b->hbuf = p;

tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c        Wed Jan 26 10:22:42 2011 +0000 +2 -1
@@ -47,7 +145,8 @@ void xc__hypercall_buffer_free_pages(xc_
     if ( b->hbuf == NULL )
         return;
 
-    xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
  }
 
  struct allocation_header {

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.c

tools/libxc/xc_private.c  Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.c  Wed Jan 26 10:22:42 2011 +0000 +12 -0
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
     xch->error_handler   = logger;           xch->error_handler_tofree   = 0;
     xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree = 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
     xch->ops_handle = XC_OSDEP_OPEN_ERROR;
     xch->ops = NULL;
 
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
  static int xc_interface_close_common(xc_interface *xch)
  {
     int rc = 0;
+
+    xc_hypercall_buffer_cache_release(xch);
 
     xtl_logger_destroy(xch->dombuild_logger_tofree);
     xtl_logger_destroy(xch->error_handler_tofree);

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.h

tools/libxc/xc_private.h  Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h  Wed Jan 26 10:22:42 2011 +0000 +22 -0
@@ -75,6 +75,28 @@ struct xc_interface_core {
     FILE *dombuild_logger_file;
     const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
     xc_osdep_info_t  osdep;

    xc_osdep_ops    *ops; /* backend operations */ xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */

tools/libxc/xc_private.h  Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h  Wed Jan 26 10:22:42 2011 +0000 +5 -0
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
  #define xc_hypercall_bounce_pre(_xch, _name) xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
  void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t *bounce);
  #define xc_hypercall_bounce_post(_xch, _name) xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
 
  /*
  * Hypercall interfaces.
#27 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

Quoted 4 lines · Haitao Shan

Hi, Ian,

I can not apply your patch. Is this patch developed against more newer version of Xen than what I can have at hand?

There was a typo below (an xc_hypercall_buffer_cache_release which should have been xc__hypercall_buffer_cache_release) but it sounds like you have trouble with actually applying the patch?

It was against a recent xen-unstable on the day I posted it. What were the rejects you saw?

If you have a particular version you want to test against I can generate a suitable patch for you.

Ian.

Quoted 249 lines

Shan Haitao

2011/1/26 Ian Campbell <[email protected]>:

Quoted 245 lines · Haitao Shan
Quoted 4 lines

I think it is basically the same idea as Keir introduced in 20841. I guess this bug would happen on platforms which has large number of physical CPUs, not only on EX system of Intel.
If you can cook the patch, that would be great! Thanks!!

Does this help?

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296038761 0
# Node ID 8b8b7e024f9d6f4c2ce1a4efbf38f07eeb460d91
# Parent e4e69622dc95037eab6740f79ecf9c1d05bca529
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be reused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_hcall_buf.c

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +102 -3
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <pthread.h>
 
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,11 +29,108 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF
    HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations > xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations = xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr < HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)
 {
-    void *p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
 
-    if (!p)
+    if ( !p )
+        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+
+    if ( !p )
        return NULL;
 
    b->hbuf = p;
@@ -47,7 +145,8 @@ void xc__hypercall_buffer_free_pages(xc_
    if ( b->hbuf == NULL )
        return;
 
-    xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
 }
 
 struct allocation_header {

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.c

tools/libxc/xc_private.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.c Wed Jan 26 10:22:42 2011 +0000 +12 -0
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
    xch->error_handler   = logger;           xch->error_handler_tofree   = 0;
    xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree = 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
    xch->ops_handle = XC_OSDEP_OPEN_ERROR;
    xch->ops = NULL;
 
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
 static int xc_interface_close_common(xc_interface *xch)
 {
    int rc = 0;
+
+    xc_hypercall_buffer_cache_release(xch);
 
    xtl_logger_destroy(xch->dombuild_logger_tofree);
    xtl_logger_destroy(xch->error_handler_tofree);

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.h

tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +27 -0
@@ -75,6 +75,28 @@ struct xc_interface_core {
    FILE *dombuild_logger_file;
    const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
    xc_osdep_info_t  osdep;
    xc_osdep_ops    *ops; /* backend operations */
    xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
 #define xc_hypercall_bounce_pre(_xch, _name) xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
 void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t *bounce);
 #define xc_hypercall_bounce_post(_xch, _name) xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
 
 /*
 * Hypercall interfaces.
#28 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

For example:

The patch:

void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)
{
- void *p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch,
xch->ops_handle, nr_pages);
+ void *p = hypercall_buffer_cache_alloc(xch, nr_pages);

- if (!p)
+ if ( !p )
+ p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch,
xch->ops_handle, nr_pages);
+
+ if ( !p )

        return NULL;

My code at hand:

void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)

{
    size_t size = nr_pages * PAGE_SIZE;
    void *p;
#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
    int ret;
    ret = posix_memalign(&p, PAGE_SIZE, size);
    if (ret != 0)
        return NULL;
#elif defined(__NetBSD__) || defined(__OpenBSD__)
    p = valloc(size);
#else
    p = memalign(PAGE_SIZE, size);

#endif

    if (!p)
        return NULL;
#ifndef __sun__
    if ( mlock(p, size) < 0 )
    {
        free(p);
        return NULL;
    }

#endif

    b->hbuf = p;

    memset(p, 0, size);
    return b->hbuf;
}

And BTW: I am using c/s 22846.

Shan Haitao

2011/1/31 Ian Campbell <[email protected]>

Quoted 284 lines · Haitao Shan
Quoted 4 lines

Hi, Ian,

I can not apply your patch. Is this patch developed against more newer version of Xen than what I can have at hand?

There was a typo below (an xc_hypercall_buffer_cache_release which should have been xc__hypercall_buffer_cache_release) but it sounds like you have trouble with actually applying the patch?

It was against a recent xen-unstable on the day I posted it. What were the rejects you saw?

If you have a particular version you want to test against I can generate a suitable patch for you.

Ian.

Quoted 50 lines

Shan Haitao

2011/1/26 Ian Campbell <[email protected]>:

Quoted 46 lines · Haitao Shan
Quoted 4 lines

I think it is basically the same idea as Keir introduced in 20841. I guess this bug would happen on platforms which has large number of physical CPUs, not only on EX system of Intel.
If you can cook the patch, that would be great! Thanks!!

Does this help?

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296038761 0
# Node ID 8b8b7e024f9d6f4c2ce1a4efbf38f07eeb460d91
# Parent e4e69622dc95037eab6740f79ecf9c1d05bca529
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be reused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_hcall_buf.c

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +1 -0
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <pthread.h>
 
Quoted 7 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 32

Quoted 7 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +1 -0
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,11 +29,108 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF
    HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
+pthread_mutex_t hypercall_buffer_cache_mutex =

PTHREAD_MUTEX_INITIALIZER;

Quoted 8 lines
Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +8 -0
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
Quoted 8 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 48

Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +8 -0
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int

nr_pages)

Quoted 8 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 56

Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +8 -0
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations >

xch->hypercall_buffer_maximum_allocations )

Quoted 1 line

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 57

Quoted 1 line
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +1 -0
+        xch->hypercall_buffer_maximum_allocations =

xch->hypercall_buffer_current_allocations;

Quoted 8 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 65

Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +8 -0
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p =

xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];

Quoted 5 lines
Quoted 5 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +5 -0
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
Quoted 8 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 78

Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +8 -0
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int

nr_pages)

Quoted 9 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 87

Quoted 9 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +9 -0
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr <

HYPERCALL_BUFFER_CACHE_SIZE )

Quoted 2 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 89

Quoted 2 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +2 -0
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++]

= p;

Quoted 7 lines
Quoted 7 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +7 -0
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
Quoted 8 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 104

Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +8 -0
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total

releases:%d",

Quoted 3 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 107

Quoted 3 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +3 -0
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum

allocations:%d",

Quoted 4 lines
Quoted 4 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +4 -0
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
Quoted 8 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 119

Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +8 -0
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p =

xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];

Quoted 1 line

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 120

Quoted 1 line
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +1 -0
+        xch->ops->u.privcmd.free_hypercall_buffer(xch,

xch->ops_handle, p, 1);

Quoted 6 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 126

Quoted 6 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +5 -0
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch,

xc_hypercall_buffer_t *b, int nr_pages)

Quoted 2 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 33

Quoted 2 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +0 -1
 {
-    void *p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch,

xch->ops_handle, nr_pages);

Quoted 5 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 xc_hypercall_buffer_t XC__HYPERCALL_BUFF · line 131

Quoted 5 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +3 -1
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
 
-    if (!p)
+    if ( !p )
+        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch,

xch->ops_handle, nr_pages);

Quoted 10 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 void xc__hypercall_buffer_free_pages(xc_ · line 50

Quoted 10 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +2 -1
+
+    if ( !p )
        return NULL;
 
    b->hbuf = p;
@@ -47,7 +145,8 @@ void xc__hypercall_buffer_free_pages(xc_
    if ( b->hbuf == NULL )
        return;
 
-    xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle,

b->hbuf, nr_pages);

Quoted 2 lines

tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 void xc__hypercall_buffer_free_pages(xc_ · line 149

Quoted 2 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +2 -0
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        xch->ops->u.privcmd.free_hypercall_buffer(xch,

xch->ops_handle, b->hbuf, nr_pages);

Quoted 8 lines

tools/libxc/xc_private.c Wed Jan 26 10:46:01 2011 +0000 static struct xc_interface_core *xc_inte · line 126

Quoted 8 lines
tools/libxc/xc_hcall_buf.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Wed Jan 26 10:22:42 2011 +0000 +0 -0
 }
 
 struct allocation_header {

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.c

tools/libxc/xc_private.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.c Wed Jan 26 10:22:42 2011 +0000 +0 -0
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
    xch->error_handler   = logger;           xch->error_handler_tofree

= 0;

Quoted 1 line

tools/libxc/xc_private.c Wed Jan 26 10:46:01 2011 +0000 static struct xc_interface_core *xc_inte · line 127

Quoted 1 line
tools/libxc/xc_private.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.c Wed Jan 26 10:22:42 2011 +0000 +0 -0
    xch->dombuild_logger = dombuild_logger;

xch->dombuild_logger_tofree = 0;

Quoted 49 lines
Quoted 49 lines
tools/libxc/xc_private.c Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.c Wed Jan 26 10:22:42 2011 +0000 +12 -0
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
    xch->ops_handle = XC_OSDEP_OPEN_ERROR;
    xch->ops = NULL;
 
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
 static int xc_interface_close_common(xc_interface *xch)
 {
    int rc = 0;
+
+    xc_hypercall_buffer_cache_release(xch);
 
    xtl_logger_destroy(xch->dombuild_logger_tofree);
    xtl_logger_destroy(xch->error_handler_tofree);

diff -r e4e69622dc95 -r 8b8b7e024f9d tools/libxc/xc_private.h

tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +19 -0
@@ -75,6 +75,28 @@ struct xc_interface_core {
    FILE *dombuild_logger_file;
    const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
Quoted 8 lines

tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 int xc__hypercall_bounce_pre(xc_interfac · line 178

Quoted 8 lines
tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +3 -0
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
    xc_osdep_info_t  osdep;
    xc_osdep_ops    *ops; /* backend operations */
    xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
 #define xc_hypercall_bounce_pre(_xch, _name)

xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))

Quoted 1 line

tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 int xc__hypercall_bounce_pre(xc_interfac · line 179

Quoted 1 line
tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +0 -0
 void xc__hypercall_bounce_post(xc_interface *xch,

xc_hypercall_buffer_t *bounce);

Quoted 1 line

tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 int xc__hypercall_bounce_pre(xc_interfac · line 180

Quoted 1 line
tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +0 -0
 #define xc_hypercall_bounce_post(_xch, _name)

xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))

Quoted 6 lines
Quoted 6 lines
tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +5 -0
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
 
Quoted 8 lines
Quoted 5 lines

tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 int xc__hypercall_bounce_pre(xc_interfac · line 188

Quoted 5 lines
tools/libxc/xc_private.h Wed Jan 26 10:46:01 2011 +0000 renamed from tools/libxc/xc_private.h Wed Jan 26 10:22:42 2011 +0000 +0 -0
 /*
 * Hypercall interfaces.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#29 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

Quoted 1 line · Haitao Shan

And BTW: I am using c/s 22846.

Sorry, I didn't notice a patch which I was holding off for 4.2 in my queue before this one.

Version rebased onto 22846:52e928af3637 below.

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296466278 0
# Node ID 9e6175469e6f246ee9370ef57f987bb435b00bef
# Parent 5b6663ba2bb2c54e8fa6745afa16297ebe43328d
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be resused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

tools/libxc/xc_hcall_buf.c +123 -19
diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_hcall_buf.c
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <malloc.h>
+#include <pthread.h>
 
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,31 +29,137 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF
     HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations > xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations = xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr < HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+static void do_hypercall_buffer_free_pages(void *ptr, int nr_pages)
+{
+#ifndef __sun__
+    (void) munlock(ptr, nr_pages * PAGE_SIZE);
+#endif
+
+    free(ptr);
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        do_hypercall_buffer_free_pages(p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)
 {
     size_t size = nr_pages * PAGE_SIZE;
-    void *p;
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
+
+    if ( !p ) {
 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
-    int ret;
-    ret = posix_memalign(&p, PAGE_SIZE, size);
-    if (ret != 0)
-        return NULL;
+        int ret;
+        ret = posix_memalign(&p, PAGE_SIZE, size);
+        if (ret != 0)
+            return NULL;
 #elif defined(__NetBSD__) || defined(__OpenBSD__)
-    p = valloc(size);
+        p = valloc(size);
 #else
-    p = memalign(PAGE_SIZE, size);
+        p = memalign(PAGE_SIZE, size);
 #endif
 
-    if (!p)
-        return NULL;
+        if (!p)
+            return NULL;
 
 #ifndef __sun__
-    if ( mlock(p, size) < 0 )
-    {
-        free(p);
-        return NULL;
+        if ( mlock(p, size) < 0 )
+        {
+            free(p);
+            return NULL;
+        }
+#endif
     }
-#endif
 
     b->hbuf = p;
 
@@ -65,11 +172,8 @@ void xc__hypercall_buffer_free_pages(xc_
     if ( b->hbuf == NULL )
         return;
 
-#ifndef __sun__
-    (void) munlock(b->hbuf, nr_pages * PAGE_SIZE);
-#endif
-
-    free(b->hbuf);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        do_hypercall_buffer_free_pages(b->hbuf, nr_pages);
 }
 
 struct allocation_header {
tools/libxc/xc_private.c +12 -0
diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_private.c
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
     xch->error_handler   = logger;           xch->error_handler_tofree   = 0;
     xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree = 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
     xch->ops_handle = XC_OSDEP_OPEN_ERROR;
     xch->ops = NULL;
 
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
 static int xc_interface_close_common(xc_interface *xch)
 {
     int rc = 0;
+
+    xc__hypercall_buffer_cache_release(xch);
 
     xtl_logger_destroy(xch->dombuild_logger_tofree);
     xtl_logger_destroy(xch->error_handler_tofree);
tools/libxc/xc_private.h +27 -0
diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_private.h
@@ -75,6 +75,28 @@ struct xc_interface_core {
     FILE *dombuild_logger_file;
     const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
     xc_osdep_info_t  osdep;
     xc_osdep_ops    *ops; /* backend operations */
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
 #define xc_hypercall_bounce_pre(_xch, _name) xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
 void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t *bounce);
 #define xc_hypercall_bounce_post(_xch, _name) xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
 
 /*
  * Hypercall interfaces.
#30 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

Thanks, Ian.
I will test tomorrow and let you know the result.

Shan Haitao

2011/1/31 Ian Campbell <[email protected]>

Quoted 296 lines · Haitao Shan
Quoted 1 line

And BTW: I am using c/s 22846.

Sorry, I didn't notice a patch which I was holding off for 4.2 in my queue before this one.

Version rebased onto 22846:52e928af3637 below.

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296466278 0
# Node ID 9e6175469e6f246ee9370ef57f987bb435b00bef
# Parent 5b6663ba2bb2c54e8fa6745afa16297ebe43328d
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be resused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_hcall_buf.c

tools/libxc/xc_hcall_buf.c Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Mon Jan 31 09:14:52 2011 +0000 +123 -19
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <malloc.h>
+#include <pthread.h>
 
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,31 +29,137 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF
     HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations >
 xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations =
 xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int
 nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr <
 HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+static void do_hypercall_buffer_free_pages(void *ptr, int nr_pages)
+{
+#ifndef __sun__
+    (void) munlock(ptr, nr_pages * PAGE_SIZE);
+#endif
+
+    free(ptr);
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum
 allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        do_hypercall_buffer_free_pages(p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch,
 xc_hypercall_buffer_t *b, int nr_pages)
 {
     size_t size = nr_pages * PAGE_SIZE;
-    void *p;
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
+
+    if ( !p ) {
 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
-    int ret;
-    ret = posix_memalign(&p, PAGE_SIZE, size);
-    if (ret != 0)
-        return NULL;
+        int ret;
+        ret = posix_memalign(&p, PAGE_SIZE, size);
+        if (ret != 0)
+            return NULL;
 #elif defined(__NetBSD__) || defined(__OpenBSD__)
-    p = valloc(size);
+        p = valloc(size);
 #else
-    p = memalign(PAGE_SIZE, size);
+        p = memalign(PAGE_SIZE, size);
 #endif
 
-    if (!p)
-        return NULL;
+        if (!p)
+            return NULL;
 
 #ifndef __sun__
-    if ( mlock(p, size) < 0 )
-    {
-        free(p);
-        return NULL;
+        if ( mlock(p, size) < 0 )
+        {
+            free(p);
+            return NULL;
+        }
+#endif
    }
-#endif
 
    b->hbuf = p;
@@ -65,11 +172,8 @@ void xc__hypercall_buffer_free_pages(xc_
     if ( b->hbuf == NULL )
        return;
 
-#ifndef __sun__
-    (void) munlock(b->hbuf, nr_pages * PAGE_SIZE);
-#endif
-
-    free(b->hbuf);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        do_hypercall_buffer_free_pages(b->hbuf, nr_pages);
 }
 
 struct allocation_header {

diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_private.c

tools/libxc/xc_private.c Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_private.c Mon Jan 31 09:14:52 2011 +0000 +10 -0
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
    xch->error_handler   = logger;           xch->error_handler_tofree   =
 0;
    xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree =
 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
    xch->ops_handle = XC_OSDEP_OPEN_ERROR;
    xch->ops = NULL;
tools/libxc/xc_private.c Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_private.c Mon Jan 31 09:14:52 2011 +0000 +2 -0
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
 static int xc_interface_close_common(xc_interface *xch)
 {
    int rc = 0;
+
+    xc__hypercall_buffer_cache_release(xch);
 
    xtl_logger_destroy(xch->dombuild_logger_tofree);
    xtl_logger_destroy(xch->error_handler_tofree);

diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_private.h

tools/libxc/xc_private.h Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_private.h Mon Jan 31 09:14:52 2011 +0000 +27 -0
@@ -75,6 +75,28 @@ struct xc_interface_core {
    FILE *dombuild_logger_file;
    const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
    xc_osdep_info_t  osdep;
    xc_osdep_ops    *ops; /* backend operations */
    xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
 #define xc_hypercall_bounce_pre(_xch, _name)
 xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
 void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t
 *bounce);
 #define xc_hypercall_bounce_post(_xch, _name)
 xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
Quoted 5 lines

/*
* Hypercall interfaces.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#31 lore raw

Re: libxc: maintain a small, per-handle, cache of hypercall buffer memory (Was: Re: Xen 4.1 rc1 test report)

I have tested the patch. The bug can be fixed by Ian's patch. Thanks!

So, Ian, please go on the process of your submitting the patch.

Shan Haitao

2011/1/31 Haitao Shan <[email protected]>

Quoted 308 lines

Thanks, Ian.
I will test tomorrow and let you know the result.

Shan Haitao

2011/1/31 Ian Campbell <[email protected]>

Quoted 301 lines · Haitao Shan
Quoted 2 lines

And BTW: I am using c/s 22846.

Sorry, I didn't notice a patch which I was holding off for 4.2 in my queue before this one.

Version rebased onto 22846:52e928af3637 below.

Ian.

8<---------------------------------------

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1296466278 0
# Node ID 9e6175469e6f246ee9370ef57f987bb435b00bef
# Parent 5b6663ba2bb2c54e8fa6745afa16297ebe43328d
libxc: maintain a small, per-handle, cache of hypercall buffer memory

Constantly m(un)locking memory can have significant overhead on systems with large numbers of CPUs. This was previously fixed by 20841:fbe8f32fa257 but this was dropped during the transition to hypercall buffers.

Introduce a small cache of single page hypercall buffer allocations which can be resused to avoid this overhead.

Add some statistics tracking to the hypercall buffer allocations.

The cache size of 4 was chosen based on these statistics since they indicated that 2 pages was sufficient to satisfy all concurrent single page hypercall buffer allocations seen during "xl create", "xl shutdown" and "xl destroy" of both a PV and HVM guest therefore 4 pages should cover the majority of important cases.

diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_hcall_buf.c

tools/libxc/xc_hcall_buf.c Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_hcall_buf.c Mon Jan 31 09:14:52 2011 +0000 +123 -19
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <malloc.h>
+#include <pthread.h>
 
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,31 +29,137 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFF
     HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
+pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void hypercall_buffer_cache_lock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
+}
+
+static void hypercall_buffer_cache_unlock(xc_interface *xch)
+{
+    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
+}
+
+static void *hypercall_buffer_cache_alloc(xc_interface *xch, int
 nr_pages)
+{
+    void *p = NULL;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_allocations++;
+    xch->hypercall_buffer_current_allocations++;
+    if ( xch->hypercall_buffer_current_allocations >
 xch->hypercall_buffer_maximum_allocations )
+        xch->hypercall_buffer_maximum_allocations =
 xch->hypercall_buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xch->hypercall_buffer_cache_toobig++;
+    }
+    else if ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p =
 xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        xch->hypercall_buffer_cache_hits++;
+    }
+    else
+    {
+        xch->hypercall_buffer_cache_misses++;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return p;
+}
+
+static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int
 nr_pages)
+{
+    int rc = 0;
+
+    hypercall_buffer_cache_lock(xch);
+
+    xch->hypercall_buffer_total_releases++;
+    xch->hypercall_buffer_current_allocations--;
+
+    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr <
 HYPERCALL_BUFFER_CACHE_SIZE )
+    {
+        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] =
 p;
+        rc = 1;
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+
+    return rc;
+}
+
+static void do_hypercall_buffer_free_pages(void *ptr, int nr_pages)
+{
+#ifndef __sun__
+    (void) munlock(ptr, nr_pages * PAGE_SIZE);
+#endif
+
+    free(ptr);
+}
+
+void xc__hypercall_buffer_cache_release(xc_interface *xch)
+{
+    void *p;
+
+    hypercall_buffer_cache_lock(xch);
+
+    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
+              xch->hypercall_buffer_total_allocations,
+              xch->hypercall_buffer_total_releases);
+    DBGPRINTF("hypercall buffer: current allocations:%d maximum
 allocations:%d",
+              xch->hypercall_buffer_current_allocations,
+              xch->hypercall_buffer_maximum_allocations);
+    DBGPRINTF("hypercall buffer: cache current size:%d",
+              xch->hypercall_buffer_cache_nr);
+    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
+              xch->hypercall_buffer_cache_hits,
+              xch->hypercall_buffer_cache_misses,
+              xch->hypercall_buffer_cache_toobig);
+
+    while ( xch->hypercall_buffer_cache_nr > 0 )
+    {
+        p =
 xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
+        do_hypercall_buffer_free_pages(p, 1);
+    }
+
+    hypercall_buffer_cache_unlock(xch);
+}
+
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch,
 xc_hypercall_buffer_t *b, int nr_pages)
 {
     size_t size = nr_pages * PAGE_SIZE;
-    void *p;
+    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
+
+    if ( !p ) {
 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
-    int ret;
-    ret = posix_memalign(&p, PAGE_SIZE, size);
-    if (ret != 0)
-        return NULL;
+        int ret;
+        ret = posix_memalign(&p, PAGE_SIZE, size);
+        if (ret != 0)
+            return NULL;
 #elif defined(__NetBSD__) || defined(__OpenBSD__)
-    p = valloc(size);
+        p = valloc(size);
 #else
-    p = memalign(PAGE_SIZE, size);
+        p = memalign(PAGE_SIZE, size);
 #endif
 
-    if (!p)
-        return NULL;
+        if (!p)
+            return NULL;
 
 #ifndef __sun__
-    if ( mlock(p, size) < 0 )
-    {
-        free(p);
-        return NULL;
+        if ( mlock(p, size) < 0 )
+        {
+            free(p);
+            return NULL;
+        }
+#endif
    }
-#endif
 
    b->hbuf = p;
@@ -65,11 +172,8 @@ void xc__hypercall_buffer_free_pages(xc_
     if ( b->hbuf == NULL )
        return;
 
-#ifndef __sun__
-    (void) munlock(b->hbuf, nr_pages * PAGE_SIZE);
-#endif
-
-    free(b->hbuf);
+    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
+        do_hypercall_buffer_free_pages(b->hbuf, nr_pages);
 }
 
 struct allocation_header {

diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_private.c

tools/libxc/xc_private.c Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_private.c Mon Jan 31 09:14:52 2011 +0000 +10 -0
@@ -126,6 +126,16 @@ static struct xc_interface_core *xc_inte
    xch->error_handler   = logger;           xch->error_handler_tofree   =
 0;
    xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree =
 0;
 
+    xch->hypercall_buffer_cache_nr = 0;
+
+    xch->hypercall_buffer_total_allocations = 0;
+    xch->hypercall_buffer_total_releases = 0;
+    xch->hypercall_buffer_current_allocations = 0;
+    xch->hypercall_buffer_maximum_allocations = 0;
+    xch->hypercall_buffer_cache_hits = 0;
+    xch->hypercall_buffer_cache_misses = 0;
+    xch->hypercall_buffer_cache_toobig = 0;
+
    xch->ops_handle = XC_OSDEP_OPEN_ERROR;
    xch->ops = NULL;
tools/libxc/xc_private.c Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_private.c Mon Jan 31 09:14:52 2011 +0000 +2 -0
@@ -171,6 +181,8 @@ static int xc_interface_close_common(xc_
 static int xc_interface_close_common(xc_interface *xch)
 {
    int rc = 0;
+
+    xc__hypercall_buffer_cache_release(xch);
 
    xtl_logger_destroy(xch->dombuild_logger_tofree);
    xtl_logger_destroy(xch->error_handler_tofree);

diff -r 5b6663ba2bb2 -r 9e6175469e6f tools/libxc/xc_private.h

tools/libxc/xc_private.h Mon Jan 31 09:31:18 2011 +0000 renamed from tools/libxc/xc_private.h Mon Jan 31 09:14:52 2011 +0000 +27 -0
@@ -75,6 +75,28 @@ struct xc_interface_core {
    FILE *dombuild_logger_file;
    const char *currently_progress_reporting;
 
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define HYPERCALL_BUFFER_CACHE_SIZE 4
+    int hypercall_buffer_cache_nr;
+    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * hypercall_buffer_cache lock.
+     */
+    int hypercall_buffer_total_allocations;
+    int hypercall_buffer_total_releases;
+    int hypercall_buffer_current_allocations;
+    int hypercall_buffer_maximum_allocations;
+    int hypercall_buffer_cache_hits;
+    int hypercall_buffer_cache_misses;
+    int hypercall_buffer_cache_toobig;
+
+    /* Low lovel OS interface */
    xc_osdep_info_t  osdep;
    xc_osdep_ops    *ops; /* backend operations */
    xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
@@ -156,6 +178,11 @@ int xc__hypercall_bounce_pre(xc_interfac
 #define xc_hypercall_bounce_pre(_xch, _name)
 xc__hypercall_bounce_pre(_xch, HYPERCALL_BUFFER(_name))
 void xc__hypercall_bounce_post(xc_interface *xch, xc_hypercall_buffer_t
 *bounce);
 #define xc_hypercall_bounce_post(_xch, _name)
 xc__hypercall_bounce_post(_xch, HYPERCALL_BUFFER(_name))
+
+/*
+ * Release hypercall buffer cache
+ */
+void xc__hypercall_buffer_cache_release(xc_interface *xch);
Quoted 6 lines
Quoted 5 lines

/*
* Hypercall interfaces.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel