libxc: checkpoint compression
PATCH v3 1/1 libxc: checkpoint compression
This patch series adds checkpoint compression functionality, while running under Remus.
Tested under xen-4.2-unstable.
A simple benchmark: SpecJBB (Java Benchmark)
- 4 minute Remus run, with PV domU (2.6.32 xenolinux kernel), 1G memory and 2 VCPUs
- Data sent without compression: ~70GB.
- Data sent with compression: ~8GB.
Changes since last version:
1. A revised compression stream format
2. Changed the xc_remus_* interface to a generic xc_compression_* format
3. Fixed issues like unaligned access, missing bounds check, etc
4. General code cleanups.
Shriram
PATCH v3 1/1 tools/libxc: Remus Checkpoint Compression
# HG changeset patch
# User Shriram Rajagopalan <[email protected]> # Date 1318545413 25200
# Node ID 47fdd52af616131142ac5faeacd83f40786da59a # Parent 4b0907c6a08c348962bd976c2976257b412408be tools/libxc: Remus Checkpoint Compression
Instead of sending dirty pages of guest memory as-is, use a simple compression algorithm that sends a RLE-encoded XOR of the page against its last sent copy. A small LRU cache is used to hold recently dirtied pages. Pagetable pages are sent as-is, as they are canonicalized at sender side and uncanonicalized at receiver.
tools/libxc/Makefile +1 -1
@@ -42,7 +42,7 @@ GUEST_SRCS-y := GUEST_SRCS-y += xg_private.c xc_suspend.c GUEST_SRCS-$(CONFIG_MIGRATE) += xc_domain_restore.c xc_domain_save.c -GUEST_SRCS-$(CONFIG_MIGRATE) += xc_offline_page.c +GUEST_SRCS-$(CONFIG_MIGRATE) += xc_offline_page.c xc_compression.c GUEST_SRCS-$(CONFIG_HVM) += xc_hvm_build.c vpath %.c ../../xen/common/libelf
tools/libxc/xc_compression.c new file +545 -0
545 lines not on this page · raw message
tools/libxc/xc_domain_restore.c +72 -1
133 lines not on this page · raw message
tools/libxc/xc_domain_save.c +194 -8
286 lines not on this page · raw message
tools/libxc/xenctrl.h +60 -0
64 lines not on this page · raw message
tools/libxc/xenguest.h +1 -0
@@ -27,6 +27,7 @@#define XCFLAGS_DEBUG 2#define XCFLAGS_HVM 4#define XCFLAGS_STDVGA 8 +#define XCFLAGS_CHECKPOINT_COMPRESS 16#define X86_64_B_SIZE 64#define X86_32_B_SIZE 32
tools/libxc/xg_save_restore.h +2 -0
@@ -134,6 +134,8 @@#define XC_SAVE_ID_HVM_CONSOLE_PFN -8/* (HVM-only) */ #define XC_SAVE_ID_LAST_CHECKPOINT -9/* Commit to restoring after completion of current iteration. */ #define XC_SAVE_ID_HVM_ACPI_IOPORTS_LOCATION -10 +#define XC_SAVE_ID_COMPRESSED_DATA -11/* Marker to indicate arrival of compressed data */ +#define XC_SAVE_ID_ENABLE_COMPRESSION -12/* Marker to enable compression logic at receiver side */ /* ** We process save/restore/migrate in batches of pages; the below
Re: [PATCH 1 of 3 V3] tools/libxc: Remus Checkpoint Compression
Quoted 7 lines · On Thursday, 13 October 2011 at 16:47, [email protected] wrote:
# HG changeset patch
# User Shriram Rajagopalan <[email protected]> # Date 1318545413 25200
# Node ID47fdd52af616131142ac5faeacd83f40786da59a# Parent4b0907c6a08c348962bd976c2976257b412408betools/libxc: Remus Checkpoint Compression
Quoted 7 lines
Instead of sending dirty pages of guest memory as-is, use a simple compression algorithm that sends a RLE-encoded XOR of the page against its last sent copy. A small LRU cache is used to hold recently dirtied pages. Pagetable pages are sent as-is, as they are canonicalized at sender side and uncanonicalized at receiver.
Signed-off-by Shriram Rajagopalan <[email protected]>
Re: [PATCH 1 of 3 V3] tools/libxc: Remus Checkpoint Compression
In reply to [email protected][email protected] writes ("[PATCH 1 of 3 V3] tools/libxc: Remus Checkpoint Compression"):
Quoted 6 lines
tools/libxc/xc_compression.c new file +6
-0+ flag = *src &FLAGMASK ; + len = *src &LENMASK ; + pos++; + src++; + +if (flag ==RUNFLAG )
tools/libxc/xc_compression.c line 272
Quoted 10 lines
tools/libxc/xc_compression.c new file +10
-0+ { +if ((pos + len *sizeof (uint32_t )) > compbuf_size) + { +ERROR ("Out of bounds exception in compression buffer (d):" +"read ptr %lu, runlen = %u, bufsize = %lu\n" , + pos, len *sizeof (uint32_t ), compbuf_size); +return -1 ; + } + +memcpy (&destpage[pagepos], src, len *sizeof (uint32_t ));
I think this may have a buffer overrun vulnerability. It seems to me that it can write beyond destpage + XC_PAGE_SIZE. Decompressors are often a source of security vulnerabilities of this kind, so it's important that we look at it closely.
tools/libxc/xc_compression.c line 447
Quoted 7 lines
tools/libxc/xc_compression.c new file +7
-0+int xc_compression_uncompress_page (xc_interface *xch,char *compbuf, +unsigned long compbuf_size, +unsigned long *compbuf_pos,char *dest) +{ +return uncompress_page (xch, dest, compbuf_pos, + (uint8_t *)compbuf, compbuf_size); +}
What is the purpose of this wrapper function ? All it seems to do is massage the type and order of arguments.
Thanks,
Ian.
Re: [PATCH 1 of 3 V3] tools/libxc: Remus Checkpoint Compression
In reply to [email protected]Quoted 61 lines · On Fri, Oct 14, 2011 at 12:47 AM, <[email protected]> wrote:
diff -r
4b0907c6a08c-r47fdd52af616tools/libxc/xc_domain_save.ctools/libxc/xc_domain_save.c Thu Oct 13 15:36:53 2011 -0700 renamed from tools/libxc/xc_domain_save.c Tue Oct 11 12:02:58 2011 +0100 +50
-0@@ -218,6 +218,56 @@ return noncached_write(xch, ob, fd, buf, len); } +static int write_compressed(xc_interface *xch, comp_ctx *compress_ctx, + int dobuf, struct outbuf* ob, int fd) +{ + int rc = 0; + int header = sizeof(int) + sizeof(unsigned long); + int marker = XC_SAVE_ID_COMPRESSED_DATA; + unsigned long compbuf_len = 0; + + do + { + /* check for available space (atleast 8k) */ + if ((ob->pos + header + XC_PAGE_SIZE * 2) > ob->size) + { + if (outbuf_flush(xch, ob, fd) < 0) + { + ERROR("Error when flushing outbuf intermediate"); + return -1; + } + } + + rc = xc_compression_compress_pages(xch, compress_ctx, + ob->buf + ob->pos + header, + ob->size - ob->pos - header, + &compbuf_len); + if (!rc) + return 0; + + if (outbuf_hardwrite(xch, ob, fd, &marker, sizeof(marker)) < 0) + { + PERROR("Error when writing marker (errno %d)", errno); + return -1; + } + + if (outbuf_hardwrite(xch, ob, fd, &compbuf_len, sizeof(compbuf_len)) < 0) + { + PERROR("Error when writing compbuf_len (errno %d)", errno); + return -1; + } + + ob->pos += (size_t) compbuf_len; + if (!dobuf && outbuf_flush(xch, ob, fd) < 0) + { + ERROR("Error when writing compressed chunk"); + return -1; + } + } while (rc != 0); + + return 0; +} + struct time_stats { struct timeval wall; long long d0_cpu, d1_cpu;
tools/libxc/xc_domain_save.c Thu Oct 13 15:36:53 2011 -0700 line 868
Quoted 5 lines
tools/libxc/xc_domain_save.c Thu Oct 13 15:36:53 2011 -0700 renamed from tools/libxc/xc_domain_save.c Tue Oct 11 12:02:58 2011 +0100 +1
-1@@ -815,11 +865,19 @@ unsigned long mfn; - struct outbuf ob; + struct outbuf ob_pagebuf, ob_tailbuf, *ob = NULL;
I think you need a comment here explaining what each of these are for.
Other than that, looks fine.
-George
Re: [PATCH 1 of 3 V3] tools/libxc: Remus Checkpoint Compression
George Dunlap writes ("Re: [Xen-devel] [PATCH 1 of 3 V3] tools/libxc: Remus Checkpoint Compression"):
Quoted 6 lines · On Fri, Oct 14, 2011 at 12:47 AM, <[email protected]> wrote:
Quoted 3 lines
- struct outbuf ob;
+ struct outbufob_pagebuf,ob_tailbuf, *ob = NULL;I think you need a comment here explaining what each of these are for.
Yes, please, that would be helpful.
Ian.
PATCH v3 1/1 remus: command line switche to enable/disable checkpoint compression
In reply to [email protected]# HG changeset patch
# User Shriram Rajagopalan <[email protected]>
# Date 1318545415 25200
# Node ID 32c4d844813aa22ca05df0b855857a607ac4c056
# Parent 47fdd52af616131142ac5faeacd83f40786da59a
remus: command line switche to enable/disable checkpoint compression
Add a command line switch to remus script that allows the user to enable or disable checkpoint compression in the libxc code.
tools/python/xen/lowlevel/checkpoint/checkpoint.c +4 -3
@@ -104,13 +104,14 @@PyObject * postcopy_cb =NULL ;PyObject * checkpoint_cb =NULL ;unsigned int interval =0 ; +unsigned int flags =0 ;int fd;struct save_callbacks callbacks;int rc; -if (!PyArg_ParseTuple (args,"O|OOOI" , &iofile, &suspend_cb, &postcopy_cb, - &checkpoint_cb, &interval)) +if (!PyArg_ParseTuple (args,"O|OOOII" , &iofile, &suspend_cb, &postcopy_cb, + &checkpoint_cb, &interval, &flags))return NULL ; self->interval = interval;
@@ -160,7 +161,7 @@ callbacks.data = self; self->threadstate =PyEval_SaveThread (); - rc =checkpoint_start (&self->cps, fd, &callbacks); + rc =checkpoint_start (&self->cps, fd, &callbacks, flags);PyEval_RestoreThread (self->threadstate);if (rc <0 ) {
tools/python/xen/lowlevel/checkpoint/checkpoint.h +3 -1
@@ -40,13 +40,15 @@timer_t timer; } checkpoint_state; +#define CHECKPOINT_FLAGS_COMPRESSION 1char *checkpoint_error (checkpoint_state * s);void checkpoint_init (checkpoint_state * s);int checkpoint_open (checkpoint_state * s,unsigned int domid);void checkpoint_close (checkpoint_state * s);int checkpoint_start (checkpoint_state * s,int fd, -struct save_callbacks * callbacks); +struct save_callbacks * callbacks, +unsigned int remus_flags);int checkpoint_suspend (checkpoint_state * s);int checkpoint_resume (checkpoint_state * s);int checkpoint_postflush (checkpoint_state * s);
tools/python/xen/lowlevel/checkpoint/libcheckpoint.c +4 -1
@@ -170,7 +170,8 @@ }int checkpoint_start (checkpoint_state * s,int fd, -struct save_callbacks * callbacks) +struct save_callbacks * callbacks, +unsigned int remus_flags) {int hvm, rc;int flags =XCFLAGS_LIVE ;
@@ -188,6 +189,8 @@if (switch_qemu_logdirty (s,1 ))return -1 ; } +if (remus_flags &CHECKPOINT_FLAGS_COMPRESSION ) + flags |=XCFLAGS_CHECKPOINT_COMPRESS ; callbacks->switch_qemu_logdirty = noop_switch_logdirty;
tools/python/xen/remus/save.py +5 -2
@@ -133,7 +133,7 @@ class Saver(object): def __init__(self, domid, fd, suspendcb=None, resumecb=None, - checkpointcb=None, interval=0): + checkpointcb=None, interval=0, flags=0): """Create a Saver object for taking guest checkpoints. domid: name, number or UUID of a running domain fd: a stream to which checkpoint data will be written.
@@ -141,12 +141,14 @@ resumecb: callback invoked before guest resumes checkpointcb: callback invoked when a checkpoint is complete. Return True to take another checkpoint, or False to stop. + flags: Remus flags to be passed to xc_domain_save """ self.fd = fd self.suspendcb = suspendcb self.resumecb = resumecb self.checkpointcb = checkpointcb self.interval = interval + self.flags = flags self.vm = vm.VM(domid)
@@ -164,7 +166,8 @@ try: self.checkpointer.open(self.vm.domid) self.checkpointer.start(self.fd, self.suspendcb, self.resumecb, - self.checkpointcb, self.interval) + self.checkpointcb, self.interval, + self.flags) except xen.lowlevel.checkpoint.error, e: raise CheckpointError(e) finally:
tools/remus/remus +9 -1
@@ -16,6 +16,9 @@ class CfgException(Exception): pass class Cfg(object): + + REMUS_FLAGS_COMPRESSION = 1 + def __init__(self): # must be set self.domid = 0
@@ -25,6 +28,7 @@ self.port = XendOptions.instance().get_xend_relocation_port() self.interval = 200 self.netbuffer = True + self.flags = self.REMUS_FLAGS_COMPRESSION self.timer = False parser = optparse.OptionParser()
@@ -38,6 +42,8 @@ help='replicate to /dev/null (no disk checkpoints, only memory & net buffering)') parser.add_option('', '--no-net', dest='nonet', action='store_true', help='run without net buffering (benchmark option)') + parser.add_option('', '--no-compression', dest='nocompress', action='store_true', + help='run without checkpoint compression') parser.add_option('', '--timer', dest='timer', action='store_true', help='force pause at checkpoint interval (experimental)') self.parser = parser
@@ -56,6 +62,8 @@ self.nullremus = True if opts.nonet: self.netbuffer = False + if opts.nocompress: + self.flags &= ~self.REMUS_FLAGS_COMPRESSION if opts.timer: self.timer = True
@@ -190,7 +198,7 @@ rc = 0 checkpointer = save.Saver(cfg.domid, fd, postsuspend, preresume, commit, - interval) + interval, cfg.flags) try: checkpointer.start()
Re: [PATCH 2 of 3 V3] remus: command line switche to enable/disable checkpoint compression
Quoted 11 lines · On Thursday, 13 October 2011 at 16:47, [email protected] wrote:
# HG changeset patch
# User Shriram Rajagopalan <[email protected]>
# Date 1318545415 25200
# Node ID32c4d844813aa22ca05df0b855857a607ac4c056
# Parent47fdd52af616131142ac5faeacd83f40786da59a
remus: command line switche to enable/disable checkpoint compressionAdd a command line switch to remus script that allows the user to enable or disable checkpoint compression in the libxc code.
Signed-off-by Shriram Rajagopalan <[email protected]>
PATCH v3 1/1 tools/libxc: Document checkpoint compression in xg_save_restore.h
In reply to [email protected]# HG changeset patch
# User Shriram Rajagopalan <[email protected]>
# Date 1318545418 25200
# Node ID 37fa1bdbecacd8fefcc382183ad25fd6c76300c3
# Parent 32c4d844813aa22ca05df0b855857a607ac4c056 tools/libxc: Document checkpoint compression in xg_save_restore.h
Add comments to xg_save_restore.h explaining changes in Remus wire protocol when checkpoint compression is enabled.
tools/libxc/xg_save_restore.h +117 -1
@@ -67,7 +67,7 @@* * consists of p2m_size bytes comprising an array of xen_pfn_t sized entries. * -* BODY PHASE +* BODY PHASE - Format A (for live migration or Remus without compression) * ---------- * * A series of chunks with a common header:
@@ -87,6 +87,122 @@* * If chunk type is 0 then body phase is complete. * +* +* BODY PHASE - Format B (for Remus with compression) +* ---------- +* +* A series of chunks with a common header: +* int : chunk type +* +* If the chunk type is +ve then chunk contains array of PFNs corresponding +* to guest memory and type contains the number of PFNs in the batch: +* +* unsigned long[] : PFN array, length == number of pages in batch +* Each entry consists of XEN_DOMCTL_PFINFO_* +* in bits 31-28 and the PFN number in bits 27-0. +* +* If the chunk type is -ve then chunk consists of one of a number of +* metadata types. See definitions of XC_SAVE_ID_* below. +* +* If the chunk type is -ve and equals XC_SAVE_ID_COMPRESSED_DATA, then the +* chunk consists of compressed page data, in the following format: +* +* unsigned long : Size of the compressed chunk to follow +* compressed data : variable length data of size indicated above. +* This chunk consists of compressed page data. +* The number of pages in one chunk depends on +* the amount of space available in the sender's +* output buffer. +* +* Format of compressed data: +* compressed_data = <deltas>* +* delta = <marker, run*> +* marker = (RUNFLAG|SKIPFLAG) bitwise-or RUNLEN [1 byte marker] +* RUNFLAG = 0 +* SKIPFLAG = 1 << 7 +* RUNLEN = 7-bit unsigned value indicating number of WORDS in the run +* run = string of bytes of length sizeof(WORD) * RUNLEN +* +* If marker contains RUNFLAG, then RUNLEN * sizeof(WORD) bytes of data following +* the marker is copied into the target page at the appropriate offset indicated by +* the offset_ptr +* If marker contains SKIPFLAG, then the offset_ptr is advanced +* by RUNLEN * sizeof(WORD). +* +* If chunk type is 0 then body phase is complete. +* +* There can be one or more chunks with type XC_SAVE_ID_COMPRESSED_DATA, +* containing compressed pages. The compressed chunks are collated to form +* one single compressed chunk for the entire iteration. The number of pages +* present in this final compressed chunk will be equal to the total number +* of valid PFNs specified by the +ve chunks. +* +* At the sender side, compressed pages are inserted into the output stream +* in the same order as they would have been if compression logic was absent. +* +* Until last iteration, the BODY is sent in Format A, to maintain live +* migration compatibility with receivers of older Xen versions. +* At the last iteration, if Remus compression was enabled, the sender sends +* a trigger, XC_SAVE_ID_ENABLE_COMPRESSION to tell the receiver to parse the +* BODY in Format B from the next iteration onwards. +* +* An example sequence of chunks received in Format B: +* +16 +ve chunk +* unsigned long[16] PFN array +* +100 +ve chunk +* unsigned long[100] PFN array +* +50 +ve chunk +* unsigned long[50] PFN array +* +* XC_SAVE_ID_COMPRESSED_DATA TAG +* N Length of compressed data +* N bytes of DATA Decompresses to 166 pages +* +* XC_SAVE_ID_* other xc save chunks +* 0 END BODY TAG +* +* Corner case with checkpoint compression: +* At sender side, after pausing the domain, dirty pages are usually +* copied out to a temporary buffer. After the domain is resumed, +* compression is done and the compressed chunk(s) are sent, followed by +* other XC_SAVE_ID_* chunks. +* If the temporary buffer gets full while scanning for dirty pages, +* the sender stops buffering of dirty pages, compresses the temporary +* buffer and sends the compressed data with XC_SAVE_ID_COMPRESSED_DATA. +* The sender then resumes the buffering of dirty pages and continues +* scanning for the dirty pages. +* For e.g., assume that the temporary buffer can hold 4096 pages and +* there are 5000 dirty pages. The following is the sequence of chunks +* that the receiver will see: +* +* +1024 +ve chunk +* unsigned long[1024] PFN array +* +1024 +ve chunk +* unsigned long[1024] PFN array +* +1024 +ve chunk +* unsigned long[1024] PFN array +* +1024 +ve chunk +* unsigned long[1024] PFN array +* +* XC_SAVE_ID_COMPRESSED_DATA TAG +* N Length of compressed data +* N bytes of DATA Decompresses to 4096 pages +* +* +4 +ve chunk +* unsigned long[4] PFN array +* +* XC_SAVE_ID_COMPRESSED_DATA TAG +* M Length of compressed data +* M bytes of DATA Decompresses to 4 pages +* +* XC_SAVE_ID_* other xc save chunks +* 0 END BODY TAG +* +* In other words, XC_SAVE_ID_COMPRESSED_DATA can be interleaved with +* +ve chunks arbitrarily. But at the receiver end, the following condition +* always holds true until the end of BODY PHASE: +* num(PFN entries +ve chunks) >= num(pages received in compressed form) +* * TAIL PHASE * ---------- *
Re: [PATCH 3 of 3 V3] tools/libxc: Document checkpoint compression in xg_save_restore.h
Quoted 11 lines · On Thursday, 13 October 2011 at 16:47, [email protected] wrote:
# HG changeset patch
# User Shriram Rajagopalan <[email protected]>
# Date 1318545418 25200
# Node ID37fa1bdbecacd8fefcc382183ad25fd6c76300c3
# Parent32c4d844813aa22ca05df0b855857a607ac4c056tools/libxc: Document checkpoint compression inxg_save_restore.hAdd comments to
xg_save_restore.hexplaining changes in Remus wire protocol when checkpoint compression is enabled.Signed-off-by Shriram Rajagopalan <[email protected]>
Re: [PATCH 3 of 3 V3] tools/libxc: Document checkpoint compression in xg_save_restore.h
In reply to [email protected]Quoted 9 lines · On Fri, Oct 14, 2011 at 12:47 AM, <[email protected]> wrote:
# HG changeset patch
# User Shriram Rajagopalan <[email protected]>
# Date 1318545418 25200
# Node ID37fa1bdbecacd8fefcc382183ad25fd6c76300c3
# Parent 32c4d844813aa22ca05df0b855857a607ac4c056tools/libxc: Document checkpoint compression inxg_save_restore.hAdd comments to
xg_save_restore.hexplaining changes in Remus wire protocol when checkpoint compression is enabled.
I'd put this in the first patch, but I'll leave that up to the tools maintainer.
-George
Re: [PATCH 3 of 3 V3] tools/libxc: Document checkpoint compression in xg_save_restore.h
George Dunlap writes ("Re: [Xen-devel] [PATCH 3 of 3 V3] tools/libxc: Document checkpoint compression in xg_save_restore.h"):
Quoted 6 lines · On Fri, Oct 14, 2011 at 12:47 AM, <[email protected]> wrote:
Quoted 2 lines
Add comments to
xg_save_restore.hexplaining changes in Remus wire protocol when checkpoint compression is enabled.I'd put this in the first patch, but I'll leave that up to the tools maintainer.
I agree and, barring other problems, was going to apply them in the other order.
Ian.