]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/HeartbeatMap: use std::atomic<time_point> for timeout
authorKefu Chai <kchai@redhat.com>
Sat, 1 Aug 2020 14:44:48 +0000 (22:44 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 3 Aug 2020 01:30:47 +0000 (09:30 +0800)
since we now depends on GCC-7 and up for C++17 support, we can now use
std::atomic<time_point> for better readability.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/HeartbeatMap.cc
src/common/HeartbeatMap.h
src/common/WorkQueue.cc
src/common/WorkQueue.h

index aef9677d9fac3530110a994e68976dd69b735003..6069b0b351d7d7122bd0f4ffafccc26b14a2791b 100644 (file)
@@ -67,17 +67,17 @@ void HeartbeatMap::remove_worker(const heartbeat_handle_d *h)
 }
 
 bool HeartbeatMap::_check(const heartbeat_handle_d *h, const char *who,
-                         ceph::coarse_mono_clock::rep now)
+                         ceph::coarse_mono_time now)
 {
   bool healthy = true;
-  auto was = h->timeout.load();
-  if (was && was < now) {
+  if (auto was = h->timeout.load();
+      !clock::is_zero(was) && was < now) {
     ldout(m_cct, 1) << who << " '" << h->name << "'"
                    << " had timed out after " << h->grace << dendl;
     healthy = false;
   }
-  was = h->suicide_timeout;
-  if (was && was < now) {
+  if (auto was = h->suicide_timeout.load();
+      !clock::is_zero(was) && was < now) {
     ldout(m_cct, 1) << who << " '" << h->name << "'"
                    << " had suicide timed out after " << h->suicide_grace << dendl;
     pthread_kill(h->thread_id, SIGABRT);
@@ -88,33 +88,32 @@ bool HeartbeatMap::_check(const heartbeat_handle_d *h, const char *who,
 }
 
 void HeartbeatMap::reset_timeout(heartbeat_handle_d *h,
-                                ceph::coarse_mono_clock::rep grace,
-                                ceph::coarse_mono_clock::rep suicide_grace)
+                                ceph::timespan grace,
+                                ceph::timespan suicide_grace)
 {
   ldout(m_cct, 20) << "reset_timeout '" << h->name << "' grace " << grace
                   << " suicide " << suicide_grace << dendl;
-  auto now = duration_cast<seconds>(coarse_mono_clock::now()
-                                   .time_since_epoch()).count();
+  const auto now = clock::now();
   _check(h, "reset_timeout", now);
 
   h->timeout = now + grace;
   h->grace = grace;
 
-  if (suicide_grace)
+  if (suicide_grace > ceph::timespan::zero()) {
     h->suicide_timeout = now + suicide_grace;
-  else
-    h->suicide_timeout = 0;
+  } else {
+    h->suicide_timeout = clock::zero();
+  }
   h->suicide_grace = suicide_grace;
 }
 
 void HeartbeatMap::clear_timeout(heartbeat_handle_d *h)
 {
   ldout(m_cct, 20) << "clear_timeout '" << h->name << "'" << dendl;
-  auto now = duration_cast<seconds>(coarse_mono_clock::now()
-                                   .time_since_epoch()).count();
+  auto now = clock::now();
   _check(h, "clear_timeout", now);
-  h->timeout = 0;
-  h->suicide_timeout = 0;
+  h->timeout = clock::zero();
+  h->suicide_timeout = clock::zero();
 }
 
 bool HeartbeatMap::is_healthy()
@@ -141,8 +140,7 @@ bool HeartbeatMap::is_healthy()
        p != m_workers.end();
        ++p) {
     heartbeat_handle_d *h = *p;
-    auto epoch = duration_cast<seconds>(now.time_since_epoch()).count();
-    if (!_check(h, "is_healthy", epoch)) {
+    if (!_check(h, "is_healthy", now)) {
       healthy = false;
       unhealthy++;
     }
index 65f8f94e97871dd5898d6963828a1395cc983dd6..6f486b21ca864fc07a356c2f7fa37ccdb929831d 100644 (file)
@@ -39,14 +39,17 @@ namespace ceph {
 
 struct heartbeat_handle_d {
   const std::string name;
-  pthread_t thread_id;
-  // TODO: use atomic<time_point>, once we can ditch GCC 4.8
-  std::atomic<unsigned> timeout = { 0 }, suicide_timeout = { 0 };
-  time_t grace, suicide_grace;
+  pthread_t thread_id = 0;
+  using clock = ceph::coarse_mono_clock;
+  using time = ceph::coarse_mono_time;
+  std::atomic<time> timeout = clock::zero();
+  std::atomic<time> suicide_timeout = clock::zero();
+  ceph::timespan grace = ceph::timespan::zero();
+  ceph::timespan suicide_grace = ceph::timespan::zero();
   std::list<heartbeat_handle_d*>::iterator list_item;
 
   explicit heartbeat_handle_d(const std::string& n)
-    : name(n), thread_id(0), grace(0), suicide_grace(0)
+    : name(n)
   { }
 };
 
@@ -58,8 +61,8 @@ class HeartbeatMap {
 
   // reset the timeout so that it expects another touch within grace amount of time
   void reset_timeout(heartbeat_handle_d *h,
-                    ceph::coarse_mono_clock::rep grace,
-                    ceph::coarse_mono_clock::rep suicide_grace);
+                    ceph::timespan grace,
+                    ceph::timespan suicide_grace);
   // clear the timeout so that it's not checked on
   void clear_timeout(heartbeat_handle_d *h);
 
@@ -79,16 +82,17 @@ class HeartbeatMap {
   ~HeartbeatMap();
 
  private:
+  using clock = ceph::coarse_mono_clock;
   CephContext *m_cct;
   ceph::shared_mutex m_rwlock =
     ceph::make_shared_mutex("HeartbeatMap::m_rwlock");
-  ceph::coarse_mono_clock::time_point m_inject_unhealthy_until;
+  clock::time_point m_inject_unhealthy_until;
   std::list<heartbeat_handle_d*> m_workers;
   std::atomic<unsigned> m_unhealthy_workers = { 0 };
   std::atomic<unsigned> m_total_workers = { 0 };
 
   bool _check(const heartbeat_handle_d *h, const char *who,
-             ceph::coarse_mono_clock::rep now);
+             ceph::coarse_mono_time now);
 };
 
 }
index c1b8c18e0972995578d2c668718161f771a932ed..9e68b28a767028a76fe2280a0c319779b2371a23 100644 (file)
@@ -133,8 +133,8 @@ void ThreadPool::worker(WorkThread *wt)
     ldout(cct,20) << "worker waiting" << dendl;
     cct->get_heartbeat_map()->reset_timeout(
       hb,
-      cct->_conf->threadpool_default_timeout,
-      0);
+      ceph::make_timespan(cct->_conf->threadpool_default_timeout),
+      ceph::make_timespan(0));
     auto wait = std::chrono::seconds(
       cct->_conf->threadpool_empty_queue_max_wait);
     _cond.wait_for(ul, wait);
@@ -278,7 +278,8 @@ void ShardedThreadPool::shardedthreadpool_worker(uint32_t thread_index)
       while (pause_threads) {
        cct->get_heartbeat_map()->reset_timeout(
                hb,
-               wq->timeout_interval, wq->suicide_interval);
+               ceph::make_timespan(wq->timeout_interval),
+               ceph::make_timespan(wq->suicide_interval));
        shardedpool_cond.wait_for(
         ul,
         std::chrono::seconds(cct->_conf->threadpool_empty_queue_max_wait));
@@ -293,7 +294,8 @@ void ShardedThreadPool::shardedthreadpool_worker(uint32_t thread_index)
         while (drain_threads) {
          cct->get_heartbeat_map()->reset_timeout(
            hb,
-           wq->timeout_interval, wq->suicide_interval);
+           ceph::make_timespan(wq->timeout_interval),
+           ceph::make_timespan(wq->suicide_interval));
           shardedpool_cond.wait_for(
            ul,
            std::chrono::seconds(cct->_conf->threadpool_empty_queue_max_wait));
@@ -304,7 +306,8 @@ void ShardedThreadPool::shardedthreadpool_worker(uint32_t thread_index)
 
     cct->get_heartbeat_map()->reset_timeout(
       hb,
-      wq->timeout_interval, wq->suicide_interval);
+      ceph::make_timespan(wq->timeout_interval),
+      ceph::make_timespan(wq->suicide_interval));
     wq->_process(thread_index, hb);
 
   }
index 0c16cfc31f200217fc6e8d31ccbe78c1fd5fc85b..281fececc0a2df8d55fb3d0cdfd1211d6aff0967 100644 (file)
@@ -59,8 +59,8 @@ public:
     friend class ThreadPool;
     CephContext *cct;
     ceph::heartbeat_handle_d *hb;
-    ceph::coarse_mono_clock::rep grace;
-    ceph::coarse_mono_clock::rep suicide_grace;
+    ceph::timespan grace;
+    ceph::timespan suicide_grace;
   public:
     TPHandle(
       CephContext *cct,