summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2011-12-03 00:48:51 +0100
committerHarald Welte <laforge@gnumonks.org>2011-12-03 00:48:51 +0100
commit2dc6ac66432eb5c865ad472e2e9817c6872465cb (patch)
treef8be4b74f64c2cca7511d4187d890145286e5c5f
parentc1ad1298a5f2dd0ebe2d398248b89feb111a02f5 (diff)
add host utility for usb benchmarking
-rw-r--r--usb-benchmark-project/host/Makefile3
-rw-r--r--usb-benchmark-project/host/benchmark.c107
2 files changed, 110 insertions, 0 deletions
diff --git a/usb-benchmark-project/host/Makefile b/usb-benchmark-project/host/Makefile
new file mode 100644
index 0000000..76f9bfd
--- /dev/null
+++ b/usb-benchmark-project/host/Makefile
@@ -0,0 +1,3 @@
+
+benchmark: benchmark.o
+ $(CC) $(LDLAGS) -lusb-1.0 -o $@ $^
diff --git a/usb-benchmark-project/host/benchmark.c b/usb-benchmark-project/host/benchmark.c
new file mode 100644
index 0000000..6a1060a
--- /dev/null
+++ b/usb-benchmark-project/host/benchmark.c
@@ -0,0 +1,107 @@
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdio.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <libusb-1.0/libusb.h>
+
+#define EP_DATA_IN 0x82
+
+static struct libusb_device_handle *devh = NULL;
+
+static unsigned int num_bytes = 0, num_xfer = 0;
+static struct timeval tv_start;
+
+static void cb_xfr(struct libusb_transfer *xfr)
+{
+ if (xfr->status != LIBUSB_TRANSFER_COMPLETED) {
+ fprintf(stderr, "transfer status %d\n", xfr->status);
+ libusb_free_transfer(xfr);
+ exit(3);
+ }
+
+ //printf("length:%u, actual_length:%u\n", xfr->length, xfr->actual_length);
+ num_bytes += xfr->length;
+ num_xfer++;
+
+ if (libusb_submit_transfer(xfr) < 0) {
+ fprintf(stderr, "error re-submitting URB\n");
+ exit(1);
+ }
+
+}
+
+static int benchmark_in(void)
+{
+ static uint8_t buf[2048];
+ static struct libusb_transfer *xfr;
+
+ xfr = libusb_alloc_transfer(0);
+ if (!xfr)
+ return -ENOMEM;
+
+ libusb_fill_bulk_transfer(xfr, devh, EP_DATA_IN, buf,
+ sizeof(buf), cb_xfr, NULL, 0);
+
+ gettimeofday(&tv_start, NULL);
+ return libusb_submit_transfer(xfr);
+}
+
+static void measure(void)
+{
+ struct timeval tv_stop;
+ unsigned int diff_msec;
+
+ gettimeofday(&tv_stop, NULL);
+
+ diff_msec = (tv_stop.tv_sec - tv_start.tv_sec)*1000;
+ diff_msec += (tv_stop.tv_usec - tv_start.tv_usec)/1000;
+
+ printf("%u transfers (total %u bytes) in %u miliseconds => %u bytes/sec\n",
+ num_xfer, num_bytes, diff_msec, (num_bytes*1000)/diff_msec);
+}
+
+static void sig_hdlr(int signum)
+{
+ switch (signum) {
+ case SIGINT:
+ measure();
+ exit(0);
+ break;
+ }
+}
+
+int main(int argc, char **argv)
+{
+ int rc;
+
+ signal(SIGINT, &sig_hdlr);
+
+ rc = libusb_init(NULL);
+ if (rc < 0) {
+ fprintf(stderr, "Error initializing libusb\n");
+ exit(1);
+ }
+
+ devh = libusb_open_device_with_vid_pid(NULL, 0x16c0, 0x0763);
+ if (!devh) {
+ fprintf(stderr, "Error finding USB device\n");
+ exit(1);
+ }
+
+ rc = libusb_claim_interface(devh, 0);
+ if (rc < 0) {
+ fprintf(stderr, "Error claiming interface\n");
+ exit(1);
+ }
+
+ benchmark_in();
+
+ while (1) {
+ libusb_handle_events(NULL);
+ }
+
+}
personal git repositories of Harald Welte. Your mileage may vary