mars/kernel/lib_rank.c

104 lines
2.4 KiB
C
Raw Normal View History

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-02-11 19:08:41 +00:00
// (c) 2012 Thomas Schoebel-Theuer
//#define BRICK_DEBUGGING
//#define MARS_DEBUGGING
//#define IO_DEBUGGING
#include <linux/kernel.h>
#include <linux/module.h>
#include "mars.h"
#include "lib_rank.h"
void ranking_compute(struct rank_data *rkd, const struct rank_info rki[], int x)
2012-02-11 19:08:41 +00:00
{
2012-12-12 08:38:51 +00:00
int points = 0;
2012-02-11 19:08:41 +00:00
int i;
2012-12-12 08:38:51 +00:00
for (i = 0; ; i++) {
2012-12-12 08:38:51 +00:00
int x0;
2012-02-11 19:08:41 +00:00
int x1;
int y0;
int y1;
2012-12-12 08:38:51 +00:00
x0 = rki[i].rki_x;
if (x < x0)
2012-12-12 08:38:51 +00:00
break;
2012-02-11 19:08:41 +00:00
x1 = rki[i+1].rki_x;
2012-12-12 08:38:51 +00:00
if (unlikely(x1 == RKI_DUMMY)) {
points = rki[i].rki_y;
break;
2012-12-12 08:38:51 +00:00
}
if (x > x1)
continue;
2012-02-11 19:08:41 +00:00
y0 = rki[i].rki_y;
y1 = rki[i+1].rki_y;
// linear interpolation
points = ((long long)(x - x0) * (long long)(y1 - y0)) / (x1 - x0) + y0;
MARS_IO("i = %d x0 = %d x1 = %d y0 = %d y1 = %d points = %d\n", i, x0, x1, y0, y1, points);
break;
}
2012-12-12 08:38:51 +00:00
rkd->rkd_tmp += points;
2012-02-11 19:08:41 +00:00
}
EXPORT_SYMBOL_GPL(ranking_compute);
int ranking_select(struct rank_data rkd[], int rkd_count)
{
int res = -1;
long long max = LLONG_MIN / 2;
2012-02-11 19:08:41 +00:00
int i;
MARS_IO("rkd_count = %d\n", rkd_count);
for (i = 0; i < rkd_count; i++) {
struct rank_data *tmp = &rkd[i];
long long rest = tmp->rkd_current_points;
2012-02-11 19:08:41 +00:00
if (rest <= 0)
continue;
//rest -= tmp->rkd_got;
2012-02-11 19:08:41 +00:00
if (rest > max) {
max = rest;
res = i;
}
}
/* Prevent underflow in the long term
* and reset the "clocks" after each round of
* weighted round-robin selection.
*/
if (max < 0 && res >= 0) {
2012-02-11 19:08:41 +00:00
for (i = 0; i < rkd_count; i++)
rkd[i].rkd_got += max;
2012-02-11 19:08:41 +00:00
}
MARS_IO("res = %d\n", res);
return res;
}
EXPORT_SYMBOL_GPL(ranking_select);