Skip to contentSkip to conversation

tmem: add page deduplication with optional compression or trailing-zero-elimination

PATCH New Dan Magenheimer 1 participant 1 message Apr 5, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tmem: add page deduplication with optional compression or trailing-zero-elimination

Add "page deduplication" capability (with optional compression and trailing-zero elimination) to Xen's tmem.

(Transparent to tmem-enabled guests.) Ephemeral pages that have the exact same content are "combined" so that only one page frame is needed. Since ephemeral pages are essentially read-only, no C-O-W (and thus no equivalent of swapping) is necessary. Deduplication can be combined with compression or "trailing zero elimination" for even more space savings.

No non-tmem code is affected.

Points of interest:
- Modifications to LRU eviction algorithm to accommodate dedup'ed pages
- New data structures to allow lookup of matching pages and track references. (Algorithm used is similar to that used by KSM in KVM/Linux: No hashing required.)
- Lock (and rbtree) chosen by first byte of data to allow reasonably high concurrency without greatly complicating lock management.
- Statistics added so "dedup ratio" can be monitored.
- Dedup is disabled/enabled by Xen command line option and must be combined with the tmem Xen option.

Trailing zero elimination ("tze") saves significant tmem RAM when many data files are less than 1-3 pages and remaining (unused) space at the end of a page is zero-filled on disk or in memory, as may be the case for example when VMs are serving a large number of (deduplicate'able) web pages.

Compression already was a tmem option; v2 of this patch combines compression with deduplication to further improve tmem RAM utilization.

Either option (tmem_tze or tmem_compress) can be enabled at xen boot time in addition to deduplication (tmem_dedup) but compression overrides/disables tze. Both have a significant CPU cost so are useful primarily when memory is more constrained than CPU cycles, for example on a many-core machine with many low CPU-utilization RAM-needy VMs.

tools/misc/xen-tmem-list-parse.c | 30 ++

 xen/common/tmem.c                |  474 ++++++++++++++++++++++++++++++++-------
 xen/common/tmem_xen.c            |   33 ++
 xen/include/xen/tmem_xen.h       |  122 +++++++++-
 4 files changed, 576 insertions(+), 83 deletions(-)
tools/misc/xen-tmem-list-parse.c +28 -2
diff -r b8d2a4134a68 tools/misc/xen-tmem-list-parse.c
@@ -110,13 +110,39 @@ void parse_global(char *s)
     unsigned long long rtree_node_max = parse(s,"Nm");
     unsigned long long pgp_count = parse(s,"Pc");
     unsigned long long pgp_max = parse(s,"Pm");
+    unsigned long long page_count = parse(s,"Fc");
+    unsigned long long max_page_count = parse(s,"Fm");
+    unsigned long long pcd_count = parse(s,"Sc");
+    unsigned long long max_pcd_count = parse(s,"Sm");
+    unsigned long long pcd_tot_tze_size = parse(s,"Zt");
+    unsigned long long pcd_tot_csize = parse(s,"Gz");
+    unsigned long long deduped_puts = parse(s,"Gd");
+    unsigned long long tot_good_eph_puts = parse(s,"Ep");
 
     printf("total tmem ops=%llu (errors=%llu) -- tmem pages avail=%llu\n",
            total_ops, errored_ops, avail_pages);
     printf("datastructs: objs=%llu (max=%llu) pgps=%llu (max=%llu) "
-           "nodes=%llu (max=%llu)\n",
+           "nodes=%llu (max=%llu) pages=%llu (max=%llu) ",
            obj_count, obj_max, pgp_count, pgp_max,
-           rtree_node_count, rtree_node_max);
+           rtree_node_count, rtree_node_max,
+           page_count,max_page_count);
+    if (max_pcd_count != 0 && global_eph_count != 0 && tot_good_eph_puts != 0) {
+           printf("pcds=%llu (max=%llu) ",
+               pcd_count,max_pcd_count);
+           printf("deduped: avg=%4.2f%% (curr=%4.2f%%) ",
+                   ((deduped_puts*1.0)/tot_good_eph_puts)*100,
+                   (1.0-(pcd_count*1.0)/global_eph_count)*100);
+    }
+    if (pcd_count != 0)
+    {
+           if (pcd_tot_tze_size && (pcd_tot_tze_size < pcd_count*PAGE_SIZE))
+               printf("tze savings=%4.2f%% ",
+                   (1.0-(pcd_tot_tze_size*1.0)/(pcd_count*PAGE_SIZE))*100);
+           if (pcd_tot_csize && (pcd_tot_csize < pcd_count*PAGE_SIZE))
+               printf("compression savings=%4.2f%% ",
+                   (1.0-(pcd_tot_csize*1.0)/(pcd_count*PAGE_SIZE))*100);
+    }
+    printf("\n");
     printf("misc: failed_copies=%llu alloc_failed=%llu alloc_page_failed=%llu "
            "low_mem=%llu evicted=%llu/%llu relinq=%llu/%llu, "
            "max_evicts_per_relinq=%llu, flush_pools=%llu, "
xen/common/tmem.c +399 -75

670 lines not on this page · raw message

xen/common/tmem_xen.c +30 -3
diff -r b8d2a4134a68 xen/common/tmem_xen.c
@@ -19,6 +19,12 @@ boolean_param("tmem", opt_tmem);
 
 EXPORT int opt_tmem_compress = 0;
 boolean_param("tmem_compress", opt_tmem_compress);
+
+EXPORT int opt_tmem_dedup = 0;
+boolean_param("tmem_dedup", opt_tmem_dedup);
+
+EXPORT int opt_tmem_tze = 0;
+boolean_param("tmem_tze", opt_tmem_tze);
 
 EXPORT int opt_tmem_shared_auth = 0;
 boolean_param("tmem_shared_auth", opt_tmem_shared_auth);
@@ -103,8 +109,8 @@ static inline void *cli_mfn_to_va(tmem_c
 #endif
 
 EXPORT int tmh_copy_from_client(pfp_t *pfp,
-    tmem_cli_mfn_t cmfn, uint32_t tmem_offset,
-    uint32_t pfn_offset, uint32_t len, void *cli_va)
+    tmem_cli_mfn_t cmfn, pagesize_t tmem_offset,
+    pagesize_t pfn_offset, pagesize_t len, void *cli_va)
 {
     unsigned long tmem_mfn;
     void *tmem_va;
@@ -148,7 +154,7 @@ EXPORT int tmh_compress_from_client(tmem
 }
 
 EXPORT int tmh_copy_to_client(tmem_cli_mfn_t cmfn, pfp_t *pfp,
-    uint32_t tmem_offset, uint32_t pfn_offset, uint32_t len, void *cli_va)
+    pagesize_t tmem_offset, pagesize_t pfn_offset, pagesize_t len, void *cli_va)
 {
     unsigned long tmem_mfn, cli_mfn = 0;
     int mark_dirty = 1;
@@ -195,6 +201,27 @@ EXPORT int tmh_decompress_to_client(tmem
         unmap_domain_page(cli_va);
         paging_mark_dirty(current->domain,cli_mfn);
     }
+    mb();
+    return 1;
+}
+
+EXPORT int tmh_copy_tze_to_client(tmem_cli_mfn_t cmfn, void *tmem_va,
+                                    pagesize_t len)
+{
+    void *cli_va;
+    unsigned long cli_mfn;
+
+    ASSERT(!(len & (sizeof(uint64_t)-1)));
+    ASSERT(len <= PAGE_SIZE);
+    ASSERT(len > 0 || tmem_va == NULL);
+    if ( (cli_va = cli_mfn_to_va(cmfn,&cli_mfn)) == NULL)
+        return -EFAULT;
+    if ( len > 0 )
+        memcpy((char *)cli_va,(char *)tmem_va,len);
+    if ( len < PAGE_SIZE )
+        memset((char *)cli_va+len,0,PAGE_SIZE-len);
+    unmap_domain_page(cli_va);
+    paging_mark_dirty(current->domain,cli_mfn);
     mb();
     return 1;
 }
xen/include/xen/tmem_xen.h +119 -3

148 lines not on this page · raw message