Allow the initial time to be externally provided.

Allow the initial start time to be externally provided. Saves a call to
getttimeofday and allows running from an external clock (i.e. trace files).
This commit is contained in:
Mark Stemm 2017-04-27 12:02:21 -07:00
parent f5ce6752be
commit c066be3905
2 changed files with 9 additions and 3 deletions

View File

@ -31,12 +31,18 @@ token_bucket::~token_bucket()
{ {
} }
void token_bucket::init(double rate, double max_tokens) void token_bucket::init(double rate, double max_tokens, uint64_t now)
{ {
m_rate = rate; m_rate = rate;
m_max_tokens = max_tokens; m_max_tokens = max_tokens;
m_tokens = max_tokens; m_tokens = max_tokens;
m_last_seen = sinsp_utils::get_current_time_ns();
if(now == 0)
{
now = sinsp_utils::get_current_time_ns();
}
m_last_seen = now;
} }
bool token_bucket::claim(uint64_t now) bool token_bucket::claim(uint64_t now)

View File

@ -31,7 +31,7 @@ public:
// //
// Initialize the token bucket and start accumulating tokens // Initialize the token bucket and start accumulating tokens
// //
void init(double rate, double max_tokens); void init(double rate, double max_tokens, uint64_t now = 0);
// //
// Returns true if a token can be claimed. Also updates // Returns true if a token can be claimed. Also updates