diff mbox

[13/21] savevm, buffered_file: introduce method to drain buffer of buffered file

Message ID bbf88a0eefe1f943a1d4c97a2dccc2a09dea4366.1325055139.git.yamahata@valinux.co.jp
State New
Headers show

Commit Message

Isaku Yamahata Dec. 29, 2011, 1:25 a.m. UTC
Introduce a new method to drain the buffer of QEMUBufferedFile.
When postcopy migration, buffer size can increase unboundedly.
To keep the buffer size reasonably small, introduce the method to
wait for buffer to drain.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
---
 buffered_file.c |   20 +++++++++++++++-----
 buffered_file.h |    1 +
 hw/hw.h         |    1 +
 savevm.c        |    6 ++++++
 4 files changed, 23 insertions(+), 5 deletions(-)
diff mbox

Patch

diff --git a/buffered_file.c b/buffered_file.c
index fed9a22..be1a192 100644
--- a/buffered_file.c
+++ b/buffered_file.c
@@ -168,6 +168,15 @@  static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, in
     return offset;
 }
 
+static void buffered_drain(QEMUFileBuffered *s)
+{
+    while (!qemu_file_get_error(s->file) && s->buffer_size) {
+        buffered_flush(s);
+        if (s->freeze_output)
+            s->wait_for_unfreeze(s->opaque);
+    }
+}
+
 static int buffered_close(void *opaque)
 {
     QEMUFileBuffered *s = opaque;
@@ -175,11 +184,7 @@  static int buffered_close(void *opaque)
 
     DPRINTF("closing\n");
 
-    while (!qemu_file_get_error(s->file) && s->buffer_size) {
-        buffered_flush(s);
-        if (s->freeze_output)
-            s->wait_for_unfreeze(s->opaque);
-    }
+    buffered_drain(s);
 
     ret = s->close(s->opaque);
 
@@ -289,3 +294,8 @@  QEMUFile *qemu_fopen_ops_buffered(void *opaque,
 
     return s->file;
 }
+
+void qemu_buffered_file_drain_buffer(void *buffered_file)
+{
+    buffered_drain(buffered_file);
+}
diff --git a/buffered_file.h b/buffered_file.h
index 98d358b..cd8e1e8 100644
--- a/buffered_file.h
+++ b/buffered_file.h
@@ -26,5 +26,6 @@  QEMUFile *qemu_fopen_ops_buffered(void *opaque, size_t xfer_limit,
                                   BufferedPutReadyFunc *put_ready,
                                   BufferedWaitForUnfreezeFunc *wait_for_unfreeze,
                                   BufferedCloseFunc *close);
+void qemu_buffered_file_drain_buffer(void *buffered_file);
 
 #endif
diff --git a/hw/hw.h b/hw/hw.h
index d508b4e..a59b770 100644
--- a/hw/hw.h
+++ b/hw/hw.h
@@ -61,6 +61,7 @@  QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
 QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
 int qemu_stdio_fd(QEMUFile *f);
 void qemu_fflush(QEMUFile *f);
+void qemu_buffered_file_drain(QEMUFile *f);
 int qemu_fclose(QEMUFile *f);
 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
 void qemu_put_byte(QEMUFile *f, int v);
diff --git a/savevm.c b/savevm.c
index 1d9e218..891c4fd 100644
--- a/savevm.c
+++ b/savevm.c
@@ -83,6 +83,7 @@ 
 #include "qemu-queue.h"
 #include "qemu-timer.h"
 #include "cpus.h"
+#include "buffered_file.h"
 
 #define SELF_ANNOUNCE_ROUNDS 5
 
@@ -475,6 +476,11 @@  void qemu_fflush(QEMUFile *f)
     }
 }
 
+void qemu_buffered_file_drain(QEMUFile *f)
+{
+    qemu_buffered_file_drain_buffer(f->opaque);
+}
+
 static void qemu_fill_buffer(QEMUFile *f)
 {
     int len;