From de422f77a7271f8af9861c940b1c7879d2e2ee10 Mon Sep 17 00:00:00 2001 From: David Zafman Date: Mon, 18 Feb 2019 18:02:16 -0800 Subject: [PATCH] osd: Improve use of atomic operations to add and subtract from local_num_bytes Signed-off-by: David Zafman --- src/osd/PG.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) 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, -- 2.47.3