619821
From ea939f77fa0b152746821afb017cfef8170e5500 Mon Sep 17 00:00:00 2001
619821
From: Gerd Hoffmann <kraxel@redhat.com>
619821
Date: Wed, 22 Feb 2017 12:36:21 +0100
619821
Subject: [PATCH 03/24] ui/vnc: optimize dirty bitmap tracking
619821
MIME-Version: 1.0
619821
Content-Type: text/plain; charset=UTF-8
619821
Content-Transfer-Encoding: 8bit
619821
619821
RH-Author: Gerd Hoffmann <kraxel@redhat.com>
619821
Message-id: <1487766986-6329-4-git-send-email-kraxel@redhat.com>
619821
Patchwork-id: 73979
619821
O-Subject: [RHEL-7.4 qemu-kvm PATCH 3/8] ui/vnc: optimize dirty bitmap tracking
619821
Bugzilla: 1377977
619821
RH-Acked-by: Thomas Huth <thuth@redhat.com>
619821
RH-Acked-by: Marc-André Lureau <mlureau@redhat.com>
619821
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
619821
619821
From: Peter Lieven <pl@kamp.de>
619821
619821
vnc_update_client currently scans the dirty bitmap of each client
619821
bitwise which is a very costly operation if only few bits are dirty.
619821
vnc_refresh_server_surface does almost the same.
619821
this patch optimizes both by utilizing the heavily optimized
619821
function find_next_bit to find the offset of the next dirty
619821
bit in the dirty bitmaps.
619821
619821
The following artifical test (just the bitmap operation part) running
619821
vnc_update_client 65536 times on a 2560x2048 surface illustrates the
619821
performance difference:
619821
619821
All bits clean - vnc_update_client_new: 0.07 secs
619821
 vnc_update_client_old: 10.98 secs
619821
619821
All bits dirty - vnc_update_client_new: 11.26 secs
619821
 vnc_update_client_old: 20.19 secs
619821
619821
Few bits dirty - vnc_update_client_new: 0.08 secs
619821
 vnc_update_client_old: 10.98 secs
619821
619821
The case for all bits dirty is still rather slow, this
619821
is due to the implementation of find_and_clear_dirty_height.
619821
This will be addresses in a separate patch.
619821
619821
Signed-off-by: Peter Lieven <pl@kamp.de>
619821
Reviewed-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
619821
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
619821
(cherry picked from commit 12b316d4c173bf07f421ef9dc98ba4b53916066e)
619821
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
619821
---
619821
 ui/vnc.c | 155 ++++++++++++++++++++++++++++++++++-----------------------------
619821
 ui/vnc.h |   4 ++
619821
 2 files changed, 88 insertions(+), 71 deletions(-)
619821
619821
diff --git a/ui/vnc.c b/ui/vnc.c
619821
index 13fb34b..54530a2 100644
619821
--- a/ui/vnc.c
619821
+++ b/ui/vnc.c
619821
@@ -572,6 +572,15 @@ void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
619821
     ptr += x * VNC_SERVER_FB_BYTES;
619821
     return ptr;
619821
 }
619821
+/* this sets only the visible pixels of a dirty bitmap */
619821
+#define VNC_SET_VISIBLE_PIXELS_DIRTY(bitmap, w, h) {\
619821
+        int y;\
619821
+        memset(bitmap, 0x00, sizeof(bitmap));\
619821
+        for (y = 0; y < h; y++) {\
619821
+            bitmap_set(bitmap[y], 0,\
619821
+                       DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));\
619821
+        } \
619821
+    }
619821
 
619821
 static void vnc_dpy_switch(DisplayChangeListener *dcl,
619821
                            DisplaySurface *surface)
619821
@@ -597,7 +606,9 @@ static void vnc_dpy_switch(DisplayChangeListener *dcl,
619821
     qemu_pixman_image_unref(vd->guest.fb);
619821
     vd->guest.fb = pixman_image_ref(surface->image);
619821
     vd->guest.format = surface->format;
619821
-    memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty));
619821
+    VNC_SET_VISIBLE_PIXELS_DIRTY(vd->guest.dirty,
619821
+                                 surface_width(vd->ds),
619821
+                                 surface_height(vd->ds));
619821
 
619821
     QTAILQ_FOREACH(vs, &vd->clients, next) {
619821
         vnc_colordepth(vs);
619821
@@ -605,7 +616,9 @@ static void vnc_dpy_switch(DisplayChangeListener *dcl,
619821
         if (vs->vd->cursor) {
619821
             vnc_cursor_define(vs);
619821
         }
619821
-        memset(vs->dirty, 0xFF, sizeof(vs->dirty));
619821
+        VNC_SET_VISIBLE_PIXELS_DIRTY(vs->dirty,
619821
+                                     surface_width(vd->ds),
619821
+                                     surface_height(vd->ds));
619821
     }
619821
 }
619821
 
619821
@@ -891,10 +904,9 @@ static int vnc_update_client(VncState *vs, int has_dirty)
619821
         VncDisplay *vd = vs->vd;
619821
         VncJob *job;
619821
         int y;
619821
-        int width, height;
619821
+        int height;
619821
         int n = 0;
619821
 
619821
-
619821
         if (vs->output.offset && !vs->audio_cap && !vs->force_update)
619821
             /* kernel send buffers are full -> drop frames to throttle */
619821
             return 0;
619821
@@ -910,39 +922,27 @@ static int vnc_update_client(VncState *vs, int has_dirty)
619821
          */
619821
         job = vnc_job_new(vs);
619821
 
619821
-        width = MIN(pixman_image_get_width(vd->server), vs->client_width);
619821
         height = MIN(pixman_image_get_height(vd->server), vs->client_height);
619821
 
619821
-        for (y = 0; y < height; y++) {
619821
-            int x;
619821
-            int last_x = -1;
619821
-            for (x = 0; x < width / VNC_DIRTY_PIXELS_PER_BIT; x++) {
619821
-                if (test_and_clear_bit(x, vs->dirty[y])) {
619821
-                    if (last_x == -1) {
619821
-                        last_x = x;
619821
-                    }
619821
-                } else {
619821
-                    if (last_x != -1) {
619821
-                        int h = find_and_clear_dirty_height(vs, y, last_x, x,
619821
-                                                            height);
619821
-
619821
-                        n += vnc_job_add_rect(job,
619821
-                                              last_x * VNC_DIRTY_PIXELS_PER_BIT,
619821
-                                              y,
619821
-                                              (x - last_x) *
619821
-                                              VNC_DIRTY_PIXELS_PER_BIT,
619821
-                                              h);
619821
-                    }
619821
-                    last_x = -1;
619821
-                }
619821
-            }
619821
-            if (last_x != -1) {
619821
-                int h = find_and_clear_dirty_height(vs, y, last_x, x, height);
619821
-                n += vnc_job_add_rect(job, last_x * VNC_DIRTY_PIXELS_PER_BIT,
619821
-                                      y,
619821
-                                      (x - last_x) * VNC_DIRTY_PIXELS_PER_BIT,
619821
-                                      h);
619821
+        y = 0;
619821
+        for (;;) {
619821
+            int x, h;
619821
+            unsigned long x2;
619821
+            unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
619821
+                                                 height * VNC_DIRTY_BPL(vs),
619821
+                                                 y * VNC_DIRTY_BPL(vs));
619821
+            if (offset == height * VNC_DIRTY_BPL(vs)) {
619821
+                /* no more dirty bits */
619821
+                break;
619821
             }
619821
+            y = offset / VNC_DIRTY_BPL(vs);
619821
+            x = offset % VNC_DIRTY_BPL(vs);
619821
+            x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
619821
+                                    VNC_DIRTY_BPL(vs), x);
619821
+            bitmap_clear(vs->dirty[y], x, x2 - x);
619821
+            h = find_and_clear_dirty_height(vs, y, x, x2, height);
619821
+            n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
619821
+                                  (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
619821
         }
619821
 
619821
         vnc_job_push(job);
619821
@@ -2690,8 +2690,8 @@ static int vnc_refresh_server_surface(VncDisplay *vd)
619821
     int width = pixman_image_get_width(vd->guest.fb);
619821
     int height = pixman_image_get_height(vd->guest.fb);
619821
     int y;
619821
-    uint8_t *guest_row;
619821
-    uint8_t *server_row;
619821
+    uint8_t *guest_row0 = NULL, *server_row0;
619821
+    int guest_stride = 0, server_stride;
619821
     int cmp_bytes;
619821
     VncState *vs;
619821
     int has_dirty = 0;
619821
@@ -2716,44 +2716,57 @@ static int vnc_refresh_server_surface(VncDisplay *vd)
619821
     if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
619821
         int width = pixman_image_get_width(vd->server);
619821
         tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
619821
-    }
619821
-    guest_row = (uint8_t *)pixman_image_get_data(vd->guest.fb);
619821
-    server_row = (uint8_t *)pixman_image_get_data(vd->server);
619821
-    for (y = 0; y < height; y++) {
619821
-        if (!bitmap_empty(vd->guest.dirty[y], VNC_DIRTY_BITS)) {
619821
-            int x;
619821
-            uint8_t *guest_ptr;
619821
-            uint8_t *server_ptr;
619821
-
619821
-            if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
619821
-                qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
619821
-                guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
619821
-            } else {
619821
-                guest_ptr = guest_row;
619821
-            }
619821
-            server_ptr = server_row;
619821
+    } else {
619821
+        guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
619821
+        guest_stride = pixman_image_get_stride(vd->guest.fb);
619821
+    }
619821
+    server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
619821
+    server_stride = pixman_image_get_stride(vd->server);
619821
+
619821
+    y = 0;
619821
+    for (;;) {
619821
+        int x;
619821
+        uint8_t *guest_ptr, *server_ptr;
619821
+        unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
619821
+                                             height * VNC_DIRTY_BPL(&vd->guest),
619821
+                                             y * VNC_DIRTY_BPL(&vd->guest));
619821
+        if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
619821
+            /* no more dirty bits */
619821
+            break;
619821
+        }
619821
+        y = offset / VNC_DIRTY_BPL(&vd->guest);
619821
+        x = offset % VNC_DIRTY_BPL(&vd->guest);
619821
 
619821
-            for (x = 0; x + VNC_DIRTY_PIXELS_PER_BIT - 1 < width;
619821
-                 x += VNC_DIRTY_PIXELS_PER_BIT, guest_ptr += cmp_bytes,
619821
-                 server_ptr += cmp_bytes) {
619821
-                if (!test_and_clear_bit((x / VNC_DIRTY_PIXELS_PER_BIT),
619821
-                    vd->guest.dirty[y])) {
619821
-                    continue;
619821
-                }
619821
-                if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) {
619821
-                    continue;
619821
-                }
619821
-                memcpy(server_ptr, guest_ptr, cmp_bytes);
619821
-                if (!vd->non_adaptive)
619821
-                    vnc_rect_updated(vd, x, y, &tv;;
619821
-                QTAILQ_FOREACH(vs, &vd->clients, next) {
619821
-                    set_bit((x / VNC_DIRTY_PIXELS_PER_BIT), vs->dirty[y]);
619821
-                }
619821
-                has_dirty++;
619821
+        server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
619821
+
619821
+        if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
619821
+            qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
619821
+            guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
619821
+        } else {
619821
+            guest_ptr = guest_row0 + y * guest_stride;
619821
+        }
619821
+        guest_ptr += x * cmp_bytes;
619821
+
619821
+        for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
619821
+             x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
619821
+            if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
619821
+                continue;
619821
+            }
619821
+            if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) {
619821
+                continue;
619821
+            }
619821
+            memcpy(server_ptr, guest_ptr, cmp_bytes);
619821
+            if (!vd->non_adaptive) {
619821
+                vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
619821
+                                 y, &tv;;
619821
             }
619821
+            QTAILQ_FOREACH(vs, &vd->clients, next) {
619821
+                set_bit(x, vs->dirty[y]);
619821
+            }
619821
+            has_dirty++;
619821
         }
619821
-        guest_row  += pixman_image_get_stride(vd->guest.fb);
619821
-        server_row += pixman_image_get_stride(vd->server);
619821
+
619821
+        y++;
619821
     }
619821
     qemu_pixman_image_unref(tmpbuf);
619821
     return has_dirty;
619821
diff --git a/ui/vnc.h b/ui/vnc.h
619821
index 561f383..ebf4bdd 100644
619821
--- a/ui/vnc.h
619821
+++ b/ui/vnc.h
619821
@@ -88,6 +88,10 @@ typedef void VncSendHextileTile(VncState *vs,
619821
 /* VNC_DIRTY_BITS is the number of bits in the dirty bitmap. */
619821
 #define VNC_DIRTY_BITS (VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT)
619821
 
619821
+/* VNC_DIRTY_BPL (BPL = bits per line) might be greater than
619821
+ * VNC_DIRTY_BITS due to alignment */
619821
+#define VNC_DIRTY_BPL(x) (sizeof((x)->dirty) / VNC_MAX_HEIGHT * BITS_PER_BYTE)
619821
+
619821
 #define VNC_STAT_RECT  64
619821
 #define VNC_STAT_COLS (VNC_MAX_WIDTH / VNC_STAT_RECT)
619821
 #define VNC_STAT_ROWS (VNC_MAX_HEIGHT / VNC_STAT_RECT)
619821
-- 
619821
1.8.3.1
619821