From 4aabf0ee5dd2d44561245eac82e12a4b9baa86bc Mon Sep 17 00:00:00 2001 From: Corey Zumar Date: Fri, 23 Jun 2017 16:54:45 -0700 Subject: [PATCH 1/3] ewma fixes --- src/libclipper/src/metrics.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libclipper/src/metrics.cpp b/src/libclipper/src/metrics.cpp index dd67c09b8..1441d8023 100644 --- a/src/libclipper/src/metrics.cpp +++ b/src/libclipper/src/metrics.cpp @@ -22,7 +22,6 @@ namespace clipper { namespace metrics { constexpr long MICROS_PER_SECOND = 1000000; -constexpr long CLOCKS_PER_MILLISECOND = CLOCKS_PER_SEC / MICROS_PER_SECOND; constexpr double SECONDS_PER_MINUTE = 60; constexpr double ONE_MINUTE = 1; constexpr double FIVE_MINUTES = 5; @@ -229,8 +228,11 @@ void RatioCounter::clear() { } long RealTimeClock::get_time_micros() const { - clock_t clocks = clock(); - return static_cast(clocks) * CLOCKS_PER_MILLISECOND; + long current_time_micros = + std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + return current_time_micros; } void PresetClock::set_time_micros(const long time_micros) { @@ -308,7 +310,8 @@ void Meter::mark(uint32_t num) { void Meter::tick_if_necessary() { long curr_micros = clock_->get_time_micros(); - long tick_interval_micros = ewma_tick_interval_seconds_ * MICROS_PER_SECOND; + auto tick_interval_seconds = std::chrono::seconds(ewma_tick_interval_seconds_); + long tick_interval_micros = std::chrono::microseconds(tick_interval_seconds).count(); long last_tick = last_ewma_tick_micros_.load(std::memory_order_seq_cst); long time_since_last_tick = curr_micros - last_tick; @@ -358,6 +361,7 @@ double Meter::get_five_minute_rate_seconds() { } double Meter::get_fifteen_minute_rate_seconds() { + tick_if_necessary(); return m15_rate.get_rate_seconds(); } From f397cb84323a76f66ba9c6111886ccdc270a110f Mon Sep 17 00:00:00 2001 From: Corey Zumar Date: Fri, 23 Jun 2017 17:24:35 -0700 Subject: [PATCH 2/3] Add missing rate ticks, use chrono to ensure temporal correctness --- src/libclipper/src/metrics.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libclipper/src/metrics.cpp b/src/libclipper/src/metrics.cpp index 1441d8023..b04d4804d 100644 --- a/src/libclipper/src/metrics.cpp +++ b/src/libclipper/src/metrics.cpp @@ -302,6 +302,7 @@ Meter::Meter(std::string name, std::shared_ptr clock) m15_rate(ewma_tick_interval_seconds_, LoadAverage::FifteenMinute) {} void Meter::mark(uint32_t num) { + tick_if_necessary(); count_.fetch_add(num, std::memory_order_relaxed); m1_rate.mark_uncounted(num); m5_rate.mark_uncounted(num); @@ -328,7 +329,7 @@ void Meter::tick_if_necessary() { if (last_tick_update_successful) { double num_ticks = static_cast(time_since_last_tick) / - static_cast(tick_interval_micros); + static_cast(tick_interval_micros); for (int i = 0; i < static_cast(num_ticks); i++) { m1_rate.tick(); m5_rate.tick(); From 228fe934d854bf6ece575562474cda12a08074d5 Mon Sep 17 00:00:00 2001 From: Corey Zumar Date: Fri, 23 Jun 2017 17:26:07 -0700 Subject: [PATCH 3/3] Format code --- src/libclipper/src/metrics.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libclipper/src/metrics.cpp b/src/libclipper/src/metrics.cpp index b04d4804d..64350a8c1 100644 --- a/src/libclipper/src/metrics.cpp +++ b/src/libclipper/src/metrics.cpp @@ -311,8 +311,10 @@ void Meter::mark(uint32_t num) { void Meter::tick_if_necessary() { long curr_micros = clock_->get_time_micros(); - auto tick_interval_seconds = std::chrono::seconds(ewma_tick_interval_seconds_); - long tick_interval_micros = std::chrono::microseconds(tick_interval_seconds).count(); + auto tick_interval_seconds = + std::chrono::seconds(ewma_tick_interval_seconds_); + long tick_interval_micros = + std::chrono::microseconds(tick_interval_seconds).count(); long last_tick = last_ewma_tick_micros_.load(std::memory_order_seq_cst); long time_since_last_tick = curr_micros - last_tick; @@ -329,7 +331,7 @@ void Meter::tick_if_necessary() { if (last_tick_update_successful) { double num_ticks = static_cast(time_since_last_tick) / - static_cast(tick_interval_micros); + static_cast(tick_interval_micros); for (int i = 0; i < static_cast(num_ticks); i++) { m1_rate.tick(); m5_rate.tick();