2014-11-21 10:51:34 +00:00
|
|
|
/*
|
|
|
|
* MARS Long Distance Replication Software
|
|
|
|
*
|
|
|
|
* This file is part of MARS project: http://schoebel.github.io/mars/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
|
|
|
|
* Copyright (C) 2011-2014 1&1 Internet AG
|
|
|
|
*
|
|
|
|
* 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 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2012-09-24 09:17:00 +00:00
|
|
|
|
|
|
|
#include "lib_limiter.h"
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2013-11-20 13:10:54 +00:00
|
|
|
#define LIMITER_TIME_RESOLUTION NSEC_PER_SEC
|
|
|
|
|
2012-09-24 09:17:00 +00:00
|
|
|
int mars_limit(struct mars_limiter *lim, int amount)
|
|
|
|
{
|
2012-09-24 09:49:29 +00:00
|
|
|
int delay = 0;
|
2012-12-06 13:14:59 +00:00
|
|
|
long long now;
|
2012-09-24 09:17:00 +00:00
|
|
|
|
2017-06-01 06:30:36 +00:00
|
|
|
if (unlikely(amount < 0))
|
|
|
|
amount = 0;
|
|
|
|
|
2012-09-24 09:17:00 +00:00
|
|
|
now = cpu_clock(raw_smp_processor_id());
|
|
|
|
|
2012-09-24 09:49:29 +00:00
|
|
|
/* Compute the maximum delay along the path
|
|
|
|
* down to the root of the hierarchy tree.
|
|
|
|
*/
|
|
|
|
while (lim != NULL) {
|
2013-11-20 13:33:38 +00:00
|
|
|
long long window = now - lim->lim_stamp;
|
2013-07-15 07:58:35 +00:00
|
|
|
/* Sometimes, raw CPU clocks may do weired things...
|
2013-11-20 13:33:38 +00:00
|
|
|
* Smaller windows in the denominator than 1s could fake unrealistic rates.
|
2013-07-15 07:58:35 +00:00
|
|
|
*/
|
2013-11-20 14:14:05 +00:00
|
|
|
if (unlikely(lim->lim_min_window <= 0))
|
|
|
|
lim->lim_min_window = 1000;
|
|
|
|
if (unlikely(lim->lim_max_window <= lim->lim_min_window))
|
|
|
|
lim->lim_max_window = lim->lim_min_window + 8000;
|
|
|
|
if (unlikely(window < (long long)lim->lim_min_window * (LIMITER_TIME_RESOLUTION / 1000)))
|
|
|
|
window = (long long)lim->lim_min_window * (LIMITER_TIME_RESOLUTION / 1000);
|
2013-07-15 07:58:35 +00:00
|
|
|
|
2015-06-24 07:33:21 +00:00
|
|
|
/* Update total statistics.
|
|
|
|
* They will intentionally wrap around.
|
|
|
|
* Userspace must take care of that.
|
|
|
|
*/
|
|
|
|
lim->lim_total_ops++;
|
|
|
|
lim->lim_total_sum += amount;
|
|
|
|
|
2013-07-15 07:58:35 +00:00
|
|
|
/* Only use incremental accumulation at repeated calls, but
|
|
|
|
* never after longer pauses.
|
|
|
|
*/
|
2013-11-20 14:14:05 +00:00
|
|
|
if (likely(lim->lim_stamp &&
|
|
|
|
window < (long long)lim->lim_max_window * (LIMITER_TIME_RESOLUTION / 1000))) {
|
2013-07-15 07:58:35 +00:00
|
|
|
long long rate_raw;
|
2012-09-24 09:49:29 +00:00
|
|
|
int rate;
|
|
|
|
|
|
|
|
/* Races are possible, but taken into account.
|
|
|
|
* There is no real harm from rarely lost updates.
|
|
|
|
*/
|
2013-11-19 11:22:45 +00:00
|
|
|
if (likely(amount > 0)) {
|
2012-12-06 13:14:59 +00:00
|
|
|
lim->lim_accu += amount;
|
2013-11-19 11:22:45 +00:00
|
|
|
lim->lim_cumul += amount;
|
2013-11-20 14:46:16 +00:00
|
|
|
lim->lim_count++;
|
2013-11-19 11:22:45 +00:00
|
|
|
}
|
2012-09-24 09:49:29 +00:00
|
|
|
|
2013-11-20 13:33:38 +00:00
|
|
|
rate_raw = lim->lim_accu * LIMITER_TIME_RESOLUTION / window;
|
2013-07-15 07:58:35 +00:00
|
|
|
rate = rate_raw;
|
|
|
|
if (unlikely(rate_raw > INT_MAX)) {
|
|
|
|
rate = INT_MAX;
|
|
|
|
}
|
2012-09-24 09:49:29 +00:00
|
|
|
lim->lim_rate = rate;
|
|
|
|
|
|
|
|
// limit exceeded?
|
|
|
|
if (lim->lim_max_rate > 0 && rate > lim->lim_max_rate) {
|
2013-11-20 16:17:26 +00:00
|
|
|
int this_delay = (window * rate / lim->lim_max_rate - window) / (LIMITER_TIME_RESOLUTION / 1000);
|
2012-09-24 09:49:29 +00:00
|
|
|
// compute maximum
|
2013-11-20 16:17:26 +00:00
|
|
|
if (this_delay > delay && this_delay > 0)
|
2012-09-24 09:49:29 +00:00
|
|
|
delay = this_delay;
|
|
|
|
}
|
2013-11-20 13:33:38 +00:00
|
|
|
|
2013-11-20 14:14:05 +00:00
|
|
|
/* Try to keep the next window below min_window
|
2013-11-20 13:33:38 +00:00
|
|
|
*/
|
2013-11-20 14:14:05 +00:00
|
|
|
window -= lim->lim_min_window * (LIMITER_TIME_RESOLUTION / 1000);
|
|
|
|
if (window > 0) {
|
|
|
|
long long used_up = (long long)lim->lim_rate * window / LIMITER_TIME_RESOLUTION;
|
|
|
|
if (used_up > 0) {
|
|
|
|
lim->lim_stamp += window;
|
|
|
|
lim->lim_accu -= used_up;
|
|
|
|
if (unlikely(lim->lim_accu < 0))
|
|
|
|
lim->lim_accu = 0;
|
|
|
|
}
|
2012-09-24 09:49:29 +00:00
|
|
|
}
|
2013-11-20 13:33:38 +00:00
|
|
|
} else { // reset, start over with new measurement cycle
|
2012-09-24 09:49:29 +00:00
|
|
|
lim->lim_accu = amount;
|
2013-11-20 14:14:05 +00:00
|
|
|
lim->lim_stamp = now - lim->lim_min_window * (LIMITER_TIME_RESOLUTION / 1000);
|
2012-09-24 09:49:29 +00:00
|
|
|
lim->lim_rate = 0;
|
2012-09-24 09:17:00 +00:00
|
|
|
}
|
2012-09-24 09:49:29 +00:00
|
|
|
lim = lim->lim_father;
|
2012-09-24 09:17:00 +00:00
|
|
|
}
|
2012-09-24 09:49:29 +00:00
|
|
|
return delay;
|
2012-09-24 09:17:00 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_limit);
|
2013-11-20 13:10:54 +00:00
|
|
|
|
|
|
|
void mars_limit_sleep(struct mars_limiter *lim, int amount)
|
|
|
|
{
|
|
|
|
int sleep = mars_limit(lim, amount);
|
|
|
|
if (sleep > 0) {
|
|
|
|
if (unlikely(lim->lim_max_delay <= 0))
|
|
|
|
lim->lim_max_delay = 1000;
|
|
|
|
if (sleep > lim->lim_max_delay)
|
|
|
|
sleep = lim->lim_max_delay;
|
|
|
|
brick_msleep(sleep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_limit_sleep);
|
2017-06-01 06:30:36 +00:00
|
|
|
|
|
|
|
void mars_limit_reset(struct mars_limiter *lim)
|
|
|
|
{
|
|
|
|
if (!lim)
|
|
|
|
return;
|
|
|
|
lim->lim_stamp = 0;
|
|
|
|
mars_limit(lim, 0);
|
|
|
|
}
|