Skip to content

Commit c07c2a1

Browse files
MB-20231: Replacing mutex with spin-lock for queue ops
+ Performance regression observed in run-time of fdb_extended_test. + Profiling showed the mutex lock contention was the main bottle-neck. + Replaced the mutex with a spin-lock + Run-time of fdb_extended_test back to normal. - Before change: 1/1 Test #10: fdb_extended_test ................ Passed 116.43 sec - After change: 1/1 Test #10: fdb_extended_test ................ Passed 103.96 sec Change-Id: Iba5909510b99353543a8db7328e1b0ec8a35e95f Reviewed-on: http://review.couchbase.org/66346 Reviewed-by: Chiyoung Seo <[email protected]> Tested-by: buildbot <[email protected]>
1 parent 1f0db37 commit c07c2a1

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/memory_pool.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
#include <memory_pool.h>
1919

2020
MemoryPool::MemoryPool(int num_bins, size_t bin_size) {
21+
spin_init(&lock);
2122
for (int i = 0; i < num_bins; ++i) {
2223
memPool.push_back((uint8_t *) malloc(bin_size));
2324
enQueue(i);
2425
}
2526
}
2627

2728
MemoryPool::~MemoryPool() {
29+
spin_destroy(&lock);
2830
for (auto &it : memPool) {
2931
free(it);
3032
}
@@ -47,16 +49,18 @@ void MemoryPool::returnBlock(int index) {
4749
}
4850

4951
inline void MemoryPool::enQueue(int index) {
50-
std::lock_guard<std::mutex> guard(queueGuard);
52+
spin_lock(&lock);
5153
indexQ.push(index);
54+
spin_unlock(&lock);
5255
}
5356

5457
inline int MemoryPool::deQueue() {
5558
int data = -1;
56-
std::lock_guard<std::mutex> guard(queueGuard);
59+
spin_lock(&lock);
5760
if (indexQ.size()) {
5861
data = indexQ.front();
5962
indexQ.pop();
6063
}
64+
spin_unlock(&lock);
6165
return data;
6266
}

src/memory_pool.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
#include <stdlib.h>
2121

22-
#include <mutex>
2322
#include <queue>
2423
#include <vector>
2524

25+
#include "common.h"
26+
2627
class MemoryPool {
2728
/**
2829
The memory pool is a memory container designed for concurrent access and
@@ -81,8 +82,8 @@ class MemoryPool {
8182
*/
8283
int deQueue();
8384

84-
// Lock to protect queue operations
85-
std::mutex queueGuard;
85+
// Spin lock for queue ops
86+
spin_t lock;
8687
// Queue of indexes
8788
std::queue<int> indexQ;
8889
// Vector of pre-allocated memory

0 commit comments

Comments
 (0)