2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netdb.h> /* getaddrinfo */
37 #include <arpa/inet.h>
42 #include <unistd.h> /* close */
48 #include <time.h> /* clock_getres() */
50 static int round_to(int n, int l)
52 return ((n + l - 1)/l)*l;
56 * Each socket uses multiple threads so the receiver is
57 * more efficient. A collector thread runs the stats.
61 uint64_t count; /* rx counter */
62 uint64_t byte_count; /* rx byte counter */
72 fprintf(stderr, "netreceive port [nthreads]\n");
77 timespec_add(struct timespec *tsa, struct timespec *tsb)
80 tsa->tv_sec += tsb->tv_sec;
81 tsa->tv_nsec += tsb->tv_nsec;
82 if (tsa->tv_nsec >= 1000000000) {
84 tsa->tv_nsec -= 1000000000;
89 timespec_sub(struct timespec *tsa, struct timespec *tsb)
92 tsa->tv_sec -= tsb->tv_sec;
93 tsa->tv_nsec -= tsb->tv_nsec;
94 if (tsa->tv_nsec < 0) {
96 tsa->tv_nsec += 1000000000;
103 struct td_desc *t = data;
111 if (poll(&fds, 1, -1) < 0)
112 perror("poll on thread");
113 if (!(fds.revents & POLLIN))
116 y = recv(t->fd, t->buf, t->buflen, MSG_DONTWAIT);
126 static struct td_desc **
127 make_threads(int *s, int nsock, int nthreads)
129 int i, si, nt = nsock * nthreads;
130 int lb = round_to(nt * sizeof (struct td_desc *), 64);
131 int td_len = round_to(sizeof(struct td_desc), 64); // cache align
132 char *m = calloc(1, lb + td_len * nt);
135 printf("td len %d -> %d\n", (int)sizeof(struct td_desc) , td_len);
136 /* pointers plus the structs */
138 perror("no room for pointers!");
141 tp = (struct td_desc **)m;
142 m += lb; /* skip the pointers */
143 for (si = i = 0; i < nt; i++, m += td_len) {
144 tp[i] = (struct td_desc *)m;
146 tp[i]->buflen = 65536;
147 tp[i]->buf = calloc(1, tp[i]->buflen);
150 if (pthread_create(&tp[i]->td_id, NULL, rx_body, tp[i])) {
151 perror("unable to create thread");
159 main_thread(struct td_desc **tp, int nsock, int nthreads)
161 uint64_t c0, c1, bc0, bc1;
162 struct timespec now, then, delta;
163 /* now the parent collects and prints results */
164 c0 = c1 = bc0 = bc1 = 0;
165 clock_gettime(CLOCK_REALTIME, &then);
166 fprintf(stderr, "start at %ld.%09ld\n", then.tv_sec, then.tv_nsec);
168 int i, nt = nsock * nthreads;
172 if (poll(NULL, 0, 500) < 0)
175 for (i = 0; i < nt; i++) {
177 bc0 += tp[i]->byte_count;
180 clock_gettime(CLOCK_REALTIME, &now);
182 timespec_sub(&delta, &then);
185 pps = (pps * 1000000000) / (delta.tv_sec*1000000000 + delta.tv_nsec + 1);
186 bps = ((bc0 - bc1) * 8000000000) / (delta.tv_sec*1000000000 + delta.tv_nsec + 1);
187 fprintf(stderr, " %9ld pps %8.3f Mbps", (long)pps, .000001*bps);
188 fprintf(stderr, " - %d pkts in %ld.%09ld ns\n",
189 (int)dn, delta.tv_sec, delta.tv_nsec);
196 main(int argc, char *argv[])
198 struct addrinfo hints, *res, *res0;
199 char *dummy, *packet;
201 int error, v, nthreads = 1;
203 const char *cause = NULL;
210 memset(&hints, 0, sizeof(hints));
211 hints.ai_family = PF_UNSPEC;
212 hints.ai_socktype = SOCK_DGRAM;
213 hints.ai_flags = AI_PASSIVE;
215 port = strtoul(argv[1], &dummy, 10);
216 if (port < 1 || port > 65535 || *dummy != '\0')
219 nthreads = strtoul(argv[2], &dummy, 10);
220 if (nthreads < 1 || nthreads > 64)
223 packet = malloc(65536);
224 if (packet == NULL) {
228 bzero(packet, 65536);
230 error = getaddrinfo(NULL, argv[1], &hints, &res0);
232 perror(gai_strerror(error));
238 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
239 s[nsock] = socket(res->ai_family, res->ai_socktype,
247 if (setsockopt(s[nsock], SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) < 0) {
252 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
257 (void) listen(s[nsock], 5);
266 printf("netreceive %d sockets x %d threads listening on UDP port %d\n",
267 nsock, nthreads, (u_short)port);
269 tp = make_threads(s, nsock, nthreads);
270 main_thread(tp, nsock, nthreads);