From 4a8b270c490928b6fe8b611b89b14882c3ddb16c Mon Sep 17 00:00:00 2001 From: HexToString <506181616@qq.com> Date: Mon, 21 Jun 2021 12:48:13 +0000 Subject: [PATCH] add free memory --- core/predictor/framework/infer.h | 1 + core/predictor/framework/memory.cpp | 17 +++++++++++++++++ core/predictor/framework/memory.h | 2 ++ core/predictor/mempool/mempool.cpp | 2 +- core/predictor/mempool/mempool.h | 9 ++++++--- 5 files changed, 27 insertions(+), 4 deletions(-) mode change 100644 => 100755 core/predictor/framework/infer.h mode change 100644 => 100755 core/predictor/framework/memory.cpp mode change 100644 => 100755 core/predictor/framework/memory.h mode change 100644 => 100755 core/predictor/mempool/mempool.cpp mode change 100644 => 100755 core/predictor/mempool/mempool.h diff --git a/core/predictor/framework/infer.h b/core/predictor/framework/infer.h old mode 100644 new mode 100755 index 6113dc8e..24e81742 --- a/core/predictor/framework/infer.h +++ b/core/predictor/framework/infer.h @@ -501,6 +501,7 @@ class FluidInferEngine : public CloneDBReloadableInferEngine { paddle::PaddleBuf paddleBuf(databuf_char, databuf_size); tensor_out.data = paddleBuf; tensorVector_out_pointer->push_back(tensor_out); + MempoolWrapper::instance().free(databuf_char, databuf_size); } return 0; } diff --git a/core/predictor/framework/memory.cpp b/core/predictor/framework/memory.cpp old mode 100644 new mode 100755 index 273277a1..2516ba81 --- a/core/predictor/framework/memory.cpp +++ b/core/predictor/framework/memory.cpp @@ -112,6 +112,23 @@ void* MempoolWrapper::malloc(size_t size) { return mempool->malloc(size); } +void MempoolWrapper::free(void* p, size_t size) { + MempoolRegion* mempool_region = + (MempoolRegion*)THREAD_GETSPECIFIC(_bspec_key); + if (mempool_region == NULL) { + LOG(WARNING) << "THREAD_GETSPECIFIC() returned NULL"; + return; + } + + im::Mempool* mempool = mempool_region->mempool(); + if (!mempool) { + LOG(WARNING) << "Cannot free memory:" << size + << ", since mempool is not thread initialized"; + return; + } + return mempool->free(p,size); +} + } // namespace predictor } // namespace paddle_serving } // namespace baidu diff --git a/core/predictor/framework/memory.h b/core/predictor/framework/memory.h old mode 100644 new mode 100755 index af3591ee..74fba4eb --- a/core/predictor/framework/memory.h +++ b/core/predictor/framework/memory.h @@ -38,6 +38,8 @@ class MempoolWrapper { void* malloc(size_t size); + void free(void* p, size_t size); + private: // im::fugue::memory::Region _region; THREAD_KEY_T _bspec_key; diff --git a/core/predictor/mempool/mempool.cpp b/core/predictor/mempool/mempool.cpp old mode 100644 new mode 100755 index f9c57138..fb9337b1 --- a/core/predictor/mempool/mempool.cpp +++ b/core/predictor/mempool/mempool.cpp @@ -24,7 +24,7 @@ namespace fugue { namespace memory { void Region::init() { - _big_mem_capacity = 32 * 1024 * 1024; + _big_mem_capacity = 64 * 1024 * 1024;//64MB _big_mem_start = new char[_big_mem_capacity]; } diff --git a/core/predictor/mempool/mempool.h b/core/predictor/mempool/mempool.h old mode 100644 new mode 100755 index ce423a66..e30b19e5 --- a/core/predictor/mempool/mempool.h +++ b/core/predictor/mempool/mempool.h @@ -264,7 +264,7 @@ struct BlockReference { // total number is 32*1024 class BlockFreeList { public: - static const int MAX_BLOCK_COUNT = 32 * 1024; + static const int MAX_BLOCK_COUNT = 256 * 1024; typedef lockfree::FreeList BlockFreeListType; static BlockFreeListType* instance() { static BlockFreeListType singleton; @@ -340,7 +340,9 @@ class Region { static const int BIG_MEM_THRESHOLD = 2 * 1024 * 1024; // 2MB,means when you need less than 2M, get memory from Block. - static const int BIGNODE_MEM_THRESHOLD = 4 * 1024 * 1024; // 4MB + + // 64MB,means when you need less than 64MB, get memory from BigMemory instead of BigNode + static const int BIGNODE_MEM_THRESHOLD = (64 * 1024 * 1024 + 1); static const int COUNTER_SIZE = BIGNODE_MEM_THRESHOLD / BIG_MEM_THRESHOLD + 1; // this is not used @@ -421,7 +423,8 @@ class Mempool { } // 可能返回的是单独Region中malloc的内存。 - // 也可能是Block,例如new_size=1M, old_data原本的指针头就在1.2M处,old_size = + // 也可能是Block,例如new_size=1M, old_data原本的指针头就在1.2M处,old_size + // = // 0.5M // 此时,_free_size = 0.3M,new_size<2M,但是required = 1-0.5 >0.3 // 分配出来的就是Block,但是该Block没有并很完美的利用完全。 -- GitLab