]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - tools/tools/netrate/tcpp/tcpp_client.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / tools / tools / netrate / tcpp / tcpp_client.c
1 /*-
2  * Copyright (c) 2008-2009 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/types.h>
30 #include <sys/event.h>
31 #include <sys/resource.h>
32 #include <sys/sched.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
35 #include <sys/time.h>
36 #include <sys/uio.h>
37 #include <sys/wait.h>
38
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <inttypes.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "tcpp.h"
53
54 #define min(x, y)       (x < y ? x : y)
55
56 #define timespecsub(vvp, uvp)                                           \
57         do {                                                            \
58                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
59                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
60                 if ((vvp)->tv_nsec < 0) {                               \
61                         (vvp)->tv_sec--;                                \
62                         (vvp)->tv_nsec += 1000000000;                   \
63                 }                                                       \
64         } while (0)
65
66
67 /*
68  * Gist of each client worker: build up to mflag connections at a time, and
69  * pump data in to them somewhat fairly until tflag connections have been
70  * completed.
71  */
72 #define CONNECTION_MAGIC        0x87a3f56e
73 struct connection {
74         uint32_t        conn_magic;             /* Just magic. */
75         int             conn_fd;
76         struct tcpp_header      conn_header;    /* Header buffer. */
77         u_int           conn_header_sent;       /* Header bytes sent. */
78         u_int64_t       conn_data_sent;         /* Data bytes sent.*/
79 };
80
81 static u_char                    buffer[256 * 1024];    /* Buffer to send. */
82 static pid_t                    *pid_list;
83 static int                       kq;
84 static int                       started;       /* Number started so far. */
85 static int                       finished;      /* Number finished so far. */
86 static int                       counter;       /* IP number offset. */
87 static uint64_t                  payload_len;
88
89 static struct connection *
90 tcpp_client_newconn(void)
91 {
92         struct sockaddr_in sin;
93         struct connection *conn;
94         struct kevent kev;
95         int fd, i;
96
97         /*
98          * Spread load over available IPs, roating through them as we go.  No
99          * attempt to localize IPs to particular workers.
100          */
101         sin = localipbase;
102         sin.sin_addr.s_addr = htonl(ntohl(localipbase.sin_addr.s_addr) +
103             (counter++ % Mflag));
104
105         fd = socket(PF_INET, SOCK_STREAM, 0);
106         if (fd < 0)
107                 err(-1, "socket");
108
109         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
110                 err(-1, "fcntl");
111
112         i = 1;
113         if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i)) < 0)
114                 err(-1, "setsockopt");
115         i = 1;
116         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i)) < 0)
117                 err(-1, "setsockopt");
118 #if 0
119         i = 1;
120         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
121                 err(-1, "setsockopt");
122 #endif
123
124         if (lflag) {
125                 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
126                         err(-1, "bind");
127         }
128
129         if (connect(fd, (struct sockaddr *)&remoteip, sizeof(remoteip)) < 0 &&
130             errno != EINPROGRESS)
131                 err(-1, "connect");
132
133         conn = malloc(sizeof(*conn));
134         if (conn == NULL)
135                 return (NULL);
136         bzero(conn, sizeof(*conn));
137         conn->conn_magic = CONNECTION_MAGIC;
138         conn->conn_fd = fd;
139         conn->conn_header.th_magic = TCPP_MAGIC;
140         conn->conn_header.th_len = payload_len;
141         tcpp_header_encode(&conn->conn_header);
142
143         EV_SET(&kev, fd, EVFILT_WRITE, EV_ADD, 0, 0, conn);
144         if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0)
145                 err(-1, "newconn kevent");
146
147         started++;
148         return (conn);
149 }
150
151 static void
152 tcpp_client_closeconn(struct connection *conn)
153 {
154
155         close(conn->conn_fd);
156         bzero(conn, sizeof(*conn));
157         free(conn);
158         finished++;
159 }
160
161 static void
162 tcpp_client_handleconn(struct kevent *kev)
163 {
164         struct connection *conn;
165         struct iovec iov[2];
166         ssize_t len, header_left;
167
168         conn = kev->udata;
169         if (conn->conn_magic != CONNECTION_MAGIC)
170                 errx(-1, "tcpp_client_handleconn: magic");
171
172         if (conn->conn_header_sent < sizeof(conn->conn_header)) {
173                 header_left = sizeof(conn->conn_header) -
174                     conn->conn_header_sent;
175                 iov[0].iov_base = ((u_char *)&conn->conn_header) +
176                     conn->conn_header_sent;
177                 iov[0].iov_len = header_left;
178                 iov[1].iov_base = buffer;
179                 iov[1].iov_len = min(sizeof(buffer), payload_len);
180                 len = writev(conn->conn_fd, iov, 2);
181                 if (len < 0) {
182                         tcpp_client_closeconn(conn);
183                         err(-1, "tcpp_client_handleconn: header write");
184                 }
185                 if (len == 0) {
186                         tcpp_client_closeconn(conn);
187                         errx(-1, "tcpp_client_handleconn: header write "
188                             "premature EOF");
189                 }
190                 if (len > header_left) {
191                         conn->conn_data_sent += (len - header_left);
192                         conn->conn_header_sent += header_left;
193                 } else
194                         conn->conn_header_sent += len;
195         } else {
196                 len = write(conn->conn_fd, buffer, min(sizeof(buffer),
197                     payload_len - conn->conn_data_sent));
198                 if (len < 0) {
199                         tcpp_client_closeconn(conn);
200                         err(-1, "tcpp_client_handleconn: data write");
201                 }
202                 if (len == 0) {
203                         tcpp_client_closeconn(conn);
204                         errx(-1, "tcpp_client_handleconn: data write: "
205                             "premature EOF");
206                 }
207                 conn->conn_data_sent += len;
208         }
209         if (conn->conn_data_sent >= payload_len) {
210                 /*
211                  * All is well.
212                  */
213                 tcpp_client_closeconn(conn);
214         }
215 }
216
217 static void
218 tcpp_client_worker(int workernum)
219 {
220         struct kevent *kev_array;
221         int i, numevents, kev_bytes;
222 #if defined(CPU_SETSIZE) && 0
223         cpu_set_t mask;
224         int ncpus;
225         size_t len;
226
227         len = sizeof(ncpus);
228         if (sysctlbyname(SYSCTLNAME_CPUS, &ncpus, &len, NULL, 0) < 0)
229                 err(-1, "sysctlbyname: %s", SYSCTLNAME_CPUS);
230         if (len != sizeof(ncpus))
231                 errx(-1, "sysctlbyname: %s: len %jd", SYSCTLNAME_CPUS,
232                     (intmax_t)len);
233
234         CPU_ZERO(&mask);
235         CPU_SET(workernum % ncpus, &mask);
236         if (sched_setaffinity(0, CPU_SETSIZE, &mask) < 0)
237                 err(-1, "sched_setaffinity");
238 #endif
239         setproctitle("tcpp_client %d", workernum);
240
241         /*
242          * Add the worker number to the remote port.
243          */
244         remoteip.sin_port = htons(rflag + workernum);
245
246         kev_bytes = sizeof(*kev_array) * mflag;
247         kev_array = malloc(kev_bytes);
248         if (kev_array == NULL)
249                 err(-1, "malloc");
250         bzero(kev_array, kev_bytes);
251
252         kq = kqueue();
253         if (kq < 0)
254                 err(-1, "kqueue");
255
256         while (finished < tflag) {
257                 while ((started - finished < mflag) && (started < tflag))
258                         (void)tcpp_client_newconn();
259                 numevents = kevent(kq, NULL, 0, kev_array, mflag, NULL);
260                 if (numevents < 0)
261                         err(-1, "kevent");
262                 if (numevents > mflag)
263                         errx(-1, "kevent: %d", numevents);
264                 for (i = 0; i < numevents; i++)
265                         tcpp_client_handleconn(&kev_array[i]);
266         }
267         /* printf("Worker %d done - %d finished\n", workernum, finished); */
268 }
269
270 void
271 tcpp_client(void)
272 {
273         struct timespec ts_start, ts_finish;
274         long cp_time_start[CPUSTATES], cp_time_finish[CPUSTATES];
275         long ticks;
276         size_t size;
277         pid_t pid;
278         int i, failed, status;
279
280         if (bflag < sizeof(struct tcpp_header))
281                 errx(-1, "Can't use -b less than %zu\n",
282                    sizeof(struct tcpp_header));
283         payload_len = bflag - sizeof(struct tcpp_header);
284
285         pid_list = malloc(sizeof(*pid_list) * pflag);
286         if (pid_list == NULL)
287                 err(-1, "malloc pid_list");
288         bzero(pid_list, sizeof(*pid_list) * pflag);
289
290         /*
291          * Start workers.
292          */
293         size = sizeof(cp_time_start);
294         if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_start, &size, NULL, 0)
295             < 0)
296                 err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
297         if (clock_gettime(CLOCK_REALTIME, &ts_start) < 0)
298                 err(-1, "clock_gettime");
299         for (i = 0; i < pflag; i++) {
300                 pid = fork();
301                 if (pid < 0) {
302                         warn("fork");
303                         for (i = 0; i < pflag; i++) {
304                                 if (pid_list[i] != 0)
305                                         (void)kill(pid_list[i], SIGKILL);
306                         }
307                         exit(-1);
308                 }
309                 if (pid == 0) {
310                         tcpp_client_worker(i);
311                         exit(0);
312                 }
313                 pid_list[i] = pid;
314         }
315
316         /*
317          * GC workers.
318          */
319         failed = 0;
320         for (i = 0; i < pflag; i++) {
321                 if (pid_list[i] != 0) {
322                         while (waitpid(pid_list[i], &status, 0) != pid_list[i]);
323                         if (WEXITSTATUS(status) != 0)
324                                 failed = 1;
325                 }
326         }
327         if (clock_gettime(CLOCK_REALTIME, &ts_finish) < 0)
328                 err(-1, "clock_gettime");
329         size = sizeof(cp_time_finish);
330         if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_finish, &size, NULL, 0)
331             < 0)
332                 err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
333         timespecsub(&ts_finish, &ts_start);
334
335         if (failed)
336                 errx(-1, "Too many errors");
337
338         printf("%jd bytes transferred in %jd.%09jd seconds\n", 
339             (bflag * tflag * pflag), (intmax_t)ts_finish.tv_sec,
340             (intmax_t)(ts_finish.tv_nsec));
341
342         if (Tflag)
343                 printf("%d procs ", pflag);
344         if (Cflag) {
345                 printf("%f cps%s", (double)(pflag * tflag)/
346                     (ts_finish.tv_sec + ts_finish.tv_nsec * 1e-9),
347                     Tflag ? " " : "\n");
348         } else {
349                 printf("%f Gbps%s", (double)(bflag * tflag * pflag * 8) /
350                     (ts_finish.tv_sec + ts_finish.tv_nsec * 1e-9) * 1e-9,
351                     Tflag ? " " : "\n");
352         }
353         if (Tflag) {
354                 ticks = 0;
355                 for (i = 0; i < CPUSTATES; i++) {
356                         cp_time_finish[i] -= cp_time_start[i];
357                         ticks += cp_time_finish[i];
358                 }
359                 printf("user%% %lu nice%% %lu sys%% %lu intr%% %lu "
360                     "idle%% %lu\n",
361                     (100 * cp_time_finish[CP_USER]) / ticks,
362                     (100 * cp_time_finish[CP_NICE]) / ticks,
363                     (100 * cp_time_finish[CP_SYS]) / ticks,
364                     (100 * cp_time_finish[CP_INTR]) / ticks,
365                     (100 * cp_time_finish[CP_IDLE]) / ticks);
366         }
367 }