Commit e9f933b51c2ea25bd5f75b8b5be45470ab4f2f53

Authored by Hu Chunming
1 parent 497868ee

代码优化;修复显存泄漏:解码结束后剩余数据未清理

vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/DxDecoderWrap.cpp
@@ -7,14 +7,14 @@ @@ -7,14 +7,14 @@
7 void decoded_cbk(const void * userPtr, GPUFrame * gpuFrame){ 7 void decoded_cbk(const void * userPtr, GPUFrame * gpuFrame){
8 DxDecoderWrap* _this = (DxDecoderWrap*)userPtr; 8 DxDecoderWrap* _this = (DxDecoderWrap*)userPtr;
9 if(nullptr != _this){ 9 if(nullptr != _this){
10 - _this->post_decode_thread(gpuFrame); 10 + _this->post_decode_callback(gpuFrame);
11 } 11 }
12 } 12 }
13 13
14 void decode_finished_cbk(const void * userPtr){ 14 void decode_finished_cbk(const void * userPtr){
15 DxDecoderWrap* _this = (DxDecoderWrap*)userPtr; 15 DxDecoderWrap* _this = (DxDecoderWrap*)userPtr;
16 if(nullptr != _this){ 16 if(nullptr != _this){
17 - _this->decode_finished_thread(); 17 + _this->decode_finished_callback();
18 } 18 }
19 } 19 }
20 20
@@ -145,7 +145,7 @@ int DxDecoderWrap::ResumeDecoder() @@ -145,7 +145,7 @@ int DxDecoderWrap::ResumeDecoder()
145 bool DxDecoderWrap::DxDecoderIsRun() const 145 bool DxDecoderWrap::DxDecoderIsRun() const
146 { 146 {
147 if(m_pDec) { 147 if(m_pDec) {
148 - return m_pDec->isRunning(); 148 + return !m_pDec->isFinished();
149 } 149 }
150 150
151 return false; 151 return false;
@@ -175,7 +175,7 @@ int DxDecoderWrap::DxLockFrame(DxGPUFrame& frame) @@ -175,7 +175,7 @@ int DxDecoderWrap::DxLockFrame(DxGPUFrame& frame)
175 return 0; 175 return 0;
176 } 176 }
177 177
178 -void DxDecoderWrap::post_decode_thread(GPUFrame * decodedFrame) { 178 +void DxDecoderWrap::post_decode_callback(GPUFrame * decodedFrame) {
179 while(!m_bClose) { 179 while(!m_bClose) {
180 m_queue_frames_mutex.lock(); 180 m_queue_frames_mutex.lock();
181 if(m_queue_frames.size() < 3) { 181 if(m_queue_frames.size() < 3) {
@@ -205,9 +205,6 @@ void DxDecoderWrap::post_decode_thread(GPUFrame * decodedFrame) { @@ -205,9 +205,6 @@ void DxDecoderWrap::post_decode_thread(GPUFrame * decodedFrame) {
205 frame.frame = pHwData; 205 frame.frame = pHwData;
206 frame.timestamp = decodedFrame->ts; 206 frame.timestamp = decodedFrame->ts;
207 207
208 - delete decodedFrame;  
209 - decodedFrame = nullptr;  
210 -  
211 m_queue_frames.push(frame); 208 m_queue_frames.push(frame);
212 m_queue_frames_mutex.unlock(); 209 m_queue_frames_mutex.unlock();
213 break; 210 break;
@@ -218,6 +215,6 @@ void DxDecoderWrap::post_decode_thread(GPUFrame * decodedFrame) { @@ -218,6 +215,6 @@ void DxDecoderWrap::post_decode_thread(GPUFrame * decodedFrame) {
218 } 215 }
219 } 216 }
220 217
221 -void DxDecoderWrap::decode_finished_thread() { 218 +void DxDecoderWrap::decode_finished_callback() {
222 m_bClose = true; 219 m_bClose = true;
223 } 220 }
224 \ No newline at end of file 221 \ No newline at end of file
vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/DxDecoderWrap.h
@@ -66,8 +66,8 @@ public: @@ -66,8 +66,8 @@ public:
66 66
67 public: 67 public:
68 string GetName() {return m_name;}; 68 string GetName() {return m_name;};
69 - void post_decode_thread(GPUFrame * gpuFrame);  
70 - void decode_finished_thread(); 69 + void post_decode_callback(GPUFrame * gpuFrame);
  70 + void decode_finished_callback();
71 71
72 private: 72 private:
73 bool m_bClose; 73 bool m_bClose;
vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/FFNvDecoder.cpp
@@ -8,6 +8,8 @@ @@ -8,6 +8,8 @@
8 8
9 #include "logger.hpp" 9 #include "logger.hpp"
10 10
  11 +#define MAX_QUEUE_SIZE 3
  12 +
11 using namespace std; 13 using namespace std;
12 14
13 // 参考博客: https://blog.csdn.net/qq_40116098/article/details/120704340 15 // 参考博客: https://blog.csdn.net/qq_40116098/article/details/120704340
@@ -214,7 +216,7 @@ void FFNvDecoder::decode_thread() @@ -214,7 +216,7 @@ void FFNvDecoder::decode_thread()
214 } 216 }
215 217
216 m_queue_mutex.lock(); 218 m_queue_mutex.lock();
217 - if(mFrameQueue.size() >= 10){ 219 + if(mFrameQueue.size() >= MAX_QUEUE_SIZE){
218 m_queue_mutex.unlock(); 220 m_queue_mutex.unlock();
219 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 221 std::this_thread::sleep_for(std::chrono::milliseconds(10));
220 continue; 222 continue;
@@ -270,7 +272,7 @@ void FFNvDecoder::decode_thread() @@ -270,7 +272,7 @@ void FFNvDecoder::decode_thread()
270 272
271 if(gpuFrame != nullptr){ 273 if(gpuFrame != nullptr){
272 m_queue_mutex.lock(); 274 m_queue_mutex.lock();
273 - if(mFrameQueue.size() <= 10){ 275 + if(mFrameQueue.size() <= MAX_QUEUE_SIZE){
274 GPUFrame* frame = new GPUFrame(); 276 GPUFrame* frame = new GPUFrame();
275 frame->ts = index; 277 frame->ts = index;
276 frame->gpuFrame = gpuFrame; 278 frame->gpuFrame = gpuFrame;
@@ -301,6 +303,7 @@ void FFNvDecoder::decode_thread() @@ -301,6 +303,7 @@ void FFNvDecoder::decode_thread()
301 // 清空队列 303 // 清空队列
302 while(mFrameQueue.size() > 0){ 304 while(mFrameQueue.size() > 0){
303 GPUFrame * frame = mFrameQueue.front(); 305 GPUFrame * frame = mFrameQueue.front();
  306 + av_frame_free(&frame->gpuFrame);
304 delete frame; 307 delete frame;
305 frame = nullptr; 308 frame = nullptr;
306 mFrameQueue.pop(); 309 mFrameQueue.pop();
@@ -316,10 +319,6 @@ void FFNvDecoder::decode_thread() @@ -316,10 +319,6 @@ void FFNvDecoder::decode_thread()
316 void FFNvDecoder::decode_finished(){ 319 void FFNvDecoder::decode_finished(){
317 if (avctx) 320 if (avctx)
318 { 321 {
319 - // if (avctx->hw_device_ctx) {  
320 - // av_buffer_unref(&avctx->hw_device_ctx);  
321 - // avctx->hw_device_ctx = nullptr;  
322 - // }  
323 avcodec_free_context(&avctx); 322 avcodec_free_context(&avctx);
324 avctx = nullptr; 323 avctx = nullptr;
325 } 324 }
@@ -358,7 +357,9 @@ void FFNvDecoder::post_decode_thread(){ @@ -358,7 +357,9 @@ void FFNvDecoder::post_decode_thread(){
358 frame_count++; 357 frame_count++;
359 } 358 }
360 359
361 - // av_frame_free(&gpuFrame); 360 + av_frame_free(&frame->gpuFrame);
  361 + delete frame;
  362 + frame = nullptr;
362 363
363 index++; 364 index++;
364 } 365 }
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/ImageSaveCache.cpp
@@ -38,7 +38,7 @@ void ImageSaveCache::show() @@ -38,7 +38,7 @@ void ImageSaveCache::show()
38 } 38 }
39 //#include <fstream> 39 //#include <fstream>
40 //std::ofstream os1("./mp_frameSize.txt", std::ofstream::out | std::ofstream::trunc); 40 //std::ofstream os1("./mp_frameSize.txt", std::ofstream::out | std::ofstream::trunc);
41 -void ImageSaveCache::insert(const OBJ_KEY &snaphot_id, const FRAME_KEY & frame_id, const DxGPUFrame & frame) 41 +void ImageSaveCache::add_frame(const OBJ_KEY &snaphot_id, const FRAME_KEY & frame_id, const DxGPUFrame & frame)
42 { 42 {
43 //std::lock_guard<std::mutex> l(tx); 43 //std::lock_guard<std::mutex> l(tx);
44 //os1 << std::unitbuf; 44 //os1 << std::unitbuf;
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/ImageSaveCache.h
@@ -10,7 +10,7 @@ class ImageSaveCache @@ -10,7 +10,7 @@ class ImageSaveCache
10 { 10 {
11 public: 11 public:
12 12
13 - void insert(const OBJ_KEY & snaphot_id, const FRAME_KEY & frame_id, const DxGPUFrame & frame); 13 + void add_frame(const OBJ_KEY & snaphot_id, const FRAME_KEY & frame_id, const DxGPUFrame & frame);
14 void release(const OBJ_KEY & snaphot_id); 14 void release(const OBJ_KEY & snaphot_id);
15 DxGPUFrame* get_frame(const OBJ_KEY & snaphot_id); 15 DxGPUFrame* get_frame(const OBJ_KEY & snaphot_id);
16 void show(); 16 void show();
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp
@@ -276,6 +276,7 @@ void CMutliSourceVideoProcess::FinishTask(const int taskID) @@ -276,6 +276,7 @@ void CMutliSourceVideoProcess::FinishTask(const int taskID)
276 276
277 if (viewTaskID == taskID) viewTaskID = -1; 277 if (viewTaskID == taskID) viewTaskID = -1;
278 278
  279 + LOG_INFO("task {} is finished. timeusing: {}", taskID, get_cur_time_ms() - tasks[i].timestamp);
279 280
280 break; 281 break;
281 } 282 }
@@ -452,6 +453,7 @@ bool CMutliSourceVideoProcess::AddTask(task_param tparam) //debug by zsh @@ -452,6 +453,7 @@ bool CMutliSourceVideoProcess::AddTask(task_param tparam) //debug by zsh
452 453
453 CreateResultFolder(new_task.folderNameFace, ""); 454 CreateResultFolder(new_task.folderNameFace, "");
454 } 455 }
  456 + new_task.timestamp = get_cur_time_ms();
455 457
456 TASK_INFO new_task_info = {}; 458 TASK_INFO new_task_info = {};
457 new_task_info.image_folder = new_task.folderName; 459 new_task_info.image_folder = new_task.folderName;
@@ -471,7 +473,7 @@ bool CMutliSourceVideoProcess::AddTask(task_param tparam) //debug by zsh @@ -471,7 +473,7 @@ bool CMutliSourceVideoProcess::AddTask(task_param tparam) //debug by zsh
471 473
472 AddTaskSucFlag = 1; 474 AddTaskSucFlag = 1;
473 475
474 - LOG_INFO("add task: {}", new_task.taskID); 476 + LOG_INFO("add task: {} succeed. timestamp: {} ", new_task.taskID, get_cur_time_ms());
475 return true; 477 return true;
476 } 478 }
477 479
@@ -690,7 +692,6 @@ void CMutliSourceVideoProcess::algorthim_process() @@ -690,7 +692,6 @@ void CMutliSourceVideoProcess::algorthim_process()
690 printf("begin finish last error: %s\n", cudaGetErrorString(cudaStatus)); 692 printf("begin finish last error: %s\n", cudaGetErrorString(cudaStatus));
691 } 693 }
692 694
693 - cout << "***************** Task " << i << " is Finished *****************" << endl;  
694 tasks[i].taskState = FINISH; 695 tasks[i].taskState = FINISH;
695 696
696 FinishTask(tasks[i].taskID); 697 FinishTask(tasks[i].taskID);
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/common.h
@@ -91,6 +91,8 @@ struct Task{ @@ -91,6 +91,8 @@ struct Task{
91 char* folderName; 91 char* folderName;
92 char* folderNameFace; 92 char* folderNameFace;
93 sy_rect task_min_boxsize[DETECTTYPE]; 93 sy_rect task_min_boxsize[DETECTTYPE];
  94 +
  95 + unsigned long long timestamp;
94 }; 96 };
95 97
96 typedef struct VPT_ObjInfo //����ṹ�� 98 typedef struct VPT_ObjInfo //����ṹ��
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.cpp
@@ -73,7 +73,7 @@ int HumanCarParsing::init(int gpuid, char* auth_license) @@ -73,7 +73,7 @@ int HumanCarParsing::init(int gpuid, char* auth_license)
73 73
74 int HumanCarParsing::process(sy_img * batch_img, int batch_size, hcp_analysis_result *result) 74 int HumanCarParsing::process(sy_img * batch_img, int batch_size, hcp_analysis_result *result)
75 { 75 {
76 - LOG_DEBUG("batch_size: {}", batch_size); 76 + // LOG_DEBUG("batch_size: {}", batch_size);
77 hcp_batch(handle, batch_img, batch_size, result); 77 hcp_batch(handle, batch_img, batch_size, result);
78 78
79 for (int b = 0; b < batch_size; b++) 79 for (int b = 0; b < batch_size; b++)
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.cpp
@@ -456,6 +456,8 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是 @@ -456,6 +456,8 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是
456 456
457 if (!hp_keys.empty()) 457 if (!hp_keys.empty())
458 { 458 {
  459 + LOG_DEBUG("hp_keys size: {}", hp_keys.size());
  460 +
459 const int obj_batch_count = OBJ_BATCH_COUNT / OBJ_SCALE; 461 const int obj_batch_count = OBJ_BATCH_COUNT / OBJ_SCALE;
460 const int hp_batch_size = hp_keys.size(); 462 const int hp_batch_size = hp_keys.size();
461 int hp_batch_count = obj_batch_count; 463 int hp_batch_count = obj_batch_count;
@@ -637,6 +639,7 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是 @@ -637,6 +639,7 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是
637 639
638 if (!vehicle_keys.empty()) 640 if (!vehicle_keys.empty())
639 { 641 {
  642 + LOG_DEBUG("vehicle_keys size: {}", vehicle_keys.size());
640 int det_batch_size = 10; 643 int det_batch_size = 10;
641 while (!vehicle_keys.empty()) { 644 while (!vehicle_keys.empty()) {
642 645
@@ -756,6 +759,7 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是 @@ -756,6 +759,7 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是
756 759
757 if (!vehicle_else.empty()) 760 if (!vehicle_else.empty())
758 { 761 {
  762 + LOG_DEBUG("vehicle_else size: {}", vehicle_else.size());
759 const int else_size = vehicle_else.size(); 763 const int else_size = vehicle_else.size();
760 764
761 for (int k = 0; k < else_size; k++) 765 for (int k = 0; k < else_size; k++)
@@ -1983,6 +1987,7 @@ int snapshot_helper::SaveResultInFile(OBJ_KEY deleteObj) @@ -1983,6 +1987,7 @@ int snapshot_helper::SaveResultInFile(OBJ_KEY deleteObj)
1983 const OBJ_KEY & obj_key = it->first; 1987 const OBJ_KEY & obj_key = it->first;
1984 const OBJ_VALUE & obj_value = it->second; 1988 const OBJ_VALUE & obj_value = it->second;
1985 1989
  1990 + // LOG_DEBUG("({}, {})", obj_key.videoID, obj_key.objID);
1986 if (0 == obj_value.index.index && obj_value.snapShotLittle.width == HP_WIDTH && obj_value.snapShotLittle.height == HP_HEIGHT) 1991 if (0 == obj_value.index.index && obj_value.snapShotLittle.width == HP_WIDTH && obj_value.snapShotLittle.height == HP_HEIGHT)
1987 { 1992 {
1988 if (face_detect_cf == SY_CONFIG_OPEN) 1993 if (face_detect_cf == SY_CONFIG_OPEN)
@@ -2004,7 +2009,6 @@ int snapshot_helper::SaveResultInFile(OBJ_KEY deleteObj) @@ -2004,7 +2009,6 @@ int snapshot_helper::SaveResultInFile(OBJ_KEY deleteObj)
2004 { 2009 {
2005 if (hcp_analysis_cf == SY_CONFIG_OPEN || hcf_recg_cf == SY_CONFIG_OPEN) 2010 if (hcp_analysis_cf == SY_CONFIG_OPEN || hcf_recg_cf == SY_CONFIG_OPEN)
2006 { 2011 {
2007 - LOG_DEBUG("({}, {})", obj_key.videoID, obj_key.objID);  
2008 save_snapshot(obj_key); 2012 save_snapshot(obj_key);
2009 hcp_analysis(obj_key); 2013 hcp_analysis(obj_key);
2010 } 2014 }
@@ -2090,7 +2094,7 @@ CropInfo snapshot_helper::cacheSnapShotInfo(OBJ_KEY newObj, VPT_ObjInfo obj, Tas @@ -2090,7 +2094,7 @@ CropInfo snapshot_helper::cacheSnapShotInfo(OBJ_KEY newObj, VPT_ObjInfo obj, Tas
2090 2094
2091 if (task.folderName != NULL){ 2095 if (task.folderName != NULL){
2092 FRAME_KEY frame_id = { newObj.videoID, task.taskFrameCount }; 2096 FRAME_KEY frame_id = { newObj.videoID, task.taskFrameCount };
2093 - ImgSaveCache.insert(newObj, frame_id, task.task_algorithm_data); 2097 + ImgSaveCache.add_frame(newObj, frame_id, task.task_algorithm_data);
2094 2098
2095 snapShotInfo[newObj].snapShot.height = frameHeight; 2099 snapShotInfo[newObj].snapShot.height = frameHeight;
2096 snapShotInfo[newObj].snapShot.width = frameWidth; 2100 snapShotInfo[newObj].snapShot.width = frameWidth;
@@ -2208,7 +2212,7 @@ CropInfo snapshot_helper::cacheSnapShotInfo(OBJ_KEY newObj, VPT_ObjInfo obj, Tas @@ -2208,7 +2212,7 @@ CropInfo snapshot_helper::cacheSnapShotInfo(OBJ_KEY newObj, VPT_ObjInfo obj, Tas
2208 if (task.folderName != NULL) 2212 if (task.folderName != NULL)
2209 { 2213 {
2210 FRAME_KEY frame_id = { newObj.videoID, task.taskFrameCount }; 2214 FRAME_KEY frame_id = { newObj.videoID, task.taskFrameCount };
2211 - ImgSaveCache.insert(newObj, frame_id, task.task_algorithm_data); 2215 + ImgSaveCache.add_frame(newObj, frame_id, task.task_algorithm_data);
2212 } 2216 }
2213 2217
2214 //--------------------- 保存快照抠图 -----------------------------// 2218 //--------------------- 保存快照抠图 -----------------------------//
@@ -2464,9 +2468,10 @@ void snapshot_helper::clearSnapshotInfo() { @@ -2464,9 +2468,10 @@ void snapshot_helper::clearSnapshotInfo() {
2464 } 2468 }
2465 2469
2466 void snapshot_helper::erase_snapshot_info(OBJ_KEY& objKey){ 2470 void snapshot_helper::erase_snapshot_info(OBJ_KEY& objKey){
2467 - if (5 == objKey.videoID) {  
2468 - LOG_DEBUG("task:{} objId: {}", objKey.videoID, objKey.objID);  
2469 - } 2471 + // if (5 == objKey.videoID)
  2472 + // {
  2473 + // LOG_DEBUG("task:{} objId: {}", objKey.videoID, objKey.objID);
  2474 + // }
2470 2475
2471 snapShotInfo.erase(objKey); 2476 snapShotInfo.erase(objKey);
2472 } 2477 }
2473 \ No newline at end of file 2478 \ No newline at end of file