diff --git a/device/gxx-linux/scanner/scannerregs.cpp b/device/gxx-linux/scanner/scannerregs.cpp index e044821..de49d86 100644 --- a/device/gxx-linux/scanner/scannerregs.cpp +++ b/device/gxx-linux/scanner/scannerregs.cpp @@ -104,13 +104,13 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) { img_cb = val; img_cb <<= 32; - printf("image callback (1/2) = %p\n", img_cb); return true; } else if(addr == (unsigned int)-2) { img_cb |= val; + printf("image callback - api(1/2) = %p\n", img_cb); return true; } @@ -118,7 +118,6 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) { img_param = val; img_param <<= 32; - printf("image callback (1/2) = %p\n", img_param); return true; } @@ -128,6 +127,7 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) void* param = nullptr; img_param |= val; + printf("image callback - param(2/2) = %p\n", img_param); param = (void*)img_param; *((uint64_t*)&func) = img_cb; usbimages->set_image_keeper(func, param); diff --git a/device/gxx-linux/usb/src/common/sys_util.cpp b/device/gxx-linux/usb/src/common/sys_util.cpp index 66f91dc..d322ea4 100644 --- a/device/gxx-linux/usb/src/common/sys_util.cpp +++ b/device/gxx-linux/usb/src/common/sys_util.cpp @@ -352,24 +352,32 @@ namespace sys_util return 0; } - std::string get_memory_usage(const char* prog_name) + uint64_t get_memory_usage(const char* prog_name) { std::string val(get_command_output((std::string("top -b -n 1 | grep ") + prog_name).c_str())); + uint32_t ind = 4; if(val.length()) { - val = pick_str_separate_by(val.c_str(), 4); + if(val[0] == ' ') + ind++; + val = pick_str_separate_by(val.c_str(), ind); } else { val = get_command_output((std::string("ps -Al | grep ") + prog_name).c_str()); if(val.length()) { - val = pick_str_separate_by(val.c_str(), 9); + ind = 9; + if(val[0] == ' ') + ind++; + val = pick_str_separate_by(val.c_str(), ind); } } - return val; + uint64_t size = atoi(val.c_str()); + + return size * 1024; } std::string format_readable_bytes(uint64_t bytes) { diff --git a/device/gxx-linux/usb/src/common/sys_util.h b/device/gxx-linux/usb/src/common/sys_util.h index 4b2cac3..34fe405 100644 --- a/device/gxx-linux/usb/src/common/sys_util.h +++ b/device/gxx-linux/usb/src/common/sys_util.h @@ -65,8 +65,8 @@ namespace sys_util } int32_t get_memory_info(uint64_t* total, uint64_t* available); - std::string get_memory_usage(const char* prog_name); // return output of command "ps -Al | grep prog_name" - std::string format_readable_bytes(uint64_t bytes); // convert to readable text: 512B, 1.21KB, 1.10MB, 3.45GB, 1,234.56GB ... + uint64_t get_memory_usage(const char* prog_name); // return output of command "top -b -n 1 | grep prog_name" + std::string format_readable_bytes(uint64_t bytes); // convert to readable text: 512B, 1.21KB, 1.10MB, 3.45GB, 1,234.56GB ... std::string get_command_output(const char* cmd, uint16_t max_line_len = 256, bool one_line = true); int get_disk_size(const char* path, uint64_t* total, uint64_t* avail, uint64_t* block_size); diff --git a/device/gxx-linux/usb/src/common/usb_io.cpp b/device/gxx-linux/usb/src/common/usb_io.cpp index 0f69206..953bb11 100644 --- a/device/gxx-linux/usb/src/common/usb_io.cpp +++ b/device/gxx-linux/usb/src/common/usb_io.cpp @@ -45,9 +45,9 @@ usb_gadget* usb_gadget_config::get_config(void) static void print_mem_usage(const char* tips) { - std::string size(sys_util::get_memory_usage("scan")); + uint64_t size = sys_util::get_memory_usage("scan"); - printf("\n--Memory usage of %s: %s--\n", tips, size.c_str()); + printf("\n--Memory usage of %s: %s--\n", tips, sys_util::format_readable_bytes(size).c_str()); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // async_usb_gadget diff --git a/device/gxx-linux/usb/src/usbdevice.cpp b/device/gxx-linux/usb/src/usbdevice.cpp index 4658777..4bbc0fe 100644 --- a/device/gxx-linux/usb/src/usbdevice.cpp +++ b/device/gxx-linux/usb/src/usbdevice.cpp @@ -317,9 +317,9 @@ void UsbDevice::fill_dev_descriptor(camtp_ctx * ctx, usb_gadget * usbctx,struct #ifdef ASYNC_EP static void print_mem_usage(const char* desc) { - std::string size(sys_util::get_memory_usage("scan")); + uint64_t size = sys_util::get_memory_usage("scan"); - printf("\n--Memory usage of %s: %s--\n", desc, size.c_str()); + printf("\n--Memory usage of %s: %s--\n", desc, sys_util::format_readable_bytes(size).c_str()); } static void image_receiver(MemoryPtr data, bool img, void* param) { @@ -1529,14 +1529,15 @@ class image_packet : public data_source MemoryPtr img_; dyn_mem_ptr head_; uint32_t offset_; + uint32_t ind_; + uint64_t cur_mem_; public: - image_packet(MemoryPtr img, uint32_t cnt, uint32_t scanid) : img_(img), offset_(0) + image_packet(MemoryPtr img, uint32_t cnt, uint32_t scanid, uint64_t total_mem) : img_(img), offset_(0), ind_(cnt), cur_mem_(total_mem) { LPPACK_BASE pack = nullptr; LPPACKIMAGE pimg = nullptr; - printf("sizeof(PACKIMAGE) = %d\n", sizeof(PACKIMAGE)); head_ = dyn_mem::memory(sizeof(PACK_BASE) + sizeof(PACKIMAGE)); pack = (LPPACK_BASE)head_->ptr(); pimg = (LPPACKIMAGE)pack->payload; @@ -1550,12 +1551,16 @@ public: pimg->info_size = 0; head_->set_len(sizeof(PACK_BASE) + sizeof(PACKIMAGE)); + log_cls::log(LOG_LEVEL_ALL, "Image-%04u wait to be sent: size = %u, memory usage %s\n", ind_, img->size(), sys_util::format_readable_bytes(cur_mem_).c_str()); } protected: virtual ~image_packet() { + uint32_t size = img_->size(); head_->release(); + img_.reset(); + log_cls::log(LOG_LEVEL_ALL, "Image-%04u sending complete: %u/%u, memory usage %s\n", ind_, offset_, size, sys_util::format_readable_bytes(sys_util::get_memory_usage("scan")).c_str()); } public: @@ -1846,6 +1851,8 @@ dyn_mem_ptr UsbDevice::handle_bulk_cmd(LPPACK_BASE pack, uint32_t* used, packet_ } { + ctrl_handler(-1, (usb_ctrlrequest*)SR_IM_CLEAR, (unsigned char*)0); + log_cls::log(LOG_LEVEL_ALL, "Memory usage before starting to scan: %s\n", sys_util::format_readable_bytes(sys_util::get_memory_usage("scan")).c_str()); bool ret = ctrl_handler(-1, (usb_ctrlrequest*)15, (unsigned char*)0x160); // hardware configuration ctrl_handler(-1, (usb_ctrlrequest*)SR_SCAN_CNT, (unsigned char*)scan_cnt_); @@ -1854,6 +1861,7 @@ dyn_mem_ptr UsbDevice::handle_bulk_cmd(LPPACK_BASE pack, uint32_t* used, packet_ log_cls::log(LOG_LEVEL_ALL, "Start scanning %d papers and %d DPI ...\n", scan_cnt_, dpi_); ret = ctrl_handler(-1, nullptr, nullptr); log_cls::log(LOG_LEVEL_ALL, "Start scanning %s\n", ret ? "OK" : "Failed"); + log_cls::log(LOG_LEVEL_ALL, "Memory usage after scanning started: %s\n", sys_util::format_readable_bytes(sys_util::get_memory_usage("scan")).c_str()); } reply = dyn_mem::memory(base_head_size); @@ -1912,16 +1920,14 @@ void UsbDevice::save_image(MemoryPtr data, bool img) if(img) { - image_packet *ip = new image_packet(data, ++img_cnt_, scan_id_); - std::string mem(sys_util::get_memory_usage("scan")); - uint32_t n = atoi(mem.c_str()), que = 0; + uint64_t n = sys_util::get_memory_usage("scan"); + uint32_t que = 0; + image_packet *ip = new image_packet(data, ++img_cnt_, scan_id_, n); if(max_mem < n) max_mem = n; - printf("image arrived with %u bytes!\n", data->size()); que = usb_->write_bulk(ip); - log_cls::log(LOG_LEVEL_ALL, "New image with bytes: %u, memory usage: %s, sent-que length: %d\n", data->size(), sys_util::format_readable_bytes((uint64_t)n * 1024).c_str(), que); ip->release(); // check has completed ? @@ -2008,7 +2014,6 @@ void UsbDevice::save_image(MemoryPtr data, bool img) { char ebuf[20] = {0}; - max_mem *= 1024; err = scan_err_; log_cls::log(LOG_LEVEL_ALL, "Scan over with error: %s; Max memory usage: %s\n", log_cls::str_scanner_status((scanner_status)scan_err_, ebuf), sys_util::format_readable_bytes(max_mem).c_str()); max_mem = 0; diff --git a/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe b/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe index 5738fad..f5d34e4 100644 Binary files a/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe and b/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe differ diff --git a/pc/code_twain/sln/usb_tools/DlgScanner.cpp b/pc/code_twain/sln/usb_tools/DlgScanner.cpp index a954876..9381e24 100644 --- a/pc/code_twain/sln/usb_tools/DlgScanner.cpp +++ b/pc/code_twain/sln/usb_tools/DlgScanner.cpp @@ -1202,9 +1202,9 @@ void CDlgScanner::OnBnClickedButtonScan() if (scanner_) { int err = scanner_->scan_start(); - log_cls::log(LOG_LEVEL_DEBUG, "Start to scan = %d\r\n", err); + log_cls::log(LOG_LEVEL_DEBUG, "Start to scan = %s\r\n", usb::u2a(scanner_status(err, title)).c_str()); if (err) - msg_box(m_hWnd, MB_OK, L"Error", L"Failed in startin scanning with code %d", err); + msg_box(m_hWnd, MB_OK, L"Error", L"Failed in startin scanning with code %s", scanner_status(err, title)); else { ::SetDlgItemTextW(m_hWnd, IDC_BUTTON_SCAN, L"Stop");