Skip to content

Commit

Permalink
HydraUSB3_USB_benchmark v1.0.2
Browse files Browse the repository at this point in the history
Fixed some bugs on log_printf()/log_printf_dbg()
Fixed (libusb)handle leaks/memory leaks(checked with Valgrind on Ubuntu 20.04LTS)
Fixed typo in report
  • Loading branch information
bvernoux committed Aug 31, 2022
1 parent 0217853 commit 00843ef
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 56 deletions.
61 changes: 32 additions & 29 deletions HydraUSB3_USB_benchmark/HydraUSB3_USB_benchmark.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : HydraUSB3_USB_benchmark.c
* Author : bvernoux
* Version : V1.0.1
* Date : 2022/08/22
* Version : V1.0.2
* Date : 2022/08/31
* Description :
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -32,6 +32,11 @@ static uint8_t mWriteBuf[ (USB3_EP1_BULK_BURST_SIZE+4) ];
#define TEST_NUM_HS 10 // Number of test for USB2 High Speed(480Mbit/s)
#define TEST_DATA_LEN 0x800000 // 8 MiB
//#define TEST_DATA_LEN 0x1000000 // 16 MiB fail on some PC with libusb (Can return libusb_bulk_transfer return error=-11 => NO MEM)

#define USB_SWITCH_DELAY_MILLISEC (10)
#define USB_REBOOT_DELAY_MILLISEC (500)
#define USB_ENUM_DELAY_MILLISEC (800)

static __attribute__ ((aligned (128))) uint32_t mReadBigBuf[ ((TEST_DATA_LEN+16)/sizeof(uint32_t)) ];
static __attribute__ ((aligned (128))) uint32_t mWriteBigBuf[ ((TEST_DATA_LEN+16)/sizeof(uint32_t)) ];

Expand All @@ -42,12 +47,14 @@ void cleanup(void)
if(pFile != NULL)
{
fclose(pFile);
pFile = NULL;
}
if(handle != NULL)
{
libusb_release_interface(handle, 0);
libusb_close(handle);
usb_closedev(handle);
handle = NULL;
}
usb_exit();
}

void error_exit(char* error_str)
Expand All @@ -56,10 +63,9 @@ void error_exit(char* error_str)
{
log_printf("%sTests end with failure(s)\n", error_str);
}
cleanup();
log_printf("Press Enter to exit\n");
getchar();
libusb_exit(NULL);
cleanup();
exit(-1);
}

Expand Down Expand Up @@ -187,18 +193,22 @@ int main(int argc, char *argv[])
pFile = fopen(filename, "w");
if(pFile == NULL)
{
log_printf("fopen(filename, \"w\") error (filename=\"%s\")\n", filename);
fprintf(stderr, "fopen(filename, \"w\") error (filename=\"%s\")\n", filename);
fflush(stderr);
error_exit(NULL);
}
log_printf_init(pFile);

log_printf("HydraUSB3_USB_benchmark v%s B.VERNOUX 22-Aug-2022\n", VERSION);
log_printf("HydraUSB3_USB_benchmark v%s B.VERNOUX 31-Aug-2022\n", VERSION);

log_printf("Options: verbose=%d\n",
config.verbose);

log_printf("USB3_EP1_BULK_BURST_SIZE=%d USB3_EP2_BULK_BURST_SIZE=%d\n", USB3_EP1_BULK_BURST_SIZE, USB3_EP2_BULK_BURST_SIZE);

if(usb_init() < 0)
{
error_exit("usb_init() error exit\n");
}

handle = usb_opendev(config.verbose);
if(handle == NULL)
Expand Down Expand Up @@ -232,17 +242,16 @@ int main(int argc, char *argv[])

log_printf_dbg("Start USB2 HS Force\n");
{
/* Write command to Get USB STATUS */
memWBuf[0] = USB_CMD_USB2;
memWBuf[0] = USB_CMD_USB2; // Force USB2 HS
usb_write_EP1(handle, data_tx);
}
/* Wait to be sure the command is executed */
sleep_ms(10);
sleep_ms(USB_SWITCH_DELAY_MILLISEC);
/* Release and close the handle as USB enumeration is done again for USB2 HS */
libusb_release_interface(handle, 0);
libusb_close(handle);
usb_closedev(handle);
handle = NULL;
/* Wait USB2 HS enumeration */
sleep_ms(800);
sleep_ms(USB_ENUM_DELAY_MILLISEC);
handle = usb_opendev(config.verbose);
log_printf_dbg("End USB2 HS Force\n");
if(handle == NULL)
Expand Down Expand Up @@ -275,8 +284,7 @@ int main(int argc, char *argv[])

log_printf_dbg("Start Read USB2 Status\n");
{
/* Write command to Get USB STATUS */
memWBuf[0] = USB_CMD_USBS;
memWBuf[0] = USB_CMD_USBS; // Get USB2 Status
usb_write_EP1(handle, data_tx);
/* Read USB Status Report */
if(usb_read_EP1(handle, data_rx) == 1)
Expand All @@ -302,17 +310,16 @@ int main(int argc, char *argv[])

log_printf_dbg("Start USB3 Force\n");
{
/* Write command to Get USB STATUS */
memWBuf[0] = USB_CMD_USB3;
memWBuf[0] = USB_CMD_USB3; // Force USB3
usb_write_EP1(handle, data_tx);
}
/* Wait to be sure the command is executed */
sleep_ms(10);
sleep_ms(USB_SWITCH_DELAY_MILLISEC);
/* Release and close the handle as USB enumeration is done again for USB2 HS */
libusb_release_interface(handle, 0);
libusb_close(handle);
usb_closedev(handle);
handle = NULL;
/* Wait USB2 HS enumeration */
sleep_ms(800);
sleep_ms(USB_ENUM_DELAY_MILLISEC);
handle = usb_opendev(config.verbose);
log_printf_dbg("End USB3 Force\n");
if(handle == NULL)
Expand Down Expand Up @@ -346,7 +353,6 @@ int main(int argc, char *argv[])

log_printf_dbg("Read Log\n");
{
/* Write command to Read Log */
memWBuf[0] = USB_CMD_LOGR; /* Read Log command */
usb_write_EP1(handle, data_tx);
/* Read Log */
Expand All @@ -365,8 +371,7 @@ int main(int argc, char *argv[])

log_printf_dbg("Start Read USB3 Status\n");
{
/* Write command to Get USB STATUS */
memWBuf[0] = USB_CMD_USBS;
memWBuf[0] = USB_CMD_USBS; // Get USB3 Status
usb_write_EP1(handle, data_tx);
/* Read USB Status Report */
if(usb_read_EP1(handle, data_rx) == 1)
Expand All @@ -393,7 +398,6 @@ int main(int argc, char *argv[])

log_printf_dbg("Read Log\n");
{
/* Write command to Read Log */
memWBuf[0] = USB_CMD_LOGR; /* Read Log command */
usb_write_EP1(handle, data_tx);
/* Read Log */
Expand Down Expand Up @@ -425,10 +429,9 @@ int main(int argc, char *argv[])
memWBuf[0] = USB_CMD_BOOT;
usb_write_EP1(handle, data_tx);
/* Wait to be sure the command is executed */
sleep_ms(500);
sleep_ms(USB_REBOOT_DELAY_MILLISEC);

cleanup();
libusb_exit(NULL);

return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion HydraUSB3_USB_benchmark/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 1.0.1
VERSION = 1.0.2

# Install paths
PREFIX = /usr/local
Expand Down
95 changes: 71 additions & 24 deletions libusb_portable/libusb_portable.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : libusb_portable.c
* Author : bvernoux
* Version : V1.0.1
* Date : 2022/08/22
* Version : V1.0.2
* Date : 2022/08/31
* Description :
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -52,6 +52,7 @@ static FILE *pFile = NULL; /* File for logging */
static enum libusb_speed usb_speed;

static struct libusb_device_handle *handle = NULL;
int usb_claim_interface = 0;

void log_printf_init(FILE *log_file_handle)
{
Expand Down Expand Up @@ -127,13 +128,16 @@ void log_printf_dbg(const char* fmt, ...)

va_start(args, fmt);
vfprintf(stdout, fmt, args);
fflush(stdout);
va_end(args);
fflush(stdout);

if(pFile != NULL)
{
va_start(args, fmt);
vfprintf(pFile, fmt, args);
va_end(args);
fflush(pFile);
}
va_end(args);
}

void log_printf(const char* fmt, ...)
Expand All @@ -142,13 +146,17 @@ void log_printf(const char* fmt, ...)

va_start(args, fmt);
vfprintf(stdout, fmt, args);
fflush(stdout);
va_end(args);
fflush(stdout);

if(pFile != NULL)
{
va_start(args, fmt);
vfprintf(pFile, fmt, args);
va_end(args);
fflush(pFile);
}
va_end(args);

}

void sleep_ms(int milliseconds) // Cross-platform sleep in milliseconds function
Expand Down Expand Up @@ -576,18 +584,6 @@ struct libusb_device_handle* usb_opendev(int verbose)
int i = 0;
char found = 0;

const struct libusb_version* version;
version = libusb_get_version();
log_printf("Using libusb v%d.%d.%d.%d\n", version->major, version->minor, version->micro, version->nano);

// Init libusb
r = libusb_init(NULL);
if(r < 0)
{
log_printf("libusb_opendev() error failed to initialise libusb\n");
return NULL;
}

// Get a list of USB devices
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
Expand All @@ -604,7 +600,6 @@ struct libusb_device_handle* usb_opendev(int verbose)
{
log_printf("libusb_opendev() error failed to get device descriptor\n");
libusb_free_device_list(devs, 1);
libusb_close(handle);
break;
}

Expand All @@ -615,7 +610,7 @@ struct libusb_device_handle* usb_opendev(int verbose)
continue;
}

if(desc.idVendor == VID && desc.idProduct == PID) // HydraUSB3
if(desc.idVendor == VID && desc.idProduct == PID) // VID & PID found
{
if (desc.iSerialNumber)
{
Expand Down Expand Up @@ -646,13 +641,23 @@ struct libusb_device_handle* usb_opendev(int verbose)
}
break;
}
else // VID & PID not found
{
if(handle != NULL)
{
usb_closedev(handle);
}
}
}//end of while

if(found == 0)
{
log_printf("libusb_opendev() error device with VID:0x%04X PID:0x%04X not found\n", VID, PID);
libusb_free_device_list(devs, 1);
libusb_close(handle);
if(handle != NULL)
{
usb_closedev(handle);
}
return NULL;
}

Expand All @@ -669,7 +674,10 @@ struct libusb_device_handle* usb_opendev(int verbose)
{
log_printf("libusb_opendev() error could not detach kernel driver!\n");
libusb_free_device_list(devs, 1);
libusb_close(handle);
if(handle != NULL)
{
usb_closedev(handle);
}
return NULL;
}
}
Expand All @@ -679,15 +687,54 @@ struct libusb_device_handle* usb_opendev(int verbose)
{
log_printf("libusb_opendev() error cannot claim interface\n");
libusb_free_device_list(devs, 1);
libusb_close(handle);
if(handle != NULL)
{
usb_closedev(handle);
}
return NULL;
}
else
{
usb_claim_interface = 1;
return handle;
}
}

void usb_closedev(struct libusb_device_handle* libusb_dev_handle)
{
if(libusb_dev_handle != NULL)
{
if(usb_claim_interface == 1)
{
libusb_release_interface(libusb_dev_handle, 0);
usb_claim_interface = 0;
}
libusb_close(libusb_dev_handle);
handle = NULL;
}
}

int usb_init(void)
{
int r;
const struct libusb_version* version;
version = libusb_get_version();
log_printf("Using libusb v%d.%d.%d.%d\n", version->major, version->minor, version->micro, version->nano);

// Init libusb
r = libusb_init(NULL);
if(r < 0)
{
log_printf("libusb_init() error failed to initialise libusb\n");
}
return r;
}

void usb_exit(void)
{
libusb_exit(NULL);
}

enum libusb_speed usb_get_device_speed(libusb_device_handle *handle)
{
return usb_speed;
Expand Down Expand Up @@ -839,7 +886,7 @@ int USB_TestDataSpeed(struct libusb_device_handle *handle, uint32_t *writebuf, u
speed_mbytes_per_sec = (float)(((double)mTotal) / (1000.0 * 1000.0)) / time_diff_s;
log_printf("Average speed %.1f MBytes/Sec, Total=%zu Bytes/%zu MBytes\n", speed_mbytes_per_sec, mTotal, (mTotal/(1000 * 1000)));

log_printf("End USB_TestDataIntegrity(libusb_bulk_transfer) Tests\n");
log_printf("End USB_TestDataSpeed(libusb_bulk_transfer) Tests\n");
log_printf("Test end with success\n");
return 0;
}
Loading

0 comments on commit 00843ef

Please sign in to comment.