fix sys_util::get_memory_usage

This commit is contained in:
gb 2023-04-15 17:42:55 +08:00
parent f61111caca
commit 7fdeb2d0ee
7 changed files with 35 additions and 22 deletions

View File

@ -104,13 +104,13 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val)
{ {
img_cb = val; img_cb = val;
img_cb <<= 32; img_cb <<= 32;
printf("image callback (1/2) = %p\n", img_cb);
return true; return true;
} }
else if(addr == (unsigned int)-2) else if(addr == (unsigned int)-2)
{ {
img_cb |= val; img_cb |= val;
printf("image callback - api(1/2) = %p\n", img_cb);
return true; return true;
} }
@ -118,7 +118,6 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val)
{ {
img_param = val; img_param = val;
img_param <<= 32; img_param <<= 32;
printf("image callback (1/2) = %p\n", img_param);
return true; return true;
} }
@ -128,6 +127,7 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val)
void* param = nullptr; void* param = nullptr;
img_param |= val; img_param |= val;
printf("image callback - param(2/2) = %p\n", img_param);
param = (void*)img_param; param = (void*)img_param;
*((uint64_t*)&func) = img_cb; *((uint64_t*)&func) = img_cb;
usbimages->set_image_keeper(func, param); usbimages->set_image_keeper(func, param);

View File

@ -352,24 +352,32 @@ namespace sys_util
return 0; 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())); std::string val(get_command_output((std::string("top -b -n 1 | grep ") + prog_name).c_str()));
uint32_t ind = 4;
if(val.length()) 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 else
{ {
val = get_command_output((std::string("ps -Al | grep ") + prog_name).c_str()); val = get_command_output((std::string("ps -Al | grep ") + prog_name).c_str());
if(val.length()) 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) std::string format_readable_bytes(uint64_t bytes)
{ {

View File

@ -65,8 +65,8 @@ namespace sys_util
} }
int32_t get_memory_info(uint64_t* total, uint64_t* available); 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" 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 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); 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); int get_disk_size(const char* path, uint64_t* total, uint64_t* avail, uint64_t* block_size);

View File

@ -45,9 +45,9 @@ usb_gadget* usb_gadget_config::get_config(void)
static void print_mem_usage(const char* tips) 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 // async_usb_gadget

View File

@ -317,9 +317,9 @@ void UsbDevice::fill_dev_descriptor(camtp_ctx * ctx, usb_gadget * usbctx,struct
#ifdef ASYNC_EP #ifdef ASYNC_EP
static void print_mem_usage(const char* desc) 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) static void image_receiver(MemoryPtr data, bool img, void* param)
{ {
@ -1529,14 +1529,15 @@ class image_packet : public data_source
MemoryPtr img_; MemoryPtr img_;
dyn_mem_ptr head_; dyn_mem_ptr head_;
uint32_t offset_; uint32_t offset_;
uint32_t ind_;
uint64_t cur_mem_;
public: 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; LPPACK_BASE pack = nullptr;
LPPACKIMAGE pimg = nullptr; LPPACKIMAGE pimg = nullptr;
printf("sizeof(PACKIMAGE) = %d\n", sizeof(PACKIMAGE));
head_ = dyn_mem::memory(sizeof(PACK_BASE) + sizeof(PACKIMAGE)); head_ = dyn_mem::memory(sizeof(PACK_BASE) + sizeof(PACKIMAGE));
pack = (LPPACK_BASE)head_->ptr(); pack = (LPPACK_BASE)head_->ptr();
pimg = (LPPACKIMAGE)pack->payload; pimg = (LPPACKIMAGE)pack->payload;
@ -1550,12 +1551,16 @@ public:
pimg->info_size = 0; pimg->info_size = 0;
head_->set_len(sizeof(PACK_BASE) + sizeof(PACKIMAGE)); 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: protected:
virtual ~image_packet() virtual ~image_packet()
{ {
uint32_t size = img_->size();
head_->release(); 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: 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 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_); 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_); log_cls::log(LOG_LEVEL_ALL, "Start scanning %d papers and %d DPI ...\n", scan_cnt_, dpi_);
ret = ctrl_handler(-1, nullptr, nullptr); 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, "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); reply = dyn_mem::memory(base_head_size);
@ -1912,16 +1920,14 @@ void UsbDevice::save_image(MemoryPtr data, bool img)
if(img) if(img)
{ {
image_packet *ip = new image_packet(data, ++img_cnt_, scan_id_); uint64_t n = sys_util::get_memory_usage("scan");
std::string mem(sys_util::get_memory_usage("scan")); uint32_t que = 0;
uint32_t n = atoi(mem.c_str()), que = 0; image_packet *ip = new image_packet(data, ++img_cnt_, scan_id_, n);
if(max_mem < n) if(max_mem < n)
max_mem = n; max_mem = n;
printf("image arrived with %u bytes!\n", data->size());
que = usb_->write_bulk(ip); 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(); ip->release();
// check has completed ? // check has completed ?
@ -2008,7 +2014,6 @@ void UsbDevice::save_image(MemoryPtr data, bool img)
{ {
char ebuf[20] = {0}; char ebuf[20] = {0};
max_mem *= 1024;
err = scan_err_; 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()); 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; max_mem = 0;

View File

@ -1202,9 +1202,9 @@ void CDlgScanner::OnBnClickedButtonScan()
if (scanner_) if (scanner_)
{ {
int err = scanner_->scan_start(); 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) 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 else
{ {
::SetDlgItemTextW(m_hWnd, IDC_BUTTON_SCAN, L"Stop"); ::SetDlgItemTextW(m_hWnd, IDC_BUTTON_SCAN, L"Stop");