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 "buffered_file.h"
20 #include "qemu/thread.h"
22 //#define DEBUG_BUFFERED_FILE
24 typedef struct QEMUFileBuffered
26 MigrationState
*migration_state
;
32 size_t buffer_capacity
;
36 #ifdef DEBUG_BUFFERED_FILE
37 #define DPRINTF(fmt, ...) \
38 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
40 #define DPRINTF(fmt, ...) \
44 static void buffered_append(QEMUFileBuffered
*s
,
45 const uint8_t *buf
, size_t size
)
47 if (size
> (s
->buffer_capacity
- s
->buffer_size
)) {
48 DPRINTF("increasing buffer capacity from %zu by %zu\n",
49 s
->buffer_capacity
, size
+ 1024);
51 s
->buffer_capacity
+= size
+ 1024;
53 s
->buffer
= g_realloc(s
->buffer
, s
->buffer_capacity
);
56 memcpy(s
->buffer
+ s
->buffer_size
, buf
, size
);
57 s
->buffer_size
+= size
;
60 static ssize_t
buffered_flush(QEMUFileBuffered
*s
)
65 DPRINTF("flushing %zu byte(s) of data\n", s
->buffer_size
);
67 while (s
->bytes_xfer
< s
->xfer_limit
&& offset
< s
->buffer_size
) {
68 size_t to_send
= MIN(s
->buffer_size
- offset
, s
->xfer_limit
- s
->bytes_xfer
);
69 ret
= migrate_fd_put_buffer(s
->migration_state
, s
->buffer
+ offset
,
72 DPRINTF("error flushing data, %zd\n", ret
);
75 DPRINTF("flushed %zd byte(s)\n", ret
);
81 DPRINTF("flushed %zu of %zu byte(s)\n", offset
, s
->buffer_size
);
82 memmove(s
->buffer
, s
->buffer
+ offset
, s
->buffer_size
- offset
);
83 s
->buffer_size
-= offset
;
91 static int buffered_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
93 QEMUFileBuffered
*s
= opaque
;
96 DPRINTF("putting %d bytes at %" PRId64
"\n", size
, pos
);
98 error
= qemu_file_get_error(s
->file
);
100 DPRINTF("flush when error, bailing: %s\n", strerror(-error
));
105 DPRINTF("buffering %d bytes\n", size
- offset
);
106 buffered_append(s
, buf
, size
);
109 error
= buffered_flush(s
);
111 DPRINTF("buffered flush error. bailing: %s\n", strerror(-error
));
115 if (pos
== 0 && size
== 0) {
116 DPRINTF("file is ready\n");
117 if (s
->bytes_xfer
< s
->xfer_limit
) {
118 DPRINTF("notifying client\n");
119 migrate_fd_put_ready(s
->migration_state
);
126 static int buffered_close(void *opaque
)
128 QEMUFileBuffered
*s
= opaque
;
132 DPRINTF("closing\n");
134 s
->xfer_limit
= INT_MAX
;
135 while (!qemu_file_get_error(s
->file
) && s
->buffer_size
) {
136 ret
= buffered_flush(s
);
142 ret2
= migrate_fd_close(s
->migration_state
);
146 ret
= migrate_fd_close(s
->migration_state
);
147 s
->migration_state
->complete
= true;
152 * The meaning of the return values is:
153 * 0: We can continue sending
155 * negative: There has been an error
157 static int buffered_get_fd(void *opaque
)
159 QEMUFileBuffered
*s
= opaque
;
161 return qemu_get_fd(s
->file
);
164 static int buffered_rate_limit(void *opaque
)
166 QEMUFileBuffered
*s
= opaque
;
169 ret
= qemu_file_get_error(s
->file
);
174 if (s
->bytes_xfer
> s
->xfer_limit
)
180 static int64_t buffered_set_rate_limit(void *opaque
, int64_t new_rate
)
182 QEMUFileBuffered
*s
= opaque
;
183 if (qemu_file_get_error(s
->file
)) {
186 if (new_rate
> SIZE_MAX
) {
190 s
->xfer_limit
= new_rate
/ 10;
193 return s
->xfer_limit
;
196 static int64_t buffered_get_rate_limit(void *opaque
)
198 QEMUFileBuffered
*s
= opaque
;
200 return s
->xfer_limit
;
203 /* 10ms xfer_limit is the limit that we should write each 10ms */
204 #define BUFFER_DELAY 100
206 static void *buffered_file_thread(void *opaque
)
208 QEMUFileBuffered
*s
= opaque
;
209 int64_t expire_time
= qemu_get_clock_ms(rt_clock
) + BUFFER_DELAY
;
212 int64_t current_time
= qemu_get_clock_ms(rt_clock
);
214 if (s
->migration_state
->complete
) {
217 if (current_time
>= expire_time
) {
219 expire_time
= current_time
+ BUFFER_DELAY
;
221 if (s
->bytes_xfer
>= s
->xfer_limit
) {
222 /* usleep expects microseconds */
223 g_usleep((expire_time
- current_time
)*1000);
225 buffered_put_buffer(s
, NULL
, 0, 0);
232 static const QEMUFileOps buffered_file_ops
= {
233 .get_fd
= buffered_get_fd
,
234 .put_buffer
= buffered_put_buffer
,
235 .close
= buffered_close
,
236 .rate_limit
= buffered_rate_limit
,
237 .get_rate_limit
= buffered_get_rate_limit
,
238 .set_rate_limit
= buffered_set_rate_limit
,
241 void qemu_fopen_ops_buffered(MigrationState
*migration_state
)
245 s
= g_malloc0(sizeof(*s
));
247 s
->migration_state
= migration_state
;
248 s
->xfer_limit
= migration_state
->bandwidth_limit
/ 10;
249 s
->migration_state
->complete
= false;
251 s
->file
= qemu_fopen_ops(s
, &buffered_file_ops
);
253 migration_state
->file
= s
->file
;
255 qemu_thread_create(&s
->thread
, buffered_file_thread
, s
,
256 QEMU_THREAD_DETACHED
);