From 63ced34d3ee4bb413cf85401f767385daca55a20 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 22 Nov 2018 00:38:37 +0800 Subject: [PATCH] common/Throttle: update BackoffThrottle::get() to work with unsigned delay * in fa3acba3, we switched the delay's type from std::chrono::duration to ceph::timespan, the former's Rep is a signed type, while the latter is unsigned. so we need to update the algorithm in get() to accomodate this change. * use mono_clock instead of real_clock for better performance * check for 0 timespan using count() == 0, instead of comparing it with a temporary object created using ceph::timespan(0) for better performance Signed-off-by: Kefu Chai --- src/common/Throttle.cc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/common/Throttle.cc b/src/common/Throttle.cc index 73c10789f01..c80ad2b571e 100644 --- a/src/common/Throttle.cc +++ b/src/common/Throttle.cc @@ -423,7 +423,7 @@ ceph::timespan BackoffThrottle::get(uint64_t c) } // fast path - if (delay == ceph::make_timespan(0) && + if (delay.count() == 0 && waiters.empty() && ((max == 0) || (current == 0) || ((current + c) <= max))) { current += c; @@ -444,20 +444,26 @@ ceph::timespan BackoffThrottle::get(uint64_t c) waited = true; } - auto start = ceph::real_clock::now(); + auto start = mono_clock::now(); delay = _get_delay(c); while (true) { - if (!((max == 0) || (current == 0) || (current + c) <= max)) { + if (max != 0 && current != 0 && (current + c) > max) { (*ticket)->wait(l); waited = true; - } else if (delay > ceph::timespan(0)) { + } else if (delay.count() > 0) { (*ticket)->wait_for(l, delay); waited = true; } else { break; } ceph_assert(ticket == waiters.begin()); - delay = _get_delay(c) - (ceph::real_clock::now() - start); + delay = _get_delay(c); + auto elapsed = mono_clock::now() - start; + if (delay <= elapsed) { + delay = timespan::zero(); + } else { + delay -= elapsed; + } } waiters.pop_front(); _kick_waiters(); @@ -471,7 +477,7 @@ ceph::timespan BackoffThrottle::get(uint64_t c) } } - return ceph::real_clock::now() - start; + return mono_clock::now() - start; } uint64_t BackoffThrottle::put(uint64_t c) -- 2.39.5