From: David Zafman Date: Tue, 19 Feb 2019 02:02:16 +0000 (-0800) Subject: osd: Improve use of atomic operations to add and subtract from local_num_bytes X-Git-Tag: v14.1.0~35^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=de422f77a7271f8af9861c940b1c7879d2e2ee10;p=ceph.git osd: Improve use of atomic operations to add and subtract from local_num_bytes Signed-off-by: David Zafman --- diff --git a/src/osd/PG.h b/src/osd/PG.h index cc8db27f55f8..b07c2f5ff5bb 100644 --- a/src/osd/PG.h +++ b/src/osd/PG.h @@ -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,