diff --git a/hardware/hardware.cpp b/hardware/hardware.cpp index 198de05..dd36870 100644 --- a/hardware/hardware.cpp +++ b/hardware/hardware.cpp @@ -9,6 +9,9 @@ #include #include +// #define DBG_PRINT printf +#define DBG_PRINT no_printf + ///////////////////////////////////////////////////////////////////////////////////////////////////////// // scanner_hw // #define TEST_1 @@ -26,6 +29,8 @@ static std::string device_opt_json[] = { }; #endif +static void no_printf(const char* fmt, ...) +{} ///////////////////////////////////////////////////////////////////////////////////////////////////////// // scanner_hw @@ -373,7 +378,14 @@ void scanner_hw::thread_image_capture(bool paper_ready) std::function img_callback(img_handler_); utils::to_log(LOG_LEVEL_DEBUG, "scanning thread working ...\n"); - utils::to_log(LOG_LEVEL_DEBUG, "set capture thread to CPU %d = %d\n", CPU_MAJOR_0, utils::set_cpu_affinity(CPU_MAJOR_0)); + +#ifdef BIND_CPU + std::vector cpu; + + for(int i = 0; i < CPU_MAJOR_CNT; ++i) + cpu.push_back(CPU_MAJOR_0 + i); + utils::to_log(LOG_LEVEL_DEBUG, "set capture thread to CPU %d = %d\n", CPU_MAJOR_0, utils::set_cpu_affinity(&cpu[0], cpu.size())); +#endif avail_mem.enable_wait_log(false); motor_->clear_error(); @@ -457,7 +469,7 @@ void scanner_hw::thread_image_capture(bool paper_ready) } scanning_ = false; - img_handler_((dyn_mem_ptr)WORKER_STATUS_IDLE, false, (LPPACKIMAGE)err); + img_callback((dyn_mem_ptr)WORKER_STATUS_IDLE, false, (LPPACKIMAGE)err); } int scanner_hw::start_and_wait_lifter(int to_ms, int* ui_words_id) { @@ -487,6 +499,7 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c chronograph watch; std::pair mbev; dyn_mem_shared_ptr mem = nullptr; + std::function img_callback(img_handler_); auto put_v4l2_mem = [&](BEFORE_DESTROY_PARAM) -> BEFORE_DESTROY_RET { @@ -502,10 +515,14 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c img->life = chronograph::from_process_born(); while(scanning_) { + bool resok = true; + uint64_t bgn = chronograph::from_process_born(); + uint32_t oper_index = 0; #ifdef REBUILD_IN_CIS_THREAD if(mem) { - img_handler_(mem, true, img); + img_callback(mem, true, img); + DBG_PRINT("\n\t%d: to rebuild spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); mem->release(); mem = nullptr; } @@ -513,6 +530,9 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c int fatal = 0; ret = SCANNER_ERR_DEVICE_HD_002; words = ID_WORDS_STATUS_DEVICE_HD_002; + resok = res_(TASK_CAPTURER, false, 0); + DBG_PRINT("\t%d: to check resource spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); + if(!motor_->wait_event(MOTOR_BORD_EVENT_PAPER_PASSING, &ret, to_paper_out_, &fatal, &words)) { if(ret != SCANNER_ERR_DEVICE_DOUBLE_FEEDING) @@ -533,6 +553,7 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c ret = SCANNER_ERR_OK; words = 0; } + DBG_PRINT("\t%d: to wait paper-out spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); pass = watch.elapse_ms(); img->pos.paper_ind++; @@ -544,11 +565,13 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c else { img->height = get_image_real_height(minh); + DBG_PRINT("\t%d: to get_image_real_height spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); size_t size = 0; int ind = -1; void* frame = camera_->read_frame(to_stream_, size, ind); + DBG_PRINT("\t%d: to read_frame spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); if(!frame) { if(fatal != MOTOR_BORD_EVENT_SCAN_DONE) @@ -566,6 +589,7 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c mem->set_param(cism, 0); mem->set_param((void*)(long)ind, 1); img->pos.status = hg_err_2_image_status(ret); + DBG_PRINT("\t%d: to construct dyn_mem_shared spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); #ifndef REBUILD_IN_CIS_THREAD img_handler_(mem, true, img); // (frame, ind, LPPACKIMAGE, cism, ret, pass, bool) mem->release(); @@ -579,6 +603,7 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c // retrieve V4L2 memory ... retrieve_v4l2_mem(cism, cism_cnt); + DBG_PRINT("\t%d: to retrieve_v4l2_mem spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); if(cism_cnt[0] >= camera_->get_mem_count()) { ret = SCANNER_ERR_DEVICE_CIS_OUT_OF_MEM; @@ -592,8 +617,12 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c break; // resource checking ... - if(res_(TASK_CAPTURER, true, 3000) && scanning_) + if(!resok) + resok = res_(TASK_CAPTURER, true, SEC_2_MS(10)); + if(resok && scanning_) { + DBG_PRINT("\t%d: to flow completed spend %ums.\n", ++oper_index, chronograph::from_process_born() - bgn); + // should check paper ready ? watch.reset(); motor_->pick_paper(); @@ -601,6 +630,9 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c } else { + if(scanning_) + utils::to_log(LOG_LEVEL_FATAL, "Resource is up to limit, stop scanning work now!\n"); + ret = SCANNER_ERR_DEVICE_HD_003; words = ID_WORDS_STATUS_DEVICE_HD_003; break; @@ -610,7 +642,7 @@ int scanner_hw::scan_one_turn(LPPACKIMAGE img, safe_fifo* cism, int* cism_c #ifdef REBUILD_IN_CIS_THREAD if(mem) { - img_handler_(mem, true, img); + img_callback(mem, true, img); mem->release(); mem = nullptr; } diff --git a/imgproc/imgprc_mgr.cpp b/imgproc/imgprc_mgr.cpp index bdffa7b..2a8c29f 100644 --- a/imgproc/imgprc_mgr.cpp +++ b/imgproc/imgprc_mgr.cpp @@ -276,10 +276,15 @@ void imgproc_mgr::thread_worker(void* param) void(*dump)(DUMP_PROTO) = dump_img_ ? &imgproc_mgr::real_dump_image : &imgproc_mgr::empty_dump_image; LPPAGEDPARAM para = (LPPAGEDPARAM)(void*)dump; #endif - int cpu = (ind % (CPU_CORES - CPU_MAJOR_CNT)) + CPU_MINOR_0; +#ifdef BIND_CPU + std::vector cpu; + + for(int i = 0; i < CPU_CORES - CPU_MAJOR_CNT; ++i) + cpu.push_back(CPU_MINOR_0 + i); utils::to_log(LOG_LEVEL_DEBUG, "set image process thread %d to CPU %d = %d\n" - , ind + 1, cpu, utils::set_cpu_affinity(cpu)); + , ind + 1, cpu, utils::set_cpu_affinity(&cpu[0], cpu.size())); +#endif add_busy_worker(); #ifdef USE_THREAD_PAGED_DATA @@ -303,13 +308,13 @@ void imgproc_mgr::thread_worker(void* param) if(img.img && !img.data) #endif break; - process(&img, para); + process(&img, para, ind); } } #else while(run_) { - if(res_) + if(ind && res_) { while(!res_(TASK_IMG_PROCESSOR, true, 3000, res_param_)) { diff --git a/scanner/res_mgr.cpp b/scanner/res_mgr.cpp index 01b5875..fbaf9dd 100644 --- a/scanner/res_mgr.cpp +++ b/scanner/res_mgr.cpp @@ -49,7 +49,7 @@ std::string resource_mgr::task_type(_task task) void resource_mgr::start(void) { - printf("\tstart resource monitor with mem limit = %llu\n", mem_limit_); + printf("\tprocess %llu start resource monitor with mem limit = %llu\n", GetCurrentProcessId(), mem_limit_); monitor_ = true; } void resource_mgr::stop(void) @@ -64,7 +64,7 @@ bool resource_mgr::is_resource_enable(_task task, bool wait, int to_ms) { #ifndef USE_MONITOR_THREAD if(task == TASK_CAPTURER) - utils::get_memory_usage(nullptr, (uint64_t*)&mem_now_, nullptr); + utils::get_memory_usage(nullptr, nullptr, (uint64_t*)&mem_now_); #endif if(wait && mem_now_ > mem_limit_) @@ -75,10 +75,14 @@ bool resource_mgr::is_resource_enable(_task task, bool wait, int to_ms) while(monitor_ && watch.elapse_ms() < to_ms) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); +#ifndef USE_MONITOR_THREAD + if(task == TASK_CAPTURER) + utils::get_memory_usage(nullptr, nullptr, (uint64_t*)&mem_now_); +#endif if(mem_now_ < mem_limit_) break; } - utils::to_log(LOG_LEVEL_WARNING, "Resources wait result: %lld\n", mem_now_); + utils::to_log(LOG_LEVEL_WARNING, "Resources(task '%s') wait result: %lld\n", resource_mgr::task_type(task).c_str(), mem_now_); } return mem_now_ < mem_limit_; diff --git a/scanner/res_mgr.h b/scanner/res_mgr.h index 8cc7fe8..ad9341e 100644 --- a/scanner/res_mgr.h +++ b/scanner/res_mgr.h @@ -14,7 +14,7 @@ class resource_mgr { volatile bool run_ = true; volatile bool monitor_ = false; - uint64_t mem_limit_ = SIZE_GB(3); + uint64_t mem_limit_ = SIZE_MB(2500); volatile uint64_t mem_now_ = 0; std::unique_ptr thread_; diff --git a/sdk/base/data.h b/sdk/base/data.h index a87496e..6e9c354 100644 --- a/sdk/base/data.h +++ b/sdk/base/data.h @@ -402,8 +402,10 @@ public: #define FUNCTION_PROTO_PARAMETERS dyn_mem_ptr, uint32_t*, packet_data_base_ptr* #define FUNCTION_PROTO_COMMAND_HANDLE dyn_mem_ptr(FUNCTION_PROTO_PARAMETERS) -// #define USE_THREAD_PAGED_DATA +#define USE_THREAD_PAGED_DATA #define REBUILD_IN_CIS_THREAD +// #define BIND_CPU + #define SENDER_PROTO data_source_ptr ptr, void* param #define RES_CHK_PROTO int type, bool wait, int to_ms, void* param diff --git a/sdk/base/ui.cpp b/sdk/base/ui.cpp index 0f82f64..8d076a7 100644 --- a/sdk/base/ui.cpp +++ b/sdk/base/ui.cpp @@ -256,7 +256,7 @@ namespace devui ms.msg = UI_STATUS_PEER_CLOSED; cb_(&ms); - printf("PIPE: peer closed(read ZERO byte and error = %d).\n", errno); + printf("[%s] PIPE: peer closed(read ZERO byte and error = %d).\n", utils::format_current_time().c_str(), errno); utils::to_log(LOG_LEVEL_DEBUG, "PIPE: peer closed(read ZERO byte and error = %d).\n", errno); } std::this_thread::sleep_for(std::chrono::milliseconds(10)); diff --git a/sdk/base/utils.cpp b/sdk/base/utils.cpp index ad22246..bc58b8b 100644 --- a/sdk/base/utils.cpp +++ b/sdk/base/utils.cpp @@ -1529,7 +1529,7 @@ namespace utils return ps; } - int set_cpu_affinity(int cpuind, pthread_t thread) + int set_cpu_affinity(int *cpuind, int cnt, pthread_t thread) { int ret = 0; @@ -1537,9 +1537,13 @@ namespace utils thread = GetCurrentThreadId(); cpu_set_t cpu; + int mask = 0; + + for(int i = 0; i < cnt; ++i) + mask |= 1 << (cpuind[i] & 0x1f); CPU_ZERO(&cpu); - CPU_SET(cpuind, &cpu); + CPU_SET(mask, &cpu); ret = pthread_setaffinity_np(thread, sizeof(cpu), &cpu); return ret; diff --git a/sdk/base/utils.h b/sdk/base/utils.h index 0b74ff1..6d90f67 100644 --- a/sdk/base/utils.h +++ b/sdk/base/utils.h @@ -114,7 +114,7 @@ namespace utils void print_memory_usage(const char* tips, bool to_log_file); int get_disk_space(const char* path, unsigned long long* total, unsigned long long* avail, unsigned long long* block); unsigned int get_page_size(unsigned int* map_unit = nullptr); - int set_cpu_affinity(int cpuind, pthread_t thread = -1); + int set_cpu_affinity(int *cpuind, int cnt = 1, pthread_t thread = -1); // return logging file path if 'type' was LOG_TYPE_FILE std::string init_log(log_type type, log_level level = LOG_LEVEL_ALL, const char* fn_appendix = nullptr/*appendix to default log-file-name*/); diff --git a/usb/usb_io.cpp b/usb/usb_io.cpp index f1391cb..44bac3d 100644 --- a/usb/usb_io.cpp +++ b/usb/usb_io.cpp @@ -52,6 +52,14 @@ async_usb_gadget::async_usb_gadget(std::function }; auto bulkw = [this](void*) -> void { +#ifdef BIND_CPU + std::vector cpu; + + for(int i = 0; i < CPU_MAJOR_CNT; ++i) + cpu.push_back(CPU_MAJOR_0 + i); + utils::set_cpu_affinity(&cpu[0], cpu.size()); +#endif + while(run_) { wait_in_.wait(); diff --git a/xmake.lua b/xmake.lua index 94dca6c..742de1e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -61,7 +61,7 @@ add_defines("BUILD_AS_DEVICE") add_defines("VER_MAIN=2") add_defines("VER_FAMILY=200") add_defines("VER_DATE=20240308") -add_defines("VER_BUILD=8") +add_defines("VER_BUILD=25") target("conf") set_kind("phony")