sam3u_benchmark.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * libusb example program to measure Atmel SAM3U isochronous performance
  3. * Copyright (C) 2012 Harald Welte <laforge@gnumonks.org>
  4. *
  5. * Copied with the author's permission under LGPL-2.1 from
  6. * http://git.gnumonks.org/cgi-bin/gitweb.cgi?p=sam3u-tests.git;a=blob;f=usb-benchmark-project/host/benchmark.c;h=74959f7ee88f1597286cd435f312a8ff52c56b7e
  7. *
  8. * An Atmel SAM3U test firmware is also available in the above repository.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <config.h>
  25. #include <errno.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #ifdef HAVE_SYS_TIME_H
  30. #include <sys/time.h>
  31. #endif
  32. #include <time.h>
  33. #include "libusb.h"
  34. #define EP_DATA_IN 0x82
  35. #define EP_ISO_IN 0x86
  36. static volatile sig_atomic_t do_exit = 0;
  37. static struct libusb_device_handle *devh = NULL;
  38. static unsigned long num_bytes = 0, num_xfer = 0;
  39. static struct timeval tv_start;
  40. static void get_timestamp(struct timeval *tv)
  41. {
  42. #if defined(PLATFORM_WINDOWS)
  43. static LARGE_INTEGER frequency;
  44. LARGE_INTEGER counter;
  45. if (!frequency.QuadPart)
  46. QueryPerformanceFrequency(&frequency);
  47. QueryPerformanceCounter(&counter);
  48. counter.QuadPart *= 1000000;
  49. counter.QuadPart /= frequency.QuadPart;
  50. tv->tv_sec = (long)(counter.QuadPart / 1000000ULL);
  51. tv->tv_usec = (long)(counter.QuadPart % 1000000ULL);
  52. #elif defined(HAVE_CLOCK_GETTIME)
  53. struct timespec ts;
  54. (void)clock_gettime(CLOCK_MONOTONIC, &ts);
  55. tv->tv_sec = ts.tv_sec;
  56. tv->tv_usec = (int)(ts.tv_nsec / 1000L);
  57. #else
  58. gettimeofday(tv, NULL);
  59. #endif
  60. }
  61. static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr)
  62. {
  63. int i;
  64. if (xfr->status != LIBUSB_TRANSFER_COMPLETED) {
  65. fprintf(stderr, "transfer status %d\n", xfr->status);
  66. libusb_free_transfer(xfr);
  67. exit(3);
  68. }
  69. if (xfr->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
  70. for (i = 0; i < xfr->num_iso_packets; i++) {
  71. struct libusb_iso_packet_descriptor *pack = &xfr->iso_packet_desc[i];
  72. if (pack->status != LIBUSB_TRANSFER_COMPLETED) {
  73. fprintf(stderr, "Error: pack %d status %d\n", i, pack->status);
  74. exit(5);
  75. }
  76. printf("pack%d length:%u, actual_length:%u\n", i, pack->length, pack->actual_length);
  77. }
  78. }
  79. printf("length:%u, actual_length:%u\n", xfr->length, xfr->actual_length);
  80. for (i = 0; i < xfr->actual_length; i++) {
  81. printf("%02x", xfr->buffer[i]);
  82. if (i % 16)
  83. printf("\n");
  84. else if (i % 8)
  85. printf(" ");
  86. else
  87. printf(" ");
  88. }
  89. num_bytes += xfr->actual_length;
  90. num_xfer++;
  91. if (libusb_submit_transfer(xfr) < 0) {
  92. fprintf(stderr, "error re-submitting URB\n");
  93. exit(1);
  94. }
  95. }
  96. static int benchmark_in(uint8_t ep)
  97. {
  98. static uint8_t buf[2048];
  99. static struct libusb_transfer *xfr;
  100. int num_iso_pack = 0;
  101. if (ep == EP_ISO_IN)
  102. num_iso_pack = 16;
  103. xfr = libusb_alloc_transfer(num_iso_pack);
  104. if (!xfr) {
  105. errno = ENOMEM;
  106. return -1;
  107. }
  108. if (ep == EP_ISO_IN) {
  109. libusb_fill_iso_transfer(xfr, devh, ep, buf,
  110. sizeof(buf), num_iso_pack, cb_xfr, NULL, 0);
  111. libusb_set_iso_packet_lengths(xfr, sizeof(buf)/num_iso_pack);
  112. } else
  113. libusb_fill_bulk_transfer(xfr, devh, ep, buf,
  114. sizeof(buf), cb_xfr, NULL, 0);
  115. get_timestamp(&tv_start);
  116. /* NOTE: To reach maximum possible performance the program must
  117. * submit *multiple* transfers here, not just one.
  118. *
  119. * When only one transfer is submitted there is a gap in the bus
  120. * schedule from when the transfer completes until a new transfer
  121. * is submitted by the callback. This causes some jitter for
  122. * isochronous transfers and loss of throughput for bulk transfers.
  123. *
  124. * This is avoided by queueing multiple transfers in advance, so
  125. * that the host controller is always kept busy, and will schedule
  126. * more transfers on the bus while the callback is running for
  127. * transfers which have completed on the bus.
  128. */
  129. return libusb_submit_transfer(xfr);
  130. }
  131. static void measure(void)
  132. {
  133. struct timeval tv_stop;
  134. unsigned long diff_msec;
  135. get_timestamp(&tv_stop);
  136. diff_msec = (tv_stop.tv_sec - tv_start.tv_sec) * 1000L;
  137. diff_msec += (tv_stop.tv_usec - tv_start.tv_usec) / 1000L;
  138. printf("%lu transfers (total %lu bytes) in %lu milliseconds => %lu bytes/sec\n",
  139. num_xfer, num_bytes, diff_msec, (num_bytes * 1000L) / diff_msec);
  140. }
  141. static void sig_hdlr(int signum)
  142. {
  143. (void)signum;
  144. measure();
  145. do_exit = 1;
  146. }
  147. int main(void)
  148. {
  149. int rc;
  150. #if defined(PLATFORM_POSIX)
  151. struct sigaction sigact;
  152. sigact.sa_handler = sig_hdlr;
  153. sigemptyset(&sigact.sa_mask);
  154. sigact.sa_flags = 0;
  155. (void)sigaction(SIGINT, &sigact, NULL);
  156. #else
  157. (void)signal(SIGINT, sig_hdlr);
  158. #endif
  159. rc = libusb_init(NULL);
  160. if (rc < 0) {
  161. fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
  162. exit(1);
  163. }
  164. devh = libusb_open_device_with_vid_pid(NULL, 0x16c0, 0x0763);
  165. if (!devh) {
  166. fprintf(stderr, "Error finding USB device\n");
  167. goto out;
  168. }
  169. rc = libusb_claim_interface(devh, 2);
  170. if (rc < 0) {
  171. fprintf(stderr, "Error claiming interface: %s\n", libusb_error_name(rc));
  172. goto out;
  173. }
  174. benchmark_in(EP_ISO_IN);
  175. while (!do_exit) {
  176. rc = libusb_handle_events(NULL);
  177. if (rc != LIBUSB_SUCCESS)
  178. break;
  179. }
  180. /* Measurement has already been done by the signal handler. */
  181. libusb_release_interface(devh, 2);
  182. out:
  183. if (devh)
  184. libusb_close(devh);
  185. libusb_exit(NULL);
  186. return rc;
  187. }