]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
osd: Improve use of atomic operations to add and subtract from local_num_bytes 26465/head
authorDavid Zafman <dzafman@redhat.com>
Tue, 19 Feb 2019 02:02:16 +0000 (18:02 -0800)
committerDavid Zafman <dzafman@redhat.com>
Tue, 19 Feb 2019 02:02:16 +0000 (18:02 -0800)
Signed-off-by: David Zafman <dzafman@redhat.com>
src/osd/PG.h

index cc8db27f55f8f263e533b35e515b6d437ea2e498..b07c2f5ff5bbb51a4dd5131c42ae2d0a5ad4fd82 100644 (file)
@@ -1241,19 +1241,25 @@ public:
   // but we don't let local_num_bytes go negative.
   void add_local_num_bytes(int64_t num_bytes) {
     if (num_bytes) {
-      int64_t prev = local_num_bytes.fetch_add(num_bytes);
-      ceph_assert(prev >= 0);
-      if (num_bytes < 0 && prev < -num_bytes) {
-        local_num_bytes.store(0);
-      }
+      int64_t prev_bytes = local_num_bytes.load();
+      int64_t new_bytes;
+      do {
+        new_bytes = prev_bytes + num_bytes;
+        if (new_bytes < 0)
+          new_bytes = 0;
+      } while(!local_num_bytes.compare_exchange_weak(prev_bytes, new_bytes));
     }
   }
   void sub_local_num_bytes(int64_t num_bytes) {
     ceph_assert(num_bytes >= 0);
     if (num_bytes) {
-      if (local_num_bytes.fetch_sub(num_bytes) < num_bytes) {
-        local_num_bytes.store(0);
-      }
+      int64_t prev_bytes = local_num_bytes.load();
+      int64_t new_bytes;
+      do {
+        new_bytes = prev_bytes - num_bytes;
+        if (new_bytes < 0)
+          new_bytes = 0;
+      } while(!local_num_bytes.compare_exchange_weak(prev_bytes, new_bytes));
     }
   }
   // The value of num_bytes could be negative,