2 * QEMU buffered QEMUFile
4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
18 #include "qemu-timer.h"
19 #include "qemu-char.h"
20 #include "buffered_file.h"
22 //#define DEBUG_BUFFERED_FILE
24 typedef struct QEMUFileBuffered
26 MigrationState
*migration_state
;
33 size_t buffer_capacity
;
37 #ifdef DEBUG_BUFFERED_FILE
38 #define DPRINTF(fmt, ...) \
39 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
41 #define DPRINTF(fmt, ...) \
45 static void buffered_append(QEMUFileBuffered
*s
,
46 const uint8_t *buf
, size_t size
)
48 if (size
> (s
->buffer_capacity
- s
->buffer_size
)) {
49 DPRINTF("increasing buffer capacity from %zu by %zu\n",
50 s
->buffer_capacity
, size
+ 1024);
52 s
->buffer_capacity
+= size
+ 1024;
54 s
->buffer
= g_realloc(s
->buffer
, s
->buffer_capacity
);
57 memcpy(s
->buffer
+ s
->buffer_size
, buf
, size
);
58 s
->buffer_size
+= size
;
61 static ssize_t
buffered_flush(QEMUFileBuffered
*s
)
66 DPRINTF("flushing %zu byte(s) of data\n", s
->buffer_size
);
68 while (s
->bytes_xfer
< s
->xfer_limit
&& offset
< s
->buffer_size
) {
70 ret
= migrate_fd_put_buffer(s
->migration_state
, s
->buffer
+ offset
,
71 s
->buffer_size
- offset
);
73 DPRINTF("backend not ready, freezing\n");
80 DPRINTF("error flushing data, %zd\n", ret
);
83 DPRINTF("flushed %zd byte(s)\n", ret
);
89 DPRINTF("flushed %zu of %zu byte(s)\n", offset
, s
->buffer_size
);
90 memmove(s
->buffer
, s
->buffer
+ offset
, s
->buffer_size
- offset
);
91 s
->buffer_size
-= offset
;
99 static int buffered_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
101 QEMUFileBuffered
*s
= opaque
;
104 DPRINTF("putting %d bytes at %" PRId64
"\n", size
, pos
);
106 error
= qemu_file_get_error(s
->file
);
108 DPRINTF("flush when error, bailing: %s\n", strerror(-error
));
112 DPRINTF("unfreezing output\n");
113 s
->freeze_output
= 0;
116 DPRINTF("buffering %d bytes\n", size
- offset
);
117 buffered_append(s
, buf
, size
);
120 error
= buffered_flush(s
);
122 DPRINTF("buffered flush error. bailing: %s\n", strerror(-error
));
126 if (pos
== 0 && size
== 0) {
127 DPRINTF("file is ready\n");
128 if (!s
->freeze_output
&& s
->bytes_xfer
< s
->xfer_limit
) {
129 DPRINTF("notifying client\n");
130 migrate_fd_put_ready(s
->migration_state
);
137 static int buffered_close(void *opaque
)
139 QEMUFileBuffered
*s
= opaque
;
143 DPRINTF("closing\n");
145 s
->xfer_limit
= INT_MAX
;
146 while (!qemu_file_get_error(s
->file
) && s
->buffer_size
) {
147 ret
= buffered_flush(s
);
151 if (s
->freeze_output
) {
152 ret
= migrate_fd_wait_for_unfreeze(s
->migration_state
);
159 ret2
= migrate_fd_close(s
->migration_state
);
163 qemu_del_timer(s
->timer
);
164 qemu_free_timer(s
->timer
);
172 * The meaning of the return values is:
173 * 0: We can continue sending
175 * negative: There has been an error
177 static int buffered_get_fd(void *opaque
)
179 QEMUFileBuffered
*s
= opaque
;
181 return qemu_get_fd(s
->file
);
184 static int buffered_rate_limit(void *opaque
)
186 QEMUFileBuffered
*s
= opaque
;
189 ret
= qemu_file_get_error(s
->file
);
193 if (s
->freeze_output
)
196 if (s
->bytes_xfer
> s
->xfer_limit
)
202 static int64_t buffered_set_rate_limit(void *opaque
, int64_t new_rate
)
204 QEMUFileBuffered
*s
= opaque
;
205 if (qemu_file_get_error(s
->file
)) {
208 if (new_rate
> SIZE_MAX
) {
212 s
->xfer_limit
= new_rate
/ 10;
215 return s
->xfer_limit
;
218 static int64_t buffered_get_rate_limit(void *opaque
)
220 QEMUFileBuffered
*s
= opaque
;
222 return s
->xfer_limit
;
225 static void buffered_rate_tick(void *opaque
)
227 QEMUFileBuffered
*s
= opaque
;
229 if (qemu_file_get_error(s
->file
)) {
234 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);
236 if (s
->freeze_output
)
241 buffered_put_buffer(s
, NULL
, 0, 0);
244 static const QEMUFileOps buffered_file_ops
= {
245 .get_fd
= buffered_get_fd
,
246 .put_buffer
= buffered_put_buffer
,
247 .close
= buffered_close
,
248 .rate_limit
= buffered_rate_limit
,
249 .get_rate_limit
= buffered_get_rate_limit
,
250 .set_rate_limit
= buffered_set_rate_limit
,
253 QEMUFile
*qemu_fopen_ops_buffered(MigrationState
*migration_state
)
257 s
= g_malloc0(sizeof(*s
));
259 s
->migration_state
= migration_state
;
260 s
->xfer_limit
= migration_state
->bandwidth_limit
/ 10;
262 s
->file
= qemu_fopen_ops(s
, &buffered_file_ops
);
264 s
->timer
= qemu_new_timer_ms(rt_clock
, buffered_rate_tick
, s
);
266 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);