From 87a6c742905672a3ac5929fb675a35c1e0a64dc6 Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Tue, 25 Apr 2017 10:01:25 -0700 Subject: [PATCH 1/2] Allow for an external clock in token bucket. Allow now to be externally provided to avoid unnecessary gettimeofday() calls. --- userspace/engine/token_bucket.cpp | 8 ++++++-- userspace/engine/token_bucket.h | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/userspace/engine/token_bucket.cpp b/userspace/engine/token_bucket.cpp index 93f0492475e..bd9668bce06 100644 --- a/userspace/engine/token_bucket.cpp +++ b/userspace/engine/token_bucket.cpp @@ -39,11 +39,15 @@ void token_bucket::init(uint32_t rate, uint32_t max_tokens) m_last_seen = sinsp_utils::get_current_time_ns(); } -bool token_bucket::claim() +bool token_bucket::claim(uint64_t now) { // Determine the number of tokens gained. Delta between // last_seen and now, divided by the rate. - uint64_t now = sinsp_utils::get_current_time_ns(); + if(now == 0) + { + now = sinsp_utils::get_current_time_ns(); + } + uint64_t tokens_gained = (now - m_last_seen) / (m_rate * 1000000000); m_last_seen = now; diff --git a/userspace/engine/token_bucket.h b/userspace/engine/token_bucket.h index b9c0d8b9d22..7fe97181618 100644 --- a/userspace/engine/token_bucket.h +++ b/userspace/engine/token_bucket.h @@ -37,7 +37,7 @@ class token_bucket // Returns true if a token can be claimed. Also updates // internal metrics. // - bool claim(); + bool claim(uint64_t now = 0); private: // From e183de3b89d9bb7864d24be0bec4554ce5d1b778 Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Tue, 25 Apr 2017 13:02:34 -0700 Subject: [PATCH 2/2] Allow rate to be less than 1. Change all the token-related variables to doubles so the rate can be less than 1. --- userspace/engine/token_bucket.cpp | 4 ++-- userspace/engine/token_bucket.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/userspace/engine/token_bucket.cpp b/userspace/engine/token_bucket.cpp index bd9668bce06..b72ae399419 100644 --- a/userspace/engine/token_bucket.cpp +++ b/userspace/engine/token_bucket.cpp @@ -31,7 +31,7 @@ token_bucket::~token_bucket() { } -void token_bucket::init(uint32_t rate, uint32_t max_tokens) +void token_bucket::init(double rate, double max_tokens) { m_rate = rate; m_max_tokens = max_tokens; @@ -48,7 +48,7 @@ bool token_bucket::claim(uint64_t now) now = sinsp_utils::get_current_time_ns(); } - uint64_t tokens_gained = (now - m_last_seen) / (m_rate * 1000000000); + double tokens_gained = (now - m_last_seen) / (m_rate * 1000000000); m_last_seen = now; m_tokens += tokens_gained; diff --git a/userspace/engine/token_bucket.h b/userspace/engine/token_bucket.h index 7fe97181618..b60b531909b 100644 --- a/userspace/engine/token_bucket.h +++ b/userspace/engine/token_bucket.h @@ -31,7 +31,7 @@ class token_bucket // // Initialize the token bucket and start accumulating tokens // - void init(uint32_t rate, uint32_t max_tokens); + void init(double rate, double max_tokens); // // Returns true if a token can be claimed. Also updates @@ -43,18 +43,18 @@ class token_bucket // // The number of tokens generated per second. // - uint64_t m_rate; + double m_rate; // // The maximum number of tokens that can be banked for future // claim()s. // - uint64_t m_max_tokens; + double m_max_tokens; // // The current number of tokens // - uint64_t m_tokens; + double m_tokens; // // The last time claim() was called (or the object was created).