diff mbox

[v3,3/5] qemu-file: Add fast ftell code path

Message ID 1419604968-87437-4-git-send-email-agraf@suse.de
State New
Headers show

Commit Message

Alexander Graf Dec. 26, 2014, 2:42 p.m. UTC
For ftell we flush the output buffer to ensure that we don't have anything
lingering in our internal buffers. This is a very safe thing to do.

However, with the dynamic size measurement that the dynamic vmstate
description will bring this would turn out quite slow.

Instead, we can fast path this specific measurement and just take the
internal buffers into account when telling the kernel our position.

I'm sure I overlooked some corner cases where this doesn't work, so
instead of tuning the safe, existing version, this patch adds a fast
variant of ftell that gets used by the dynamic vmstate description code
which isn't critical when it fails.

Signed-off-by: Alexander Graf <agraf@suse.de>

---

v2 -> v3:

  - improve ftell_fast, now works with bdrv too
---
 include/migration/qemu-file.h |  1 +
 migration/qemu-file.c         | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)

Comments

Eric Blake Jan. 6, 2015, 3:46 p.m. UTC | #1
On 12/26/2014 07:42 AM, Alexander Graf wrote:
> For ftell we flush the output buffer to ensure that we don't have anything
> lingering in our internal buffers. This is a very safe thing to do.
> 
> However, with the dynamic size measurement that the dynamic vmstate
> description will bring this would turn out quite slow.
> 
> Instead, we can fast path this specific measurement and just take the
> internal buffers into account when telling the kernel our position.
> 
> I'm sure I overlooked some corner cases where this doesn't work, so
> instead of tuning the safe, existing version, this patch adds a fast
> variant of ftell that gets used by the dynamic vmstate description code
> which isn't critical when it fails.
> 
> Signed-off-by: Alexander Graf <agraf@suse.de>
> 
> ---
> 
> v2 -> v3:
> 
>   - improve ftell_fast, now works with bdrv too
> ---
>  include/migration/qemu-file.h |  1 +
>  migration/qemu-file.c         | 16 ++++++++++++++++
>  2 files changed, 17 insertions(+)

Reviewed-by: Eric Blake <eblake@redhat.com>
diff mbox

Patch

diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
index 401676b..6b6772b 100644
--- a/include/migration/qemu-file.h
+++ b/include/migration/qemu-file.h
@@ -112,6 +112,7 @@  QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input);
 int qemu_get_fd(QEMUFile *f);
 int qemu_fclose(QEMUFile *f);
 int64_t qemu_ftell(QEMUFile *f);
+int64_t qemu_ftell_fast(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/migration/qemu-file.c b/migration/qemu-file.c
index d2d4007..cdfec56 100644
--- a/migration/qemu-file.c
+++ b/migration/qemu-file.c
@@ -440,6 +440,22 @@  int qemu_get_byte(QEMUFile *f)
     return result;
 }
 
+int64_t qemu_ftell_fast(QEMUFile *f)
+{
+    int64_t ret = f->pos;
+    int i;
+
+    if (f->ops->writev_buffer) {
+        for (i = 0; i < f->iovcnt; i++) {
+            ret += f->iov[i].iov_len;
+        }
+    } else {
+        ret += f->buf_index;
+    }
+
+    return ret;
+}
+
 int64_t qemu_ftell(QEMUFile *f)
 {
     qemu_fflush(f);