Skip to content

Commit

Permalink
throttle: Use throttle_config_init() to initialize ThrottleConfig
Browse files Browse the repository at this point in the history
We can currently initialize ThrottleConfig by zeroing all its fields,
but this will change with the new fields to define the length of the
burst periods.

This patch introduces a new throttle_config_init() function and uses it
to replace all memset() calls that initialize ThrottleConfig directly.

Signed-off-by: Alberto Garcia <berto@igalia.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
bertogg authored and kevmw committed Feb 22, 2016
1 parent d585108 commit 1588ab5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions blockdev.c
Expand Up @@ -387,7 +387,7 @@ static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
}

if (throttle_cfg) {
memset(throttle_cfg, 0, sizeof(*throttle_cfg));
throttle_config_init(throttle_cfg);
throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
qemu_opt_get_number(opts, "throttling.bps-total", 0);
throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
Expand Down Expand Up @@ -2611,7 +2611,7 @@ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
goto out;
}

memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
Expand Down
2 changes: 2 additions & 0 deletions include/qemu/throttle.h
Expand Up @@ -114,6 +114,8 @@ void throttle_config(ThrottleState *ts,

void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg);

void throttle_config_init(ThrottleConfig *cfg);

/* usage */
bool throttle_schedule_timer(ThrottleState *ts,
ThrottleTimers *tt,
Expand Down
28 changes: 17 additions & 11 deletions tests/test-throttle.c
Expand Up @@ -35,6 +35,9 @@ static bool double_cmp(double x, double y)
/* tests for single bucket operations */
static void test_leak_bucket(void)
{
throttle_config_init(&cfg);
bkt = cfg.buckets[THROTTLE_BPS_TOTAL];

/* set initial value */
bkt.avg = 150;
bkt.max = 15;
Expand Down Expand Up @@ -64,6 +67,9 @@ static void test_compute_wait(void)
int64_t wait;
int64_t result;

throttle_config_init(&cfg);
bkt = cfg.buckets[THROTTLE_BPS_TOTAL];

/* no operation limit set */
bkt.avg = 0;
bkt.max = 15;
Expand Down Expand Up @@ -233,17 +239,17 @@ static void test_enabled(void)
{
int i;

memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
g_assert(!throttle_enabled(&cfg));

for (i = 0; i < BUCKETS_COUNT; i++) {
memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
set_cfg_value(false, i, 150);
g_assert(throttle_enabled(&cfg));
}

for (i = 0; i < BUCKETS_COUNT; i++) {
memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
set_cfg_value(false, i, -150);
g_assert(!throttle_enabled(&cfg));
}
Expand All @@ -256,29 +262,29 @@ static void test_conflicts_for_one_set(bool is_max,
int read,
int write)
{
memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
g_assert(throttle_is_valid(&cfg, NULL));

set_cfg_value(is_max, total, 1);
set_cfg_value(is_max, read, 1);
g_assert(!throttle_is_valid(&cfg, NULL));

memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
set_cfg_value(is_max, total, 1);
set_cfg_value(is_max, write, 1);
g_assert(!throttle_is_valid(&cfg, NULL));

memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
set_cfg_value(is_max, total, 1);
set_cfg_value(is_max, read, 1);
set_cfg_value(is_max, write, 1);
g_assert(!throttle_is_valid(&cfg, NULL));

memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
set_cfg_value(is_max, total, 1);
g_assert(throttle_is_valid(&cfg, NULL));

memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
set_cfg_value(is_max, read, 1);
set_cfg_value(is_max, write, 1);
g_assert(throttle_is_valid(&cfg, NULL));
Expand Down Expand Up @@ -315,7 +321,7 @@ static void test_is_valid_for_value(int value, bool should_be_valid)
int is_max, index;
for (is_max = 0; is_max < 2; is_max++) {
for (index = 0; index < BUCKETS_COUNT; index++) {
memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
set_cfg_value(is_max, index, value);
g_assert(throttle_is_valid(&cfg, NULL) == should_be_valid);
}
Expand All @@ -337,7 +343,7 @@ static void test_max_is_missing_limit(void)
int i;

for (i = 0; i < BUCKETS_COUNT; i++) {
memset(&cfg, 0, sizeof(cfg));
throttle_config_init(&cfg);
cfg.buckets[i].max = 100;
cfg.buckets[i].avg = 0;
g_assert(!throttle_is_valid(&cfg, NULL));
Expand Down Expand Up @@ -552,7 +558,7 @@ static void test_groups(void)
g_assert(bdrv1->throttle_state == bdrv3->throttle_state);

/* Setting the config of a group member affects the whole group */
memset(&cfg1, 0, sizeof(cfg1));
throttle_config_init(&cfg1);
cfg1.buckets[THROTTLE_BPS_READ].avg = 500000;
cfg1.buckets[THROTTLE_BPS_WRITE].avg = 285000;
cfg1.buckets[THROTTLE_OPS_READ].avg = 20000;
Expand Down
10 changes: 10 additions & 0 deletions util/throttle.c
Expand Up @@ -171,10 +171,20 @@ void throttle_timers_attach_aio_context(ThrottleTimers *tt,
tt->write_timer_cb, tt->timer_opaque);
}

/*
* Initialize the ThrottleConfig structure to a valid state
* @cfg: the config to initialize
*/
void throttle_config_init(ThrottleConfig *cfg)
{
memset(cfg, 0, sizeof(*cfg));
}

/* To be called first on the ThrottleState */
void throttle_init(ThrottleState *ts)
{
memset(ts, 0, sizeof(ThrottleState));
throttle_config_init(&ts->cfg);
}

/* To be called first on the ThrottleTimers */
Expand Down

0 comments on commit 1588ab5

Please sign in to comment.