xref: /aosp_15_r20/external/libusb/examples/sam3u_benchmark.c (revision 86b64dcb59b3a0b37502ecd56e119234366a6f7e)
1 /*
2  * libusb example program to measure Atmel SAM3U isochronous performance
3  * Copyright (C) 2012 Harald Welte <[email protected]>
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 
25 #include <config.h>
26 
27 #include <errno.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #include <time.h>
35 
36 #include "libusb.h"
37 
38 #define EP_DATA_IN	0x82
39 #define EP_ISO_IN	0x86
40 
41 static volatile sig_atomic_t do_exit = 0;
42 static struct libusb_device_handle *devh = NULL;
43 
44 static unsigned long num_bytes = 0, num_xfer = 0;
45 static struct timeval tv_start;
46 
get_timestamp(struct timeval * tv)47 static void get_timestamp(struct timeval *tv)
48 {
49 #if defined(PLATFORM_WINDOWS)
50 	static LARGE_INTEGER frequency;
51 	LARGE_INTEGER counter;
52 
53 	if (!frequency.QuadPart)
54 		QueryPerformanceFrequency(&frequency);
55 
56 	QueryPerformanceCounter(&counter);
57 	counter.QuadPart *= 1000000;
58 	counter.QuadPart /= frequency.QuadPart;
59 
60 	tv->tv_sec = (long)(counter.QuadPart / 1000000ULL);
61 	tv->tv_usec = (long)(counter.QuadPart % 1000000ULL);
62 #elif defined(HAVE_CLOCK_GETTIME)
63 	struct timespec ts;
64 
65 	(void)clock_gettime(CLOCK_MONOTONIC, &ts);
66 	tv->tv_sec = ts.tv_sec;
67 	tv->tv_usec = (int)(ts.tv_nsec / 1000L);
68 #else
69 	gettimeofday(tv, NULL);
70 #endif
71 }
72 
cb_xfr(struct libusb_transfer * xfr)73 static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr)
74 {
75 	int i;
76 
77 	if (xfr->status != LIBUSB_TRANSFER_COMPLETED) {
78 		fprintf(stderr, "transfer status %d\n", xfr->status);
79 		libusb_free_transfer(xfr);
80 		exit(3);
81 	}
82 
83 	if (xfr->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
84 		for (i = 0; i < xfr->num_iso_packets; i++) {
85 			struct libusb_iso_packet_descriptor *pack = &xfr->iso_packet_desc[i];
86 
87 			if (pack->status != LIBUSB_TRANSFER_COMPLETED) {
88 				fprintf(stderr, "Error: pack %d status %d\n", i, pack->status);
89 				exit(5);
90 			}
91 
92 			printf("pack%d length:%u, actual_length:%u\n", i, pack->length, pack->actual_length);
93 		}
94 	}
95 
96 	printf("length:%u, actual_length:%u\n", xfr->length, xfr->actual_length);
97 	for (i = 0; i < xfr->actual_length; i++) {
98 		printf("%02x", xfr->buffer[i]);
99 		if (i % 16)
100 			printf("\n");
101 		else if (i % 8)
102 			printf("  ");
103 		else
104 			printf(" ");
105 	}
106 	num_bytes += xfr->actual_length;
107 	num_xfer++;
108 
109 	if (libusb_submit_transfer(xfr) < 0) {
110 		fprintf(stderr, "error re-submitting URB\n");
111 		exit(1);
112 	}
113 }
114 
benchmark_in(uint8_t ep)115 static int benchmark_in(uint8_t ep)
116 {
117 	static uint8_t buf[2048];
118 	static struct libusb_transfer *xfr;
119 	int num_iso_pack = 0;
120 
121 	if (ep == EP_ISO_IN)
122 		num_iso_pack = 16;
123 
124 	xfr = libusb_alloc_transfer(num_iso_pack);
125 	if (!xfr) {
126 		errno = ENOMEM;
127 		return -1;
128 	}
129 
130 	if (ep == EP_ISO_IN) {
131 		libusb_fill_iso_transfer(xfr, devh, ep, buf,
132 				sizeof(buf), num_iso_pack, cb_xfr, NULL, 0);
133 		libusb_set_iso_packet_lengths(xfr, sizeof(buf)/num_iso_pack);
134 	} else
135 		libusb_fill_bulk_transfer(xfr, devh, ep, buf,
136 				sizeof(buf), cb_xfr, NULL, 0);
137 
138 	get_timestamp(&tv_start);
139 
140 	/* NOTE: To reach maximum possible performance the program must
141 	 * submit *multiple* transfers here, not just one.
142 	 *
143 	 * When only one transfer is submitted there is a gap in the bus
144 	 * schedule from when the transfer completes until a new transfer
145 	 * is submitted by the callback. This causes some jitter for
146 	 * isochronous transfers and loss of throughput for bulk transfers.
147 	 *
148 	 * This is avoided by queueing multiple transfers in advance, so
149 	 * that the host controller is always kept busy, and will schedule
150 	 * more transfers on the bus while the callback is running for
151 	 * transfers which have completed on the bus.
152 	 */
153 
154 	return libusb_submit_transfer(xfr);
155 }
156 
measure(void)157 static void measure(void)
158 {
159 	struct timeval tv_stop;
160 	unsigned long diff_msec;
161 
162 	get_timestamp(&tv_stop);
163 
164 	diff_msec = (tv_stop.tv_sec - tv_start.tv_sec) * 1000L;
165 	diff_msec += (tv_stop.tv_usec - tv_start.tv_usec) / 1000L;
166 
167 	printf("%lu transfers (total %lu bytes) in %lu milliseconds => %lu bytes/sec\n",
168 		num_xfer, num_bytes, diff_msec, (num_bytes * 1000L) / diff_msec);
169 }
170 
sig_hdlr(int signum)171 static void sig_hdlr(int signum)
172 {
173 	(void)signum;
174 
175 	measure();
176 	do_exit = 1;
177 }
178 
main(void)179 int main(void)
180 {
181 	int rc;
182 
183 #if defined(PLATFORM_POSIX)
184 	struct sigaction sigact;
185 
186 	sigact.sa_handler = sig_hdlr;
187 	sigemptyset(&sigact.sa_mask);
188 	sigact.sa_flags = 0;
189 	(void)sigaction(SIGINT, &sigact, NULL);
190 #else
191 	(void)signal(SIGINT, sig_hdlr);
192 #endif
193 
194 	rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
195 	if (rc < 0) {
196 		fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
197 		exit(1);
198 	}
199 
200 	devh = libusb_open_device_with_vid_pid(NULL, 0x16c0, 0x0763);
201 	if (!devh) {
202 		fprintf(stderr, "Error finding USB device\n");
203 		goto out;
204 	}
205 
206 	rc = libusb_claim_interface(devh, 2);
207 	if (rc < 0) {
208 		fprintf(stderr, "Error claiming interface: %s\n", libusb_error_name(rc));
209 		goto out;
210 	}
211 
212 	benchmark_in(EP_ISO_IN);
213 
214 	while (!do_exit) {
215 		rc = libusb_handle_events(NULL);
216 		if (rc != LIBUSB_SUCCESS)
217 			break;
218 	}
219 
220 	/* Measurement has already been done by the signal handler. */
221 
222 	libusb_release_interface(devh, 2);
223 out:
224 	if (devh)
225 		libusb_close(devh);
226 	libusb_exit(NULL);
227 	return rc;
228 }
229