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
;
35 size_t buffer_capacity
;
39 #ifdef DEBUG_BUFFERED_FILE
40 #define DPRINTF(fmt, ...) \
41 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) \
47 static void buffered_append(QEMUFileBuffered
*s
,
48 const uint8_t *buf
, size_t size
)
50 if (size
> (s
->buffer_capacity
- s
->buffer_size
)) {
53 DPRINTF("increasing buffer capacity from %zu by %zu\n",
54 s
->buffer_capacity
, size
+ 1024);
56 s
->buffer_capacity
+= size
+ 1024;
58 tmp
= g_realloc(s
->buffer
, s
->buffer_capacity
);
60 fprintf(stderr
, "qemu file buffer expansion failed\n");
67 memcpy(s
->buffer
+ s
->buffer_size
, buf
, size
);
68 s
->buffer_size
+= size
;
71 static void buffered_flush(QEMUFileBuffered
*s
)
76 error
= qemu_file_get_error(s
->file
);
78 DPRINTF("flush when error, bailing: %s\n", strerror(-error
));
82 DPRINTF("flushing %zu byte(s) of data\n", s
->buffer_size
);
84 while (offset
< s
->buffer_size
) {
87 ret
= s
->put_buffer(s
->opaque
, s
->buffer
+ offset
,
88 s
->buffer_size
- offset
);
90 DPRINTF("backend not ready, freezing\n");
96 DPRINTF("error flushing data, %zd\n", ret
);
97 qemu_file_set_error(s
->file
, ret
);
100 DPRINTF("flushed %zd byte(s)\n", ret
);
105 DPRINTF("flushed %zu of %zu byte(s)\n", offset
, s
->buffer_size
);
106 memmove(s
->buffer
, s
->buffer
+ offset
, s
->buffer_size
- offset
);
107 s
->buffer_size
-= offset
;
110 static int buffered_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
112 QEMUFileBuffered
*s
= opaque
;
113 int offset
= 0, error
;
116 DPRINTF("putting %d bytes at %" PRId64
"\n", size
, pos
);
118 error
= qemu_file_get_error(s
->file
);
120 DPRINTF("flush when error, bailing: %s\n", strerror(-error
));
124 DPRINTF("unfreezing output\n");
125 s
->freeze_output
= 0;
129 while (!s
->freeze_output
&& offset
< size
) {
130 if (s
->bytes_xfer
> s
->xfer_limit
) {
131 DPRINTF("transfer limit exceeded when putting\n");
135 ret
= s
->put_buffer(s
->opaque
, buf
+ offset
, size
- offset
);
136 if (ret
== -EAGAIN
) {
137 DPRINTF("backend not ready, freezing\n");
138 s
->freeze_output
= 1;
143 DPRINTF("error putting\n");
144 qemu_file_set_error(s
->file
, ret
);
149 DPRINTF("put %zd byte(s)\n", ret
);
151 s
->bytes_xfer
+= ret
;
155 DPRINTF("buffering %d bytes\n", size
- offset
);
156 buffered_append(s
, buf
+ offset
, size
- offset
);
160 if (pos
== 0 && size
== 0) {
161 DPRINTF("file is ready\n");
162 if (s
->bytes_xfer
<= s
->xfer_limit
) {
163 DPRINTF("notifying client\n");
164 s
->put_ready(s
->opaque
);
171 static int buffered_close(void *opaque
)
173 QEMUFileBuffered
*s
= opaque
;
176 DPRINTF("closing\n");
178 while (!qemu_file_get_error(s
->file
) && s
->buffer_size
) {
180 if (s
->freeze_output
)
181 s
->wait_for_unfreeze(s
->opaque
);
184 ret
= s
->close(s
->opaque
);
186 qemu_del_timer(s
->timer
);
187 qemu_free_timer(s
->timer
);
195 * The meaning of the return values is:
196 * 0: We can continue sending
198 * negative: There has been an error
200 static int buffered_rate_limit(void *opaque
)
202 QEMUFileBuffered
*s
= opaque
;
205 ret
= qemu_file_get_error(s
->file
);
209 if (s
->freeze_output
)
212 if (s
->bytes_xfer
> s
->xfer_limit
)
218 static int64_t buffered_set_rate_limit(void *opaque
, int64_t new_rate
)
220 QEMUFileBuffered
*s
= opaque
;
221 if (qemu_file_get_error(s
->file
)) {
224 if (new_rate
> SIZE_MAX
) {
228 s
->xfer_limit
= new_rate
/ 10;
231 return s
->xfer_limit
;
234 static int64_t buffered_get_rate_limit(void *opaque
)
236 QEMUFileBuffered
*s
= opaque
;
238 return s
->xfer_limit
;
241 static void buffered_rate_tick(void *opaque
)
243 QEMUFileBuffered
*s
= opaque
;
245 if (qemu_file_get_error(s
->file
)) {
250 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);
252 if (s
->freeze_output
)
259 /* Add some checks around this */
260 s
->put_ready(s
->opaque
);
263 QEMUFile
*qemu_fopen_ops_buffered(void *opaque
,
264 size_t bytes_per_sec
,
265 BufferedPutFunc
*put_buffer
,
266 BufferedPutReadyFunc
*put_ready
,
267 BufferedWaitForUnfreezeFunc
*wait_for_unfreeze
,
268 BufferedCloseFunc
*close
)
272 s
= g_malloc0(sizeof(*s
));
275 s
->xfer_limit
= bytes_per_sec
/ 10;
276 s
->put_buffer
= put_buffer
;
277 s
->put_ready
= put_ready
;
278 s
->wait_for_unfreeze
= wait_for_unfreeze
;
281 s
->file
= qemu_fopen_ops(s
, buffered_put_buffer
, NULL
,
282 buffered_close
, buffered_rate_limit
,
283 buffered_set_rate_limit
,
284 buffered_get_rate_limit
);
286 s
->timer
= qemu_new_timer_ms(rt_clock
, buffered_rate_tick
, s
);
288 qemu_mod_timer(s
->timer
, qemu_get_clock_ms(rt_clock
) + 100);