From 8b43e79f42e7db44b9dfedce9409a315daceb7a3 Mon Sep 17 00:00:00 2001 From: Kano Date: Thu, 18 Jul 2013 09:39:23 +1000 Subject: [PATCH 1/2] usbutils only one bulk transfer call per stat --- usbutils.c | 52 ++++++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/usbutils.c b/usbutils.c index f42c7d49..031f544b 100644 --- a/usbutils.c +++ b/usbutils.c @@ -402,12 +402,9 @@ struct resource_reply { // Replies to lock requests struct resource_reply *res_reply_head = NULL; -// Set this to 0 to remove stats processing -#define DO_USB_STATS 1 - -static bool stats_initialised = false; - -#if DO_USB_STATS +// Some stats need to always be defined +#define SEQ0 0 +#define SEQ1 1 // NONE must be 0 - calloced #define MODE_NONE 0 @@ -416,6 +413,13 @@ static bool stats_initialised = false; #define MODE_BULK_READ (1 << 2) #define MODE_BULK_WRITE (1 << 3) +// Set this to 0 to remove stats processing +#define DO_USB_STATS 1 + +static bool stats_initialised = false; + +#if DO_USB_STATS + #define MODE_SEP_STR "+" #define MODE_NONE_STR "X" #define MODE_CTRL_READ_STR "cr" @@ -451,9 +455,6 @@ struct cg_usb_stats { struct cg_usb_stats_details *details; }; -#define SEQ0 0 -#define SEQ1 1 - static struct cg_usb_stats *usb_stats = NULL; static int next_stat = USB_NOSTAT; @@ -2208,10 +2209,14 @@ static int usb_bulk_transfer(struct libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout, - struct cgpu_info *cgpu, enum usb_cmds cmd) + struct cgpu_info *cgpu, __maybe_unused int mode, + enum usb_cmds cmd, __maybe_unused int seq) { uint16_t MaxPacketSize; int err, errn, tries = 0; +#if DO_USB_STATS + struct timeval tv_start, tv_finish; +#endif /* Limit length of transfer to the largest this descriptor supports * and leave the higher level functions to transfer more if needed. */ @@ -2222,11 +2227,14 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, if (length > MaxPacketSize) length = MaxPacketSize; + STATS_TIMEVAL(&tv_start); cg_rlock(&cgusb_fd_lock); err = libusb_bulk_transfer(dev_handle, endpoint, data, length, transferred, timeout); errn = errno; cg_runlock(&cgusb_fd_lock); + STATS_TIMEVAL(&tv_finish); + USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq); if (err < 0) applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)", @@ -2246,11 +2254,14 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, break; } + STATS_TIMEVAL(&tv_start); cg_rlock(&cgusb_fd_lock); err = libusb_bulk_transfer(dev_handle, endpoint, data, length, transferred, timeout); errn = errno; cg_runlock(&cgusb_fd_lock); + STATS_TIMEVAL(&tv_finish); + USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq); if (err < 0) applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)", @@ -2274,9 +2285,6 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro { struct cg_usb_device *usbdev; bool ftdi; -#if DO_USB_STATS - struct timeval tv_start; -#endif struct timeval read_start, tv_finish; unsigned int initial_timeout; double max, done; @@ -2358,14 +2366,11 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro } } } - STATS_TIMEVAL(&tv_start); err = usb_bulk_transfer(usbdev->handle, usbdev->found->eps[ep].ep, ptr, usbbufread, &got, timeout, - cgpu, cmd); + cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1); cgtime(&tv_finish); - USB_STATS(cgpu, &tv_start, &tv_finish, err, - MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1); ptr[got] = '\0'; USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread); @@ -2467,14 +2472,11 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro } } } - STATS_TIMEVAL(&tv_start); err = usb_bulk_transfer(usbdev->handle, usbdev->found->eps[ep].ep, ptr, usbbufread, &got, timeout, - cgpu, cmd); + cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1); cgtime(&tv_finish); - USB_STATS(cgpu, &tv_start, &tv_finish, err, - MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1); ptr[got] = '\0'; USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread); @@ -2561,9 +2563,6 @@ out_unlock: int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd) { struct cg_usb_device *usbdev; -#if DO_USB_STATS - struct timeval tv_start; -#endif struct timeval read_start, tv_finish; unsigned int initial_timeout; double max, done; @@ -2615,14 +2614,11 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr cgtime(&(usbdev->last_write_tv)); usbdev->last_write_siz = bufsiz; } - STATS_TIMEVAL(&tv_start); err = usb_bulk_transfer(usbdev->handle, usbdev->found->eps[ep].ep, (unsigned char *)buf, bufsiz, &sent, - timeout, cgpu, cmd); + timeout, cgpu, MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1); cgtime(&tv_finish); - USB_STATS(cgpu, &tv_start, &tv_finish, err, - MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1); USBDEBUG("USB debug: @_usb_write(%s (nodev=%s)) err=%d%s sent=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), sent); From 4cb7cfb2268375befe022347ed18b201d9d94604 Mon Sep 17 00:00:00 2001 From: Kano Date: Thu, 18 Jul 2013 09:59:10 +1000 Subject: [PATCH 2/2] usbutils use a heap buffer for bulk read rather than stack --- usbutils.c | 4 +--- usbutils.h | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/usbutils.c b/usbutils.c index 031f544b..25e8f02b 100644 --- a/usbutils.c +++ b/usbutils.c @@ -2292,9 +2292,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro bool first = true; char *search; int endlen; - - // We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes - unsigned char usbbuf[USB_MAX_READ+4], *ptr; + unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf; size_t usbbufread; DEVLOCK(cgpu, pstate); diff --git a/usbutils.h b/usbutils.h index 9f3c283f..75f1e511 100644 --- a/usbutils.h +++ b/usbutils.h @@ -184,6 +184,8 @@ struct cg_usb_device { #define USB_NOSTAT 0 +#define USB_MAX_READ 8192 + struct cg_usb_info { uint8_t bus_number; uint8_t device_address; @@ -215,6 +217,13 @@ struct cg_usb_info { double total_read_delay; uint64_t write_delay_count; double total_write_delay; + + /* + * We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes + * If a single device ever has multiple end points then it will need + * multiple of these + */ + unsigned char bulkbuf[USB_MAX_READ+4]; }; enum usb_cmds {