1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Mon, 4 May 2020 11:05:08 +0200
4 Subject: [PATCH] PVE: add optional buffer size to QEMUFile
6 So we can use a 4M buffer for savevm-async which should
7 increase performance storing the state onto ceph.
9 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
10 [increase max IOV count in QEMUFile to actually write more data]
11 Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
12 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
14 migration/qemu-file.c | 38 +++++++++++++++++++++++++-------------
15 migration/qemu-file.h | 1 +
16 migration/savevm-async.c | 4 ++--
17 3 files changed, 28 insertions(+), 15 deletions(-)
19 diff --git a/migration/qemu-file.c b/migration/qemu-file.c
20 index 1479cddad9..21a3355ae2 100644
21 --- a/migration/qemu-file.c
22 +++ b/migration/qemu-file.c
25 #include "qapi/error.h"
27 -#define IO_BUF_SIZE 32768
28 -#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
29 +#define DEFAULT_IO_BUF_SIZE 32768
30 +#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 256)
33 const QEMUFileOps *ops;
34 @@ -46,7 +46,8 @@ struct QEMUFile {
37 int buf_size; /* 0 when writing */
38 - uint8_t buf[IO_BUF_SIZE];
39 + size_t buf_allocated_size;
42 DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
43 struct iovec iov[MAX_IOV_SIZE];
44 @@ -104,7 +105,7 @@ bool qemu_file_mode_is_not_valid(const char *mode)
48 -QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc)
49 +QEMUFile *qemu_fopen_ops_sized(void *opaque, const QEMUFileOps *ops, bool has_ioc, size_t buffer_size)
53 @@ -113,9 +114,17 @@ QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc)
57 + f->buf_allocated_size = buffer_size;
58 + f->buf = malloc(buffer_size);
63 +QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc)
65 + return qemu_fopen_ops_sized(opaque, ops, has_ioc, DEFAULT_IO_BUF_SIZE);
69 void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
71 @@ -350,7 +359,7 @@ static ssize_t qemu_fill_buffer(QEMUFile *f)
74 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
75 - IO_BUF_SIZE - pending, &local_error);
76 + f->buf_allocated_size - pending, &local_error);
80 @@ -390,6 +399,9 @@ int qemu_fclose(QEMUFile *f)
87 /* If any error was spotted before closing, we should report it
88 * instead of the close() return value.
90 @@ -444,7 +456,7 @@ static void add_buf_to_iovec(QEMUFile *f, size_t len)
92 if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
94 - if (f->buf_index == IO_BUF_SIZE) {
95 + if (f->buf_index == f->buf_allocated_size) {
99 @@ -470,7 +482,7 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
103 - l = IO_BUF_SIZE - f->buf_index;
104 + l = f->buf_allocated_size - f->buf_index;
108 @@ -517,8 +529,8 @@ size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
111 assert(!qemu_file_is_writable(f));
112 - assert(offset < IO_BUF_SIZE);
113 - assert(size <= IO_BUF_SIZE - offset);
114 + assert(offset < f->buf_allocated_size);
115 + assert(size <= f->buf_allocated_size - offset);
117 /* The 1st byte to read from */
118 index = f->buf_index + offset;
119 @@ -568,7 +580,7 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
123 - res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
124 + res = qemu_peek_buffer(f, &src, MIN(pending, f->buf_allocated_size), 0);
128 @@ -602,7 +614,7 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
130 size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
132 - if (size < IO_BUF_SIZE) {
133 + if (size < f->buf_allocated_size) {
137 @@ -627,7 +639,7 @@ int qemu_peek_byte(QEMUFile *f, int offset)
138 int index = f->buf_index + offset;
140 assert(!qemu_file_is_writable(f));
141 - assert(offset < IO_BUF_SIZE);
142 + assert(offset < f->buf_allocated_size);
144 if (index >= f->buf_size) {
146 @@ -779,7 +791,7 @@ static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
147 ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
148 const uint8_t *p, size_t size)
150 - ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
151 + ssize_t blen = f->buf_allocated_size - f->buf_index - sizeof(int32_t);
153 if (blen < compressBound(size)) {
155 diff --git a/migration/qemu-file.h b/migration/qemu-file.h
156 index 3f36d4dc8c..67501fd9cf 100644
157 --- a/migration/qemu-file.h
158 +++ b/migration/qemu-file.h
159 @@ -121,6 +121,7 @@ typedef struct QEMUFileHooks {
162 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc);
163 +QEMUFile *qemu_fopen_ops_sized(void *opaque, const QEMUFileOps *ops, bool has_ioc, size_t buffer_size);
164 void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks);
165 int qemu_get_fd(QEMUFile *f);
166 int qemu_fclose(QEMUFile *f);
167 diff --git a/migration/savevm-async.c b/migration/savevm-async.c
168 index 88215cdb70..615a4484c8 100644
169 --- a/migration/savevm-async.c
170 +++ b/migration/savevm-async.c
171 @@ -417,7 +417,7 @@ void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
175 - snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
176 + snap_state.file = qemu_fopen_ops_sized(&snap_state, &block_file_ops, false, 4 * 1024 * 1024);
178 if (!snap_state.file) {
179 error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
180 @@ -565,7 +565,7 @@ int load_snapshot_from_blockdev(const char *filename, Error **errp)
181 blk_op_block_all(be, blocker);
183 /* restore the VM state */
184 - f = qemu_fopen_ops(be, &loadstate_file_ops);
185 + f = qemu_fopen_ops_sized(be, &loadstate_file_ops, false, 4 * 1024 * 1024);
187 error_setg(errp, "Could not open VM state file");