{
assert(!bs->io_limits_enabled);
throttle_init(&bs->throttle_state,
+ bdrv_get_aio_context(bs),
QEMU_CLOCK_VIRTUAL,
bdrv_throttle_read_timer_cb,
bdrv_throttle_write_timer_cb,
return;
}
+ if (bs->io_limits_enabled) {
+ throttle_detach_aio_context(&bs->throttle_state);
+ }
if (bs->drv->bdrv_detach_aio_context) {
bs->drv->bdrv_detach_aio_context(bs);
}
if (bs->drv->bdrv_attach_aio_context) {
bs->drv->bdrv_attach_aio_context(bs, new_context);
}
+ if (bs->io_limits_enabled) {
+ throttle_attach_aio_context(&bs->throttle_state, new_context);
+ }
}
void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
int64_t previous_leak; /* timestamp of the last leak done */
QEMUTimer * timers[2]; /* timers used to do the throttling */
QEMUClockType clock_type; /* the clock used */
+
+ /* Callbacks */
+ QEMUTimerCB *read_timer_cb;
+ QEMUTimerCB *write_timer_cb;
+ void *timer_opaque;
} ThrottleState;
/* operations on single leaky buckets */
/* init/destroy cycle */
void throttle_init(ThrottleState *ts,
+ AioContext *aio_context,
QEMUClockType clock_type,
void (read_timer)(void *),
void (write_timer)(void *),
void throttle_destroy(ThrottleState *ts);
+void throttle_detach_aio_context(ThrottleState *ts);
+
+void throttle_attach_aio_context(ThrottleState *ts, AioContext *new_context);
+
bool throttle_have_timer(ThrottleState *ts);
/* configuration */
#include <glib.h>
#include <math.h>
+#include "block/aio.h"
#include "qemu/throttle.h"
+AioContext *ctx;
LeakyBucket bkt;
ThrottleConfig cfg;
ThrottleState ts;
memset(&ts, 1, sizeof(ts));
/* init the structure */
- throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
+ throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
+ read_timer_cb, write_timer_cb, &ts);
/* check initialized fields */
g_assert(ts.clock_type == QEMU_CLOCK_VIRTUAL);
static void test_destroy(void)
{
int i;
- throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
+ throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
+ read_timer_cb, write_timer_cb, &ts);
throttle_destroy(&ts);
for (i = 0; i < 2; i++) {
g_assert(!ts.timers[i]);
orig_cfg.op_size = 1;
- throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
+ throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
+ read_timer_cb, write_timer_cb, &ts);
/* structure reset by throttle_init previous_leak should be null */
g_assert(!ts.previous_leak);
throttle_config(&ts, &orig_cfg);
g_assert(!throttle_have_timer(&ts));
/* init the structure */
- throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
+ throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
+ read_timer_cb, write_timer_cb, &ts);
/* timer set by init should return true */
g_assert(throttle_have_timer(&ts));
cfg.op_size = op_size;
- throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
+ throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
+ read_timer_cb, write_timer_cb, &ts);
throttle_config(&ts, &cfg);
/* account a read */
int main(int argc, char **argv)
{
+ GSource *src;
+
init_clocks();
+
+ ctx = aio_context_new();
+ src = aio_get_g_source(ctx);
+ g_source_attach(src, NULL);
+ g_source_unref(src);
+
do {} while (g_main_context_iteration(NULL, false));
/* tests in the same order as the header function declarations */
#include "qemu/throttle.h"
#include "qemu/timer.h"
+#include "block/aio.h"
/* This function make a bucket leak
*
return false;
}
+/* Add timers to event loop */
+void throttle_attach_aio_context(ThrottleState *ts, AioContext *new_context)
+{
+ ts->timers[0] = aio_timer_new(new_context, ts->clock_type, SCALE_NS,
+ ts->read_timer_cb, ts->timer_opaque);
+ ts->timers[1] = aio_timer_new(new_context, ts->clock_type, SCALE_NS,
+ ts->write_timer_cb, ts->timer_opaque);
+}
+
/* To be called first on the ThrottleState */
void throttle_init(ThrottleState *ts,
+ AioContext *aio_context,
QEMUClockType clock_type,
QEMUTimerCB *read_timer_cb,
QEMUTimerCB *write_timer_cb,
memset(ts, 0, sizeof(ThrottleState));
ts->clock_type = clock_type;
- ts->timers[0] = timer_new_ns(clock_type, read_timer_cb, timer_opaque);
- ts->timers[1] = timer_new_ns(clock_type, write_timer_cb, timer_opaque);
+ ts->read_timer_cb = read_timer_cb;
+ ts->write_timer_cb = write_timer_cb;
+ ts->timer_opaque = timer_opaque;
+ throttle_attach_aio_context(ts, aio_context);
}
/* destroy a timer */
*timer = NULL;
}
-/* To be called last on the ThrottleState */
-void throttle_destroy(ThrottleState *ts)
+/* Remove timers from event loop */
+void throttle_detach_aio_context(ThrottleState *ts)
{
int i;
}
}
+/* To be called last on the ThrottleState */
+void throttle_destroy(ThrottleState *ts)
+{
+ throttle_detach_aio_context(ts);
+}
+
/* is any throttling timer configured */
bool throttle_have_timer(ThrottleState *ts)
{