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"
18 #include "qemu-char.h"
19 #include "buffered_file.h"
21 //#define DEBUG_BUFFERED_FILE
23 typedef struct QEMUFileBuffered
25 BufferedPutFunc
*put_buffer
;
26 BufferedPutReadyFunc
*put_ready
;
27 BufferedWaitForUnfreezeFunc
*wait_for_unfreeze
;
28 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 %ld by %ld\n",
56 s
->buffer_capacity
, size
+ 1024);
58 s
->buffer_capacity
+= size
+ 1024;
60 tmp
= qemu_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 dprintf("flush when error, bailing\n");
82 dprintf("flushing %ld 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, %ld\n", ret
);
100 dprintf("flushed %ld byte(s)\n", ret
);
105 dprintf("flushed %ld of %ld 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
;
116 dprintf("putting %d bytes at %" PRId64
"\n", size
, pos
);
119 dprintf("flush when error, bailing\n");
123 dprintf("unfreezing output\n");
124 s
->freeze_output
= 0;
128 while (!s
->freeze_output
&& offset
< size
) {
129 if (s
->bytes_xfer
> s
->xfer_limit
) {
130 dprintf("transfer limit exceeded when putting\n");
134 ret
= s
->put_buffer(s
->opaque
, buf
+ offset
, size
- offset
);
135 if (ret
== -EAGAIN
) {
136 dprintf("backend not ready, freezing\n");
137 s
->freeze_output
= 1;
142 dprintf("error putting\n");
148 dprintf("put %ld byte(s)\n", ret
);
150 s
->bytes_xfer
+= ret
;
154 dprintf("buffering %d bytes\n", size
- offset
);
155 buffered_append(s
, buf
+ offset
, size
- offset
);
162 static int buffered_close(void *opaque
)
164 QEMUFileBuffered
*s
= opaque
;
167 dprintf("closing\n");
169 while (!s
->has_error
&& s
->buffer_size
) {
171 if (s
->freeze_output
)
172 s
->wait_for_unfreeze(s
);
175 ret
= s
->close(s
->opaque
);
177 qemu_del_timer(s
->timer
);
178 qemu_free_timer(s
->timer
);
179 qemu_free(s
->buffer
);
185 static int buffered_rate_limit(void *opaque
)
187 QEMUFileBuffered
*s
= opaque
;
192 if (s
->freeze_output
)
195 if (s
->bytes_xfer
> s
->xfer_limit
)
201 static size_t buffered_set_rate_limit(void *opaque
, size_t new_rate
)
203 QEMUFileBuffered
*s
= opaque
;
208 s
->xfer_limit
= new_rate
/ 10;
211 return s
->xfer_limit
;
214 static void buffered_rate_tick(void *opaque
)
216 QEMUFileBuffered
*s
= opaque
;
221 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 100);
223 if (s
->freeze_output
)
230 /* Add some checks around this */
231 s
->put_ready(s
->opaque
);
234 QEMUFile
*qemu_fopen_ops_buffered(void *opaque
,
235 size_t bytes_per_sec
,
236 BufferedPutFunc
*put_buffer
,
237 BufferedPutReadyFunc
*put_ready
,
238 BufferedWaitForUnfreezeFunc
*wait_for_unfreeze
,
239 BufferedCloseFunc
*close
)
243 s
= qemu_mallocz(sizeof(*s
));
246 s
->xfer_limit
= bytes_per_sec
/ 10;
247 s
->put_buffer
= put_buffer
;
248 s
->put_ready
= put_ready
;
249 s
->wait_for_unfreeze
= wait_for_unfreeze
;
252 s
->file
= qemu_fopen_ops(s
, buffered_put_buffer
, NULL
,
253 buffered_close
, buffered_rate_limit
,
254 buffered_set_rate_limit
);
256 s
->timer
= qemu_new_timer(rt_clock
, buffered_rate_tick
, s
);
258 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 100);