Skip to contentSkip to conversation

vnc: destroy server surface if no client is connected

PATCH In review Peter Lieven 2 participants 12 messages Sep 25, 2015 – Oct 5, 2015

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vnc: destroy server surface if no client is connected

if no client is connected there is no need to keep the server surface. Throw it away to save memory.

---
v1->v2: don't create a dummy surface just set vd->server = NULL [Gerd]

 ui/vnc.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
ui/vnc.c +33 -5
index eba3fba..8c12ff5 100644
@@ -630,8 +630,14 @@ static void vnc_dpy_update(DisplayChangeListener *dcl,
 {
     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
     struct VncSurface *s = &vd->guest;
-    int width = pixman_image_get_width(vd->server);
-    int height = pixman_image_get_height(vd->server);
+    int width, height;
+
+    if (!vd->server) {
+        return;
+    }
+
+    width = pixman_image_get_width(vd->server);
+    height = pixman_image_get_height(vd->server);
 
     vnc_set_area_dirty(s->dirty, width, height, x, y, w, h);
 }
@@ -712,8 +718,17 @@ static void vnc_dpy_switch(DisplayChangeListener *dcl,
 
     vnc_abort_display_jobs(vd);
 
-    /* server surface */
     qemu_pixman_image_unref(vd->server);
+    vd->server = NULL;
+    qemu_pixman_image_unref(vd->guest.fb);
+    vd->guest.fb = NULL;
+
+    /* if no client is connected we don't need a server surface */
+    if (QTAILQ_EMPTY(&vd->clients)) {
+        return;
+    }
+
+    /* server surface */
     vd->ds = surface;
     width = MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
                                         VNC_DIRTY_PIXELS_PER_BIT));
@@ -726,7 +741,6 @@ static void vnc_dpy_switch(DisplayChangeListener *dcl,
     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
         console_color_init(ds);
 #endif
-    qemu_pixman_image_unref(vd->guest.fb);
     vd->guest.fb = pixman_image_ref(surface->image);
     vd->guest.format = surface->format;
     memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
@@ -900,6 +914,10 @@ static void vnc_dpy_copy(DisplayChangeListener *dcl,
     int i, x, y, pitch, inc, w_lim, s;
     int cmp_bytes;
 
+    if (!vd->server) {
+        return;
+    }
+
     vnc_refresh_server_surface(vd);
     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
@@ -1213,6 +1231,9 @@ void vnc_disconnect_finish(VncState *vs)
 
     if (vs->initialized) {
         QTAILQ_REMOVE(&vs->vd->clients, vs, next);
+        if (QTAILQ_EMPTY(&vs->vd->clients)) {
+            vnc_dpy_switch(&vs->vd->dcl, NULL);
+        }
         qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
     }
 
@@ -3049,6 +3070,7 @@ void vnc_init_state(VncState *vs)
 {
     vs->initialized = true;
     VncDisplay *vd = vs->vd;
+    bool first_client;
 
     vs->last_x = -1;
     vs->last_y = -1;
@@ -3061,9 +3083,15 @@ void vnc_init_state(VncState *vs)
     qemu_mutex_init(&vs->output_mutex);
     vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
 
+    first_client = QTAILQ_EMPTY(&vd->clients);
     QTAILQ_INSERT_TAIL(&vd->clients, vs, next);
 
-    graphic_hw_update(vd->dcl.con);
+    if (first_client) {
+        /* set/restore the correct surface in the VNC server */
+        console_select(0);
+    } else {
+        graphic_hw_update(vd->dcl.con);
+    }
 
     vnc_write(vs, "RFB 003.008\n", 12);
     vnc_flush(vs);
Signature · 1 line
1.9.1
#1 lore raw

Hi,

ui/vnc.c void vnc_init_state(VncState *vs) · line 3091

Quoted 4 lines
ui/vnc.c +3 -1
-    graphic_hw_update(vd->dcl.con);
+    if (first_client) {
+        /* set/restore the correct surface in the VNC server */
+        console_select(0);

Nah, that has unwanted side effects.

Pushed some patches to git://git.kraxel.org/qemu rebase/ui-vnc-next, could you have a look?

thanks,
Gerd

#2 lore raw
Quoted 11 lines

Am 29.09.2015 um 12:28 schrieb Gerd Hoffmann <[email protected]>:

Hi,

ui/vnc.c void vnc_init_state(VncState *vs) · line 3091

Quoted 4 lines
ui/vnc.c +3 -1
-    graphic_hw_update(vd->dcl.con);
+    if (first_client) {
+        /* set/restore the correct surface in the VNC server */
+        console_select(0);

Nah, that has unwanted side effects.

okay, ill try to respin without this hack.

Quoted 3 lines

Pushed some patches to git://git.kraxel.org/qemu rebase/ui-vnc-next, could you have a look?

if you mean the RFC patches on the list I already send a bunch of comments on them.

Peter

#3 lore raw
Quoted 11 lines · Peter Lieven
Quoted 10 lines

Am 29.09.2015 um 12:28 schrieb Gerd Hoffmann <[email protected]>:

Hi,

ui/vnc.c void vnc_init_state(VncState *vs) · line 3091

Quoted 4 lines
ui/vnc.c +3 -1
-    graphic_hw_update(vd->dcl.con);
+    if (first_client) {
+        /* set/restore the correct surface in the VNC server */
+        console_select(0);

Nah, that has unwanted side effects.

Quoted 7 lines

okay, ill try to respin without this hack.

Quoted 3 lines

Pushed some patches to git://git.kraxel.org/qemu rebase/ui-vnc-next, could you have a look?

if you mean the RFC patches on the list I already send a bunch of comments on them.

It's updated, taking your comments in account, reworking the server surface code so we can free it without clients, also added qio_buffer_shrink() but added no calls yet.

cheers,
Gerd

#4 lore raw
Quoted 18 lines

Am 29.09.2015 um 14:49 schrieb Gerd Hoffmann <[email protected]>:

Quoted 14 lines · Peter Lieven
Quoted 10 lines

Am 29.09.2015 um 12:28 schrieb Gerd Hoffmann <[email protected]>:

Hi,

ui/vnc.c void vnc_init_state(VncState *vs) · line 3091

Quoted 4 lines
ui/vnc.c +3 -1
-    graphic_hw_update(vd->dcl.con);
+    if (first_client) {
+        /* set/restore the correct surface in the VNC server */
+        console_select(0);

Nah, that has unwanted side effects.

okay, ill try to respin without this hack.

Quoted 1 line
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Pushed some patches to git://git.kraxel.org/qemu rebase/ui-vnc-next, could you have a look?

if you mean the RFC patches on the list I already send a bunch of comments on them.

It's updated, taking your comments in account, reworking the server surface code so we can free it without clients, also added qio_buffer_shrink() but added no calls yet.

had a quick look (without testing).

what about Recycling the output buffer as worker thread buffer?

about shrinking: I had the idea to count the number of times the buffer was (significantly) too big and only shrink if that counter has reached a reasonable value. and reset that counter to zero if the buffer has the right size.

i would check this in qio_buffer_resize. as you suggested earlier.

Peter

Quoted 4 lines

cheers,
Gerd

#5 lore raw

Hi,

Quoted 1 line

what about Recycling the output buffer as worker thread buffer?

You can't do that as vs is allocated on the stack, therefore not valid any more when vnc_worker_thread_loop returns.

Quoted 3 lines

about shrinking: I had the idea to count the number of times the buffer was (significantly) too big and only shrink if that counter has reached a reasonable value. and reset that counter to zero if the buffer has the right size.

i would check this in qio_buffer_resize. as you suggested earlier.

Not sure whenever it is a good idea to do that on every reserve call. Maybe better place shrink calls in places where you know it is worth checking, to have lower overhead.

Patches are welcome in any case ;)

cheers,
Gerd

#6 lore raw
Quoted 9 lines

Am 29.09.2015 um 16:08 schrieb Gerd Hoffmann <[email protected]>:

Hi,

Quoted 1 line

what about Recycling the output buffer as worker thread buffer?

You can't do that as vs is allocated on the stack, therefore not valid any more when vnc_worker_thread_loop returns.

why not? buffer_move_free only moves the heap allocated buffer allocation not the Buffer structure itself.

Quoted 4 lines
Quoted 4 lines

about shrinking: I had the idea to count the number of times the buffer was (significantly) too big and only shrink if that counter has reached a reasonable value. and reset that counter to zero if the buffer has the right size.

Quoted 8 lines
Quoted 1 line

i would check this in qio_buffer_resize. as you suggested earlier.

Not sure whenever it is a good idea to do that on every reserve call. Maybe better place shrink calls in places where you know it is worth checking, to have lower overhead.

Patches are welcome in any case ;)

will have a look.

Peter

#7 lore raw
In reply to Gerd Hoffmann

Am 29.09.2015 um 16:08 schrieb Gerd Hoffmann:

Quoted 7 lines

Hi,

Quoted 1 line

what about Recycling the output buffer as worker thread buffer?

You can't do that as vs is allocated on the stack, therefore not valid any more when vnc_worker_thread_loop returns.

Quoted 8 lines
Quoted 3 lines

about shrinking: I had the idea to count the number of times the buffer was (significantly) too big and only shrink if that counter has reached a reasonable value. and reset that counter to zero if the buffer has the right size.

i would check this in qio_buffer_resize. as you suggested earlier.

Not sure whenever it is a good idea to do that on every reserve call. Maybe better place shrink calls in places where you know it is worth checking, to have lower overhead.

Patches are welcome in any case ;)

Please have a look at:

https://github.com/plieven/qemu/tree/vnc-next-2.0

Peter

#9 lore raw
Quoted 6 lines

Am 02.10.2015 um 09:36 schrieb Gerd Hoffmann <[email protected]>:

Hi,

Quoted 8 lines
Quoted 3 lines

Please have a look at:

https://github.com/plieven/qemu/tree/vnc-next-2.0

Moving code to buf_adj_size and buf_req_size should go to a separate patch.

The avg_size logic needs some description.

will send monday.

Quoted 2 lines

Picked up the "recycle empty vs->output buffer" patch.

please stash the dtc part from that Patch.

Peter

Quoted 5 lines

cheers,
Gerd

#10 lore raw

Hi,

Quoted 3 lines
Quoted 1 line

Picked up the "recycle empty vs->output buffer" patch.

please stash the dtc part from that Patch.

Already done, git rebase handled it for me ;)

cheers,
Gerd