From f5ce6752be33da2e287c625048bd18c20d282452 Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Thu, 27 Apr 2017 11:22:19 -0700 Subject: [PATCH 1/3] Add ability to get number of tokens. Add a method to fetch the current number of available tokens. --- userspace/engine/token_bucket.cpp | 5 +++++ userspace/engine/token_bucket.h | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/userspace/engine/token_bucket.cpp b/userspace/engine/token_bucket.cpp index f40112e7..1c30c649 100644 --- a/userspace/engine/token_bucket.cpp +++ b/userspace/engine/token_bucket.cpp @@ -73,3 +73,8 @@ bool token_bucket::claim(uint64_t now) return true; } + +double token_bucket::get_tokens() +{ + return m_tokens; +} diff --git a/userspace/engine/token_bucket.h b/userspace/engine/token_bucket.h index b60b5319..2f767ef4 100644 --- a/userspace/engine/token_bucket.h +++ b/userspace/engine/token_bucket.h @@ -38,6 +38,10 @@ public: // internal metrics. // bool claim(uint64_t now = 0); + + // Return the current number of tokens available + double get_tokens(); + private: // From c066be39052302aaee593864f058f991b7508efd Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Thu, 27 Apr 2017 12:02:21 -0700 Subject: [PATCH 2/3] 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 From dafc4c2b8832374fd7ef9c65be8fee48d957b2fe Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Thu, 27 Apr 2017 12:03:02 -0700 Subject: [PATCH 3/3] Expose last seen time. Also expose last seen time for token bucket. --- userspace/engine/token_bucket.cpp | 5 +++++ userspace/engine/token_bucket.h | 3 +++ 2 files changed, 8 insertions(+) diff --git a/userspace/engine/token_bucket.cpp b/userspace/engine/token_bucket.cpp index 955c2808..e9f28ce0 100644 --- a/userspace/engine/token_bucket.cpp +++ b/userspace/engine/token_bucket.cpp @@ -84,3 +84,8 @@ double token_bucket::get_tokens() { return m_tokens; } + +uint64_t token_bucket::get_last_seen() +{ + return m_last_seen; +} diff --git a/userspace/engine/token_bucket.h b/userspace/engine/token_bucket.h index 9bd91e1c..076ba07b 100644 --- a/userspace/engine/token_bucket.h +++ b/userspace/engine/token_bucket.h @@ -42,6 +42,9 @@ public: // Return the current number of tokens available double get_tokens(); + // Return the last time someone tried to claim a token. + uint64_t get_last_seen(); + private: //