qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alon Levy <alevy@redhat.com>
To: qemu-devel@nongnu.org, kraxel@redhat.com,
	spice-devel@freedesktop.org, yhalperi@redhat.com,
	elmarco@redhat.com
Subject: [Qemu-devel] [RFC v4 9/9] qxl-render: call ppm_save on bh
Date: Tue, 21 Feb 2012 23:39:37 +0200	[thread overview]
Message-ID: <1329860377-6284-10-git-send-email-alevy@redhat.com> (raw)
In-Reply-To: <1329860377-6284-1-git-send-email-alevy@redhat.com>

This changes the behavior of the monitor command. After the previous
patch, there is no longer an option of deadlock with virt-manager, but
ppm_save is called too early, before the update has completed. With this
patch it is called at the correct moment, but that means there is a race
between the monitor command completing and the screendump file being saved.

The only solution is to use an asynchronous monitor command. For a
previous round of this see:
 http://lists.gnu.org/archive/html/qemu-devel/2011-10/msg02810.html

Since that's contentious, I'm suggesting we do something that is almost
correct and doesn't hang, instead of correct and hangs. The screendump
user can inotify on the directory and the file if need be. For casual
monitor usage there is no difference.

Signed-off-by: Alon Levy <alevy@redhat.com>
---
 hw/qxl-render.c    |   60 ++++++++++++++++++++++++++++++++++++++++++++++++---
 hw/qxl.c           |   13 ++++++++--
 hw/qxl.h           |    2 +-
 ui/spice-display.h |    2 +
 4 files changed, 69 insertions(+), 8 deletions(-)

diff --git a/hw/qxl-render.c b/hw/qxl-render.c
index 23e6929..4b34c6f 100644
--- a/hw/qxl-render.c
+++ b/hw/qxl-render.c
@@ -78,6 +78,11 @@ void qxl_render_resize(PCIQXLDevice *qxl)
     }
 }
 
+typedef struct QXLPPMSaveBHData {
+    PCIQXLDevice *qxl;
+    QXLCookie *cookie;
+} QXLPPMSaveBHData;
+
 static void qxl_render_clear_update_redraw_unlocked(PCIQXLDevice *qxl)
 {
     qxl->render_update_redraw = 0;
@@ -86,6 +91,33 @@ static void qxl_render_clear_update_redraw_unlocked(PCIQXLDevice *qxl)
     qxl->num_dirty_rects = 0;
 }
 
+static void qxl_render_ppm_save_bh(void *opaque)
+{
+    QXLPPMSaveBHData *data = opaque;
+    PCIQXLDevice *qxl = data->qxl;
+    QXLCookie *cookie = data->cookie;
+    QEMUBH *bh = cookie->u.render.ppm_save_bh;
+    QXLRect area = {
+        .left = 0,
+        .right = qxl->guest_primary.surface.width,
+        .top = 0,
+        .bottom = qxl->guest_primary.surface.height
+    };
+
+    qemu_mutex_lock(&qxl->ssd.lock);
+    dprint(qxl, 1, "%s: %p (primary %p)\n", __func__,
+           qxl->ssd.ds->surface->data, qxl->guest_primary.data);
+    qxl_flip(qxl, &area);
+    qxl_render_clear_update_redraw_unlocked(qxl);
+    ppm_save(cookie->u.render.filename, qxl->ssd.ds->surface);
+    g_free(cookie->u.render.filename);
+    g_free(cookie);
+    --qxl->render_update_cookie_num;
+    g_free(data);
+    qemu_mutex_unlock(&qxl->ssd.lock);
+    qemu_bh_delete(bh);
+}
+
 static void qxl_displaysurface_resize(PCIQXLDevice *qxl)
 {
     VGACommonState *vga = &qxl->vga;
@@ -143,16 +175,17 @@ static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
  * callbacks are called by spice_server thread, defering to bh called from the
  * io thread.
  */
-void qxl_render_update(PCIQXLDevice *qxl)
+void qxl_render_update(PCIQXLDevice *qxl, const char *filename)
 {
     int redraw = 0;
     QXLCookie *cookie;
+    QEMUBH *ppm_save_bh;
+    QXLPPMSaveBHData *ppm_save_bh_data;
 
     qemu_mutex_lock(&qxl->ssd.lock);
 
     if (qxl->guest_primary.resized) {
         qxl->guest_primary.resized = 0;
-
         qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
         dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d\n",
                __FUNCTION__,
@@ -165,6 +198,12 @@ void qxl_render_update(PCIQXLDevice *qxl)
     }
 
     if (!qxl->guest_primary.commands) {
+        if (filename) {
+            dprint(qxl, 1, "%s: screendump with no pending commands\n",
+                   __func__);
+            qxl_render_update_area_unlocked(qxl);
+            ppm_save(filename, qxl->ssd.ds->surface);
+        }
         qemu_mutex_unlock(&qxl->ssd.lock);
         return;
     }
@@ -177,6 +216,14 @@ void qxl_render_update(PCIQXLDevice *qxl)
     cookie->u.render.area.top    = 0;
     cookie->u.render.area.bottom = qxl->guest_primary.surface.height;
     qxl->render_update_redraw_area = cookie->u.render.area;
+    if (filename) {
+        ppm_save_bh_data = g_malloc0(sizeof(*ppm_save_bh_data));
+        ppm_save_bh_data->qxl = qxl;
+        ppm_save_bh_data->cookie = cookie;
+        ppm_save_bh = qemu_bh_new(qxl_render_ppm_save_bh, ppm_save_bh_data);
+        cookie->u.render.filename = g_strdup(filename);
+        cookie->u.render.ppm_save_bh = ppm_save_bh;
+    }
     qxl->render_update_cookie_num++;
     qemu_mutex_unlock(&qxl->ssd.lock);
     qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
@@ -202,9 +249,14 @@ void qxl_render_update_area_bh(void *opaque)
 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
 {
     qemu_mutex_lock(&qxl->ssd.lock);
-    qxl->render_update_cookie_num--;
+    if (cookie->u.render.filename) {
+        dprint(qxl, 1, "%s: scheduling ppm_save_bh\n", __func__);
+        qemu_bh_schedule(cookie->u.render.ppm_save_bh);
+    } else {
+        --qxl->render_update_cookie_num;
+        g_free(cookie);
+    }
     qemu_mutex_unlock(&qxl->ssd.lock);
-    g_free(cookie);
 }
 
 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
diff --git a/hw/qxl.c b/hw/qxl.c
index b4f84f2..82f9ffa 100644
--- a/hw/qxl.c
+++ b/hw/qxl.c
@@ -1480,7 +1480,7 @@ static void qxl_hw_update(void *opaque)
         break;
     case QXL_MODE_COMPAT:
     case QXL_MODE_NATIVE:
-        qxl_render_update(qxl);
+        qxl_render_update(qxl, NULL);
         break;
     default:
         break;
@@ -1502,8 +1502,15 @@ static void qxl_hw_screen_dump(void *opaque, const char *filename)
     switch (qxl->mode) {
     case QXL_MODE_COMPAT:
     case QXL_MODE_NATIVE:
-        qxl_render_update(qxl);
-        ppm_save(filename, qxl->ssd.ds->surface);
+        /*
+         * TODO: if we use an async update_area, to avoid deadlock with
+         * virt-manager, we postpone the saving of the image until the
+         * rendering is done. This means the image isn't guranteed to be
+         * written when we return to the monitor. Fixing this needs an async
+         * monitor command, whatever the implementation of the concept is
+         * called.
+         */
+        qxl_render_update(qxl, filename);
         break;
     case QXL_MODE_VGA:
         /*
diff --git a/hw/qxl.h b/hw/qxl.h
index 57a94ca..91297bd 100644
--- a/hw/qxl.h
+++ b/hw/qxl.h
@@ -138,7 +138,7 @@ void qxl_log_command(PCIQXLDevice *qxl, const char *ring, QXLCommandExt *ext);
 
 /* qxl-render.c */
 void qxl_render_resize(PCIQXLDevice *qxl);
-void qxl_render_update(PCIQXLDevice *qxl);
+void qxl_render_update(PCIQXLDevice *qxl, const char *filename);
 void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext);
 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie);
 void qxl_render_update_area_bh(void *opaque);
diff --git a/ui/spice-display.h b/ui/spice-display.h
index 81fa1e4..076e0e5 100644
--- a/ui/spice-display.h
+++ b/ui/spice-display.h
@@ -62,6 +62,8 @@ typedef struct QXLCookie {
         struct {
             QXLRect area;
             int redraw;
+            char *filename;
+            QEMUBH *ppm_save_bh;
         } render;
     } u;
 } QXLCookie;
-- 
1.7.9.1

  parent reply	other threads:[~2012-02-21 21:40 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-21 21:39 [Qemu-devel] [RFC v4 0/9] qxl: fix hangs caused by qxl_render_update Alon Levy
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 1/9] console: don't call console_select unnecessarily Alon Levy
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 2/9] sdl: remove NULL check, g_malloc0 can't fail Alon Levy
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 3/9] qxl: drop qxl_spice_update_area_async definition Alon Levy
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 4/9] qxl: screen_dump in vga: do a single ppm_save Alon Levy
2012-02-22 11:10   ` Gerd Hoffmann
2012-02-22 12:26     ` Alon Levy
2012-02-22 13:58       ` Gerd Hoffmann
2012-02-22 14:25         ` [Qemu-devel] [Spice-devel] " Alon Levy
2012-02-22 14:37           ` Gerd Hoffmann
2012-02-22 15:27             ` Alon Levy
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 5/9] qxl: require spice >= 0.8.2 Alon Levy
2012-02-22 11:11   ` Gerd Hoffmann
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 6/9] qxl: remove flipped Alon Levy
2012-02-22 11:18   ` Gerd Hoffmann
2012-02-22 12:28     ` Alon Levy
2012-02-22 14:09       ` Gerd Hoffmann
2012-02-22 14:23         ` Alon Levy
2012-02-22 19:00         ` Alon Levy
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 7/9] qxl: introduce QXLCookie Alon Levy
2012-02-22 11:23   ` Gerd Hoffmann
2012-02-21 21:39 ` [Qemu-devel] [RFC v4 8/9] qxl: make qxl_render_update async Alon Levy
2012-02-22 11:41   ` Gerd Hoffmann
2012-02-22 12:30     ` Alon Levy
2012-02-21 21:39 ` Alon Levy [this message]
2012-02-22 11:46   ` [Qemu-devel] [RFC v4 9/9] qxl-render: call ppm_save on bh Gerd Hoffmann
2012-02-22 12:34     ` Alon Levy
2012-02-22 12:45     ` Alon Levy
2012-02-22 18:59     ` Alon Levy
2012-02-21 22:17 ` [Qemu-devel] [Spice-devel] [RFC v4 0/9] qxl: fix hangs caused by qxl_render_update Alon Levy

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=1329860377-6284-10-git-send-email-alevy@redhat.com \
    --to=alevy@redhat.com \
    --cc=elmarco@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=spice-devel@freedesktop.org \
    --cc=yhalperi@redhat.com \
    /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).