2013-09-02 14:14:37 +02:00
|
|
|
/*
|
|
|
|
* QEMU throttling infrastructure
|
|
|
|
*
|
2015-06-08 18:17:47 +02:00
|
|
|
* Copyright (C) Nodalink, EURL. 2013-2014
|
2016-02-18 11:27:01 +01:00
|
|
|
* Copyright (C) Igalia, S.L. 2015-2016
|
2013-09-02 14:14:37 +02:00
|
|
|
*
|
2015-06-08 18:17:47 +02:00
|
|
|
* Authors:
|
|
|
|
* Benoît Canet <benoit.canet@nodalink.com>
|
|
|
|
* Alberto Garcia <berto@igalia.com>
|
2013-09-02 14:14:37 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 or
|
|
|
|
* (at your option) version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef THROTTLE_H
|
|
|
|
#define THROTTLE_H
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2018-02-11 10:36:01 +01:00
|
|
|
#include "qapi/qapi-types-block-core.h"
|
2013-09-02 14:14:37 +02:00
|
|
|
#include "qemu/timer.h"
|
|
|
|
|
2016-01-20 05:21:20 +01:00
|
|
|
#define THROTTLE_VALUE_MAX 1000000000000000LL
|
|
|
|
|
2013-09-02 14:14:37 +02:00
|
|
|
typedef enum {
|
|
|
|
THROTTLE_BPS_TOTAL,
|
|
|
|
THROTTLE_BPS_READ,
|
|
|
|
THROTTLE_BPS_WRITE,
|
|
|
|
THROTTLE_OPS_TOTAL,
|
|
|
|
THROTTLE_OPS_READ,
|
|
|
|
THROTTLE_OPS_WRITE,
|
|
|
|
BUCKETS_COUNT,
|
|
|
|
} BucketType;
|
|
|
|
|
|
|
|
/*
|
2016-02-18 11:27:01 +01:00
|
|
|
* This module implements I/O limits using the leaky bucket
|
|
|
|
* algorithm. The code is independent of the I/O units, but it is
|
|
|
|
* currently used for bytes per second and operations per second.
|
|
|
|
*
|
|
|
|
* Three parameters can be set by the user:
|
|
|
|
*
|
|
|
|
* - avg: the desired I/O limits in units per second.
|
|
|
|
* - max: the limit during bursts, also in units per second.
|
|
|
|
* - burst_length: the maximum length of the burst period, in seconds.
|
|
|
|
*
|
|
|
|
* Here's how it works:
|
|
|
|
*
|
|
|
|
* - The bucket level (number of performed I/O units) is kept in
|
|
|
|
* bkt.level and leaks at a rate of bkt.avg units per second.
|
|
|
|
*
|
|
|
|
* - The size of the bucket is bkt.max * bkt.burst_length. Once the
|
|
|
|
* bucket is full no more I/O is performed until the bucket leaks
|
|
|
|
* again. This is what makes the I/O rate bkt.avg.
|
|
|
|
*
|
|
|
|
* - The bkt.avg rate does not apply until the bucket is full,
|
|
|
|
* allowing the user to do bursts until then. The I/O limit during
|
|
|
|
* bursts is bkt.max. To enforce this limit we keep an additional
|
2017-08-24 15:24:43 +02:00
|
|
|
* bucket in bkt.burst_level that leaks at a rate of bkt.max units
|
2016-02-18 11:27:01 +01:00
|
|
|
* per second.
|
|
|
|
*
|
|
|
|
* - Because of all of the above, the user can perform I/O at a
|
|
|
|
* maximum of bkt.max units per second for at most bkt.burst_length
|
|
|
|
* seconds in a row. After that the bucket will be full and the I/O
|
|
|
|
* rate will go down to bkt.avg.
|
|
|
|
*
|
|
|
|
* - Since the bucket always leaks at a rate of bkt.avg, this also
|
|
|
|
* determines how much the user needs to wait before being able to
|
|
|
|
* do bursts again.
|
2013-09-02 14:14:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct LeakyBucket {
|
2017-08-24 15:24:47 +02:00
|
|
|
uint64_t avg; /* average goal in units per second */
|
|
|
|
uint64_t max; /* leaky bucket max burst in units */
|
2013-09-02 14:14:37 +02:00
|
|
|
double level; /* bucket level in units */
|
2016-02-18 11:27:01 +01:00
|
|
|
double burst_level; /* bucket level in units (for computing bursts) */
|
2017-08-24 15:24:48 +02:00
|
|
|
uint64_t burst_length; /* max length of the burst period, in seconds */
|
2013-09-02 14:14:37 +02:00
|
|
|
} LeakyBucket;
|
|
|
|
|
|
|
|
/* The following structure is used to configure a ThrottleState
|
|
|
|
* It contains a bit of state: the bucket field of the LeakyBucket structure.
|
|
|
|
* However it allows to keep the code clean and the bucket field is reset to
|
|
|
|
* zero at the right time.
|
|
|
|
*/
|
|
|
|
typedef struct ThrottleConfig {
|
|
|
|
LeakyBucket buckets[BUCKETS_COUNT]; /* leaky buckets */
|
|
|
|
uint64_t op_size; /* size of an operation in bytes */
|
|
|
|
} ThrottleConfig;
|
|
|
|
|
|
|
|
typedef struct ThrottleState {
|
|
|
|
ThrottleConfig cfg; /* configuration */
|
|
|
|
int64_t previous_leak; /* timestamp of the last leak done */
|
2015-06-08 18:17:41 +02:00
|
|
|
} ThrottleState;
|
|
|
|
|
|
|
|
typedef struct ThrottleTimers {
|
|
|
|
QEMUTimer *timers[2]; /* timers used to do the throttling */
|
2013-09-02 14:14:37 +02:00
|
|
|
QEMUClockType clock_type; /* the clock used */
|
2014-05-14 16:22:45 +02:00
|
|
|
|
|
|
|
/* Callbacks */
|
|
|
|
QEMUTimerCB *read_timer_cb;
|
|
|
|
QEMUTimerCB *write_timer_cb;
|
|
|
|
void *timer_opaque;
|
2015-06-08 18:17:41 +02:00
|
|
|
} ThrottleTimers;
|
2013-09-02 14:14:37 +02:00
|
|
|
|
|
|
|
/* operations on single leaky buckets */
|
|
|
|
void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta);
|
|
|
|
|
|
|
|
int64_t throttle_compute_wait(LeakyBucket *bkt);
|
|
|
|
|
|
|
|
/* init/destroy cycle */
|
2015-06-08 18:17:41 +02:00
|
|
|
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);
|
2013-09-02 14:14:37 +02:00
|
|
|
|
2015-06-08 18:17:41 +02:00
|
|
|
void throttle_timers_destroy(ThrottleTimers *tt);
|
2013-09-02 14:14:37 +02:00
|
|
|
|
2015-06-08 18:17:41 +02:00
|
|
|
void throttle_timers_detach_aio_context(ThrottleTimers *tt);
|
2014-05-14 16:22:45 +02:00
|
|
|
|
2015-06-08 18:17:41 +02:00
|
|
|
void throttle_timers_attach_aio_context(ThrottleTimers *tt,
|
|
|
|
AioContext *new_context);
|
2014-05-14 16:22:45 +02:00
|
|
|
|
2015-06-08 18:17:41 +02:00
|
|
|
bool throttle_timers_are_initialized(ThrottleTimers *tt);
|
2013-09-02 14:14:37 +02:00
|
|
|
|
|
|
|
/* configuration */
|
|
|
|
bool throttle_enabled(ThrottleConfig *cfg);
|
|
|
|
|
2016-02-18 11:26:57 +01:00
|
|
|
bool throttle_is_valid(ThrottleConfig *cfg, Error **errp);
|
2013-09-02 14:14:37 +02:00
|
|
|
|
2015-06-08 18:17:41 +02:00
|
|
|
void throttle_config(ThrottleState *ts,
|
2017-07-02 12:06:45 +02:00
|
|
|
QEMUClockType clock_type,
|
2015-06-08 18:17:41 +02:00
|
|
|
ThrottleConfig *cfg);
|
2013-09-02 14:14:37 +02:00
|
|
|
|
|
|
|
void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg);
|
|
|
|
|
2016-02-18 11:27:00 +01:00
|
|
|
void throttle_config_init(ThrottleConfig *cfg);
|
|
|
|
|
2013-09-02 14:14:37 +02:00
|
|
|
/* usage */
|
2015-06-08 18:17:41 +02:00
|
|
|
bool throttle_schedule_timer(ThrottleState *ts,
|
|
|
|
ThrottleTimers *tt,
|
|
|
|
bool is_write);
|
2013-09-02 14:14:37 +02:00
|
|
|
|
|
|
|
void throttle_account(ThrottleState *ts, bool is_write, uint64_t size);
|
block: convert ThrottleGroup to object with QOM
ThrottleGroup is converted to an object. This will allow the future
throttle block filter drive easy creation and configuration of throttle
groups in QMP and cli.
A new QAPI struct, ThrottleLimits, is introduced to provide a shared
struct for all throttle configuration needs in QMP.
ThrottleGroups can be created via CLI as
-object throttle-group,id=foo,x-iops-total=100,x-..
where x-* are individual limit properties. Since we can't add non-scalar
properties in -object this interface must be used instead. However,
setting these properties must be disabled after initialization because
certain combinations of limits are forbidden and thus configuration
changes should be done in one transaction. The individual properties
will go away when support for non-scalar values in CLI is implemented
and thus are marked as experimental.
ThrottleGroup also has a `limits` property that uses the ThrottleLimits
struct. It can be used to create ThrottleGroups or set the
configuration in existing groups as follows:
{ "execute": "object-add",
"arguments": {
"qom-type": "throttle-group",
"id": "foo",
"props" : {
"limits": {
"iops-total": 100
}
}
}
}
{ "execute" : "qom-set",
"arguments" : {
"path" : "foo",
"property" : "limits",
"value" : {
"iops-total" : 99
}
}
}
This also means a group's configuration can be fetched with qom-get.
Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-08-25 15:20:26 +02:00
|
|
|
void throttle_limits_to_config(ThrottleLimits *arg, ThrottleConfig *cfg,
|
|
|
|
Error **errp);
|
|
|
|
void throttle_config_to_limits(ThrottleConfig *cfg, ThrottleLimits *var);
|
2013-09-02 14:14:37 +02:00
|
|
|
|
|
|
|
#endif
|