提交 0150ab38 编写于 作者: X xulongteng

merger frome icode : fix malloc bug & log bug

...@@ -19,6 +19,27 @@ namespace baidu { ...@@ -19,6 +19,27 @@ namespace baidu {
namespace paddle_serving { namespace paddle_serving {
namespace predictor { namespace predictor {
struct MempoolRegion {
MempoolRegion(im::fugue::memory::Region *region,
im::Mempool *mempool) :
_region(region), _mempool(mempool){}
im::fugue::memory::Region *region() {return _region;}
im::Mempool *mempool() {return _mempool;}
im::fugue::memory::Region* _region;
im::Mempool* _mempool;
~MempoolRegion() {
if (_region) {
delete _region;
_region = NULL;
}
if (_mempool) {
delete _mempool;
_mempool = NULL;
}
}
};
int MempoolWrapper::initialize() { int MempoolWrapper::initialize() {
if (THREAD_KEY_CREATE(&_bspec_key, NULL) != 0) { if (THREAD_KEY_CREATE(&_bspec_key, NULL) != 0) {
LOG(ERROR) << "unable to create thread_key of thrd_data"; LOG(ERROR) << "unable to create thread_key of thrd_data";
...@@ -33,16 +54,20 @@ int MempoolWrapper::initialize() { ...@@ -33,16 +54,20 @@ int MempoolWrapper::initialize() {
} }
int MempoolWrapper::thread_initialize() { int MempoolWrapper::thread_initialize() {
_region.init(); im::fugue::memory::Region *region = new im::fugue::memory::Region();
im::Mempool* p_mempool = new (std::nothrow) im::Mempool(&_region); region->init();
if (p_mempool == NULL) { im::Mempool* mempool = new (std::nothrow) im::Mempool(region);
MempoolRegion *mempool_region = new MempoolRegion(region, mempool);
if (mempool == NULL) {
LOG(ERROR) << "Failed create thread mempool"; LOG(ERROR) << "Failed create thread mempool";
return -1; return -1;
} }
if (THREAD_SETSPECIFIC(_bspec_key, p_mempool) != 0) { if (THREAD_SETSPECIFIC(_bspec_key, mempool_region) != 0) {
LOG(ERROR) << "unable to set the thrd_data"; LOG(ERROR) << "unable to set the thrd_data";
delete p_mempool; delete region;
delete mempool;
delete mempool_region;
return -1; return -1;
} }
...@@ -51,23 +76,34 @@ int MempoolWrapper::thread_initialize() { ...@@ -51,23 +76,34 @@ int MempoolWrapper::thread_initialize() {
} }
int MempoolWrapper::thread_clear() { int MempoolWrapper::thread_clear() {
im::Mempool* p_mempool = (im::Mempool*)THREAD_GETSPECIFIC(_bspec_key); MempoolRegion* mempool_region = (MempoolRegion*)THREAD_GETSPECIFIC(_bspec_key);
if (p_mempool) { if (mempool_region == NULL) {
p_mempool->release_block(); LOG(WARNING) << "THREAD_GETSPECIFIC() returned NULL";
_region.reset(); return -1;
}
im::Mempool* mempool = mempool_region->mempool();
im::fugue::memory::Region* region = mempool_region->region();
if (mempool) {
mempool->release_block();
region->reset();
} }
return 0; return 0;
} }
void* MempoolWrapper::malloc(size_t size) { void* MempoolWrapper::malloc(size_t size) {
im::Mempool* p_mempool = (im::Mempool*)THREAD_GETSPECIFIC(_bspec_key); MempoolRegion* mempool_region = (MempoolRegion*)THREAD_GETSPECIFIC(_bspec_key);
if (!p_mempool) { if (mempool_region == NULL) {
LOG(WARNING) << "THREAD_GETSPECIFIC() returned NULL";
return NULL;
}
im::Mempool* mempool = mempool_region->mempool();
if (!mempool) {
LOG(WARNING) << "Cannot malloc memory:" << size LOG(WARNING) << "Cannot malloc memory:" << size
<< ", since mempool is not thread initialized"; << ", since mempool is not thread initialized";
return NULL; return NULL;
} }
return p_mempool->malloc(size); return mempool->malloc(size);
} }
} // namespace predictor } // namespace predictor
......
...@@ -39,7 +39,7 @@ class MempoolWrapper { ...@@ -39,7 +39,7 @@ class MempoolWrapper {
void* malloc(size_t size); void* malloc(size_t size);
private: private:
im::fugue::memory::Region _region; //im::fugue::memory::Region _region;
THREAD_KEY_T _bspec_key; THREAD_KEY_T _bspec_key;
}; };
......
...@@ -143,7 +143,7 @@ int main(int argc, char** argv) { ...@@ -143,7 +143,7 @@ int main(int argc, char** argv) {
std::string filename(argv[0]); std::string filename(argv[0]);
filename = filename.substr(filename.find_last_of('/') + 1); filename = filename.substr(filename.find_last_of('/') + 1);
settings.log_file = (std::string("./log/") + filename + ".log").c_str(); settings.log_file = strdup((std::string("./log/") + filename + ".log").c_str());
settings.delete_old = logging::DELETE_OLD_LOG_FILE; settings.delete_old = logging::DELETE_OLD_LOG_FILE;
logging::InitLogging(settings); logging::InitLogging(settings);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册