Skip to contentSkip to conversation

Throttle group cooperative round robin scheduling

RFC New Benoît Canet 1 participant 6 messages Aug 12, 2014 Block layer

lore
Share & tools
lore
#0 lore raw

cover Throttle group cooperative round robin scheduling

Hi,

Here is my current wip on the throttle group support.

For the user interface I implemented Stefanha's idea proposed in Stuttgart.

For the throttling algorithm I use a cooperative round robin scheduler.

Classical round robin works with a fixed HZ ticks and it's totaly incompatible with the throttling algorithm.

So the cooperative round robin scheduler is a way for each block device to decide if a pause must be done and a timer be armed and most important of all which other block device of the group must resume the work once the timer is fired.

The advantages of this algorigthm are:

-only one timer active at a given time (no more cpu usage than regular throttling) -no central place didacting the sheduling policy like a didactureship:
we love collaboration isn't it ?:)
-No need to deal with incoming queues to collect requests before scheduling
then with and dispatchs queues
-Compatible with the throttling code with almost no changes
-As you go scheduling

Best regards

Benoît

Benoît Canet (5):
throttle: Extract timers from ThrottleState into a separate
ThrottleTimers structure
throttle: Add throttle group infrastructure.
throttle: Add throttle group infrastructure tests
throttle: Prepare to have multiple timers for one ThrottleState
throttle: Add throttle group support.

 block.c                        | 173 +++++++++++++++++++++++++++++++-----
 block/qapi.c                   |   2 +-
 blockdev.c                     |  16 +++-
 hmp.c                          |   4 +-
 include/block/block.h          |   2 +-
 include/block/block_int.h      |   9 +-
 include/qemu/throttle-groups.h |  43 +++++++++
 include/qemu/throttle.h        |  42 ++++++---
 qapi/block-core.json           |   5 +-
 qemu-options.hx                |   1 +
 qmp-commands.hx                |   3 +-
 tests/test-throttle.c          | 133 ++++++++++++++++++++--------
 util/Makefile.objs             |   1 +
 util/throttle-groups.c         | 195 +++++++++++++++++++++++++++++++++++++++++
 util/throttle.c                | 103 +++++++++++++++-------
 15 files changed, 616 insertions(+), 116 deletions(-)
 create mode 100644 include/qemu/throttle-groups.h
 create mode 100644 util/throttle-groups.c
Signature · 1 line
2.1.0.rc1
#1 lore raw

RFC 1/5 throttle: Extract timers from ThrottleState into a separate ThrottleTimers structure

Group throttling will share ThrottleState between multiple bs. As a consequence the ThrottleState will be accessed by multiple aio context.

Timers are tied to their aio context so they must go out of the ThrottleState structure.

This commit pave the way for each bs of a common ThrottleState to have it's own timer.

---
 block.c                   | 32 ++++++++++--------
 include/block/block_int.h |  1 +
 include/qemu/throttle.h   | 36 +++++++++++++--------
 tests/test-throttle.c     | 82 ++++++++++++++++++++++++++---------------------
 util/throttle.c           | 73 ++++++++++++++++++++++++-----------------
 5 files changed, 131 insertions(+), 93 deletions(-)
block.c +18 -14
index 8cf519b..d2ca851 100644
@@ -126,7 +126,7 @@ void bdrv_set_io_limits(BlockDriverState *bs,
 {
     int i;
 
-    throttle_config(&bs->throttle_state, cfg);
+    throttle_config(&bs->throttle_state, &bs->throttle_timers, cfg);
 
     for (i = 0; i < 2; i++) {
         qemu_co_enter_next(&bs->throttled_reqs[i]);
@@ -159,7 +159,7 @@ void bdrv_io_limits_disable(BlockDriverState *bs)
 
     bdrv_start_throttled_reqs(bs);
 
-    throttle_destroy(&bs->throttle_state);
+    throttle_timers_destroy(&bs->throttle_timers);
 }
 
 static void bdrv_throttle_read_timer_cb(void *opaque)
@@ -178,12 +178,13 @@ static void bdrv_throttle_write_timer_cb(void *opaque)
 void bdrv_io_limits_enable(BlockDriverState *bs)
 {
     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,
-                  bs);
+    throttle_init(&bs->throttle_state);
+    throttle_timers_init(&bs->throttle_timers,
+                         bdrv_get_aio_context(bs),
+                         QEMU_CLOCK_VIRTUAL,
+                         bdrv_throttle_read_timer_cb,
+                         bdrv_throttle_write_timer_cb,
+                         bs);
     bs->io_limits_enabled = true;
 }
 
@@ -197,7 +198,9 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs,
                                      bool is_write)
 {
     /* does this io must wait */
-    bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write);
+    bool must_wait = throttle_schedule_timer(&bs->throttle_state,
+                                             &bs->throttle_timers,
+                                             is_write);
 
     /* if must wait or any request of this type throttled queue the IO */
     if (must_wait ||
@@ -210,7 +213,8 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs,
 
 
     /* if the next request must wait -> do nothing */
-    if (throttle_schedule_timer(&bs->throttle_state, is_write)) {
+    if (throttle_schedule_timer(&bs->throttle_state, &bs->throttle_timers,
+                                is_write)) {
         return;
     }
 
@@ -2031,7 +2035,7 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     assert(bs_new->job == NULL);
     assert(bs_new->dev == NULL);
     assert(bs_new->io_limits_enabled == false);
-    assert(!throttle_have_timer(&bs_new->throttle_state));
+    assert(!throttle_timers_are_init(&bs_new->throttle_timers));
 
     tmp = *bs_new;
     *bs_new = *bs_old;
@@ -2049,7 +2053,7 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
     assert(bs_new->dev == NULL);
     assert(bs_new->job == NULL);
     assert(bs_new->io_limits_enabled == false);
-    assert(!throttle_have_timer(&bs_new->throttle_state));
+    assert(!throttle_timers_are_init(&bs_new->throttle_timers));
 
     /* insert the nodes back into the graph node list if needed */
     if (bs_new->node_name[0] != '\0') {
@@ -5663,7 +5667,7 @@ void bdrv_detach_aio_context(BlockDriverState *bs)
     }
 
     if (bs->io_limits_enabled) {
-        throttle_detach_aio_context(&bs->throttle_state);
+        throttle_timers_detach_aio_context(&bs->throttle_timers);
     }
     if (bs->drv->bdrv_detach_aio_context) {
         bs->drv->bdrv_detach_aio_context(bs);
@@ -5697,7 +5701,7 @@ void bdrv_attach_aio_context(BlockDriverState *bs,
         bs->drv->bdrv_attach_aio_context(bs, new_context);
     }
     if (bs->io_limits_enabled) {
-        throttle_attach_aio_context(&bs->throttle_state, new_context);
+        throttle_timers_attach_aio_context(&bs->throttle_timers, new_context);
     }
 }
 
include/block/block_int.h +1 -0
index 7b541a0..2a0c146 100644
@@ -336,6 +336,7 @@ struct BlockDriverState {
 
     /* I/O throttling */
     ThrottleState throttle_state;
+    ThrottleTimers throttle_timers; 
     CoQueue      throttled_reqs[2];
     bool         io_limits_enabled;
 
include/qemu/throttle.h +23 -13
index b890613..b89d4d8 100644
@@ -65,6 +65,9 @@ typedef struct ThrottleConfig {
 typedef struct ThrottleState {
     ThrottleConfig cfg;       /* configuration */
     int64_t previous_leak;    /* timestamp of the last leak done */
+} ThrottleState;
+
+typedef struct ThrottleTimers {
     QEMUTimer * timers[2];    /* timers used to do the throttling */
     QEMUClockType clock_type; /* the clock used */
 
@@ -72,7 +75,7 @@ typedef struct ThrottleState {
     QEMUTimerCB *read_timer_cb;
     QEMUTimerCB *write_timer_cb;
     void *timer_opaque;
-} ThrottleState;
+} ThrottleTimers;
 
 /* operations on single leaky buckets */
 void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta);
@@ -86,20 +89,23 @@ bool throttle_compute_timer(ThrottleState *ts,
                             int64_t *next_timestamp);
 
 /* init/destroy cycle */
-void throttle_init(ThrottleState *ts,
-                   AioContext *aio_context,
-                   QEMUClockType clock_type,
-                   void (read_timer)(void *),
-                   void (write_timer)(void *),
-                   void *timer_opaque);
+void throttle_init(ThrottleState *ts);
+
+void throttle_timers_init(ThrottleTimers *tt,
+                          AioContext *aio_context,
+                          QEMUClockType clock_type,
+                          QEMUTimerCB *read_timer_cb,
+                          QEMUTimerCB *write_timer_cb,
+                          void *timer_opaque);
 
-void throttle_destroy(ThrottleState *ts);
+void throttle_timers_destroy(ThrottleTimers *tt);
 
-void throttle_detach_aio_context(ThrottleState *ts);
+void throttle_timers_detach_aio_context(ThrottleTimers *tt);
 
-void throttle_attach_aio_context(ThrottleState *ts, AioContext *new_context);
+void throttle_timers_attach_aio_context(ThrottleTimers *tt,
+                                        AioContext *new_context);
 
-bool throttle_have_timer(ThrottleState *ts);
+bool throttle_timers_are_init(ThrottleTimers *tt);
 
 /* configuration */
 bool throttle_enabled(ThrottleConfig *cfg);
@@ -108,12 +114,16 @@ bool throttle_conflicting(ThrottleConfig *cfg);
 
 bool throttle_is_valid(ThrottleConfig *cfg);
 
-void throttle_config(ThrottleState *ts, ThrottleConfig *cfg);
+void throttle_config(ThrottleState *ts,
+                     ThrottleTimers *tt,
+                     ThrottleConfig *cfg);
 
 void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg);
 
 /* usage */
-bool throttle_schedule_timer(ThrottleState *ts, bool is_write);
+bool throttle_schedule_timer(ThrottleState *ts,
+                             ThrottleTimers *tt,
+                             bool is_write);
 
 void throttle_account(ThrottleState *ts, bool is_write, uint64_t size);
 
tests/test-throttle.c +46 -36

163 lines not on this page · raw message

util/throttle.c +43 -30

140 lines not on this page · raw message

Signature · 1 line
2.1.0.rc1
#2 lore raw

RFC 2/5 throttle: Add throttle group infrastructure.

In reply to Benoît Canet

The throttle_group_incref increment the refcount of a throttle group given it's name and return the associated throttle state.

The throttle_group_unref is the mirror function for cleaning up.

---
 include/block/block_int.h      |   1 +
 include/qemu/throttle-groups.h |  43 +++++++++
 util/Makefile.objs             |   1 +
 util/throttle-groups.c         | 195 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 240 insertions(+)
 create mode 100644 include/qemu/throttle-groups.h
 create mode 100644 util/throttle-groups.c
include/block/block_int.h +1 -0
index 2a0c146..6066f63 100644
@@ -339,6 +339,7 @@ struct BlockDriverState {
     ThrottleTimers throttle_timers; 
     CoQueue      throttled_reqs[2];
     bool         io_limits_enabled;
+    QLIST_ENTRY(BlockDriverState) round_robin;
 
     /* I/O stats (display with "info blockstats"). */
     uint64_t nr_bytes[BDRV_MAX_IOTYPE];
include/qemu/throttle-groups.h new file +43 -0

43 lines not on this page · raw message

util/Makefile.objs +1 -0
index 6b3c83b..3c95397 100644
@@ -12,6 +12,7 @@ util-obj-y += qemu-option.o qemu-progress.o
 util-obj-y += hexdump.o
 util-obj-y += crc32c.o
 util-obj-y += throttle.o
+util-obj-y += throttle-groups.o
 util-obj-y += getauxval.o
 util-obj-y += readline.o
 util-obj-y += rfifolock.o
util/throttle-groups.c new file +195 -0

195 lines not on this page · raw message

Signature · 1 line
2.1.0.rc1
#3 lore raw

RFC 3/5 throttle: Add throttle group infrastructure tests

In reply to Benoît Canet
---
 tests/test-throttle.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
tests/test-throttle.c +51 -0
index 66e4982..494a806 100644
@@ -14,6 +14,7 @@
 #include <math.h>
 #include "block/aio.h"
 #include "qemu/throttle.h"
+#include "qemu/throttle-groups.h"
 
 static AioContext     *ctx;
 static LeakyBucket    bkt;
@@ -499,6 +500,55 @@ static void test_accounting(void)
                                 (64.0 / 13)));
 }
 
+static void test_groups(void)
+{
+    bool removed;
+
+    ThrottleState *ts_foo, *ts_bar, *tmp;
+
+    ts_bar = throttle_group_incref("bar");
+    throttle_group_set_token(ts_bar, (BlockDriverState *) 0x5, false);
+    ts_foo = throttle_group_incref("foo");
+
+    tmp = throttle_group_incref("foo");
+    throttle_group_set_token(tmp, (BlockDriverState *) 0x7, true);
+    g_assert(tmp == ts_foo);
+
+    tmp = throttle_group_incref("bar");
+    g_assert(tmp == ts_bar);
+
+    tmp = throttle_group_incref("bar");
+    g_assert(tmp == ts_bar);
+
+    g_assert((int64_t) throttle_group_token(ts_bar, false) == 0x5);
+    g_assert((int64_t) throttle_group_token(ts_foo, true) == 0x7);
+
+    removed = throttle_group_unref(ts_foo);
+    g_assert(removed);
+    removed = throttle_group_unref(ts_bar);
+    g_assert(removed);
+
+    g_assert((int64_t) throttle_group_token(ts_foo, true) == 0x7);
+
+    removed = throttle_group_unref(ts_foo);
+    g_assert(removed);
+    removed = throttle_group_unref(ts_bar);
+    g_assert(removed);
+
+    /* "foo" group should be destroyed when reaching this */
+    removed = throttle_group_unref(ts_foo);
+    g_assert(!removed);
+
+    g_assert((int64_t) throttle_group_token(ts_bar, false) == 0x5);
+
+    removed = throttle_group_unref(ts_bar);
+    g_assert(removed);
+
+    /* "bar" group should be destroyed when reaching this */
+    removed = throttle_group_unref(ts_bar);
+    g_assert(!removed);
+}
+
 int main(int argc, char **argv)
 {
     GSource *src;
@@ -525,6 +575,7 @@ int main(int argc, char **argv)
     g_test_add_func("/throttle/config/is_valid",    test_is_valid);
     g_test_add_func("/throttle/config_functions",   test_config_functions);
     g_test_add_func("/throttle/accounting",         test_accounting);
+    g_test_add_func("/throttle/groups",             test_groups);
     return g_test_run();
 }
 
Signature · 1 line
2.1.0.rc1
#4 lore raw

RFC 4/5 throttle: Prepare to have multiple timers for one ThrottleState

In reply to Benoît Canet

This patch transform the timer_pending call into two boolean values in the ThrottleState structure.

This way we are sure that when multiple timers will be used only one can be armed at a time.

---
 block.c                 |  2 ++
 include/qemu/throttle.h |  3 +++
 util/throttle.c         | 17 ++++++++++++++---
 3 files changed, 19 insertions(+), 3 deletions(-)
block.c +2 -0
index d2ca851..5edb3c3 100644
@@ -165,12 +165,14 @@ void bdrv_io_limits_disable(BlockDriverState *bs)
 static void bdrv_throttle_read_timer_cb(void *opaque)
 {
     BlockDriverState *bs = opaque;
+    throttle_timer_fired(&bs->throttle_state, false);
     qemu_co_enter_next(&bs->throttled_reqs[0]);
 }
 
 static void bdrv_throttle_write_timer_cb(void *opaque)
 {
     BlockDriverState *bs = opaque;
+    throttle_timer_fired(&bs->throttle_state, true);
     qemu_co_enter_next(&bs->throttled_reqs[1]);
 }
 
include/qemu/throttle.h +3 -0
index b89d4d8..3aece3a 100644
@@ -65,6 +65,7 @@ typedef struct ThrottleConfig {
 typedef struct ThrottleState {
     ThrottleConfig cfg;       /* configuration */
     int64_t previous_leak;    /* timestamp of the last leak done */
+    bool any_timer_armed[2];  /* is any timer armed for this throttle state */
 } ThrottleState;
 
 typedef struct ThrottleTimers {
@@ -125,6 +126,8 @@ bool throttle_schedule_timer(ThrottleState *ts,
                              ThrottleTimers *tt,
                              bool is_write);
 
+void throttle_timer_fired(ThrottleState *ts, bool is_write);
+
 void throttle_account(ThrottleState *ts, bool is_write, uint64_t size);
 
 #endif
util/throttle.c +14 -3
index 4d7c8d4..0e305e3 100644
@@ -172,6 +172,7 @@ void throttle_timers_attach_aio_context(ThrottleTimers *tt,
 void throttle_init(ThrottleState *ts)
 {
     memset(ts, 0, sizeof(ThrottleState));
+    ts->any_timer_armed[0] = ts->any_timer_armed[1] = false;
 }
 
 /* To be called first on the ThrottleTimers */
@@ -390,16 +391,26 @@ bool throttle_schedule_timer(ThrottleState *ts,
         return false;
     }
 
-    /* request throttled and timer pending -> do nothing */
-    if (timer_pending(tt->timers[is_write])) {
+    /* request throttled and any timer pending -> do nothing */
+    if (ts->any_timer_armed[is_write]) {
         return true;
     }
 
-    /* request throttled and timer not pending -> arm timer */
+    ts->any_timer_armed[is_write] = true;
     timer_mod(tt->timers[is_write], next_timestamp);
     return true;
 }
 
+/* Remember that now timers are currently armed
+ *
+ * @ts:       the throttle state we are working on
+ * @is_write: the type of operation (read/write)
+ */
+void throttle_timer_fired(ThrottleState *ts, bool is_write)
+{
+    ts->any_timer_armed[is_write] = false;
+}
+
 /* do the accounting for this operation
  *
  * @is_write: the type of operation (read/write)
Signature · 1 line
2.1.0.rc1
#5 lore raw

RFC 5/5 throttle: Add throttle group support.

In reply to Benoît Canet
---
 block.c                   | 157 ++++++++++++++++++++++++++++++++++++++++------
 block/qapi.c              |   2 +-
 blockdev.c                |  16 ++++-
 hmp.c                     |   4 +-
 include/block/block.h     |   2 +-
 include/block/block_int.h |   9 +--
 include/qemu/throttle.h   |   5 +-
 qapi/block-core.json      |   5 +-
 qemu-options.hx           |   1 +
 qmp-commands.hx           |   3 +-
 util/throttle-groups.c    |   2 +-
 util/throttle.c           |  23 +++++--
 12 files changed, 192 insertions(+), 37 deletions(-)
block.c +139 -18
index 5edb3c3..92f9171 100644
@@ -35,6 +35,7 @@
 #include "qmp-commands.h"
 #include "qemu/timer.h"
 #include "qapi-event.h"
+#include "qemu/throttle-groups.h"
 
 #ifdef CONFIG_BSD
 #include <sys/types.h>
@@ -126,7 +127,9 @@ void bdrv_set_io_limits(BlockDriverState *bs,
 {
     int i;
 
-    throttle_config(&bs->throttle_state, &bs->throttle_timers, cfg);
+    throttle_group_lock(bs->throttle_state);
+    throttle_config(bs->throttle_state, &bs->throttle_timers, cfg);
+    throttle_group_unlock(bs->throttle_state);
 
     for (i = 0; i < 2; i++) {
         qemu_co_enter_next(&bs->throttled_reqs[i]);
@@ -153,34 +156,93 @@ static bool bdrv_start_throttled_reqs(BlockDriverState *bs)
     return drained;
 }
 
+static void bdrv_throttle_group_add(BlockDriverState *bs)
+{
+    int i;
+    BlockDriverState *token;
+
+    for (i = 0; i < 2; i++) {
+        /* Get the BlockDriverState having the round robin token */
+        token = throttle_group_token(bs->throttle_state, i);
+
+        /* If the ThrottleGroup is new set the current BlockDriverState as
+         * token
+         */
+        if (!token) {
+            throttle_group_set_token(bs->throttle_state, bs, i);
+        }
+
+    }
+
+    throttle_group_register_bs(bs->throttle_state, bs);
+}
+
+static void bdrv_throttle_group_remove(BlockDriverState *bs)
+{
+    BlockDriverState *token;
+    int i;
+
+    for (i = 0; i < 2; i++) {
+        /* Get the BlockDriverState having the round robin token */
+        token = throttle_group_token(bs->throttle_state, i);
+        /* if this bs is the current token set the next bs as token */
+        if (token == bs) {
+            token = throttle_group_next_bs(token);
+            throttle_group_set_token(bs->throttle_state, token, i);
+        }
+    }
+
+    /* remove the current bs from the list */
+    QLIST_REMOVE(bs, round_robin);
+}
+
 void bdrv_io_limits_disable(BlockDriverState *bs)
 {
-    bs->io_limits_enabled = false;
 
+    throttle_group_lock(bs->throttle_state);
+
+    bs->io_limits_enabled = false;
     bdrv_start_throttled_reqs(bs);
 
+    bdrv_throttle_group_remove(bs);
+
+    /* unlock all throttle group related elements */
+    throttle_group_unlock(bs->throttle_state);
+
+    throttle_group_unref(bs->throttle_state);
+    bs->throttle_state = NULL;
+
     throttle_timers_destroy(&bs->throttle_timers);
 }
 
 static void bdrv_throttle_read_timer_cb(void *opaque)
 {
     BlockDriverState *bs = opaque;
-    throttle_timer_fired(&bs->throttle_state, false);
+    throttle_group_lock(bs->throttle_state);
+    throttle_timer_fired(bs->throttle_state, false);
+    throttle_group_unlock(bs->throttle_state);
     qemu_co_enter_next(&bs->throttled_reqs[0]);
 }
 
 static void bdrv_throttle_write_timer_cb(void *opaque)
 {
     BlockDriverState *bs = opaque;
-    throttle_timer_fired(&bs->throttle_state, true);
+    throttle_group_lock(bs->throttle_state);
+    throttle_timer_fired(bs->throttle_state, true);
+    throttle_group_unlock(bs->throttle_state);
     qemu_co_enter_next(&bs->throttled_reqs[1]);
 }
 
 /* should be called before bdrv_set_io_limits if a limit is set */
-void bdrv_io_limits_enable(BlockDriverState *bs)
+void bdrv_io_limits_enable(BlockDriverState *bs, const char *group)
 {
     assert(!bs->io_limits_enabled);
-    throttle_init(&bs->throttle_state);
+    bs->throttle_state = throttle_group_incref(group ? group: bs->device_name);
+
+    throttle_group_lock(bs->throttle_state);
+
+    bdrv_throttle_group_add(bs);
+
     throttle_timers_init(&bs->throttle_timers,
                          bdrv_get_aio_context(bs),
                          QEMU_CLOCK_VIRTUAL,
@@ -188,6 +250,33 @@ void bdrv_io_limits_enable(BlockDriverState *bs)
                          bdrv_throttle_write_timer_cb,
                          bs);
     bs->io_limits_enabled = true;
+
+    /* unlock all throttle group related elements */
+    throttle_group_unlock(bs->throttle_state);
+}
+
+/* This implement the round robin policy and must be called under ThrottleGroup
+ * lock
+ */
+static BlockDriverState *bdrv_next_throttle_token(BlockDriverState *bs,
+                                                 bool is_write)
+{
+    BlockDriverState *token, *start;
+
+    start = token = throttle_group_token(bs->throttle_state, is_write);
+
+    token = throttle_group_next_bs(token);
+    while (token != start  &&
+           qemu_co_queue_empty(&token->throttled_reqs[is_write])) {
+        token = throttle_group_next_bs(token);
+    }
+
+    if (token == start &&
+        qemu_co_queue_empty(&token->throttled_reqs[is_write])) {
+        token = bs;
+    }
+
+    return token;
 }
 
 /* This function makes an IO wait if needed
@@ -199,29 +288,63 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs,
                                      unsigned int bytes,
                                      bool is_write)
 {
+    bool empty;
+    bool armed;
+    bool token_queue_empty;
+    BlockDriverState *token;
+
+    throttle_group_lock(bs->throttle_state);
+    token = bdrv_next_throttle_token(bs, is_write);
+
     /* does this io must wait */
-    bool must_wait = throttle_schedule_timer(&bs->throttle_state,
-                                             &bs->throttle_timers,
-                                             is_write);
+    bool must_wait = throttle_schedule_timer(bs->throttle_state,
+                                             &token->throttle_timers,
+                                             is_write,
+                                             &armed);
+
+    empty = qemu_co_queue_empty(&bs->throttled_reqs[is_write]);
+
+    if (armed) {
+        throttle_group_set_token(bs->throttle_state, token, is_write);
+    }
+
+    throttle_group_unlock(bs->throttle_state);
 
     /* if must wait or any request of this type throttled queue the IO */
-    if (must_wait ||
-        !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) {
+    if (must_wait || !empty) {
         qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
     }
 
+    throttle_group_lock(bs->throttle_state);
+    token = bdrv_next_throttle_token(bs, is_write);
+    token_queue_empty = qemu_co_queue_empty(&token->throttled_reqs[is_write]);
+
+
     /* the IO will be executed, do the accounting */
-    throttle_account(&bs->throttle_state, is_write, bytes);
+    throttle_account(bs->throttle_state, is_write, bytes);
 
+    must_wait = throttle_schedule_timer(bs->throttle_state,
+                                        &token->throttle_timers,
+                                        is_write,
+                                        &armed);
+
+    if (armed || !token_queue_empty) {
+        throttle_group_set_token(bs->throttle_state, token, is_write);
+    }
 
     /* if the next request must wait -> do nothing */
-    if (throttle_schedule_timer(&bs->throttle_state, &bs->throttle_timers,
-                                is_write)) {
+    if (must_wait) {
+        throttle_group_unlock(bs->throttle_state);
         return;
     }
 
     /* else queue next request for execution */
-    qemu_co_queue_next(&bs->throttled_reqs[is_write]);
+    if (!qemu_co_queue_empty(&bs->throttled_reqs[is_write])) {
+        qemu_co_queue_next(&bs->throttled_reqs[is_write]);
+    } else if(!token_queue_empty) {
+        throttle_arm_timer(&token->throttle_timers, is_write);
+    }
+    throttle_group_unlock(bs->throttle_state);
 }
 
 size_t bdrv_opt_mem_align(BlockDriverState *bs)
@@ -1972,9 +2095,7 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
     bs_dest->enable_write_cache = bs_src->enable_write_cache;
 
     /* i/o throttled req */
-    memcpy(&bs_dest->throttle_state,
-           &bs_src->throttle_state,
-           sizeof(ThrottleState));
+    bs_dest->throttle_state     = bs_src->throttle_state,
     bs_dest->throttled_reqs[0]  = bs_src->throttled_reqs[0];
     bs_dest->throttled_reqs[1]  = bs_src->throttled_reqs[1];
     bs_dest->io_limits_enabled  = bs_src->io_limits_enabled;
block/qapi.c +1 -1
index f44f6b4..9ea11a0 100644
@@ -54,7 +54,7 @@ BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs)
 
     if (bs->io_limits_enabled) {
         ThrottleConfig cfg;
-        throttle_get_config(&bs->throttle_state, &cfg);
+        throttle_get_config(bs->throttle_state, &cfg);
         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
blockdev.c +13 -3
index 48bd9a3..67e2d29 100644
@@ -330,6 +330,7 @@ static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
     bool has_driver_specific_opts;
     BlockdevDetectZeroesOptions detect_zeroes;
     BlockDriver *drv = NULL;
+    const char *throttling_group;
 
     /* Check common options by copying from bs_opts to opts, all other options
      * stay in bs_opts for processing by bdrv_open(). */
@@ -432,6 +433,8 @@ static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
 
     cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
 
+    throttling_group = qemu_opt_get(opts, "throttling.group");
+
     if (!check_throttle_config(&cfg, &error)) {
         error_propagate(errp, error);
         goto early_err;
@@ -490,7 +493,7 @@ static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
 
     /* disk I/O throttling */
     if (throttle_enabled(&cfg)) {
-        bdrv_io_limits_enable(dinfo->bdrv);
+        bdrv_io_limits_enable(dinfo->bdrv, throttling_group);
         bdrv_set_io_limits(dinfo->bdrv, &cfg);
     }
 
@@ -679,6 +682,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
 
     qemu_opt_rename(all_opts,
                     "iops_size", "throttling.iops-size");
+    qemu_opt_rename(all_opts, "group", "throttling.group");
 
     qemu_opt_rename(all_opts, "readonly", "read-only");
 
@@ -1689,7 +1693,9 @@ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
                                bool has_iops_wr_max,
                                int64_t iops_wr_max,
                                bool has_iops_size,
-                               int64_t iops_size, Error **errp)
+                               int64_t iops_size,
+                               bool has_group,
+                               const char *group, Error **errp)
 {
     ThrottleConfig cfg;
     BlockDriverState *bs;
@@ -1741,7 +1747,7 @@ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
     aio_context_acquire(aio_context);
 
     if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
-        bdrv_io_limits_enable(bs);
+        bdrv_io_limits_enable(bs, has_group ? group : NULL);
     } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
         bdrv_io_limits_disable(bs);
     }
@@ -2643,6 +2649,10 @@ QemuOptsList qemu_common_drive_opts = {
             .type = QEMU_OPT_NUMBER,
             .help = "when limiting by iops max size of an I/O in bytes",
         },{
+            .name = "throttling.group",
+            .type = QEMU_OPT_STRING,
+            .help = "name of the block throttling group",
+        },{
             .name = "copy-on-read",
             .type = QEMU_OPT_BOOL,
             .help = "copy read data from backing file into image file",
hmp.c +3 -1
index 4d1838e..c580b0e 100644
@@ -1165,7 +1165,9 @@ void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
                               false,
                               0,
                               false, /* No default I/O size */
-                              0, &err);
+                              0,
+                              false,
+                              NULL, &err);
     hmp_handle_error(mon, &err);
 }
 
include/block/block.h +1 -1
index f08471d..761006d 100644
@@ -190,7 +190,7 @@ void bdrv_stats_print(Monitor *mon, const QObject *data);
 void bdrv_info_stats(Monitor *mon, QObject **ret_data);
 
 /* disk I/O throttling */
-void bdrv_io_limits_enable(BlockDriverState *bs);
+void bdrv_io_limits_enable(BlockDriverState *bs, const char *group);
 void bdrv_io_limits_disable(BlockDriverState *bs);
 
 void bdrv_init(void);
include/block/block_int.h +5 -4
index 6066f63..fbf5d2e 100644
@@ -334,12 +334,13 @@ struct BlockDriverState {
     /* number of in-flight serialising requests */
     unsigned int serialising_in_flight;
 
-    /* I/O throttling */
-    ThrottleState throttle_state;
-    ThrottleTimers throttle_timers; 
-    CoQueue      throttled_reqs[2];
+    /* I/O throttling - following elements protected by ThrottleGroup lock */
+    ThrottleState *throttle_state;
     bool         io_limits_enabled;
+    CoQueue      throttled_reqs[2];
     QLIST_ENTRY(BlockDriverState) round_robin;
+    /* timers have their own locking */
+    ThrottleTimers throttle_timers;
 
     /* I/O stats (display with "info blockstats"). */
     uint64_t nr_bytes[BDRV_MAX_IOTYPE];
include/qemu/throttle.h +4 -1
index 3aece3a..5f5dde2 100644
@@ -124,7 +124,10 @@ void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg);
 /* usage */
 bool throttle_schedule_timer(ThrottleState *ts,
                              ThrottleTimers *tt,
-                             bool is_write);
+                             bool is_write,
+                             bool *armed);
+
+void throttle_arm_timer(ThrottleTimers *tt, bool is_write);
 
 void throttle_timer_fired(ThrottleState *ts, bool is_write);
 
qapi/block-core.json +4 -1
index e378653..aa307a2 100644
@@ -886,6 +886,9 @@
 #
 # @iops_size: #optional an I/O size in bytes (Since 1.7)
 #
+#
+# @group: #optional throttle group name (Since 2.2)
+#
 # Returns: Nothing on success
 #          If @device is not a valid block device, DeviceNotFound
 #
@@ -897,7 +900,7 @@
             '*bps_max': 'int', '*bps_rd_max': 'int',
             '*bps_wr_max': 'int', '*iops_max': 'int',
             '*iops_rd_max': 'int', '*iops_wr_max': 'int',
-            '*iops_size': 'int' } }
+            '*iops_size': 'int', '*group': 'str' } }
 
 ##
 # @block-stream:
qemu-options.hx +1 -0
index 1549625..f1ca6aa 100644
@@ -433,6 +433,7 @@ DEF("drive", HAS_ARG, QEMU_OPTION_drive,
     "       [[,bps_max=bm]|[[,bps_rd_max=rm][,bps_wr_max=wm]]]\n"
     "       [[,iops_max=im]|[[,iops_rd_max=irm][,iops_wr_max=iwm]]]\n"
     "       [[,iops_size=is]]\n"
+    "       [[,group=g]]\n"
     "                use 'file' as a drive image\n", QEMU_ARCH_ALL)
 STEXI
 @item -drive @var{option}[,@var{option}[,@var{option}[,...]]]
qmp-commands.hx +2 -1
index 4be4765..2f25ac7 100644
@@ -1663,7 +1663,7 @@ EQMP
 
     {
         .name       = "block_set_io_throttle",
-        .args_type  = "device:B,bps:l,bps_rd:l,bps_wr:l,iops:l,iops_rd:l,iops_wr:l,bps_max:l?,bps_rd_max:l?,bps_wr_max:l?,iops_max:l?,iops_rd_max:l?,iops_wr_max:l?,iops_size:l?",
+        .args_type  = "device:B,bps:l,bps_rd:l,bps_wr:l,iops:l,iops_rd:l,iops_wr:l,bps_max:l?,bps_rd_max:l?,bps_wr_max:l?,iops_max:l?,iops_rd_max:l?,iops_wr_max:l?,iops_size:l?,group:s?",
         .mhandler.cmd_new = qmp_marshal_input_block_set_io_throttle,
     },
 
@@ -1689,6 +1689,7 @@ Arguments:
 - "iops_rd_max":  read I/O operations max (json-int)
 - "iops_wr_max":  write I/O operations max (json-int)
 - "iops_size":  I/O size in bytes when limiting (json-int)
+- "group": throttle group name (json-string)
 
 Example:
 
util/throttle-groups.c +1 -1
index 153bacc..20baca4 100644
@@ -137,7 +137,7 @@ void throttle_group_register_bs(ThrottleState *ts, BlockDriverState *bs)
  */
 BlockDriverState *throttle_group_next_bs(BlockDriverState *bs)
 {
-    ThrottleState *ts = &bs->throttle_state;
+    ThrottleState *ts = bs->throttle_state;
     ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
     BlockDriverState *next = QLIST_NEXT(bs, round_robin);
 
util/throttle.c +18 -5
index 0e305e3..1ee52eb 100644
@@ -375,11 +375,13 @@ void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg)
  */
 bool throttle_schedule_timer(ThrottleState *ts,
                              ThrottleTimers *tt,
-                             bool is_write)
+                             bool is_write,
+                             bool *armed)
 {
     int64_t now = qemu_clock_get_ns(tt->clock_type);
     int64_t next_timestamp;
     bool must_wait;
+    *armed = false;
 
     must_wait = throttle_compute_timer(ts,
                                        is_write,
@@ -392,15 +394,26 @@ bool throttle_schedule_timer(ThrottleState *ts,
     }
 
     /* request throttled and any timer pending -> do nothing */
-    if (ts->any_timer_armed[is_write]) {
-        return true;
+    if (!ts->any_timer_armed[is_write]) {
+        *armed = true;
+        ts->any_timer_armed[is_write] = true;
+        timer_mod(tt->timers[is_write], next_timestamp);
     }
 
-    ts->any_timer_armed[is_write] = true;
-    timer_mod(tt->timers[is_write], next_timestamp);
     return true;
 }
 
+/* Schedule a throttle timer like a BH
+ *
+ * @tt:       The timers structure
+ * @is_write: the type of operation (read/write)
+ */
+void throttle_arm_timer(ThrottleTimers *tt, bool is_write)
+{
+    int64_t now = qemu_clock_get_ns(tt->clock_type);
+    timer_mod(tt->timers[is_write], now + 1);
+}
+
 /* Remember that now timers are currently armed
  *
  * @ts:       the throttle state we are working on
Signature · 1 line
2.1.0.rc1