From 84ca56f6b1bcaf8652c78d25cf40eb4ec621a1d8 Mon Sep 17 00:00:00 2001 From: Thomas Schoebel-Theuer Date: Mon, 24 Sep 2012 11:17:00 +0200 Subject: [PATCH] limiter: un-inline code --- Makefile | 1 + lib_limiter.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lib_limiter.h | 38 +++----------------------------------- 3 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 lib_limiter.c diff --git a/Makefile b/Makefile index 1a76f3d8..9a0f08cb 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ mars-objs := \ mars_generic.o \ lib_log.o \ lib_rank.o \ + lib_limiter.o \ mars_net.o \ mars_server.o \ mars_client.o \ diff --git a/lib_limiter.c b/lib_limiter.c new file mode 100644 index 00000000..42fdb6c8 --- /dev/null +++ b/lib_limiter.c @@ -0,0 +1,42 @@ +// (c) 2012 Thomas Schoebel-Theuer / 1&1 Internet AG + +#include "lib_limiter.h" + +#include +#include + +int mars_limit(struct mars_limiter *lim, int amount) +{ + int res = 0; + unsigned long long now; + + now = cpu_clock(raw_smp_processor_id()); + + if (lim->lim_max_rate > 0 && likely(lim->lim_stamp)) { + long long elapsed = now - lim->lim_stamp; + long long rate; + + /* Races are possible, but taken into account. + * There is no real harm from rarely lost updates. + */ + lim->lim_accu += amount; + + rate = (long long)lim->lim_accu * LIMITER_TIME_RESOLUTION / elapsed; + + if (rate > lim->lim_max_rate) { + res = 1001 - lim->lim_max_rate * 1000 / rate; + } + + elapsed -= LIMITER_TIME_RESOLUTION * 2; + if (elapsed > LIMITER_TIME_RESOLUTION) { + lim->lim_stamp += elapsed; + if (lim->lim_accu > 0) + lim->lim_accu -= (long long)lim->lim_max_rate * elapsed / LIMITER_TIME_RESOLUTION; + } + } else { + lim->lim_accu = amount; + lim->lim_stamp = now; + } + return res; +} +EXPORT_SYMBOL_GPL(mars_limit); diff --git a/lib_limiter.h b/lib_limiter.h index f768c41b..2fefb3c7 100644 --- a/lib_limiter.h +++ b/lib_limiter.h @@ -2,6 +2,8 @@ #ifndef MARS_LIB_LIMITER_H #define MARS_LIB_LIMITER_H +#include "brick.h" + #include #define LIMITER_TIME_RESOLUTION NSEC_PER_SEC @@ -14,41 +16,7 @@ struct mars_limiter { unsigned long long lim_stamp; }; -extern inline -int mars_limit(struct mars_limiter *lim, int amount) -{ - int res = 0; - unsigned long long now; - - now = cpu_clock(raw_smp_processor_id()); - - if (lim->lim_max_rate > 0 && likely(lim->lim_stamp)) { - long long elapsed = now - lim->lim_stamp; - long long rate; - - /* Races are possible, but taken into account. - * There is no real harm from rarely lost updates. - */ - lim->lim_accu += amount; - - rate = (long long)lim->lim_accu * LIMITER_TIME_RESOLUTION / elapsed; - - if (rate > lim->lim_max_rate) { - res = 1001 - lim->lim_max_rate * 1000 / rate; - } - - elapsed -= LIMITER_TIME_RESOLUTION * 2; - if (elapsed > LIMITER_TIME_RESOLUTION) { - lim->lim_stamp += elapsed; - if (lim->lim_accu > 0) - lim->lim_accu -= (long long)lim->lim_max_rate * elapsed / LIMITER_TIME_RESOLUTION; - } - } else { - lim->lim_accu = amount; - lim->lim_stamp = now; - } - return res; -} +extern int mars_limit(struct mars_limiter *lim, int amount); extern inline void mars_limit_sleep(struct mars_limiter *lim, int amount)