/* * QEMU throttling infrastructure * * Copyright (C) Nodalink, EURL. 2013-2014 * Copyright (C) Igalia, S.L. 2015 * * Authors: * BenoƮt Canet * Alberto Garcia * * 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 . */ #include "qemu/osdep.h" #include "qapi/error.h" #include "qemu/throttle.h" #include "qemu/timer.h" #include "block/aio.h" /* 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 */ leak = (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND; /* make the bucket leak */ bkt->level = MAX(bkt->level - leak, 0); /* 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); } } /* 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) { double wait = extra * NANOSECONDS_PER_SECOND; 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; } /* 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); } /* 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); } } return 0; } /* 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 */ static bool throttle_compute_timer(ThrottleState *ts, bool is_write, int64_t now, int64_t *next_timestamp) { 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; } /* Add timers to event loop */ void throttle_timers_attach_aio_context(ThrottleTimers *tt, AioContext *new_context) { 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); } /* * Initialize the ThrottleConfig structure to a valid state * @cfg: the config to initialize */ void throttle_config_init(ThrottleConfig *cfg) { unsigned i; memset(cfg, 0, sizeof(*cfg)); for (i = 0; i < BUCKETS_COUNT; i++) { cfg->buckets[i].burst_length = 1; } } /* To be called first on the ThrottleState */ vo