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.
14 #include "qemu-common.h"
16 #include "qemu-timer.h"
17 #include "qemu-char.h"
18 #include "buffered_file.h"
20 //#define DEBUG_BUFFERED_FILE
22 typedef struct QEMUFileBuffered
24 BufferedPutFunc
*put_buffer
;
25 BufferedPutReadyFunc
*put_ready
;
26 BufferedWaitForUnfreezeFunc
*wait_for_unfreeze
;
27 BufferedCloseFunc
*close
;
36 size_t buffer_capacity
;
40 #ifdef DEBUG_BUFFERED_FILE
41 #define DPRINTF(fmt, ...) \
42 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
44 #define DPRINTF(fmt, ...) \
48 static void buffered_append(QEMUFileBuffered
*s
,
49 const uint8_t *buf
, size_t size
)
51 if (size
> (s
->buffer_capacity
- s
->buffer_size
)) {
54 DPRINTF("increasing buffer capacity from %zu by %zu\n",
55 s
->buffer_capacity
, size
+ 1024);
57 s
->buffer_capacity
+= size
+ 1024;
59 tmp
= g_realloc(s
->buffer
, s
->buffer_capacity
);
61 fprintf(stderr
, "qemu file buffer expansion failed\n");
68 memcpy(s
->buffer
+ s
->buffer_size
, buf
, size
);
69 s
->buffer_size
+= size
;
72 static void buffered_flush(QEMUFileBuffered
*s
)
77 DPRINTF("flush when error, bailing\n");
81 DPRINTF("flushing %zu byte(s) of data\n", s
->buffer_size
);
83 while (offset
< s
->buffer_size
) {
86 ret
= s
->put_buffer(s
->opaque
, s
->buffer
+ offset
,
87 s
->buffer_size
- offset
);
89 DPRINTF("backend not ready, freezing\n");
95 DPRINTF("error flushing data, %zd\n", ret
);
99 DPRINTF("flushed %zd byte(s)\n", ret
);
104 DPRINTF("flushed %zu of %zu byte(s)\n", offset
, s
->buffer_size
);
105 memmove(s
->buffer
, s
->buffer
+ offset
, s
->buffer_size
- offset
);
106 s
->buffer_size
-= offset
;
109 static int buffered_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
111 QEMUFileBuffered
*s
= opaque
;
115 DPRINTF("putting %d bytes at %" PRId64
"\n", size
, pos
);
118 DPRINTF("flush when error, bailing\n");
122 DPRINTF("unfreezing output\n");
123 s
->freeze_output
= 0;
127 while (!s
->freeze_output
&& offset
< size
) {
128 if (s
->bytes_xfer
> s
->xfer_limit
) {
129 DPRINTF("transfer limit exceeded when putting\n");
133 ret
= s
->put_buffer(s
->opaque
, buf
+ offset
, size
- offset
);
134 if (ret
== -EAGAIN
) {
135 DPRINTF("backend not ready, freezing\n");
136 s
->freeze_output
= 1;
141 DPRINTF("error putting\n");
147 DPRINTF("put %zd byte(s)\n", ret
);
149 s
->bytes_xfer
+= ret
;
153 DPRINTF("buffering %d bytes\n", size
- offset
);
154 buffered_append(s
, buf
+ offset
, size
- offset
);
158 if (pos
== 0 && size
== 0) {
159 DPRINTF("file is ready\n");
160 if (s
->bytes_xfer
<= s
->xfer_limit
) {
161 DPRINTF("notifying client\n");
162 s
->put_ready(s
->opaque
);
169 static int buffered_close(void *opaque
)
171 QEMUFileBuffered
*s
= opaque
;
174 DPRINTF("closing\n");
176 while (!s
->has_error
&& s
->buffer_size
) {
178 if (s
->freeze_output
)
179 s
->wait_for_unfreeze(s
);
182 ret
= s
->close(s
->opaque
);
184 qemu_del_timer(s
->timer
);
185 qemu_free_timer(s
->timer
);
192 static int buffered_rate_limit(void *opaque
)
194 QEMUFileBuffered
*s
= opaque
;
199 if (s
->freeze_output
)
202 if (s
->bytes_xfer
> s
->xfer_limit
)
208 static int64_t buffered_set_rate_limit(void *opaque
, int64_t new_rate
)
210 QEMUFileBuffered
*s
= opaque
;
214 if (new_rate
> SIZE_MAX
) {
218 s
->xfer_limit
= new_rate
/ 10;
221 return s
->xfer_limit
;
224 static int64_t buffered_get_rate_limit(void *opaque
)
226 QEMUFileBuffered
*s
= opaque
;
228 return s
->xfer_limit
;
231 static void buffered_rate_tick(void *opaque
)
233 QEMUFileBuffered
*s
= opaque
;
240 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);
242 if (s
->freeze_output
)
249 /* Add some checks around this */
250 s
->put_ready(s
->opaque
);
253 QEMUFile
*qemu_fopen_ops_buffered(void *opaque
,
254 size_t bytes_per_sec
,
255 BufferedPutFunc
*put_buffer
,
256 BufferedPutReadyFunc
*put_ready
,
257 BufferedWaitForUnfreezeFunc
*wait_for_unfreeze
,
258 BufferedCloseFunc
*close
)
262 s
= g_malloc0(sizeof(*s
));
265 s
->xfer_limit
= bytes_per_sec
/ 10;
266 s
->put_buffer
= put_buffer
;
267 s
->put_ready
= put_ready
;
268 s
->wait_for_unfreeze
= wait_for_unfreeze
;
271 s
->file
= qemu_fopen_ops(s
, buffered_put_buffer
, NULL
,
272 buffered_close
, buffered_rate_limit
,
273 buffered_set_rate_limit
,
274 buffered_get_rate_limit
);
276 s
->timer
= qemu_new_timer_ms(rt_clock
, buffered_rate_tick
, s
);
278 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);