@@ -36,7 +36,7 @@ DEFINE_int32(rdma_memory_pool_initial_size_mb, 1024,
3636 " Initial size of memory pool for RDMA (MB)" );
3737DEFINE_int32 (rdma_memory_pool_increase_size_mb, 1024 ,
3838 " Increased size of memory pool for RDMA (MB)" );
39- DEFINE_int32 (rdma_memory_pool_max_regions, 1 , " Max number of regions" );
39+ DEFINE_int32 (rdma_memory_pool_max_regions, 3 , " Max number of regions" );
4040DEFINE_int32 (rdma_memory_pool_buckets, 4 , " Number of buckets to reduce race" );
4141DEFINE_int32 (rdma_memory_pool_tls_cache_num, 128 , " Number of cached block in tls" );
4242DEFINE_bool (rdma_memory_pool_user_specified_memory, false ,
@@ -96,6 +96,7 @@ struct GlobalInfo {
9696 std::vector<IdleNode*> idle_list[BLOCK_SIZE_COUNT];
9797 std::vector<butil::Mutex*> lock[BLOCK_SIZE_COUNT];
9898 std::vector<size_t > idle_size[BLOCK_SIZE_COUNT];
99+ int region_num[BLOCK_SIZE_COUNT];
99100 butil::Mutex extend_lock;
100101};
101102static GlobalInfo* g_info = NULL ;
@@ -132,6 +133,7 @@ static void* ExtendBlockPoolImpl(void* region_base, size_t region_size,
132133 int block_type) {
133134 if (g_region_num == FLAGS_rdma_memory_pool_max_regions) {
134135 LOG (INFO) << " Memory pool reaches max regions" ;
136+ free (region_base);
135137 errno = ENOMEM;
136138 return NULL ;
137139 }
@@ -167,6 +169,7 @@ static void* ExtendBlockPoolImpl(void* region_base, size_t region_size,
167169 g_info->idle_list [block_type][i] = node[i];
168170 g_info->idle_size [block_type][i] += node[i]->len ;
169171 }
172+ g_info->region_num [block_type]++;
170173
171174 return region_base;
172175}
@@ -200,6 +203,16 @@ static void* ExtendBlockPool(size_t region_size, int block_type) {
200203 return ExtendBlockPoolImpl (region_base, region_size, block_type);
201204}
202205
206+ static bool CanExendBlockRuntime (int block_type) {
207+ auto rc = FLAGS_rdma_memory_pool_buckets == 1 &&
208+ g_info->region_num [block_type] == 1 ;
209+ if (!rc) {
210+ LOG_EVERY_SECOND (ERROR)
211+ << " Runtime extend memory only support one bucket per block type" ;
212+ }
213+ return rc;
214+ }
215+
203216void * ExtendBlockPoolByUser (void * region_base, size_t region_size,
204217 int block_type) {
205218 if (FLAGS_rdma_memory_pool_user_specified_memory == false ) {
@@ -215,9 +228,7 @@ void* ExtendBlockPoolByUser(void* region_base, size_t region_size,
215228 BAIDU_SCOPED_LOCK (*g_info->lock [block_type][index]);
216229 BAIDU_SCOPED_LOCK (g_info->extend_lock );
217230
218- if (g_region_num > 1 && FLAGS_rdma_memory_pool_buckets > 1 ) {
219- LOG_EVERY_SECOND (ERROR)
220- << " Runtime extend memory only support single bucket" ;
231+ if (CanExendBlockRuntime (block_type) == false ) {
221232 return NULL ;
222233 }
223234 region_size =
@@ -300,6 +311,7 @@ bool InitBlockPool(RegisterCallback cb) {
300311 if (g_info->idle_size [i].size () != g_buckets) {
301312 return false ;
302313 }
314+ g_info->region_num [i] = 0 ;
303315 for (size_t j = 0 ; j < g_buckets; ++j) {
304316 g_info->lock [i][j] = new (std::nothrow) butil::Mutex;
305317 if (!g_info->lock [i][j]) {
@@ -349,6 +361,9 @@ static void* AllocBlockFrom(int block_type) {
349361 BAIDU_SCOPED_LOCK (g_info->extend_lock );
350362 node = g_info->idle_list [block_type][index];
351363 if (!node) {
364+ if (CanExendBlockRuntime (block_type) == false ) {
365+ return NULL ;
366+ }
352367 // There is no block left, extend a new region
353368 if (!ExtendBlockPool (FLAGS_rdma_memory_pool_increase_size_mb,
354369 block_type)) {
0 commit comments