Commit 57ebbcd1 authored by Daniel Xu's avatar Daniel Xu Committed by Facebook Github Bot

Use coarse_steady_clock instead of steady_clock

Summary:
IntervalRateLimiter was already operating on millisecond
granularity. coarse_steady_clock offers better performance because
it's on millisecond granularity.

Reviewed By: yfeldblum

Differential Revision: D8245042

fbshipit-source-id: 7ce0e347c0498ff2741adc727577c3bb2ce63979
parent 0865b933
...@@ -19,14 +19,14 @@ namespace folly { ...@@ -19,14 +19,14 @@ namespace folly {
namespace logging { namespace logging {
IntervalRateLimiter::IntervalRateLimiter( IntervalRateLimiter::IntervalRateLimiter(
uint64_t maxPerInterval, uint64_t maxPerInterval,
std::chrono::steady_clock::duration interval) clock::duration interval)
: maxPerInterval_{maxPerInterval}, : maxPerInterval_{maxPerInterval},
interval_{interval}, interval_{interval},
timestamp_{std::chrono::steady_clock::now().time_since_epoch().count()} {} timestamp_{clock::now().time_since_epoch().count()} {}
bool IntervalRateLimiter::checkSlow() { bool IntervalRateLimiter::checkSlow() {
auto ts = timestamp_.load(); auto ts = timestamp_.load();
auto now = std::chrono::steady_clock::now().time_since_epoch().count(); auto now = clock::now().time_since_epoch().count();
if (now < (ts + interval_.count())) { if (now < (ts + interval_.count())) {
return false; return false;
} }
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#include <chrono> #include <chrono>
#include <cstdint> #include <cstdint>
#include <folly/Chrono.h>
namespace folly { namespace folly {
namespace logging { namespace logging {
...@@ -40,9 +42,9 @@ class RateLimiter { ...@@ -40,9 +42,9 @@ class RateLimiter {
*/ */
class IntervalRateLimiter : public RateLimiter { class IntervalRateLimiter : public RateLimiter {
public: public:
IntervalRateLimiter( using clock = chrono::coarse_steady_clock;
uint64_t maxPerInterval,
std::chrono::steady_clock::duration interval); IntervalRateLimiter(uint64_t maxPerInterval, clock::duration interval);
bool check() override final { bool check() override final {
auto origCount = count_.fetch_add(1, std::memory_order_acq_rel); auto origCount = count_.fetch_add(1, std::memory_order_acq_rel);
...@@ -56,13 +58,13 @@ class IntervalRateLimiter : public RateLimiter { ...@@ -56,13 +58,13 @@ class IntervalRateLimiter : public RateLimiter {
bool checkSlow(); bool checkSlow();
const uint64_t maxPerInterval_; const uint64_t maxPerInterval_;
const std::chrono::steady_clock::time_point::duration interval_; const clock::time_point::duration interval_;
std::atomic<uint64_t> count_{0}; std::atomic<uint64_t> count_{0};
// Ideally timestamp_ would be a // Ideally timestamp_ would be a
// std::atomic<std::chrono::steady_clock::time_point>, but this does not work // std::atomic<clock::time_point>, but this does not
// since time_point's constructor is not noexcept // work since time_point's constructor is not noexcept
std::atomic<std::chrono::steady_clock::rep> timestamp_; std::atomic<clock::rep> timestamp_;
}; };
} // namespace logging } // namespace logging
} // namespace folly } // namespace folly
...@@ -15,17 +15,19 @@ ...@@ -15,17 +15,19 @@
*/ */
#include <thread> #include <thread>
#include <folly/Chrono.h>
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/logging/RateLimiter.h> #include <folly/logging/RateLimiter.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
using folly::chrono::coarse_steady_clock;
using folly::logging::IntervalRateLimiter; using folly::logging::IntervalRateLimiter;
using std::chrono::duration_cast; using std::chrono::duration_cast;
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
void intervalTest( void intervalTest(
uint64_t eventsPerInterval, uint64_t eventsPerInterval,
std::chrono::steady_clock::duration interval) { coarse_steady_clock::duration interval) {
SCOPED_TRACE(folly::to<std::string>( SCOPED_TRACE(folly::to<std::string>(
eventsPerInterval, eventsPerInterval,
" events every ", " events every ",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment