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>
13 [FE: adapt to removal of QEMUFileOps]
14 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
16 migration/qemu-file.c | 49 +++++++++++++++++++++++++++-------------
17 migration/qemu-file.h | 2 ++
18 migration/savevm-async.c | 5 ++--
19 3 files changed, 38 insertions(+), 18 deletions(-)
21 diff --git a/migration/qemu-file.c b/migration/qemu-file.c
22 index 2d5f74ffc2..9fd97e6fe1 100644
23 --- a/migration/qemu-file.c
24 +++ b/migration/qemu-file.c
27 #include "qapi/error.h"
29 -#define IO_BUF_SIZE 32768
30 -#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
31 +#define DEFAULT_IO_BUF_SIZE 32768
32 +#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 256)
35 const QEMUFileHooks *hooks;
36 @@ -55,7 +55,8 @@ struct QEMUFile {
39 int buf_size; /* 0 when writing */
40 - uint8_t buf[IO_BUF_SIZE];
41 + size_t buf_allocated_size;
44 DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
45 struct iovec iov[MAX_IOV_SIZE];
46 @@ -127,7 +128,9 @@ bool qemu_file_mode_is_not_valid(const char *mode)
50 -static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
51 +static QEMUFile *qemu_file_new_impl(QIOChannel *ioc,
57 @@ -136,6 +139,8 @@ static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
60 f->is_writable = is_writable;
61 + f->buf_allocated_size = buffer_size;
62 + f->buf = malloc(buffer_size);
66 @@ -146,17 +151,27 @@ static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
68 QEMUFile *qemu_file_get_return_path(QEMUFile *f)
70 - return qemu_file_new_impl(f->ioc, !f->is_writable);
71 + return qemu_file_new_impl(f->ioc, !f->is_writable, DEFAULT_IO_BUF_SIZE);
74 QEMUFile *qemu_file_new_output(QIOChannel *ioc)
76 - return qemu_file_new_impl(ioc, true);
77 + return qemu_file_new_impl(ioc, true, DEFAULT_IO_BUF_SIZE);
80 +QEMUFile *qemu_file_new_output_sized(QIOChannel *ioc, size_t buffer_size)
82 + return qemu_file_new_impl(ioc, true, buffer_size);
85 QEMUFile *qemu_file_new_input(QIOChannel *ioc)
87 - return qemu_file_new_impl(ioc, false);
88 + return qemu_file_new_impl(ioc, false, DEFAULT_IO_BUF_SIZE);
91 +QEMUFile *qemu_file_new_input_sized(QIOChannel *ioc, size_t buffer_size)
93 + return qemu_file_new_impl(ioc, false, buffer_size);
96 void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
97 @@ -414,7 +429,7 @@ static ssize_t qemu_fill_buffer(QEMUFile *f)
99 len = qio_channel_read(f->ioc,
100 (char *)f->buf + pending,
101 - IO_BUF_SIZE - pending,
102 + f->buf_allocated_size - pending,
104 if (len == QIO_CHANNEL_ERR_BLOCK) {
105 if (qemu_in_coroutine()) {
106 @@ -464,6 +479,8 @@ int qemu_fclose(QEMUFile *f)
108 g_clear_pointer(&f->ioc, object_unref);
112 /* If any error was spotted before closing, we should report it
113 * instead of the close() return value.
115 @@ -518,7 +535,7 @@ static void add_buf_to_iovec(QEMUFile *f, size_t len)
117 if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
119 - if (f->buf_index == IO_BUF_SIZE) {
120 + if (f->buf_index == f->buf_allocated_size) {
124 @@ -544,7 +561,7 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
128 - l = IO_BUF_SIZE - f->buf_index;
129 + l = f->buf_allocated_size - f->buf_index;
133 @@ -591,8 +608,8 @@ size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
136 assert(!qemu_file_is_writable(f));
137 - assert(offset < IO_BUF_SIZE);
138 - assert(size <= IO_BUF_SIZE - offset);
139 + assert(offset < f->buf_allocated_size);
140 + assert(size <= f->buf_allocated_size - offset);
142 /* The 1st byte to read from */
143 index = f->buf_index + offset;
144 @@ -642,7 +659,7 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
148 - res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
149 + res = qemu_peek_buffer(f, &src, MIN(pending, f->buf_allocated_size), 0);
153 @@ -676,7 +693,7 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
155 size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
157 - if (size < IO_BUF_SIZE) {
158 + if (size < f->buf_allocated_size) {
162 @@ -701,7 +718,7 @@ int qemu_peek_byte(QEMUFile *f, int offset)
163 int index = f->buf_index + offset;
165 assert(!qemu_file_is_writable(f));
166 - assert(offset < IO_BUF_SIZE);
167 + assert(offset < f->buf_allocated_size);
169 if (index >= f->buf_size) {
171 @@ -853,7 +870,7 @@ static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
172 ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
173 const uint8_t *p, size_t size)
175 - ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
176 + ssize_t blen = f->buf_allocated_size - f->buf_index - sizeof(int32_t);
178 if (blen < compressBound(size)) {
180 diff --git a/migration/qemu-file.h b/migration/qemu-file.h
181 index fa13d04d78..914f1a63a8 100644
182 --- a/migration/qemu-file.h
183 +++ b/migration/qemu-file.h
184 @@ -63,7 +63,9 @@ typedef struct QEMUFileHooks {
187 QEMUFile *qemu_file_new_input(QIOChannel *ioc);
188 +QEMUFile *qemu_file_new_input_sized(QIOChannel *ioc, size_t buffer_size);
189 QEMUFile *qemu_file_new_output(QIOChannel *ioc);
190 +QEMUFile *qemu_file_new_output_sized(QIOChannel *ioc, size_t buffer_size);
191 void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks);
192 int qemu_fclose(QEMUFile *f);
194 diff --git a/migration/savevm-async.c b/migration/savevm-async.c
195 index dc30558713..a38e7351c1 100644
196 --- a/migration/savevm-async.c
197 +++ b/migration/savevm-async.c
198 @@ -374,7 +374,7 @@ void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
200 QIOChannel *ioc = QIO_CHANNEL(qio_channel_savevm_async_new(snap_state.target,
201 &snap_state.bs_pos));
202 - snap_state.file = qemu_file_new_output(ioc);
203 + snap_state.file = qemu_file_new_output_sized(ioc, 4 * 1024 * 1024);
205 if (!snap_state.file) {
206 error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
207 @@ -507,7 +507,8 @@ int load_snapshot_from_blockdev(const char *filename, Error **errp)
208 blk_op_block_all(be, blocker);
210 /* restore the VM state */
211 - f = qemu_file_new_input(QIO_CHANNEL(qio_channel_savevm_async_new(be, &bs_pos)));
212 + f = qemu_file_new_input_sized(QIO_CHANNEL(qio_channel_savevm_async_new(be, &bs_pos)),
215 error_setg(errp, "Could not open VM state file");