2#ifndef CLICK_TOKENBUCKET_HH
3#define CLICK_TOKENBUCKET_HH
72 typedef typename make_unsigned<typename P::duration_type>::type
ticks_type;
114 return _time_until_full == 0;
119 return _tokens_per_tick == 0;
133 return max_tokens / _token_scale;
138 return _tokens_per_tick;
151 return _time_until_full;
165 template <
typename U>
167 return P::time_point(time);
177 return P::time_monotonic_difference(a, b);
183 inline token_type tokens_per_epoch() const CLICK_DEPRECATED;
184 inline
ticks_type epochs_until_full() const CLICK_DEPRECATED;
200 _tokens_per_tick = max_tokens;
201 _time_until_full = 0;
203 _tokens_per_tick = 0;
219 unsigned long long min_capacity = (
rate - 1) / frequency + 1;
223 _token_scale = max_tokens /
capacity;
226 static_assert(
sizeof(bigint::limb_type) ==
sizeof(
token_type),
227 "bigint::limb_type should have the same size as token_type.");
228 bigint::limb_type l[2] = { 0, 0 };
229 bigint::limb_type a[2] = {
rate, 0 };
230 bigint::multiply_add(l, a, 2, _token_scale);
231 (void) bigint::divide(l, l, 2, frequency);
236 _tokens_per_tick = (l[0] != 0 ? l[0] : 1);
237 _time_until_full = (max_tokens - 1) / _tokens_per_tick + 1;
239 _tokens_per_tick = 0;
247 static_assert(
sizeof(bigint::limb_type) ==
sizeof(
token_type),
248 "bigint::limb_type should have the same size as token_type.");
249 bigint::limb_type l[2] = { _tokens_per_tick / 2, 0 };
250 bigint::limb_type a[2] = { _tokens_per_tick, 0 };
251 bigint::multiply_add(l, a, 2, P::frequency());
275template<
typename rate_type,
bool FM>
struct TokenRateConverter {
277template<
typename rate_type>
struct TokenRateConverter<rate_type, true> {
278 static bool cvt(
const rate_type &rate,
typename rate_type::token_type &t) {
279 typename rate_type::token_type high;
282 t = rate_type::max_tokens;
286template<
typename rate_type>
struct TokenRateConverter<rate_type, false> {
287 static bool cvt(
const rate_type &rate,
typename rate_type::token_type &t) {
288 if (t <= rate.capacity()) {
289 t *= rate.token_scale();
292 t = rate_type::max_tokens;
343 max_tokens = R::max_tokens
350 : _tokens(0), _time_point() {
359 : _tokens(full ? (
token_type) max_tokens : 0), _time_point() {
368 return _tokens / rate.token_scale();
396 return cvt_type::cvt(rate, t) && contains_fraction(t);
416 _tokens = max_tokens;
425 (void) cvt_type::cvt(rate, t);
446 if (old_rate.token_scale() != new_rate.token_scale()) {
447 static_assert(
sizeof(bigint::limb_type) ==
sizeof(
token_type),
448 "bigint::limb_type should have the same size as token_type.");
449 bigint::limb_type l[2] = { 0, 0 };
450 bigint::limb_type a[2] = { _tokens, 0 };
451 bigint::multiply_add(l, a, 2, new_rate.token_scale());
452 (void) bigint::divide(l, l, 2, old_rate.token_scale());
453 _tokens = l[1] ? (
token_type) max_tokens : l[0];
503 (void) cvt_type::cvt(rate, t);
516 return cvt_type::cvt(rate, t) && remove_fraction_if(t);
524 _tokens = (f <= _tokens ? _tokens - f : 0);
551 if (cvt_type::cvt(rate, t))
552 return time_until_contains_fraction(rate, t);
566 if (f <= _tokens || rate.time_until_full() == 0)
568 else if (rate.tokens_per_tick() == 0)
571 return (f - _tokens - 1) / rate.tokens_per_tick() + 1;
576 inline ticks_type epochs_until_contains(
const rate_type &rate, token_type t)
const CLICK_DEPRECATED;
577 inline ticks_type epochs_until_contains_fraction(
const rate_type &rate, token_type f)
const CLICK_DEPRECATED;
583 time_point_type _time_point;
585 typedef TokenRateConverter<rate_type, has_fast_int_multiply<token_type>::value> cvt_type;
592 ticks_type diff =
rate.time_monotonic_difference(_time_point, time);
593 if (diff >=
rate.time_until_full()) {
597 _tokens = max_tokens;
598 }
else if (diff > 0) {
600 _tokens = (new_tokens < _tokens ? (
token_type) max_tokens : new_tokens);
611template <
typename R>
template <
typename U>
614 refill(
rate,
rate.time_point(time));
621 return time_until_contains(
rate, t);
626 return time_until_contains_fraction(
rate, f);
653 return click_jiffies();
656 static time_point_type time_point(time_point_type t) {
667#if CLICK_JIFFIES_MONOTONIC
670 return (likely(a <= b) ? b - a : 0);
676 return click_jiffies_less(a, b);
723 static time_point_type time_point(time_point_type t) {
734 return (likely(a <= b) ? b - a : 0);
794 max_tokens = rate_type::max_tokens
808 : _rate(unlimited), _bucket(unlimited) {
821 : _rate(rate, capacity), _bucket(rate != 0 && capacity != 0) {
827 _rate.assign(unlimited);
844 _rate.assign(rate, capacity);
858 _rate.assign(rate, capacity);
859 _bucket.adjust(old_rate, _rate);
864 return _rate.unlimited();
885 return _rate.capacity();
893 return _bucket.size(_rate);
901 return _bucket.fraction();
906 return _bucket.empty();
911 return _bucket.full();
919 return _bucket.contains(_rate, t);
925 return _bucket.contains_fraction(f);
947 _bucket.set(_rate, t);
953 _bucket.set_fraction(f);
965 _bucket.refill(_rate);
970 _bucket.refill(_rate, time);
974 template <
typename U>
void refill(U time) {
975 _bucket.refill(_rate, time);
984 _bucket.set_time_point(time);
987 time_point_type time_point() {
988 return _bucket.time_point();
997 _bucket.remove(_rate, t);
1008 return _bucket.remove_if(_rate, t);
1016 _bucket.remove_fraction(f);
1026 return _bucket.remove_fraction_if(f);
1034 return _bucket.time_until_contains(_rate, t);
1042 return _bucket.time_until_contains_fraction(_rate, f);
1047 inline ticks_type epochs_until_contains(
const rate_type &rate, token_type t)
const CLICK_DEPRECATED;
1048 inline ticks_type epochs_until_contains_fraction(
const rate_type &rate, token_type f)
const CLICK_DEPRECATED;
1054 counter_type _bucket;
1059template <
typename P>
1062 return time_until_contains(
rate, t);
1064template <
typename P>
1067 return time_until_contains_fraction(
rate, f);
Simple multiple-precision integer arithmetic.
Represents a moment or interval in time.
Definition timestamp.hh:125
static Timestamp make_jiffies(click_jiffies_t jiffies)
Return a timestamp representing an interval of jiffies.
Definition timestamp.hh:1183
static Timestamp now_steady()
Return the current steady-clock time.
Definition timestamp.hh:986
static Timestamp make_usec(seconds_type sec, uint32_t usec)
Return a timestamp representing sec seconds plus usec microseconds.
Definition timestamp.hh:349
value_type usecval() const
Return this timestamp's interval length in microseconds.
Definition timestamp.hh:281
Microsecond-based token bucket rate limiter.
Helper class for token bucket rate limiter.
Definition tokenbucket.hh:639
T token_type
The type of tokens. Always unsigned.
Definition tokenbucket.hh:642
static Timestamp timestamp(duration_type x)
Return the Timestamp representing a given tick count.
Definition tokenbucket.hh:692
click_jiffies_t time_point_type
The type of a time point. Always unsigned.
Definition tokenbucket.hh:645
static unsigned frequency()
Return the number of time points per period.
Definition tokenbucket.hh:682
static time_point_type now()
Return the current time point.
Definition tokenbucket.hh:652
static bool time_less(time_point_type a, time_point_type b)
Return true if a happened before b.
Definition tokenbucket.hh:675
click_jiffies_difference_t duration_type
The type of a difference between time points. Always signed.
Definition tokenbucket.hh:648
static Timestamp timestamp(time_point_type x)
Return the Timestamp representing a given time point.
Definition tokenbucket.hh:687
static duration_type time_monotonic_difference(time_point_type a, time_point_type b)
Return b - a, assuming that b was measured after a.
Definition tokenbucket.hh:666
Helper class for token bucket rate limiter.
Definition tokenbucket.hh:706
static unsigned frequency()
Return the number of time points per period.
Definition tokenbucket.hh:745
static time_point_type now()
Return the current time point.
Definition tokenbucket.hh:719
static duration_type time_monotonic_difference(time_point_type a, time_point_type b)
Return b - a, assuming that b was measured after a.
Definition tokenbucket.hh:733
click_intmax_t duration_type
The type of a difference between time points. Always signed.
Definition tokenbucket.hh:715
static Timestamp timestamp(time_point_type x)
Return the Timestamp representing a given time point.
Definition tokenbucket.hh:750
click_uintmax_t time_point_type
The type of a time point. Always unsigned.
Definition tokenbucket.hh:712
static bool time_less(time_point_type a, time_point_type b)
Return true if a happened before b.
Definition tokenbucket.hh:738
static Timestamp timestamp(duration_type x)
Return the Timestamp representing a given tick count.
Definition tokenbucket.hh:755
T token_type
The type of tokens. Always unsigned.
Definition tokenbucket.hh:709
Token bucket rate limiter.
Definition tokenbucket.hh:773
token_type rate() const
Return the rate in tokens per period.
Definition tokenbucket.hh:876
bool contains_fraction(token_type f) const
Test if the token bucket is above a fraction of its capacity.
Definition tokenbucket.hh:924
void set_time_point(time_point_type time)
Set the token bucket's internal time point to time.
Definition tokenbucket.hh:983
bool full() const
Test if the token bucket is at full capacity.
Definition tokenbucket.hh:910
ticks_type time_until_contains(token_type t) const
Return the number of ticks until contains(t).
Definition tokenbucket.hh:1033
void set_fraction(token_type f)
Set the token bucket to a fraction of its capacity.
Definition tokenbucket.hh:952
ticks_type time_until_contains_fraction(ticks_type f) const
Return the number of ticks until contains_fraction(f).
Definition tokenbucket.hh:1041
bool empty() const
Test if the token bucket is completely empty.
Definition tokenbucket.hh:905
token_type fraction() const
Return the bucket's fullness fraction.
Definition tokenbucket.hh:900
bool idle() const
Return true iff the token rate is idle.
Definition tokenbucket.hh:868
void assign_adjust(token_type rate, token_type capacity)
Set the token bucket rate and capacity, preserving the token count.
Definition tokenbucket.hh:856
TokenRateX< P > rate_type
The token rate type.
Definition tokenbucket.hh:779
void refill(time_point_type time)
Refill the token bucket for time.
Definition tokenbucket.hh:969
rate_type::ticks_type ticks_type
Unsigned type of differences between time ticks.
Definition tokenbucket.hh:791
TokenBucketX(bool unlimited)
Construct an idle or unlimited token bucket.
Definition tokenbucket.hh:807
bool remove_fraction_if(token_type f)
Remove a fullness fraction from the bucket if it is full enough.
Definition tokenbucket.hh:1025
void refill(U time)
Refill the token bucket for time P::time_point(time).
Definition tokenbucket.hh:974
void assign(bool unlimited=false)
Set the token bucket rate to idle or unlimited.
Definition tokenbucket.hh:826
bool remove_if(token_type t)
Remove t tokens from the bucket if it contains t tokens.
Definition tokenbucket.hh:1007
TokenBucketX()
Construct an idle token bucket.
Definition tokenbucket.hh:800
void clear()
Clear the token bucket.
Definition tokenbucket.hh:931
P parameter_type
The template parameter type.
Definition tokenbucket.hh:776
TokenBucketX(token_type rate, token_type capacity)
Construct a token bucket representing rate.
Definition tokenbucket.hh:820
TokenCounterX< rate_type > counter_type
The token counter type.
Definition tokenbucket.hh:782
void set(token_type t)
Set the token bucket to contain t tokens.
Definition tokenbucket.hh:946
rate_type::time_point_type time_point_type
Type of time ticks.
Definition tokenbucket.hh:788
token_type size() const
Return the number of tokens in the bucket.
Definition tokenbucket.hh:892
token_type capacity() const
Return the capacity in tokens.
Definition tokenbucket.hh:884
void set_full()
Fill the token bucket to capacity.
Definition tokenbucket.hh:938
bool contains(token_type t) const
Test if the token bucket has at least t tokens.
Definition tokenbucket.hh:918
void assign(token_type rate, token_type capacity)
Set the token bucket rate and capacity.
Definition tokenbucket.hh:843
void remove(token_type t)
Remove t tokens from the bucket.
Definition tokenbucket.hh:996
bool unlimited() const
Return true iff the token rate is unlimited.
Definition tokenbucket.hh:863
void remove_fraction(token_type f)
Remove a fullness fraction from the bucket.
Definition tokenbucket.hh:1015
rate_type::token_type token_type
Unsigned type of token counts.
Definition tokenbucket.hh:785
void refill()
Refill the token bucket to time P::now().
Definition tokenbucket.hh:964
Microsecond-based token counter.
Token bucket counter template.
Definition tokenbucket.hh:328
bool contains(const rate_type &rate, token_type t) const
Test if the token counter has at least t tokens.
Definition tokenbucket.hh:395
void remove(const rate_type &rate, token_type t)
Remove t tokens from the counter.
Definition tokenbucket.hh:502
void set_fraction(token_type f)
Set the token counter to a fraction of its capacity.
Definition tokenbucket.hh:431
TokenCounterX(bool full)
Construct a TokenCounter.
Definition tokenbucket.hh:358
token_type fraction() const
Return the counter's fullness fraction.
Definition tokenbucket.hh:375
void clear()
Clear the token counter.
Definition tokenbucket.hh:408
void refill(const rate_type &rate)
Refill the token counter to time rate.now().
Definition tokenbucket.hh:606
void adjust(const rate_type &old_rate, const rate_type &new_rate)
Compensate the counter for a change of rate.
Definition tokenbucket.hh:445
time_point_type time_point()
Return the internal time point.
Definition tokenbucket.hh:492
void set_time_point(time_point_type time)
Set the token counter's internal time point to time.
Definition tokenbucket.hh:484
void refill(const rate_type &rate, U time)
Refill the token counter for time.
Definition tokenbucket.hh:612
TokenCounterX()
Construct an empty TokenCounter.
Definition tokenbucket.hh:349
void refill(const rate_type &rate, time_point_type time)
Refill the token counter for time.
Definition tokenbucket.hh:590
R rate_type
The token rate type.
Definition tokenbucket.hh:331
bool contains_fraction(token_type f) const
Test if the token counter is above a fraction of its capacity.
Definition tokenbucket.hh:401
R::ticks_type ticks_type
Unsigned type of tick counts (differences between time points).
Definition tokenbucket.hh:340
void set_full()
Fill the token counter to capacity.
Definition tokenbucket.hh:415
bool remove_fraction_if(token_type f)
Remove a fullness fraction from the counter if it is full enough.
Definition tokenbucket.hh:533
bool empty() const
Test if the token counter is completely empty.
Definition tokenbucket.hh:380
ticks_type time_until_contains_fraction(const rate_type &rate, token_type f) const
Return the number of ticks until contains_fraction(f).
Definition tokenbucket.hh:564
ticks_type time_until_contains(const rate_type &rate, token_type t) const
Return the number of ticks until contains(rate, t).
Definition tokenbucket.hh:549
bool full() const
Test if the token counter is at full capacity.
Definition tokenbucket.hh:385
void set(const rate_type &rate, token_type t)
Set the token counter to contain t tokens.
Definition tokenbucket.hh:424
token_type size(const rate_type &rate) const
Return the number of tokens in the counter.
Definition tokenbucket.hh:367
void remove_fraction(token_type f)
Remove a fullness fraction from the counter.
Definition tokenbucket.hh:523
bool remove_if(const rate_type &rate, token_type t)
Remove t tokens from the counter if it contains t tokens.
Definition tokenbucket.hh:515
R::token_type token_type
Unsigned type of token counts.
Definition tokenbucket.hh:334
R::time_point_type time_point_type
Type of time points.
Definition tokenbucket.hh:337
Microsecond-based token bucket rate.
Token bucket rate template.
Definition tokenbucket.hh:60
void assign(bool unlimited=false)
Set the token rate to idle or unlimited.
Definition tokenbucket.hh:196
token_type tokens_per_tick() const
Return the number of tokens per tick.
Definition tokenbucket.hh:137
bool unlimited() const
Return true iff the token rate is unlimited.
Definition tokenbucket.hh:113
ticks_type time_until_full() const
Return the number of ticks required to refill a counter to capacity.
Definition tokenbucket.hh:150
P parameter_type
The template parameter type.
Definition tokenbucket.hh:63
bool idle() const
Return true iff the token rate is idle.
Definition tokenbucket.hh:118
P::token_type token_type
Unsigned type of token counts.
Definition tokenbucket.hh:66
token_type rate() const
Return the rate in tokens per period.
Definition tokenbucket.hh:245
P::time_point_type time_point_type
Type of time points.
Definition tokenbucket.hh:69
ticks_type time_monotonic_difference(time_point_type a, time_point_type b) const
Return b - a, assuming that b was measured after a.
Definition tokenbucket.hh:176
TokenRateX(bool unlimited)
Construct an idle or unlimited token rate.
Definition tokenbucket.hh:85
make_unsigned< typenameP::duration_type >::type ticks_type
Unsigned type of tick counts (differences between time points).
Definition tokenbucket.hh:72
time_point_type time_point(U time) const
Return the time point corresponding to the time parameter.
Definition tokenbucket.hh:166
TokenRateX(token_type rate, token_type capacity)
Construct a token rate representing rate.
Definition tokenbucket.hh:96
token_type token_scale() const
Return the ratio of fractional tokens to real tokens.
Definition tokenbucket.hh:142
token_type capacity() const
Return the capacity in tokens.
Definition tokenbucket.hh:132
time_point_type now() const
Return the current time point.
Definition tokenbucket.hh:157
TokenRateX()
Construct an idle token rate.
Definition tokenbucket.hh:79
void int_multiply(T a, T b, T &xlow, T &xhigh)
Multiply a * b, placing the low-order bits of the result in xlow and the high-order bits in xhigh.
Definition integers.hh:395
The Timestamp class represents a moment or interval in time.