调整内存控制:USB+图像处理队列1GB;支持异步IO的100MB

This commit is contained in:
gb 2022-11-26 10:00:40 +08:00
parent 0a206f1eb2
commit 899b78523f
4 changed files with 28 additions and 4 deletions

View File

@ -763,11 +763,15 @@ int tiny_buffer::get_image_statu(void)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// final_img_queue // final_img_queue
final_img_queue::final_img_queue() final_img_queue::final_img_queue() : mem_usage_(0)
{} {}
final_img_queue::~final_img_queue() final_img_queue::~final_img_queue()
{} {}
unsigned long long final_img_queue::mem_usage(void)
{
return mem_usage_;
}
size_t final_img_queue::size(void) size_t final_img_queue::size(void)
{ {
std::lock_guard<std::mutex> lck(lock_); std::lock_guard<std::mutex> lck(lock_);
@ -778,6 +782,7 @@ void final_img_queue::clear(void)
{ {
std::lock_guard<std::mutex> lck(lock_); std::lock_guard<std::mutex> lck(lock_);
mem_usage_ = 0;
queue_.clear(); queue_.clear();
} }
bool final_img_queue::put(int w, int h, int bpp, int channels, int line_bytes, void* data, unsigned bytes bool final_img_queue::put(int w, int h, int bpp, int channels, int line_bytes, void* data, unsigned bytes
@ -813,6 +818,7 @@ bool final_img_queue::put(int w, int h, int bpp, int channels, int line_bytes, v
{ {
std::lock_guard<std::mutex> lck(lock_); std::lock_guard<std::mutex> lck(lock_);
queue_.push_back(imgd); queue_.push_back(imgd);
mem_usage_ += bytes;
ret = true; ret = true;
} }
else else
@ -869,6 +875,9 @@ void final_img_queue::fetch_front(void* buf, int* len, bool* over)
imgd.offset += *len; imgd.offset += *len;
if (imgd.offset >= imgd.header.bytes) if (imgd.offset >= imgd.header.bytes)
{ {
mem_usage_ -= imgd.header.bytes;
if (mem_usage_ < 0)
mem_usage_ = 0;
if (over) if (over)
*over = true; *over = true;
queue_.erase(queue_.begin()); queue_.erase(queue_.begin());

View File

@ -225,12 +225,14 @@ class final_img_queue
{ {
mutable std::mutex lock_; mutable std::mutex lock_;
std::vector<IMGDT> queue_; std::vector<IMGDT> queue_;
long long mem_usage_;
public: public:
final_img_queue(); final_img_queue();
~final_img_queue(); ~final_img_queue();
public: public:
unsigned long long mem_usage(void);
size_t size(void); size_t size(void);
void clear(void); void clear(void);
bool put(int w, int h, int bpp, int channels, int line_bytes, void* data, unsigned bytes bool put(int w, int h, int bpp, int channels, int line_bytes, void* data, unsigned bytes

View File

@ -99,7 +99,7 @@ hg_scanner::hg_scanner(ScannerSerial serial
, async_io_(false), is_white_0_(true), isremove_left_hole(false), isremove_right_hole(false), isremove_top_hole(false), isremove_low_hole(false) , async_io_(false), is_white_0_(true), isremove_left_hole(false), isremove_right_hole(false), isremove_top_hole(false), isremove_low_hole(false)
, isremove_left_hole_threshold(0), isremove_right_hole_threshold(0), isremove_top_hole_threshold(0), isremove_low_hole_threshold(0) , isremove_left_hole_threshold(0), isremove_right_hole_threshold(0), isremove_top_hole_threshold(0), isremove_low_hole_threshold(0)
, dump_usb_path_(""),is_kernelsnap_211209_(false), pid_(0), dump_img_(&hg_scanner::dump_image_empty), is_kernelsnap_220830_(false),is_kernelsnap3288_221106_(false) , dump_usb_path_(""),is_kernelsnap_211209_(false), pid_(0), dump_img_(&hg_scanner::dump_image_empty), is_kernelsnap_220830_(false),is_kernelsnap3288_221106_(false)
, is_kernelsnap_221027_(false), memory_size_(200/*USB图像队列JPEG压缩*/), isx86_Advan_(true) , is_kernelsnap_221027_(false), memory_size_(1024/*USB+JPEG压缩及图像处理图队列总共1GB*/), isx86_Advan_(true), stop_fatal_(SCANNER_ERR_OK)
{ {
#if !defined(_WIN32) && !defined(_WIN64) &&defined(x86_64) #if !defined(_WIN32) && !defined(_WIN64) &&defined(x86_64)
isx86_Advan_ = false; isx86_Advan_ = false;
@ -728,7 +728,7 @@ void hg_scanner::thread_handle_image_process(void)
else else
{ {
VLOG_MINI_1(LOG_LEVEL_FATAL, "Get Image Process is NULL pid is %d.\n",pid_); VLOG_MINI_1(LOG_LEVEL_FATAL, "Get Image Process is NULL pid is %d.\n",pid_);
status_ = SCANNER_ERR_INSUFFICIENT_MEMORY; stop_fatal_ = SCANNER_ERR_INSUFFICIENT_MEMORY;
stop(); stop();
break; break;
} }
@ -736,7 +736,7 @@ void hg_scanner::thread_handle_image_process(void)
catch (...) catch (...)
{ {
VLOG_MINI_1(LOG_LEVEL_FATAL, "FATAL: Insufficient memory when proecss image with %d bytes.\n", tiny_buffer->size()); VLOG_MINI_1(LOG_LEVEL_FATAL, "FATAL: Insufficient memory when proecss image with %d bytes.\n", tiny_buffer->size());
status_ = SCANNER_ERR_INSUFFICIENT_MEMORY; stop_fatal_ = SCANNER_ERR_INSUFFICIENT_MEMORY;
stop(); stop();
break; break;
} }
@ -750,6 +750,7 @@ void hg_scanner::thread_handle_image_process(void)
void hg_scanner::working_begin(void*) void hg_scanner::working_begin(void*)
{ {
stop_fatal_ = SCANNER_ERR_OK;
final_img_index_ = 0; final_img_index_ = 0;
status_ = SCANNER_ERR_OK; status_ = SCANNER_ERR_OK;
notify_ui_working_status(STATU_DESC_SCAN_WORKING, SANE_EVENT_WORKING, SCANNER_ERR_OK); notify_ui_working_status(STATU_DESC_SCAN_WORKING, SANE_EVENT_WORKING, SCANNER_ERR_OK);
@ -761,6 +762,9 @@ void hg_scanner::working_done(void*)
if(user_cancel_) if(user_cancel_)
final_imgs_.clear(); final_imgs_.clear();
if (status_ == SCANNER_ERR_OK && stop_fatal_)
status_ = stop_fatal_;
switch (status_) switch (status_)
{ {
case SCANNER_ERR_OK: case SCANNER_ERR_OK:
@ -2398,12 +2402,20 @@ int hg_scanner::save_usb_data(std::shared_ptr<tiny_buffer> data)
size_t que_size = 0; size_t que_size = 0;
imgs_.Size(&que_size); imgs_.Size(&que_size);
Memoryusae = que_size / 1024.0f / 1024.0f; Memoryusae = que_size / 1024.0f / 1024.0f;
if (async_io_)
Memoryusae *= 10;
else
Memoryusae += final_imgs_.mem_usage() / 1024.0f / 1024.0f;
while (Memoryusae >= memory_size_ && !user_cancel_ && num < 20)//三个条件以防止卡死 while (Memoryusae >= memory_size_ && !user_cancel_ && num < 20)//三个条件以防止卡死
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
//Memoryusae = hg_log::GetAppMemoryUsage(); //Memoryusae = hg_log::GetAppMemoryUsage();
imgs_.Size(&que_size); imgs_.Size(&que_size);
Memoryusae = que_size / 1024.0f / 1024.0f; Memoryusae = que_size / 1024.0f / 1024.0f;
if (async_io_)
Memoryusae *= 10;
else
Memoryusae += final_imgs_.mem_usage() / 1024.0f / 1024.0f;
if(num++ == 0) if(num++ == 0)
VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "Memory Usage is Too big:%f ,Please wait -_- ...\r\n", Memoryusae); VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "Memory Usage is Too big:%f ,Please wait -_- ...\r\n", Memoryusae);
} }

View File

@ -293,6 +293,7 @@ protected:
std::string final_path_; std::string final_path_;
unsigned long long memory_size_; unsigned long long memory_size_;
bool isx86_Advan_; bool isx86_Advan_;
int stop_fatal_;
BlockingQueue<std::shared_ptr<tiny_buffer>> imgs_; BlockingQueue<std::shared_ptr<tiny_buffer>> imgs_;
void init_settings(const char* json_setting_text); void init_settings(const char* json_setting_text);