|
Click
|
Token bucket counter template. More...
#include <click/tokenbucket.hh>
Public Types | |
| enum | { max_tokens = R::max_tokens } |
| typedef R | rate_type |
| The token rate type. | |
| typedef R::token_type | token_type |
| Unsigned type of token counts. | |
| typedef R::time_point_type | time_point_type |
| Type of time points. | |
| typedef R::ticks_type | ticks_type |
| Unsigned type of tick counts (differences between time points). | |
Public Member Functions | |
| TokenCounterX () | |
| Construct an empty TokenCounter. | |
| TokenCounterX (bool full) | |
| Construct a TokenCounter. | |
| token_type | size (const rate_type &rate) const |
| Return the number of tokens in the counter. | |
| token_type | fraction () const |
| Return the counter's fullness fraction. | |
| bool | empty () const |
| Test if the token counter is completely empty. | |
| bool | full () const |
| Test if the token counter is at full capacity. | |
| bool | contains (const rate_type &rate, token_type t) const |
| Test if the token counter has at least t tokens. | |
| bool | contains_fraction (token_type f) const |
| Test if the token counter is above a fraction of its capacity. | |
| void | clear () |
| Clear the token counter. | |
| void | set_full () |
| Fill the token counter to capacity. | |
| void | set (const rate_type &rate, token_type t) |
| Set the token counter to contain t tokens. | |
| void | set_fraction (token_type f) |
| Set the token counter to a fraction of its capacity. | |
| void | adjust (const rate_type &old_rate, const rate_type &new_rate) |
| Compensate the counter for a change of rate. | |
| void | refill (const rate_type &rate) |
| Refill the token counter to time rate.now(). | |
| void | refill (const rate_type &rate, time_point_type time) |
| Refill the token counter for time. | |
| template<typename U > | |
| void | refill (const rate_type &rate, U time) |
| Refill the token counter for time. | |
| void | set_time_point (time_point_type time) |
| Set the token counter's internal time point to time. | |
| time_point_type | time_point () |
| Return the internal time point. | |
| void | remove (const rate_type &rate, token_type t) |
| Remove t tokens from the counter. | |
| bool | remove_if (const rate_type &rate, token_type t) |
| Remove t tokens from the counter if it contains t tokens. | |
| void | remove_fraction (token_type f) |
| Remove a fullness fraction from the counter. | |
| bool | remove_fraction_if (token_type f) |
| Remove a fullness fraction from the counter if it is full enough. | |
| ticks_type | time_until_contains (const rate_type &rate, token_type t) const |
| Return the number of ticks until contains(rate, t). | |
| ticks_type | time_until_contains_fraction (const rate_type &rate, token_type f) const |
| Return the number of ticks until contains_fraction(f). | |
Token bucket counter template.
never never
The TokenCounterX class implements a token counter associated with a token bucket rate. The rate type, normally a TokenRateX, is specified as a template parameter. Most of its member functions take an explicit rate argument. The contains() method reports whether the counter has at least a given number of tokens. The counter is emptied by the remove() and remove_if() methods and refilled by the refill() methods.
Often the token rate associated with a counter will not change during the counter's lifetime. TokenCounterX will work correctly if the rate changes, however. (See the adjust() method for details.)
TokenCounterX internally maintains fractional tokens, so it should be relatively precise.
Idle and unlimited rates affect how TokenCounters are refilled. For idle rates, refill() is a no-op. For unlimited rates, any refill() makes the counter full(), containing max_tokens tokens. The set(), empty(), full(), remove(), and similar functions act as normal for idle and unlimited rates.
Most users will be satisfied with the TokenCounter type, which is equal to TokenCounterX<TokenRateX<TokenBucketUsecParameters<unsigned long long> > >.
| typedef R TokenCounterX< R >::rate_type |
The token rate type.
| typedef R::token_type TokenCounterX< R >::token_type |
Unsigned type of token counts.
| typedef R::time_point_type TokenCounterX< R >::time_point_type |
Type of time points.
| typedef R::ticks_type TokenCounterX< R >::ticks_type |
Unsigned type of tick counts (differences between time points).
|
inline |
Construct an empty TokenCounter.
The initial time point is 0.
|
inlineexplicit |
Construct a TokenCounter.
| full | whether the counter is created full |
The counter is initially full() if full is true, otherwise it is empty. The initial time point is 0.
|
inline |
Return the number of tokens in the counter.
| rate | associated token rate |
The return value is a lower bound on the number of tokens, since TokenCounterX keeps track of fractional tokens.
|
inline |
Return the counter's fullness fraction.
The return value is a number between 0 and max_tokens, where max_tokens represents full capacity.
|
inline |
Test if the token counter is completely empty.
|
inline |
Test if the token counter is at full capacity.
|
inline |
Test if the token counter has at least t tokens.
| rate | associated token rate |
| t | token count |
Returns false whenever t is greater than rate.capacity().
|
inline |
Test if the token counter is above a fraction of its capacity.
| f | fullness fraction, where max_tokens is full capacity |
|
inline |
Clear the token counter.
|
inline |
|
inline |
Set the token counter to contain t tokens.
| rate | associated token rate |
| t | number of tokens |
The result will never have more tokens than the associated capacity.
|
inline |
Set the token counter to a fraction of its capacity.
| f | fullness fraction, where max_tokens is full capacity |
|
inline |
Compensate the counter for a change of rate.
| old_rate | old associated token rate |
| new_rate | new associated token rate |
TokenCounterX's internal representation stores the token count as a fraction of the rate's capacity. This means that if you change the associated rate to have a different capacity, the token count will appear to change. To keep the token count roughly the same, call adjust() with the old and new rates; TokenCounterX will as far as possible compensate for the rate change.
| void TokenCounterX< R >::refill | ( | const rate_type & | rate | ) |
Refill the token counter to time rate.now().
| rate | associated token rate |
There are three refill() methods, useful for different methods of measuring time. This method calls rate.now(), which returns the current time. Other methods use an explicit time point and a rate.time_point(U) method.
| void TokenCounterX< R >::refill | ( | const rate_type & | rate, |
| time_point_type | time | ||
| ) |
Refill the token counter for time.
| rate | associated token rate |
| time | new time point |
| void TokenCounterX< R >::refill | ( | const rate_type & | rate, |
| U | time | ||
| ) |
Refill the token counter for time.
| rate | associated token rate |
| time | new time |
|
inline |
|
inline |
|
inline |
Remove t tokens from the counter.
| rate | associated token rate |
| t | number of tokens |
If the token counter contains less than t tokens, the new token count is 0.
|
inline |
Remove t tokens from the counter if it contains t tokens.
| rate | associated token rate |
| t | number of tokens |
If the counter contains t or more tokens, calls remove(t) and returns true. If it contains less than t tokens, returns false without removing any tokens.
|
inline |
Remove a fullness fraction from the counter.
| f | fullness fraction, where max_tokens is full capacity |
If the token counter is less than f full, the new token count is 0.
|
inline |
Remove a fullness fraction from the counter if it is full enough.
| f | fullness fraction, where max_tokens is full capacity |
If fraction() is at least f, calls remove_fraction(f) and returns true. Otherwise, returns false without removing any tokens.
|
inline |
Return the number of ticks until contains(rate, t).
| rate | associated token rate |
| t | token count |
Returns (ticks_type) -1 if passing time will never make contains(rate, t) true.
|
inline |
Return the number of ticks until contains_fraction(f).
| rate | associated token rate |
| f | fullness fraction, where max_tokens is full capacity |
Returns (ticks_type) -1 if passing time will never make contains_fraction(f) true.