qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Juan Quintela <quintela@redhat.com>
To: qemu-devel@nongnu.org
Cc: Orit Wasserman <owasserm@redhat.com>,
	Benoit Hudzia <benoit.hudzia@sap.com>,
	Eric Blake <eblake@redhat.com>,
	Aidan Shribman <aidan.shribman@sap.com>,
	Petter Svard <petters@cs.umu.se>
Subject: [Qemu-devel] [PATCH 18/22] Add xbzrle_encode_buffer and xbzrle_decode_buffer functions
Date: Fri, 13 Jul 2012 09:23:40 +0200	[thread overview]
Message-ID: <1342164224-32709-19-git-send-email-quintela@redhat.com> (raw)
In-Reply-To: <1342164224-32709-1-git-send-email-quintela@redhat.com>

From: Orit Wasserman <owasserm@redhat.com>

For performance we are encoding long word at a time.
For nzrun we use long-word-at-a-time NULL-detection tricks from strcmp():
using ((lword - 0x0101010101010101) & (~lword) & 0x8080808080808080) test
to find out if any byte in the long word is zero.

Signed-off-by: Benoit Hudzia <benoit.hudzia@sap.com>
Signed-off-by: Petter Svard <petters@cs.umu.se>
Signed-off-by: Aidan Shribman <aidan.shribman@sap.com>
Signed-off-by: Orit Wasserman <owasserm@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 migration.h |    4 ++
 savevm.c    |  159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 163 insertions(+)

diff --git a/migration.h b/migration.h
index 713aae0..743c366 100644
--- a/migration.h
+++ b/migration.h
@@ -100,4 +100,8 @@ void migrate_add_blocker(Error *reason);
  */
 void migrate_del_blocker(Error *reason);

+int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
+                         uint8_t *dst, int dlen);
+int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen);
+
 #endif
diff --git a/savevm.c b/savevm.c
index 6e82b2d..c5fd13f 100644
--- a/savevm.c
+++ b/savevm.c
@@ -2392,3 +2392,162 @@ void vmstate_register_ram_global(MemoryRegion *mr)
 {
     vmstate_register_ram(mr, NULL);
 }
+
+/*
+  page = zrun nzrun
+       | zrun nzrun page
+
+  zrun = length
+
+  nzrun = length byte...
+
+  length = uleb128 encoded integer
+ */
+int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
+                         uint8_t *dst, int dlen)
+{
+    uint32_t zrun_len = 0, nzrun_len = 0;
+    int d = 0, i = 0;
+    long res, xor;
+    uint8_t *nzrun_start = NULL;
+
+    g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
+               sizeof(long)));
+
+    while (i < slen) {
+        /* overflow */
+        if (d + 2 > dlen) {
+            return -1;
+        }
+
+        /* not aligned to sizeof(long) */
+        res = (slen - i) % sizeof(long);
+        while (res && old_buf[i] == new_buf[i]) {
+            zrun_len++;
+            i++;
+            res--;
+        }
+
+        /* word at a time for speed */
+        if (!res) {
+            while (i < slen &&
+                   (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
+                i += sizeof(long);
+                zrun_len += sizeof(long);
+            }
+
+            /* go over the rest */
+            while (i < slen && old_buf[i] == new_buf[i]) {
+                zrun_len++;
+                i++;
+            }
+        }
+
+        /* buffer unchanged */
+        if (zrun_len == slen) {
+            return 0;
+        }
+
+        /* skip last zero run */
+        if (i == slen) {
+            return d;
+        }
+
+        d += uleb128_encode_small(dst + d, zrun_len);
+
+        zrun_len = 0;
+        nzrun_start = new_buf + i;
+
+        /* overflow */
+        if (d + 2 > dlen) {
+            return -1;
+        }
+        /* not aligned to sizeof(long) */
+        res = (slen - i) % sizeof(long);
+        while (res && old_buf[i] != new_buf[i]) {
+            i++;
+            nzrun_len++;
+            res--;
+        }
+
+        /* word at a time for speed, use of 32-bit long okay */
+        if (!res) {
+            /* truncation to 32-bit long okay */
+            long mask = 0x0101010101010101ULL;
+            while (i < slen) {
+                xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
+                if ((xor - mask) & ~xor & (mask << 7)) {
+                    /* found the end of an nzrun within the current long */
+                    while (old_buf[i] != new_buf[i]) {
+                        nzrun_len++;
+                        i++;
+                    }
+                    break;
+                } else {
+                    i += sizeof(long);
+                    nzrun_len += sizeof(long);
+                }
+            }
+        }
+
+        d += uleb128_encode_small(dst + d, nzrun_len);
+        /* overflow */
+        if (d + nzrun_len > dlen) {
+            return -1;
+        }
+        memcpy(dst + d, nzrun_start, nzrun_len);
+        d += nzrun_len;
+        nzrun_len = 0;
+    }
+
+    return d;
+}
+
+int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
+{
+    int i = 0, d = 0;
+    int ret;
+    uint32_t count = 0;
+
+    while (i < slen) {
+
+        /* zrun */
+        if ((slen - i) < 2) {
+            return -1;
+        }
+
+        ret = uleb128_decode_small(src + i, &count);
+        if (ret < 0 || (i && !count)) {
+            return -1;
+        }
+        i += ret;
+        d += count;
+
+        /* overflow */
+        if (d > dlen) {
+            return -1;
+        }
+
+        /* nzrun */
+        if ((slen - i) < 2) {
+            return -1;
+        }
+
+        ret = uleb128_decode_small(src + i, &count);
+        if (ret < 0 || !count) {
+            return -1;
+        }
+        i += ret;
+
+        /* overflow */
+        if (d + count > dlen || i + count > slen) {
+            return -1;
+        }
+
+        memcpy(dst + d , src + i, count);
+        d += count;
+        i += count;
+    }
+
+    return d;
+}
-- 
1.7.10.4

  parent reply	other threads:[~2012-07-13  7:24 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-13  7:23 [Qemu-devel] [PATCH 00/22] Migration next Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 01/22] savevm: Use a struct to pass all handlers Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 02/22] savevm: Live migration handlers register the struct directly Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 03/22] savevm: remove SaveSetParamsHandler Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 04/22] savevm: remove SaveLiveStateHandler Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 05/22] savevm: Refactor cancel operation in its own operation Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 06/22] savevm: introduce is_active method Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 07/22] savevm: split save_live_setup from save_live_state Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 08/22] savevm: split save_live into stage2 and stage3 Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 09/22] ram: save_live_setup() don't need to sent pages Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 10/22] ram: save_live_complete() only do one loop Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 11/22] ram: iterate phase Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 12/22] ram: save_live_setup() we don't need to synchronize the dirty bitmap Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 13/22] Add migration capabilities Juan Quintela
2012-07-23 18:23   ` Luiz Capitulino
2012-07-23 19:30     ` Eric Blake
2012-07-24  6:25     ` Orit Wasserman
2012-07-24 12:50       ` Luiz Capitulino
2012-07-24 17:06         ` Orit Wasserman
2012-07-24 18:17           ` Luiz Capitulino
2012-07-25 13:05             ` Orit Wasserman
2012-07-25 13:11               ` Luiz Capitulino
2012-07-25 13:33                 ` Orit Wasserman
2012-07-13  7:23 ` [Qemu-devel] [PATCH 14/22] Add XBZRLE documentation Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 15/22] Add cache handling functions Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 16/22] Add uleb encoding/decoding functions Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 17/22] Change ram_save_block to return -1 if there are no more changes Juan Quintela
2012-07-13  7:23 ` Juan Quintela [this message]
2012-07-13  7:23 ` [Qemu-devel] [PATCH 19/22] Add XBZRLE to ram_save_block and ram_save_live Juan Quintela
2012-07-13  7:23 ` [Qemu-devel] [PATCH 20/22] Add migrate_set_cachesize command Juan Quintela
2012-07-23 19:21   ` Luiz Capitulino
2012-07-13  7:23 ` [Qemu-devel] [PATCH 21/22] Add XBZRLE statistics Juan Quintela
2012-07-23 19:33   ` Luiz Capitulino
2012-07-24  6:32     ` Orit Wasserman
2012-07-13  7:23 ` [Qemu-devel] [PATCH 22/22] ram: save live optimization Juan Quintela

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1342164224-32709-19-git-send-email-quintela@redhat.com \
    --to=quintela@redhat.com \
    --cc=aidan.shribman@sap.com \
    --cc=benoit.hudzia@sap.com \
    --cc=eblake@redhat.com \
    --cc=owasserm@redhat.com \
    --cc=petters@cs.umu.se \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).