@@ -16,7 +16,8 @@ MemoryPool::MemoryPool(size_t pool_size, size_t block_size, struct ibv_pd* pd)
1616 block_size_(block_size),
1717 pd_(pd),
1818 mr_(nullptr ),
19- last_search_position_(0 ) {
19+ last_search_position_(0 ),
20+ allocated_blocks_(0 ) {
2021 // 计算总的内存块数量
2122 total_blocks_ = pool_size_ / block_size_;
2223 assert (pool_size % block_size == 0 );
@@ -53,28 +54,32 @@ MemoryPool::~MemoryPool() {
5354 }
5455}
5556
56- bool MemoryPool::allocate (size_t size, size_t n, SimpleAllocationCallback callback) {
57+ int MemoryPool::allocate (size_t size, size_t n, SimpleAllocationCallback callback) {
5758 size_t required_blocks = (size + block_size_ - 1 ) / block_size_; // round up
59+ int num_allocated = 0 ;
5860
5961 if (required_blocks > total_blocks_) {
60- return false ;
62+ return 0 ;
6163 }
6264
63- int num_allocated = 0 ;
6465 size_t bit_per_word = 64 ;
6566 size_t shift = 6 ;
6667
6768 for (size_t word_index = last_search_position_; word_index < bitmap_.size (); ++word_index) {
69+ if (num_allocated == n) {
70+ break ;
71+ }
72+
6873 uint64_t word = bitmap_[word_index];
6974 if (word == 0xFFFFFFFFFFFFFFFFULL ) {
7075 continue ;
7176 }
72-
7377 for (size_t bit_index = __builtin_ctzll (~word); bit_index < bit_per_word; ++bit_index) {
7478 size_t start_block = (word_index << shift) + bit_index;
7579
7680 if (start_block + required_blocks > total_blocks_) {
77- return false ;
81+ allocated_blocks_ += num_allocated * required_blocks;
82+ return num_allocated;
7883 }
7984
8085 bool found = true ;
@@ -98,13 +103,14 @@ bool MemoryPool::allocate(size_t size, size_t n, SimpleAllocationCallback callba
98103 callback (addr, mr_->lkey , mr_->rkey );
99104 last_search_position_ = word_index;
100105 if (++num_allocated == n) {
101- return true ;
106+ break ;
102107 }
103108 }
104109 }
105110 }
106111
107- return num_allocated == n;
112+ allocated_blocks_ += num_allocated * required_blocks;
113+ return num_allocated;
108114}
109115
110116void MemoryPool::deallocate (void * ptr, size_t size) {
@@ -142,18 +148,42 @@ void MemoryPool::deallocate(void* ptr, size_t size) {
142148 }
143149}
144150
151+ void MM::add_mempool (struct ibv_pd * pd) {
152+ mempools_.push_back (new MemoryPool ((size_t )EXTEND_POOL_SIZE, (size_t )EXTEND_BLOCK_SIZE, pd));
153+ }
154+
155+ void MM::add_mempool (size_t pool_size, size_t block_size, struct ibv_pd * pd) {
156+ mempools_.push_back (new MemoryPool (pool_size, block_size, pd));
157+ }
158+
145159bool MM::allocate (size_t size, size_t n, AllocationCallback callback) {
146- for (int i = 0 ; i < mempools_.size (); ++i) {
160+ bool allocated = false ;
161+ int mempool_cnt = mempools_.size ();
162+ for (int i = 0 ; i < mempool_cnt; ++i) {
147163 // create a new callback from the original callback
148164 auto simple_callback = [callback, i](void * ptr, uint32_t lkey, uint32_t rkey) {
149165 callback (ptr, lkey, rkey, i);
150166 };
151167
152- if (mempools_[i]->allocate (size, n, simple_callback)) {
153- return true ;
168+ int num_allocated = mempools_[i]->allocate (size, n, simple_callback);
169+ n -= num_allocated;
170+
171+ auto total_blocks = mempools_[i]->get_total_blocks ();
172+ auto allocated_blocks = mempools_[i]->get_allocated_blocks ();
173+ DEBUG (
174+ " Mempool Count: {}, Pool idx: {}, Total blocks: {}, allocated blocks: {}, block usage: "
175+ " {}%" ,
176+ mempool_cnt, i, total_blocks, allocated_blocks, 100 * allocated_blocks / total_blocks);
177+ if (i == mempools_.size () - 1 &&
178+ (float )allocated_blocks / total_blocks > BLOCK_USAGE_RATIO) {
179+ need_extend = true ;
180+ }
181+ if (n == 0 ) {
182+ allocated = true ;
183+ break ;
154184 }
155185 }
156- return false ;
186+ return allocated ;
157187}
158188
159189void MM::deallocate (void * ptr, size_t size, int pool_idx) {
0 commit comments