From e33e002d98bc62cb3ed3b65bc3b0101156ac11a0 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 8 Mar 2024 08:52:52 -0500 Subject: [PATCH] MT#40962 decouple RateLimit from AmAppTimer Remove dependency on AmAppTimer's wall_clock ticks by switching to direct call of gettimeofday() and converting the internally used time units to milliseconds instead of the tick-based wall clock time. Remove ::getLastUpdate() as it's unused. Change-Id: Ia3a026103b594351c0a6943de67dd94883e30578 --- apps/sbc/RateLimit.cpp | 20 +++++++++++++------- apps/sbc/RateLimit.h | 8 ++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/apps/sbc/RateLimit.cpp b/apps/sbc/RateLimit.cpp index ba2876bc..01723b7f 100644 --- a/apps/sbc/RateLimit.cpp +++ b/apps/sbc/RateLimit.cpp @@ -1,13 +1,12 @@ #include "RateLimit.h" #include "AmAppTimer.h" +#include #define min(a,b) ((a) < (b) ? (a) : (b)) -DynRateLimit::DynRateLimit(unsigned int time_base_ms) - : last_update(0), counter(0) +DynRateLimit::DynRateLimit(unsigned int _time_base_ms) + : last_update(0), counter(0), time_base_ms(_time_base_ms) { - // wall_clock has a resolution of 20ms - time_base = time_base_ms / 20; } bool DynRateLimit::limit(unsigned int rate, unsigned int peak, @@ -15,8 +14,8 @@ bool DynRateLimit::limit(unsigned int rate, unsigned int peak, { lock(); - if(AmAppTimer::instance()->wall_clock - last_update - > time_base) { + if(wall_clock_ms() - last_update + > time_base_ms) { update_limit(rate,peak); } @@ -35,5 +34,12 @@ bool DynRateLimit::limit(unsigned int rate, unsigned int peak, void DynRateLimit::update_limit(int rate, int peak) { counter = min(peak, counter+rate); - last_update = AmAppTimer::instance()->wall_clock; + last_update = wall_clock_ms(); +} + +u_int32_t DynRateLimit::wall_clock_ms() +{ + struct timeval now; + gettimeofday(&now, NULL); + return now.tv_sec * 1000 + now.tv_usec / 1000; } diff --git a/apps/sbc/RateLimit.h b/apps/sbc/RateLimit.h index 507fc78f..b67e7550 100644 --- a/apps/sbc/RateLimit.h +++ b/apps/sbc/RateLimit.h @@ -11,7 +11,7 @@ class DynRateLimit u_int32_t last_update; int counter; - unsigned int time_base; + unsigned int time_base_ms; void update_limit(int rate, int peak); @@ -21,7 +21,7 @@ public: virtual ~DynRateLimit() {} - unsigned int getTimeBase() const { return time_base; } + unsigned int getTimeBase() const { return time_base_ms * 20; /* 20 ms increments */ } /** * rate: units/time_base @@ -30,8 +30,8 @@ public: */ bool limit(unsigned int rate, unsigned int peak, unsigned int size); - /** Get last update timestamp (wheeltimer::wallclock ticks) */ - u_int32_t getLastUpdate() { return last_update; } +private: + static u_int32_t wall_clock_ms(); }; class RateLimit