From c066be39052302aaee593864f058f991b7508efd Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Thu, 27 Apr 2017 12:02:21 -0700 Subject: [PATCH] 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). --- userspace/engine/token_bucket.cpp | 10 ++++++++-- userspace/engine/token_bucket.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/userspace/engine/token_bucket.cpp b/userspace/engine/token_bucket.cpp index 1c30c649..955c2808 100644 --- a/userspace/engine/token_bucket.cpp +++ b/userspace/engine/token_bucket.cpp @@ -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_max_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) diff --git a/userspace/engine/token_bucket.h b/userspace/engine/token_bucket.h index 2f767ef4..9bd91e1c 100644 --- a/userspace/engine/token_bucket.h +++ b/userspace/engine/token_bucket.h @@ -31,7 +31,7 @@ public: // // 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