Skip to content

Commit

Permalink
fpga: The optimal parameters of mpool are adjusted
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokamikami committed Nov 1, 2024
1 parent 8f1b15f commit 3ba5887
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
10 changes: 3 additions & 7 deletions src/test/csrc/common/mpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ bool MemoryIdxPool::write_free_chunk(uint8_t idx, const char *data) {
write_count++;
// Proceed to the next group
if (write_count == MAX_IDX) {
memory_pool[page_w_idx].is_free.store(false);
memcpy(memory_pool[page_w_idx].data.get(), data, 4096);

memory_pool[page_w_idx].is_free.store(false);
size_t next_w_idx = wait_next_free_group();
group_w_offset = (next_w_idx & REM_MAX_GROUPING_IDX) * MAX_IDX;
write_count = write_next_count;
Expand Down Expand Up @@ -132,13 +131,11 @@ bool MemoryIdxPool::read_busy_chunk(char *data) {

memcpy(data, memory_pool[page_r_idx].data.get(), 4096);
memory_pool[page_r_idx].is_free.store(true);

return true;
}

size_t MemoryIdxPool::wait_next_free_group() {
empty_blocks.fetch_sub(1);
size_t free_num = empty_blocks.load();
size_t free_num = empty_blocks.fetch_sub(1, std::memory_order_relaxed) + 1;
cv_filled.notify_all();
//Reserve at least two free blocks
if (free_num <= 2) {
Expand All @@ -149,8 +146,7 @@ size_t MemoryIdxPool::wait_next_free_group() {
}

size_t MemoryIdxPool::wait_next_full_group() {
empty_blocks.fetch_add(1);
size_t free_num = empty_blocks.load();
size_t free_num = empty_blocks.fetch_add(1, std::memory_order_relaxed) + 1;
cv_empty.notify_all();

if (free_num >= MAX_GROUP_READ) {
Expand Down
17 changes: 9 additions & 8 deletions src/test/csrc/common/mpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <mutex>
#include <vector>

#define MEMPOOL_SIZE 4096 * 1024 // 4M page
#define MEMPOOL_SIZE 16384 * 1024// 16M memory
#define MEMBLOCK_SIZE 4096 // 4K packge
#define NUM_BLOCKS (MEMPOOL_SIZE / MEMBLOCK_SIZE)
#define REM_NUM_BLOCKS (NUM_BLOCKS - 1)
Expand All @@ -35,7 +35,7 @@ struct MemoryBlock {

MemoryBlock() : is_free(true) {
void *ptr = nullptr;
if (posix_memalign(&ptr, 4096, 4096) != 0) {
if (posix_memalign(&ptr, 4096, 4096 + MEMBLOCK_SIZE) != 0) {
throw std::runtime_error("Failed to allocate aligned memory");
}
memset(ptr, 0, 4096);
Expand Down Expand Up @@ -102,15 +102,16 @@ class MemoryPool {
size_t page_end = 0;
};

static const size_t MAX_IDX = 256;
static const size_t MAX_GROUPING_IDX = NUM_BLOCKS / MAX_IDX;
static const size_t MAX_GROUP_READ = MAX_GROUPING_IDX - 2; //The window needs to reserve two free Spaces
static const size_t REM_MAX_IDX = (MAX_IDX - 1);
static const size_t REM_MAX_GROUPING_IDX = (MAX_GROUPING_IDX - 1);

// Split the memory pool into sliding Windows based on the index width
// Support multi-thread out-of-order write sequential read
class MemoryIdxPool {
private:
const size_t MAX_IDX = 256;
const size_t MAX_GROUPING_IDX = NUM_BLOCKS / MAX_IDX;
const size_t MAX_GROUP_READ = MAX_GROUPING_IDX - 2; //The window needs to reserve two free Spaces
const size_t REM_MAX_IDX = (MAX_IDX - 1);
const size_t REM_MAX_GROUPING_IDX = (MAX_GROUPING_IDX - 1);

public:
MemoryIdxPool() {
initMemoryPool();
Expand Down

0 comments on commit 3ba5887

Please sign in to comment.