| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | 
					
						
							|  |  |  | From: Wolfgang Bumiller <w.bumiller@proxmox.com> | 
					
						
							|  |  |  | Date: Mon, 4 May 2020 11:05:08 +0200 | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  | Subject: [PATCH] PVE: add optional buffer size to QEMUFile | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | So we can use a 4M buffer for savevm-async which should | 
					
						
							|  |  |  | increase performance storing the state onto ceph. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com> | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  | [increase max IOV count in QEMUFile to actually write more data] | 
					
						
							|  |  |  | Signed-off-by: Stefan Reiter <s.reiter@proxmox.com> | 
					
						
							| 
									
										
										
										
											2022-01-13 12:34:33 +03:00
										 |  |  | Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com> | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | ---
 | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  |  migration/qemu-file.c    | 38 +++++++++++++++++++++++++------------- | 
					
						
							|  |  |  |  migration/qemu-file.h    |  1 + | 
					
						
							|  |  |  |  migration/savevm-async.c |  4 ++-- | 
					
						
							|  |  |  |  3 files changed, 28 insertions(+), 15 deletions(-) | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | diff --git a/migration/qemu-file.c b/migration/qemu-file.c
 | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | index 1479cddad9..21a3355ae2 100644
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | --- a/migration/qemu-file.c
 | 
					
						
							|  |  |  | +++ b/migration/qemu-file.c
 | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -31,8 +31,8 @@
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |  #include "trace.h" | 
					
						
							|  |  |  |  #include "qapi/error.h" | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | -#define IO_BUF_SIZE 32768
 | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  | -#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | +#define DEFAULT_IO_BUF_SIZE 32768
 | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  | +#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 256)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |   | 
					
						
							|  |  |  |  struct QEMUFile { | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  |      const QEMUFileOps *ops; | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -46,7 +46,8 @@ struct QEMUFile {
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |                      when reading */ | 
					
						
							|  |  |  |      int buf_index; | 
					
						
							|  |  |  |      int buf_size; /* 0 when writing */ | 
					
						
							|  |  |  | -    uint8_t buf[IO_BUF_SIZE];
 | 
					
						
							|  |  |  | +    size_t buf_allocated_size;
 | 
					
						
							|  |  |  | +    uint8_t *buf;
 | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      DECLARE_BITMAP(may_free, MAX_IOV_SIZE); | 
					
						
							|  |  |  |      struct iovec iov[MAX_IOV_SIZE]; | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -104,7 +105,7 @@ bool qemu_file_mode_is_not_valid(const char *mode)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      return false; | 
					
						
							|  |  |  |  } | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  | -QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc)
 | 
					
						
							|  |  |  | +QEMUFile *qemu_fopen_ops_sized(void *opaque, const QEMUFileOps *ops, bool has_ioc, size_t buffer_size)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |  { | 
					
						
							|  |  |  |      QEMUFile *f; | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -113,9 +114,17 @@ QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      f->opaque = opaque; | 
					
						
							|  |  |  |      f->ops = ops; | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  |      f->has_ioc = has_ioc; | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | +    f->buf_allocated_size = buffer_size;
 | 
					
						
							|  |  |  | +    f->buf = malloc(buffer_size);
 | 
					
						
							|  |  |  | +
 | 
					
						
							|  |  |  |      return f; | 
					
						
							|  |  |  |  } | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  | +QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | +{
 | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  | +    return qemu_fopen_ops_sized(opaque, ops, has_ioc, DEFAULT_IO_BUF_SIZE);
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | +}
 | 
					
						
							|  |  |  | +
 | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |  void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks) | 
					
						
							|  |  |  |  { | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -350,7 +359,7 @@ static ssize_t qemu_fill_buffer(QEMUFile *f)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      } | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos, | 
					
						
							|  |  |  | -                             IO_BUF_SIZE - pending, &local_error);
 | 
					
						
							|  |  |  | +                             f->buf_allocated_size - pending, &local_error);
 | 
					
						
							|  |  |  |      if (len > 0) { | 
					
						
							|  |  |  |          f->buf_size += len; | 
					
						
							|  |  |  |          f->pos += len; | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -390,6 +399,9 @@ int qemu_fclose(QEMUFile *f)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |              ret = ret2; | 
					
						
							|  |  |  |          } | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  | +
 | 
					
						
							|  |  |  | +    free(f->buf);
 | 
					
						
							|  |  |  | +
 | 
					
						
							|  |  |  |      /* If any error was spotted before closing, we should report it | 
					
						
							|  |  |  |       * instead of the close() return value. | 
					
						
							|  |  |  |       */ | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -444,7 +456,7 @@ static void add_buf_to_iovec(QEMUFile *f, size_t len)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |  { | 
					
						
							|  |  |  |      if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) { | 
					
						
							|  |  |  |          f->buf_index += len; | 
					
						
							|  |  |  | -        if (f->buf_index == IO_BUF_SIZE) {
 | 
					
						
							|  |  |  | +        if (f->buf_index == f->buf_allocated_size) {
 | 
					
						
							|  |  |  |              qemu_fflush(f); | 
					
						
							|  |  |  |          } | 
					
						
							|  |  |  |      } | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -470,7 +482,7 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      } | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      while (size > 0) { | 
					
						
							|  |  |  | -        l = IO_BUF_SIZE - f->buf_index;
 | 
					
						
							|  |  |  | +        l = f->buf_allocated_size - f->buf_index;
 | 
					
						
							|  |  |  |          if (l > size) { | 
					
						
							|  |  |  |              l = size; | 
					
						
							|  |  |  |          } | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -517,8 +529,8 @@ size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      size_t index; | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      assert(!qemu_file_is_writable(f)); | 
					
						
							|  |  |  | -    assert(offset < IO_BUF_SIZE);
 | 
					
						
							|  |  |  | -    assert(size <= IO_BUF_SIZE - offset);
 | 
					
						
							|  |  |  | +    assert(offset < f->buf_allocated_size);
 | 
					
						
							|  |  |  | +    assert(size <= f->buf_allocated_size - offset);
 | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      /* The 1st byte to read from */ | 
					
						
							|  |  |  |      index = f->buf_index + offset; | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -568,7 +580,7 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |          size_t res; | 
					
						
							|  |  |  |          uint8_t *src; | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | -        res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
 | 
					
						
							|  |  |  | +        res = qemu_peek_buffer(f, &src, MIN(pending, f->buf_allocated_size), 0);
 | 
					
						
							|  |  |  |          if (res == 0) { | 
					
						
							|  |  |  |              return done; | 
					
						
							|  |  |  |          } | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -602,7 +614,7 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |   */ | 
					
						
							|  |  |  |  size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) | 
					
						
							|  |  |  |  { | 
					
						
							|  |  |  | -    if (size < IO_BUF_SIZE) {
 | 
					
						
							|  |  |  | +    if (size < f->buf_allocated_size) {
 | 
					
						
							|  |  |  |          size_t res; | 
					
						
							| 
									
										
										
										
											2021-05-27 13:43:32 +03:00
										 |  |  |          uint8_t *src = NULL; | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |   | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -627,7 +639,7 @@ int qemu_peek_byte(QEMUFile *f, int offset)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      int index = f->buf_index + offset; | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      assert(!qemu_file_is_writable(f)); | 
					
						
							|  |  |  | -    assert(offset < IO_BUF_SIZE);
 | 
					
						
							|  |  |  | +    assert(offset < f->buf_allocated_size);
 | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      if (index >= f->buf_size) { | 
					
						
							|  |  |  |          qemu_fill_buffer(f); | 
					
						
							| 
									
										
										
										
											2022-06-27 14:05:40 +03:00
										 |  |  | @@ -779,7 +791,7 @@ static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |  ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, | 
					
						
							|  |  |  |                                    const uint8_t *p, size_t size) | 
					
						
							|  |  |  |  { | 
					
						
							|  |  |  | -    ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
 | 
					
						
							|  |  |  | +    ssize_t blen = f->buf_allocated_size - f->buf_index - sizeof(int32_t);
 | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      if (blen < compressBound(size)) { | 
					
						
							|  |  |  |          return -1; | 
					
						
							|  |  |  | diff --git a/migration/qemu-file.h b/migration/qemu-file.h
 | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  | index 3f36d4dc8c..67501fd9cf 100644
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  | --- a/migration/qemu-file.h
 | 
					
						
							|  |  |  | +++ b/migration/qemu-file.h
 | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  | @@ -121,6 +121,7 @@ typedef struct QEMUFileHooks {
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |  } QEMUFileHooks; | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  |  QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc); | 
					
						
							|  |  |  | +QEMUFile *qemu_fopen_ops_sized(void *opaque, const QEMUFileOps *ops, bool has_ioc, size_t buffer_size);
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |  void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks); | 
					
						
							|  |  |  |  int qemu_get_fd(QEMUFile *f); | 
					
						
							|  |  |  |  int qemu_fclose(QEMUFile *f); | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  | diff --git a/migration/savevm-async.c b/migration/savevm-async.c
 | 
					
						
							| 
									
										
										
										
											2022-08-18 14:44:17 +03:00
										 |  |  | index b9a43c56bc..0bc5799cf8 100644
 | 
					
						
							| 
									
										
										
										
											2021-02-11 19:11:11 +03:00
										 |  |  | --- a/migration/savevm-async.c
 | 
					
						
							|  |  |  | +++ b/migration/savevm-async.c
 | 
					
						
							| 
									
										
										
										
											2022-08-18 14:44:17 +03:00
										 |  |  | @@ -420,7 +420,7 @@ void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |          goto restart; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | -    snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
 | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  | +    snap_state.file = qemu_fopen_ops_sized(&snap_state, &block_file_ops, false, 4 * 1024 * 1024);
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |   | 
					
						
							|  |  |  |      if (!snap_state.file) { | 
					
						
							|  |  |  |          error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile); | 
					
						
							| 
									
										
										
										
											2022-08-18 14:44:17 +03:00
										 |  |  | @@ -573,7 +573,7 @@ int load_snapshot_from_blockdev(const char *filename, Error **errp)
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      blk_op_block_all(be, blocker); | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |      /* restore the VM state */ | 
					
						
							|  |  |  | -    f = qemu_fopen_ops(be, &loadstate_file_ops);
 | 
					
						
							| 
									
										
										
										
											2021-10-11 14:55:34 +03:00
										 |  |  | +    f = qemu_fopen_ops_sized(be, &loadstate_file_ops, false, 4 * 1024 * 1024);
 | 
					
						
							| 
									
										
										
										
											2020-05-04 15:35:01 +03:00
										 |  |  |      if (!f) { | 
					
						
							|  |  |  |          error_setg(errp, "Could not open VM state file"); | 
					
						
							|  |  |  |          goto the_end; |