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 BufferedPutFunc
*put_buffer
;
27 BufferedPutReadyFunc
*put_ready
;
28 BufferedWaitForUnfreezeFunc
*wait_for_unfreeze
;
29 BufferedCloseFunc
*close
;
37 size_t buffer_capacity
;
41 #ifdef DEBUG_BUFFERED_FILE
42 #define DPRINTF(fmt, ...) \
43 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
45 #define DPRINTF(fmt, ...) \
49 static void buffered_append(QEMUFileBuffered
*s
,
50 const uint8_t *buf
, size_t size
)
52 if (size
> (s
->buffer_capacity
- s
->buffer_size
)) {
55 DPRINTF("increasing buffer capacity from %zu by %zu\n",
56 s
->buffer_capacity
, size
+ 1024);
58 s
->buffer_capacity
+= size
+ 1024;
60 tmp
= g_realloc(s
->buffer
, s
->buffer_capacity
);
62 fprintf(stderr
, "qemu file buffer expansion failed\n");
69 memcpy(s
->buffer
+ s
->buffer_size
, buf
, size
);
70 s
->buffer_size
+= size
;
73 static void buffered_flush(QEMUFileBuffered
*s
)
78 error
= qemu_file_get_error(s
->file
);
80 DPRINTF("flush when error, bailing: %s\n", strerror(-error
));
84 DPRINTF("flushing %zu byte(s) of data\n", s
->buffer_size
);
86 while (offset
< s
->buffer_size
) {
89 ret
= s
->put_buffer(s
->opaque
, s
->buffer
+ offset
,
90 s
->buffer_size
- offset
);
92 DPRINTF("backend not ready, freezing\n");
98 DPRINTF("error flushing data, %zd\n", ret
);
99 qemu_file_set_error(s
->file
, ret
);
102 DPRINTF("flushed %zd byte(s)\n", ret
);
107 DPRINTF("flushed %zu of %zu byte(s)\n", offset
, s
->buffer_size
);
108 memmove(s
->buffer
, s
->buffer
+ offset
, s
->buffer_size
- offset
);
109 s
->buffer_size
-= offset
;
112 static int buffered_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
114 QEMUFileBuffered
*s
= opaque
;
115 int offset
= 0, error
;
118 DPRINTF("putting %d bytes at %" PRId64
"\n", size
, pos
);
120 error
= qemu_file_get_error(s
->file
);
122 DPRINTF("flush when error, bailing: %s\n", strerror(-error
));
126 DPRINTF("unfreezing output\n");
127 s
->freeze_output
= 0;
131 while (!s
->freeze_output
&& offset
< size
) {
132 if (s
->bytes_xfer
> s
->xfer_limit
) {
133 DPRINTF("transfer limit exceeded when putting\n");
137 ret
= s
->put_buffer(s
->opaque
, buf
+ offset
, size
- offset
);
138 if (ret
== -EAGAIN
) {
139 DPRINTF("backend not ready, freezing\n");
140 s
->freeze_output
= 1;
145 DPRINTF("error putting\n");
146 qemu_file_set_error(s
->file
, ret
);
151 DPRINTF("put %zd byte(s)\n", ret
);
153 s
->bytes_xfer
+= ret
;
157 DPRINTF("buffering %d bytes\n", size
- offset
);
158 buffered_append(s
, buf
+ offset
, size
- offset
);
162 if (pos
== 0 && size
== 0) {
163 DPRINTF("file is ready\n");
164 if (s
->bytes_xfer
<= s
->xfer_limit
) {
165 DPRINTF("notifying client\n");
166 s
->put_ready(s
->opaque
);
173 static int buffered_close(void *opaque
)
175 QEMUFileBuffered
*s
= opaque
;
178 DPRINTF("closing\n");
180 while (!qemu_file_get_error(s
->file
) && s
->buffer_size
) {
182 if (s
->freeze_output
)
183 s
->wait_for_unfreeze(s
->opaque
);
186 ret
= s
->close(s
->opaque
);
188 qemu_del_timer(s
->timer
);
189 qemu_free_timer(s
->timer
);
197 * The meaning of the return values is:
198 * 0: We can continue sending
200 * negative: There has been an error
202 static int buffered_rate_limit(void *opaque
)
204 QEMUFileBuffered
*s
= opaque
;
207 ret
= qemu_file_get_error(s
->file
);
211 if (s
->freeze_output
)
214 if (s
->bytes_xfer
> s
->xfer_limit
)
220 static int64_t buffered_set_rate_limit(void *opaque
, int64_t new_rate
)
222 QEMUFileBuffered
*s
= opaque
;
223 if (qemu_file_get_error(s
->file
)) {
226 if (new_rate
> SIZE_MAX
) {
230 s
->xfer_limit
= new_rate
/ 10;
233 return s
->xfer_limit
;
236 static int64_t buffered_get_rate_limit(void *opaque
)
238 QEMUFileBuffered
*s
= opaque
;
240 return s
->xfer_limit
;
243 static void buffered_rate_tick(void *opaque
)
245 QEMUFileBuffered
*s
= opaque
;
247 if (qemu_file_get_error(s
->file
)) {
252 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);
254 if (s
->freeze_output
)
261 /* Add some checks around this */
262 s
->put_ready(s
->opaque
);
265 QEMUFile
*qemu_fopen_ops_buffered(void *opaque
,
266 size_t bytes_per_sec
,
267 BufferedPutFunc
*put_buffer
,
268 BufferedPutReadyFunc
*put_ready
,
269 BufferedWaitForUnfreezeFunc
*wait_for_unfreeze
,
270 BufferedCloseFunc
*close
)
274 s
= g_malloc0(sizeof(*s
));
277 s
->xfer_limit
= bytes_per_sec
/ 10;
278 s
->put_buffer
= put_buffer
;
279 s
->put_ready
= put_ready
;
280 s
->wait_for_unfreeze
= wait_for_unfreeze
;
283 s
->file
= qemu_fopen_ops(s
, buffered_put_buffer
, NULL
,
284 buffered_close
, buffered_rate_limit
,
285 buffered_set_rate_limit
,
286 buffered_get_rate_limit
);
288 s
->timer
= qemu_new_timer_ms(rt_clock
, buffered_rate_tick
, s
);
290 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);