[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 08/57] Add qemu_get_buffer_in_place to avoid copies s
From: |
Juan Quintela |
Subject: |
[Qemu-devel] [PULL 08/57] Add qemu_get_buffer_in_place to avoid copies some of the time |
Date: |
Tue, 10 Nov 2015 15:24:57 +0100 |
From: "Dr. David Alan Gilbert" <address@hidden>
qemu_get_buffer always copies the data it reads to a users buffer,
however in many cases the file buffer inside qemu_file could be given
back to the caller, avoiding the copy. This isn't always possible
depending on the size and alignment of the data.
Thus 'qemu_get_buffer_in_place' either copies the data to a supplied
buffer or updates a pointer to the internal buffer if convenient.
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
Reviewed-by: Juan Quintela <address@hidden>
Signed-off-by: Juan Quintela <address@hidden>
---
include/migration/qemu-file.h | 2 ++
migration/qemu-file.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
index 29a338d..86bb972 100644
--- a/include/migration/qemu-file.h
+++ b/include/migration/qemu-file.h
@@ -163,9 +163,11 @@ void qemu_put_be32(QEMUFile *f, unsigned int v);
void qemu_put_be64(QEMUFile *f, uint64_t v);
size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t
offset);
size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size);
+size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size);
ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
int level);
int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src);
+
/*
* Note that you can only peek continuous bytes from where the current pointer
* is; you aren't guaranteed to be able to peak to +n bytes unless you've
diff --git a/migration/qemu-file.c b/migration/qemu-file.c
index df49023..e41a677 100644
--- a/migration/qemu-file.c
+++ b/migration/qemu-file.c
@@ -434,6 +434,43 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t
size)
}
/*
+ * Read 'size' bytes of data from the file.
+ * 'size' can be larger than the internal buffer.
+ *
+ * The data:
+ * may be held on an internal buffer (in which case *buf is updated
+ * to point to it) that is valid until the next qemu_file operation.
+ * OR
+ * will be copied to the *buf that was passed in.
+ *
+ * The code tries to avoid the copy if possible.
+ *
+ * It will return size bytes unless there was an error, in which case it will
+ * return as many as it managed to read (assuming blocking fd's which
+ * all current QEMUFile are)
+ *
+ * Note: Since **buf may get changed, the caller should take care to
+ * keep a pointer to the original buffer if it needs to deallocate it.
+ */
+size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
+{
+ if (size < IO_BUF_SIZE) {
+ size_t res;
+ uint8_t *src;
+
+ res = qemu_peek_buffer(f, &src, size, 0);
+
+ if (res == size) {
+ qemu_file_skip(f, res);
+ *buf = src;
+ return res;
+ }
+ }
+
+ return qemu_get_buffer(f, *buf, size);
+}
+
+/*
* Peeks a single byte from the buffer; this isn't guaranteed to work if
* offset leaves a gap after the previous read/peeked data.
*/
--
2.5.0
- [Qemu-devel] [PULL 00/57] Migration pull (take 2), Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 01/57] Add postcopy documentation, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 03/57] Move configuration section writing, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 02/57] Provide runtime Target page information, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 05/57] qemu_ram_block_from_host, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 04/57] Move page_size_init earlier, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 06/57] qemu_ram_block_by_name, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 08/57] Add qemu_get_buffer_in_place to avoid copies some of the time,
Juan Quintela <=
- [Qemu-devel] [PULL 09/57] Add wrapper for setting blocking status on a QEMUFile, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 10/57] Add QEMU_MADV_NOHUGEPAGE, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 07/57] Rename mis->file to from_src_file, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 11/57] ram_debug_dump_bitmap: Dump a migration bitmap as text, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 12/57] ram_load: Factor out host_from_stream_offset call and check, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 14/57] Rename save_live_complete to save_live_complete_precopy, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 15/57] Add Linux userfaultfd.h header, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 13/57] migrate_init: Call from savevm, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 16/57] Return path: Open a return path on QEMUFile for sockets, Juan Quintela, 2015/11/10
- [Qemu-devel] [PULL 17/57] Return path: socket_writev_buffer: Block even on non-blocking fd's, Juan Quintela, 2015/11/10