2013-09-02 12:14:37 +00:00
|
|
|
/*
|
|
|
|
* QEMU throttling infrastructure
|
|
|
|
*
|
2015-06-08 16:17:47 +00:00
|
|
|
* Copyright (C) Nodalink, EURL. 2013-2014
|
|
|
|
* Copyright (C) Igalia, S.L. 2015
|
2013-09-02 12:14:37 +00:00
|
|
|
*
|
2015-06-08 16:17:47 +00:00
|
|
|
* Authors:
|
|
|
|
* Benoît Canet <benoit.canet@nodalink.com>
|
|
|
|
* Alberto Garcia <berto@igalia.com>
|
2013-09-02 12:14:37 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 17:49:55 +00:00
|
|
|
#include "qemu/osdep.h"
|
2013-09-02 12:14:37 +00:00
|
|
|
#include "qemu/throttle.h"
|
|
|
|
#include "qemu/timer.h"
|
2014-05-14 14:22:45 +00:00
|
|
|
#include "block/aio.h"
|
2013-09-02 12:14:37 +00:00
|
|
|
|
|
|
|
/* This function make a bucket leak
|
|
|
|
*
|
|
|
|
* @bkt: the bucket to make leak
|
|
|
|
* @delta_ns: the time delta
|
|
|
|
*/
|
|
|
|
void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta_ns)
|
|
|
|
{
|
|
|
|
double leak;
|
|
|
|
|
|
|
|
/* compute how much to leak */
|
2015-07-08 14:10:09 +00:00
|
|
|
leak = (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND;
|
2013-09-02 12:14:37 +00:00
|
|
|
|
|
|
|
/* make the bucket leak */
|
|
|
|
bkt->level = MAX(bkt->level - leak, 0);
|
2016-02-18 10:27:01 +00:00
|
|
|
|
|
|
|
/* if we allow bursts for more than one second we also need to
|
|
|
|
* keep track of bkt->burst_level so the bkt->max goal per second
|
|
|
|
* is attained */
|
|
|
|
if (bkt->burst_length > 1) {
|
|
|
|
leak = (bkt->max * (double) delta_ns) / NANOSECONDS_PER_SECOND;
|
|
|
|
bkt->burst_level = MAX(bkt->burst_level - leak, 0);
|
|
|
|
}
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the time delta since last leak and make proportionals leaks
|
|
|
|
*
|
|
|
|
* @now: the current timestamp in ns
|
|
|
|
*/
|
|
|
|
static void throttle_do_leak(ThrottleState *ts, int64_t now)
|
|
|
|
{
|
|
|
|
/* compute the time elapsed since the last leak */
|
|
|
|
int64_t delta_ns = now - ts->previous_leak;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ts->previous_leak = now;
|
|
|
|
|
|
|
|
if (delta_ns <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make each bucket leak */
|
|
|
|
for (i = 0; i < BUCKETS_COUNT; i++) {
|
|
|
|
throttle_leak_bucket(&ts->cfg.buckets[i], delta_ns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the real job of computing the time to wait
|
|
|
|
*
|
|
|
|
* @limit: the throttling limit
|
|
|
|
* @extra: the number of operation to delay
|
|
|
|
* @ret: the time to wait in ns
|
|
|
|
*/
|
|
|
|
static int64_t throttle_do_compute_wait(double limit, double extra)
|
|
|
|
{
|
2015-07-08 14:10:09 +00:00
|
|
|
double wait = extra * NANOSECONDS_PER_SECOND;
|
2013-09-02 12:14:37 +00:00
|
|
|
wait /= limit;
|
|
|
|
return wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function compute the wait time in ns that a leaky bucket should trigger
|
|
|
|
*
|
|
|
|
* @bkt: the leaky bucket we operate on
|
|
|
|
* @ret: the resulting wait time in ns or 0 if the operation can go through
|
|
|
|
*/
|
|
|
|
int64_t throttle_compute_wait(LeakyBucket *bkt)
|
|
|
|
{
|
|
|
|
double extra; /* the number of extra units blocking the io */
|
|
|
|
|
|
|
|
if (!bkt->avg) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-18 10:27:01 +00:00
|
|
|
/* If the bucket is full then we have to wait */
|
|
|
|
extra = bkt->level - bkt->max * bkt->burst_length;
|
|
|
|
if (extra > 0) {
|
|
|
|
return throttle_do_compute_wait(bkt->avg, extra);
|
|
|
|
}
|
2013-09-02 12:14:37 +00:00
|
|
|
|
2016-02-18 10:27:01 +00:00
|
|
|
/* If the bucket is not full yet we have to make sure that we
|
|
|
|
* fulfill the goal of bkt->max units per second. */
|
|
|
|
if (bkt->burst_length > 1) {
|
|
|
|
/* We use 1/10 of the max value to smooth the throttling.
|
|
|
|
* See throttle_fix_bucket() for more details. */
|
|
|
|
extra = bkt->burst_level - bkt->max / 10;
|
|
|
|
if (extra > 0) {
|
|
|
|
return throttle_do_compute_wait(bkt->max, extra);
|
|
|
|
}
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 10:27:01 +00:00
|
|
|
return 0;
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function compute the time that must be waited while this IO
|
|
|
|
*
|
|
|
|
* @is_write: true if the current IO is a write, false if it's a read
|
|
|
|
* @ret: time to wait
|
|
|
|
*/
|
|
|
|
static int64_t throttle_compute_wait_for(ThrottleState *ts,
|
|
|
|
bool is_write)
|
|
|
|
{
|
|
|
|
BucketType to_check[2][4] = { {THROTTLE_BPS_TOTAL,
|
|
|
|
THROTTLE_OPS_TOTAL,
|
|
|
|
THROTTLE_BPS_READ,
|
|
|
|
THROTTLE_OPS_READ},
|
|
|
|
{THROTTLE_BPS_TOTAL,
|
|
|
|
THROTTLE_OPS_TOTAL,
|
|
|
|
THROTTLE_BPS_WRITE,
|
|
|
|
THROTTLE_OPS_WRITE}, };
|
|
|
|
int64_t wait, max_wait = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
BucketType index = to_check[is_write][i];
|
|
|
|
wait = throttle_compute_wait(&ts->cfg.buckets[index]);
|
|
|
|
if (wait > max_wait) {
|
|
|
|
max_wait = wait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute the timer for this type of operation
|
|
|
|
*
|
|
|
|
* @is_write: the type of operation
|
|
|
|
* @now: the current clock timestamp
|
|
|
|
* @next_timestamp: the resulting timer
|
|
|
|
* @ret: true if a timer must be set
|
|
|
|
*/
|
2016-02-18 10:26:54 +00:00
|
|
|
static bool throttle_compute_timer(ThrottleState *ts,
|
|
|
|
bool is_write,
|
|
|
|
int64_t now,
|
|
|
|
int64_t *next_timestamp)
|
2013-09-02 12:14:37 +00:00
|
|
|
{
|
|
|
|
int64_t wait;
|
|
|
|
|
|
|
|
/* leak proportionally to the time elapsed */
|
|
|
|
throttle_do_leak(ts, now);
|
|
|
|
|
|
|
|
/* compute the wait time if any */
|
|
|
|
wait = throttle_compute_wait_for(ts, is_write);
|
|
|
|
|
|
|
|
/* if the code must wait compute when the next timer should fire */
|
|
|
|
if (wait) {
|
|
|
|
*next_timestamp = now + wait;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* else no need to wait at all */
|
|
|
|
*next_timestamp = now;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-14 14:22:45 +00:00
|
|
|
/* Add timers to event loop */
|
2015-06-08 16:17:41 +00:00
|
|
|
void throttle_timers_attach_aio_context(ThrottleTimers *tt,
|
|
|
|
AioContext *new_context)
|
2014-05-14 14:22:45 +00:00
|
|
|
{
|
2015-06-08 16:17:41 +00:00
|
|
|
tt->timers[0] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
|
|
|
|
tt->read_timer_cb, tt->timer_opaque);
|
|
|
|
tt->timers[1] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
|
|
|
|
tt->write_timer_cb, tt->timer_opaque);
|
2014-05-14 14:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 10:27:00 +00:00
|
|
|
/*
|
|
|
|
* Initialize the ThrottleConfig structure to a valid state
|
|
|
|
* @cfg: the config to initialize
|
|
|
|
*/
|
|
|
|
void throttle_config_init(ThrottleConfig *cfg)
|
|
|
|
{
|
2016-02-18 10:27:01 +00:00
|
|
|
unsigned i;
|
2016-02-18 10:27:00 +00:00
|
|
|
memset(cfg, 0, sizeof(*cfg));
|
2016-02-18 10:27:01 +00:00
|
|
|
for (i = 0; i < BUCKETS_COUNT; i++) {
|
|
|
|
cfg->buckets[i].burst_length = 1;
|
|
|
|
}
|
2016-02-18 10:27:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 12:14:37 +00:00
|
|
|
/* To be called first on the ThrottleState */
|
2015-06-08 16:17:41 +00:00
|
|
|
void throttle_init(ThrottleState *ts)
|
2013-09-02 12:14:37 +00:00
|
|
|
{
|
|
|
|
memset(ts, 0, sizeof(ThrottleState));
|
2016-02-18 10:27:00 +00:00
|
|
|
throttle_config_init(&ts->cfg);
|
2015-06-08 16:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* To be called first on the ThrottleTimers */
|
|
|
|
void throttle_timers_init(ThrottleTimers *tt,
|
|
|
|
AioContext *aio_context,
|
|
|
|
QEMUClockType clock_type,
|
|
|
|
QEMUTimerCB *read_timer_cb,
|
|
|
|
QEMUTimerCB *write_timer_cb,
|
|
|
|
void *timer_opaque)
|
|
|
|
{
|
|
|
|
memset(tt, 0, sizeof(ThrottleTimers));
|
2013-09-02 12:14:37 +00:00
|
|
|
|
2015-06-08 16:17:41 +00:00
|
|
|
tt->clock_type = clock_type;
|
|
|
|
tt->read_timer_cb = read_timer_cb;
|
|
|
|
tt->write_timer_cb = write_timer_cb;
|
|
|
|
tt->timer_opaque = timer_opaque;
|
|
|
|
throttle_timers_attach_aio_context(tt, aio_context);
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy a timer */
|
|
|
|
static void throttle_timer_destroy(QEMUTimer **timer)
|
|
|
|
{
|
|
|
|
assert(*timer != NULL);
|
|
|
|
|
|
|
|
timer_del(*timer);
|
|
|
|
timer_free(*timer);
|
|
|
|
*timer = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-14 14:22:45 +00:00
|
|
|
/* Remove timers from event loop */
|
2015-06-08 16:17:41 +00:00
|
|
|
void throttle_timers_detach_aio_context(ThrottleTimers *tt)
|
2013-09-02 12:14:37 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
2015-06-08 16:17:41 +00:00
|
|
|
throttle_timer_destroy(&tt->timers[i]);
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 16:17:41 +00:00
|
|
|
/* To be called last on the ThrottleTimers */
|
|
|
|
void throttle_timers_destroy(ThrottleTimers *tt)
|
2014-05-14 14:22:45 +00:00
|
|
|
{
|
2015-06-08 16:17:41 +00:00
|
|
|
throttle_timers_detach_aio_context(tt);
|
2014-05-14 14:22:45 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 12:14:37 +00:00
|
|
|
/* is any throttling timer configured */
|
2015-06-08 16:17:41 +00:00
|
|
|
bool throttle_timers_are_initialized(ThrottleTimers *tt)
|
2013-09-02 12:14:37 +00:00
|
|
|
{
|
2015-06-08 16:17:41 +00:00
|
|
|
if (tt->timers[0]) {
|
2013-09-02 12:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does any throttling must be done
|
|
|
|
*
|
|
|
|
* @cfg: the throttling configuration to inspect
|
|
|
|
* @ret: true if throttling must be done else false
|
|
|
|
*/
|
|
|
|
bool throttle_enabled(ThrottleConfig *cfg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < BUCKETS_COUNT; i++) {
|
|
|
|
if (cfg->buckets[i].avg > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-18 10:26:59 +00:00
|
|
|
/* check if a throttling configuration is valid
|
2013-09-02 12:14:37 +00:00
|
|
|
* @cfg: the throttling configuration to inspect
|
2016-02-18 10:26:59 +00:00
|
|
|
* @ret: true if valid else false
|
2016-02-18 10:26:55 +00:00
|
|
|
* @errp: error object
|
2013-09-02 12:14:37 +00:00
|
|
|
*/
|
2016-02-18 10:26:59 +00:00
|
|
|
bool throttle_is_valid(ThrottleConfig *cfg, Error **errp)
|
2013-09-02 12:14:37 +00:00
|
|
|
{
|
2016-02-18 10:26:59 +00:00
|
|
|
int i;
|
2013-09-02 12:14:37 +00:00
|
|
|
bool bps_flag, ops_flag;
|
|
|
|
bool bps_max_flag, ops_max_flag;
|
|
|
|
|
|
|
|
bps_flag = cfg->buckets[THROTTLE_BPS_TOTAL].avg &&
|
|
|
|
(cfg->buckets[THROTTLE_BPS_READ].avg ||
|
|
|
|
cfg->buckets[THROTTLE_BPS_WRITE].avg);
|
|
|
|
|
|
|
|
ops_flag = cfg->buckets[THROTTLE_OPS_TOTAL].avg &&
|
|
|
|
(cfg->buckets[THROTTLE_OPS_READ].avg ||
|
|
|
|
cfg->buckets[THROTTLE_OPS_WRITE].avg);
|
|
|
|
|
|
|
|
bps_max_flag = cfg->buckets[THROTTLE_BPS_TOTAL].max &&
|
|
|
|
(cfg->buckets[THROTTLE_BPS_READ].max ||
|
|
|
|
cfg->buckets[THROTTLE_BPS_WRITE].max);
|
|
|
|
|
|
|
|
ops_max_flag = cfg->buckets[THROTTLE_OPS_TOTAL].max &&
|
|
|
|
(cfg->buckets[THROTTLE_OPS_READ].max ||
|
|
|
|
cfg->buckets[THROTTLE_OPS_WRITE].max);
|
|
|
|
|
2016-02-18 10:26:55 +00:00
|
|
|
if (bps_flag || ops_flag || bps_max_flag || ops_max_flag) {
|
|
|
|
error_setg(errp, "bps/iops/max total values and read/write values"
|
|
|
|
" cannot be used at the same time");
|
2016-02-18 10:26:59 +00:00
|
|
|
return false;
|
2016-02-18 10:26:55 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 12:14:37 +00:00
|
|
|
for (i = 0; i < BUCKETS_COUNT; i++) {
|
2016-01-20 04:21:20 +00:00
|
|
|
if (cfg->buckets[i].avg < 0 ||
|
|
|
|
cfg->buckets[i].max < 0 ||
|
|
|
|
cfg->buckets[i].avg > THROTTLE_VALUE_MAX ||
|
|
|
|
cfg->buckets[i].max > THROTTLE_VALUE_MAX) {
|
2016-02-18 10:26:57 +00:00
|
|
|
error_setg(errp, "bps/iops/max values must be within [0, %lld]",
|
|
|
|
THROTTLE_VALUE_MAX);
|
2016-01-20 04:21:20 +00:00
|
|
|
return false;
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 10:27:01 +00:00
|
|
|
if (!cfg->buckets[i].burst_length) {
|
|
|
|
error_setg(errp, "the burst length cannot be 0");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg->buckets[i].burst_length > 1 && !cfg->buckets[i].max) {
|
|
|
|
error_setg(errp, "burst length set without burst rate");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-04 10:22:12 +00:00
|
|
|
if (cfg->buckets[i].max && !cfg->buckets[i].avg) {
|
2016-02-18 10:26:56 +00:00
|
|
|
error_setg(errp, "bps_max/iops_max require corresponding"
|
|
|
|
" bps/iops values");
|
2016-02-18 10:26:59 +00:00
|
|
|
return false;
|
2015-08-04 10:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 10:26:59 +00:00
|
|
|
|
|
|
|
return true;
|
2015-08-04 10:22:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 12:14:37 +00:00
|
|
|
/* fix bucket parameters */
|
|
|
|
static void throttle_fix_bucket(LeakyBucket *bkt)
|
|
|
|
{
|
|
|
|
double min;
|
|
|
|
|
|
|
|
/* zero bucket level */
|
2016-02-18 10:27:01 +00:00
|
|
|
bkt->level = bkt->burst_level = 0;
|
2013-09-02 12:14:37 +00:00
|
|
|
|
|
|
|
/* The following is done to cope with the Linux CFQ block scheduler
|
|
|
|
* which regroup reads and writes by block of 100ms in the guest.
|
|
|
|
* When they are two process one making reads and one making writes cfq
|
|
|
|
* make a pattern looking like the following:
|
|
|
|
* WWWWWWWWWWWRRRRRRRRRRRRRRWWWWWWWWWWWWWwRRRRRRRRRRRRRRRRR
|
|
|
|
* Having a max burst value of 100ms of the average will help smooth the
|
|
|
|
* throttling
|
|
|
|
*/
|
|
|
|
min = bkt->avg / 10;
|
|
|
|
if (bkt->avg && !bkt->max) {
|
|
|
|
bkt->max = min;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* take care of canceling a timer */
|
|
|
|
static void throttle_cancel_timer(QEMUTimer *timer)
|
|
|
|
{
|
|
|
|
assert(timer != NULL);
|
|
|
|
|
|
|
|
timer_del(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used to configure the throttle
|
|
|
|
*
|
|
|
|
* @ts: the throttle state we are working on
|
2015-06-08 16:17:41 +00:00
|
|
|
* @tt: the throttle timers we use in this aio context
|
2013-09-02 12:14:37 +00:00
|
|
|
* @cfg: the config to set
|
|
|
|
*/
|
2015-06-08 16:17:41 +00:00
|
|
|
void throttle_config(ThrottleState *ts,
|
|
|
|
ThrottleTimers *tt,
|
|
|
|
ThrottleConfig *cfg)
|
2013-09-02 12:14:37 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ts->cfg = *cfg;
|
|
|
|
|
|
|
|
for (i = 0; i < BUCKETS_COUNT; i++) {
|
|
|
|
throttle_fix_bucket(&ts->cfg.buckets[i]);
|
|
|
|
}
|
|
|
|
|
2015-06-08 16:17:41 +00:00
|
|
|
ts->previous_leak = qemu_clock_get_ns(tt->clock_type);
|
2013-09-02 12:14:37 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
2015-06-08 16:17:41 +00:00
|
|
|
throttle_cancel_timer(tt->timers[i]);
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* used to get config
|
|
|
|
*
|
|
|
|
* @ts: the throttle state we are working on
|
|
|
|
* @cfg: the config to write
|
|
|
|
*/
|
|
|
|
void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg)
|
|
|
|
{
|
|
|
|
*cfg = ts->cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Schedule the read or write timer if needed
|
|
|
|
*
|
|
|
|
* NOTE: this function is not unit tested due to it's usage of timer_mod
|
|
|
|
*
|
2015-06-08 16:17:41 +00:00
|
|
|
* @tt: the timers structure
|
2013-09-02 12:14:37 +00:00
|
|
|
* @is_write: the type of operation (read/write)
|
|
|
|
* @ret: true if the timer has been scheduled else false
|
|
|
|
*/
|
2015-06-08 16:17:41 +00:00
|
|
|
bool throttle_schedule_timer(ThrottleState *ts,
|
|
|
|
ThrottleTimers *tt,
|
|
|
|
bool is_write)
|
2013-09-02 12:14:37 +00:00
|
|
|
{
|
2015-06-08 16:17:41 +00:00
|
|
|
int64_t now = qemu_clock_get_ns(tt->clock_type);
|
2013-09-02 12:14:37 +00:00
|
|
|
int64_t next_timestamp;
|
|
|
|
bool must_wait;
|
|
|
|
|
|
|
|
must_wait = throttle_compute_timer(ts,
|
|
|
|
is_write,
|
|
|
|
now,
|
|
|
|
&next_timestamp);
|
|
|
|
|
|
|
|
/* request not throttled */
|
|
|
|
if (!must_wait) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* request throttled and timer pending -> do nothing */
|
2015-06-08 16:17:41 +00:00
|
|
|
if (timer_pending(tt->timers[is_write])) {
|
2013-09-02 12:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* request throttled and timer not pending -> arm timer */
|
2015-06-08 16:17:41 +00:00
|
|
|
timer_mod(tt->timers[is_write], next_timestamp);
|
2013-09-02 12:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the accounting for this operation
|
|
|
|
*
|
|
|
|
* @is_write: the type of operation (read/write)
|
|
|
|
* @size: the size of the operation
|
|
|
|
*/
|
|
|
|
void throttle_account(ThrottleState *ts, bool is_write, uint64_t size)
|
|
|
|
{
|
2016-02-18 10:27:01 +00:00
|
|
|
const BucketType bucket_types_size[2][2] = {
|
|
|
|
{ THROTTLE_BPS_TOTAL, THROTTLE_BPS_READ },
|
|
|
|
{ THROTTLE_BPS_TOTAL, THROTTLE_BPS_WRITE }
|
|
|
|
};
|
|
|
|
const BucketType bucket_types_units[2][2] = {
|
|
|
|
{ THROTTLE_OPS_TOTAL, THROTTLE_OPS_READ },
|
|
|
|
{ THROTTLE_OPS_TOTAL, THROTTLE_OPS_WRITE }
|
|
|
|
};
|
2013-09-02 12:14:37 +00:00
|
|
|
double units = 1.0;
|
2016-02-18 10:27:01 +00:00
|
|
|
unsigned i;
|
2013-09-02 12:14:37 +00:00
|
|
|
|
|
|
|
/* if cfg.op_size is defined and smaller than size we compute unit count */
|
|
|
|
if (ts->cfg.op_size && size > ts->cfg.op_size) {
|
|
|
|
units = (double) size / ts->cfg.op_size;
|
|
|
|
}
|
|
|
|
|
2016-02-18 10:27:01 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
LeakyBucket *bkt;
|
|
|
|
|
|
|
|
bkt = &ts->cfg.buckets[bucket_types_size[is_write][i]];
|
|
|
|
bkt->level += size;
|
|
|
|
if (bkt->burst_length > 1) {
|
|
|
|
bkt->burst_level += size;
|
|
|
|
}
|
2013-09-02 12:14:37 +00:00
|
|
|
|
2016-02-18 10:27:01 +00:00
|
|
|
bkt = &ts->cfg.buckets[bucket_types_units[is_write][i]];
|
|
|
|
bkt->level += units;
|
|
|
|
if (bkt->burst_length > 1) {
|
|
|
|
bkt->burst_level += units;
|
|
|
|
}
|
2013-09-02 12:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|