]>
Commit | Line | Data |
---|---|---|
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 | |
5 | ||
6 | So we can use a 4M buffer for savevm-async which should | |
7 | increase performance storing the state onto ceph. | |
8 | ||
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 | --- | |
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(-) | |
18 | ||
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 | |
23 | @@ -31,8 +31,8 @@ | |
24 | #include "trace.h" | |
25 | #include "qapi/error.h" | |
26 | ||
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) | |
31 | ||
32 | struct QEMUFile { | |
33 | const QEMUFileOps *ops; | |
34 | @@ -46,7 +46,8 @@ struct QEMUFile { | |
35 | when reading */ | |
36 | int buf_index; | |
37 | int buf_size; /* 0 when writing */ | |
38 | - uint8_t buf[IO_BUF_SIZE]; | |
39 | + size_t buf_allocated_size; | |
40 | + uint8_t *buf; | |
41 | ||
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) | |
45 | return false; | |
46 | } | |
47 | ||
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) | |
50 | { | |
51 | QEMUFile *f; | |
52 | ||
53 | @@ -113,9 +114,17 @@ QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc) | |
54 | f->opaque = opaque; | |
55 | f->ops = ops; | |
56 | f->has_ioc = has_ioc; | |
57 | + f->buf_allocated_size = buffer_size; | |
58 | + f->buf = malloc(buffer_size); | |
59 | + | |
60 | return f; | |
61 | } | |
62 | ||
63 | +QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc) | |
64 | +{ | |
65 | + return qemu_fopen_ops_sized(opaque, ops, has_ioc, DEFAULT_IO_BUF_SIZE); | |
66 | +} | |
67 | + | |
68 | ||
69 | void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks) | |
70 | { | |
71 | @@ -350,7 +359,7 @@ static ssize_t qemu_fill_buffer(QEMUFile *f) | |
72 | } | |
73 | ||
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); | |
77 | if (len > 0) { | |
78 | f->buf_size += len; | |
79 | f->pos += len; | |
80 | @@ -390,6 +399,9 @@ int qemu_fclose(QEMUFile *f) | |
81 | ret = ret2; | |
82 | } | |
83 | } | |
84 | + | |
85 | + free(f->buf); | |
86 | + | |
87 | /* If any error was spotted before closing, we should report it | |
88 | * instead of the close() return value. | |
89 | */ | |
90 | @@ -444,7 +456,7 @@ static void add_buf_to_iovec(QEMUFile *f, size_t len) | |
91 | { | |
92 | if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) { | |
93 | f->buf_index += len; | |
94 | - if (f->buf_index == IO_BUF_SIZE) { | |
95 | + if (f->buf_index == f->buf_allocated_size) { | |
96 | qemu_fflush(f); | |
97 | } | |
98 | } | |
99 | @@ -470,7 +482,7 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) | |
100 | } | |
101 | ||
102 | while (size > 0) { | |
103 | - l = IO_BUF_SIZE - f->buf_index; | |
104 | + l = f->buf_allocated_size - f->buf_index; | |
105 | if (l > size) { | |
106 | l = size; | |
107 | } | |
108 | @@ -517,8 +529,8 @@ size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset) | |
109 | size_t index; | |
110 | ||
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); | |
116 | ||
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) | |
120 | size_t res; | |
121 | uint8_t *src; | |
122 | ||
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); | |
125 | if (res == 0) { | |
126 | return done; | |
127 | } | |
128 | @@ -602,7 +614,7 @@ size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size) | |
129 | */ | |
130 | size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) | |
131 | { | |
132 | - if (size < IO_BUF_SIZE) { | |
133 | + if (size < f->buf_allocated_size) { | |
134 | size_t res; | |
135 | uint8_t *src = NULL; | |
136 | ||
137 | @@ -627,7 +639,7 @@ int qemu_peek_byte(QEMUFile *f, int offset) | |
138 | int index = f->buf_index + offset; | |
139 | ||
140 | assert(!qemu_file_is_writable(f)); | |
141 | - assert(offset < IO_BUF_SIZE); | |
142 | + assert(offset < f->buf_allocated_size); | |
143 | ||
144 | if (index >= f->buf_size) { | |
145 | qemu_fill_buffer(f); | |
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) | |
149 | { | |
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); | |
152 | ||
153 | if (blen < compressBound(size)) { | |
154 | return -1; | |
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 { | |
160 | } QEMUFileHooks; | |
161 | ||
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) | |
172 | goto restart; | |
173 | } | |
174 | ||
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); | |
177 | ||
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); | |
182 | ||
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); | |
186 | if (!f) { | |
187 | error_setg(errp, "Could not open VM state file"); | |
188 | goto the_end; |