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"
|
2019-02-20 12:48:21 +00:00
|
|
|
#include "lamport.h"
|
2012-09-24 09:17:00 +00:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2020-08-17 16:03:29 +00:00
|
|
|
/* For precisions, _internal_ time is in multiples of the following basic time units */
|
|
|
|
|
2013-11-20 13:10:54 +00:00
|
|
|
#define LIMITER_TIME_RESOLUTION NSEC_PER_SEC
|
|
|
|
|
2020-08-17 16:03:29 +00:00
|
|
|
#define DEFAULT_MIN_WINDOW (LIMITER_TIME_RESOLUTION * 1)
|
|
|
|
#define DEFAULT_MAX_WINDOW (LIMITER_TIME_RESOLUTION * 4)
|
|
|
|
|
2021-02-10 07:12:05 +00:00
|
|
|
#define MIN_DIVIDER (DEFAULT_MIN_WINDOW / 10)
|
|
|
|
#define MAX_DIVIDER (DEFAULT_MAX_WINDOW * 10)
|
|
|
|
|
|
|
|
#define CLAMP_WINDOW(x) \
|
|
|
|
(x) < MIN_DIVIDER ? MIN_DIVIDER : \
|
|
|
|
(x) > MAX_DIVIDER ? MAX_DIVIDER : \
|
|
|
|
(x)
|
2020-08-17 16:03:29 +00:00
|
|
|
|
|
|
|
#define MS_TO_TR(x) ((__s64)(x) * (LIMITER_TIME_RESOLUTION / 1000))
|
|
|
|
#define TR_TO_MS(x) ((x) / (LIMITER_TIME_RESOLUTION / 1000))
|
|
|
|
|
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;
|
2019-02-20 12:48:21 +00:00
|
|
|
struct lamport_time now;
|
2012-09-24 09:17:00 +00:00
|
|
|
|
2017-06-01 06:30:36 +00:00
|
|
|
if (unlikely(amount < 0))
|
|
|
|
amount = 0;
|
|
|
|
|
2021-01-29 13:03:17 +00:00
|
|
|
get_real_lamport(&now);
|
2012-09-24 09:17:00 +00:00
|
|
|
|
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) {
|
2019-02-20 12:48:21 +00:00
|
|
|
struct lamport_time diff = lamport_time_sub(now, lim->lim_stamp);
|
2020-08-17 16:03:29 +00:00
|
|
|
__s64 window = lamport_time_to_ns(&diff);
|
|
|
|
__s64 rate_raw;
|
|
|
|
int rate;
|
|
|
|
int max_rate;
|
2019-02-20 12:48:21 +00:00
|
|
|
|
2013-07-15 07:58:35 +00:00
|
|
|
/* Sometimes, raw CPU clocks may do weired things...
|
2020-08-17 16:03:29 +00:00
|
|
|
* Small windows in the denominator could fake unrealistic rates.
|
|
|
|
* Do not divide by too small numbers.
|
2013-07-15 07:58:35 +00:00
|
|
|
*/
|
2021-02-10 07:12:05 +00:00
|
|
|
window = CLAMP_WINDOW(window);
|
2020-08-17 16:03:29 +00:00
|
|
|
|
|
|
|
if (unlikely(lim->lim_min_window_ms <= TR_TO_MS(MAX_DIVIDER)))
|
|
|
|
lim->lim_min_window_ms = TR_TO_MS(DEFAULT_MIN_WINDOW);
|
|
|
|
if (unlikely(lim->lim_max_window_ms <= lim->lim_min_window_ms))
|
|
|
|
lim->lim_max_window_ms = lim->lim_min_window_ms + TR_TO_MS(DEFAULT_MAX_WINDOW);
|
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.
|
|
|
|
*/
|
2016-01-19 08:15:44 +00:00
|
|
|
if (likely(amount > 0)) {
|
|
|
|
lim->lim_total_amount += amount;
|
|
|
|
lim->lim_total_ops++;
|
|
|
|
}
|
2015-06-24 07:33:21 +00:00
|
|
|
|
2013-07-15 07:58:35 +00:00
|
|
|
/* Only use incremental accumulation at repeated calls, but
|
|
|
|
* never after longer pauses.
|
|
|
|
*/
|
2020-08-17 16:03:29 +00:00
|
|
|
if (!lim->lim_stamp.tv_sec ||
|
|
|
|
window > MS_TO_TR(lim->lim_max_window_ms)) {
|
|
|
|
/* reset, start over with new measurement cycle */
|
|
|
|
memset(&diff, 0, sizeof(diff));
|
|
|
|
lim->lim_stamp = now;
|
|
|
|
lim->lim_ops_accu = 0;
|
|
|
|
lim->lim_amount_accu = 0;
|
|
|
|
lim->lim_ops_rate = 0;
|
|
|
|
lim->lim_amount_rate = 0;
|
2021-02-10 07:12:05 +00:00
|
|
|
window = MIN_DIVIDER;
|
2020-08-17 16:03:29 +00:00
|
|
|
} else {
|
|
|
|
__s64 diff_window;
|
2013-11-20 13:33:38 +00:00
|
|
|
|
2020-08-17 16:03:29 +00:00
|
|
|
/* Try to keep the window between min_window and 2 * min_window.
|
|
|
|
* We wait until min_window has been exceeded _twice_,
|
|
|
|
* and then reduce the window by only 1 * min_window.
|
2013-11-20 13:33:38 +00:00
|
|
|
*/
|
2020-08-17 16:03:29 +00:00
|
|
|
diff_window = window - MS_TO_TR(lim->lim_min_window_ms);
|
|
|
|
if (diff_window > MS_TO_TR(lim->lim_min_window_ms)) {
|
|
|
|
__s64 used_up;
|
|
|
|
__s64 add_window = 0;
|
|
|
|
|
|
|
|
used_up = lim->lim_amount_accu * diff_window / window;
|
2013-11-20 14:14:05 +00:00
|
|
|
if (used_up > 0) {
|
2020-08-17 16:03:29 +00:00
|
|
|
add_window = diff_window;
|
2016-01-19 08:15:44 +00:00
|
|
|
lim->lim_amount_accu -= used_up;
|
|
|
|
if (unlikely(lim->lim_amount_accu < 0))
|
|
|
|
lim->lim_amount_accu = 0;
|
|
|
|
}
|
2020-08-17 16:03:29 +00:00
|
|
|
|
|
|
|
used_up = lim->lim_ops_accu * diff_window / window;
|
2016-01-19 08:15:44 +00:00
|
|
|
if (used_up > 0) {
|
2020-08-17 16:03:29 +00:00
|
|
|
if (diff_window > add_window)
|
|
|
|
add_window = diff_window;
|
2016-01-19 08:15:44 +00:00
|
|
|
lim->lim_ops_accu -= used_up;
|
|
|
|
if (unlikely(lim->lim_ops_accu < 0))
|
|
|
|
lim->lim_ops_accu = 0;
|
2013-11-20 14:14:05 +00:00
|
|
|
}
|
2020-08-17 16:03:29 +00:00
|
|
|
|
|
|
|
if (add_window > 0) {
|
|
|
|
lamport_time_add_ns(&lim->lim_stamp, add_window);
|
|
|
|
/* recompute the new window */
|
|
|
|
diff = lamport_time_sub(now, lim->lim_stamp);
|
|
|
|
window = lamport_time_to_ns(&diff);
|
2021-02-10 07:12:05 +00:00
|
|
|
window = CLAMP_WINDOW(window);
|
2020-08-17 16:03:29 +00:00
|
|
|
}
|
2012-09-24 09:49:29 +00:00
|
|
|
}
|
2012-09-24 09:17:00 +00:00
|
|
|
}
|
2020-08-17 16:03:29 +00:00
|
|
|
|
|
|
|
/* Races are possible, but taken into account.
|
|
|
|
* There is no real harm from rarely lost updates.
|
|
|
|
*/
|
|
|
|
if (likely(amount > 0)) {
|
|
|
|
lim->lim_amount_accu += amount;
|
|
|
|
lim->lim_ops_accu++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute amount values */
|
|
|
|
rate_raw = lim->lim_amount_accu * LIMITER_TIME_RESOLUTION / window;
|
|
|
|
rate = rate_raw;
|
|
|
|
if (unlikely(rate_raw > INT_MAX)) {
|
|
|
|
rate = INT_MAX;
|
|
|
|
}
|
|
|
|
lim->lim_amount_rate = rate;
|
|
|
|
|
|
|
|
/* amount limit exceeded? */
|
|
|
|
max_rate = lim->lim_max_amount_rate;
|
|
|
|
if (max_rate > 0 && rate > max_rate) {
|
|
|
|
int this_delay = (window * rate / max_rate - window);
|
|
|
|
|
|
|
|
// compute maximum
|
|
|
|
if (this_delay > delay && this_delay > 0)
|
|
|
|
delay = this_delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute ops values */
|
|
|
|
rate_raw = lim->lim_ops_accu * LIMITER_TIME_RESOLUTION / window;
|
|
|
|
rate = rate_raw;
|
|
|
|
if (unlikely(rate_raw > INT_MAX)) {
|
|
|
|
rate = INT_MAX;
|
|
|
|
}
|
|
|
|
lim->lim_ops_rate = rate;
|
|
|
|
|
|
|
|
/* ops limit exceeded? */
|
|
|
|
max_rate = lim->lim_max_ops_rate;
|
|
|
|
if (max_rate > 0 && rate > max_rate) {
|
|
|
|
int this_delay = (window * rate / max_rate - window);
|
|
|
|
|
|
|
|
// compute maximum
|
|
|
|
if (this_delay > delay && this_delay > 0)
|
|
|
|
delay = this_delay;
|
|
|
|
}
|
|
|
|
|
2012-09-24 09:49:29 +00:00
|
|
|
lim = lim->lim_father;
|
2012-09-24 09:17:00 +00:00
|
|
|
}
|
2020-08-17 16:03:29 +00:00
|
|
|
return TR_TO_MS(delay);
|
2012-09-24 09:17:00 +00:00
|
|
|
}
|
2013-11-20 13:10:54 +00:00
|
|
|
|
|
|
|
void mars_limit_sleep(struct mars_limiter *lim, int amount)
|
|
|
|
{
|
|
|
|
int sleep = mars_limit(lim, amount);
|
2016-01-19 08:15:44 +00:00
|
|
|
|
2013-11-20 13:10:54 +00:00
|
|
|
if (sleep > 0) {
|
2020-08-17 16:03:29 +00:00
|
|
|
if (unlikely(lim->lim_max_delay_ms <= 0))
|
|
|
|
lim->lim_max_delay_ms = 1000;
|
|
|
|
if (sleep > lim->lim_max_delay_ms)
|
|
|
|
sleep = lim->lim_max_delay_ms;
|
2013-11-20 13:10:54 +00:00
|
|
|
brick_msleep(sleep);
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 06:30:36 +00:00
|
|
|
|
|
|
|
void mars_limit_reset(struct mars_limiter *lim)
|
|
|
|
{
|
|
|
|
if (!lim)
|
|
|
|
return;
|
2019-02-20 12:48:21 +00:00
|
|
|
memset(&lim->lim_stamp, 0, sizeof(lim->lim_stamp));
|
2017-06-01 06:30:36 +00:00
|
|
|
mars_limit(lim, 0);
|
|
|
|
}
|