]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/Throttle: update BackoffThrottle::get() to work with unsigned delay
authorKefu Chai <kchai@redhat.com>
Wed, 21 Nov 2018 16:38:37 +0000 (00:38 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 22 Nov 2018 03:26:43 +0000 (11:26 +0800)
* in fa3acba3, we switched the delay's type from
  std::chrono::duration<double> 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 <kchai@redhat.com>
src/common/Throttle.cc

index 73c10789f0145b9d69fa0a019b223524d301802d..c80ad2b571e1ee8d1913d7abb2dda3d313439d37 100644 (file)
@@ -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)