2 * Copyright (c) 2008-2009 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/event.h>
31 #include <sys/resource.h>
32 #include <sys/sched.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
54 #define min(x, y) (x < y ? x : y)
56 #define timespecsub(vvp, uvp) \
58 (vvp)->tv_sec -= (uvp)->tv_sec; \
59 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
60 if ((vvp)->tv_nsec < 0) { \
62 (vvp)->tv_nsec += 1000000000; \
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
72 #define CONNECTION_MAGIC 0x87a3f56e
74 uint32_t conn_magic; /* Just magic. */
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.*/
81 static u_char buffer[256 * 1024]; /* Buffer to send. */
82 static pid_t *pid_list;
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;
89 static struct connection *
90 tcpp_client_newconn(void)
92 struct sockaddr_in sin;
93 struct connection *conn;
98 * Spread load over available IPs, roating through them as we go. No
99 * attempt to localize IPs to particular workers.
102 sin.sin_addr.s_addr = htonl(ntohl(localipbase.sin_addr.s_addr) +
103 (counter++ % Mflag));
105 fd = socket(PF_INET, SOCK_STREAM, 0);
109 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
113 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i)) < 0)
114 err(-1, "setsockopt");
116 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i)) < 0)
117 err(-1, "setsockopt");
120 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
121 err(-1, "setsockopt");
125 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
129 if (connect(fd, (struct sockaddr *)&remoteip, sizeof(remoteip)) < 0 &&
130 errno != EINPROGRESS)
133 conn = malloc(sizeof(*conn));
136 bzero(conn, sizeof(*conn));
137 conn->conn_magic = CONNECTION_MAGIC;
139 conn->conn_header.th_magic = TCPP_MAGIC;
140 conn->conn_header.th_len = payload_len;
141 tcpp_header_encode(&conn->conn_header);
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");
152 tcpp_client_closeconn(struct connection *conn)
155 close(conn->conn_fd);
156 bzero(conn, sizeof(*conn));
162 tcpp_client_handleconn(struct kevent *kev)
164 struct connection *conn;
166 ssize_t len, header_left;
169 if (conn->conn_magic != CONNECTION_MAGIC)
170 errx(-1, "tcpp_client_handleconn: magic");
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);
182 tcpp_client_closeconn(conn);
183 err(-1, "tcpp_client_handleconn: header write");
186 tcpp_client_closeconn(conn);
187 errx(-1, "tcpp_client_handleconn: header write "
190 if (len > header_left) {
191 conn->conn_data_sent += (len - header_left);
192 conn->conn_header_sent += header_left;
194 conn->conn_header_sent += len;
196 len = write(conn->conn_fd, buffer, min(sizeof(buffer),
197 payload_len - conn->conn_data_sent));
199 tcpp_client_closeconn(conn);
200 err(-1, "tcpp_client_handleconn: data write");
203 tcpp_client_closeconn(conn);
204 errx(-1, "tcpp_client_handleconn: data write: "
207 conn->conn_data_sent += len;
209 if (conn->conn_data_sent >= payload_len) {
213 tcpp_client_closeconn(conn);
218 tcpp_client_worker(int workernum)
220 struct kevent *kev_array;
221 int i, numevents, kev_bytes;
222 #if defined(CPU_SETSIZE) && 0
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,
235 CPU_SET(workernum % ncpus, &mask);
236 if (sched_setaffinity(0, CPU_SETSIZE, &mask) < 0)
237 err(-1, "sched_setaffinity");
239 setproctitle("tcpp_client %d", workernum);
242 * Add the worker number to the remote port.
244 remoteip.sin_port = htons(rflag + workernum);
246 kev_bytes = sizeof(*kev_array) * mflag;
247 kev_array = malloc(kev_bytes);
248 if (kev_array == NULL)
250 bzero(kev_array, kev_bytes);
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);
262 if (numevents > mflag)
263 errx(-1, "kevent: %d", numevents);
264 for (i = 0; i < numevents; i++)
265 tcpp_client_handleconn(&kev_array[i]);
267 /* printf("Worker %d done - %d finished\n", workernum, finished); */
273 struct timespec ts_start, ts_finish;
274 long cp_time_start[CPUSTATES], cp_time_finish[CPUSTATES];
278 int i, failed, status;
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);
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);
293 size = sizeof(cp_time_start);
294 if (sysctlbyname(SYSCTLNAME_CPTIME, &cp_time_start, &size, NULL, 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++) {
303 for (i = 0; i < pflag; i++) {
304 if (pid_list[i] != 0)
305 (void)kill(pid_list[i], SIGKILL);
310 tcpp_client_worker(i);
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)
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)
332 err(-1, "sysctlbyname: %s", SYSCTLNAME_CPTIME);
333 timespecsub(&ts_finish, &ts_start);
336 errx(-1, "Too many errors");
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));
343 printf("%d procs ", pflag);
345 printf("%f cps%s", (double)(pflag * tflag)/
346 (ts_finish.tv_sec + ts_finish.tv_nsec * 1e-9),
349 printf("%f Gbps%s", (double)(bflag * tflag * pflag * 8) /
350 (ts_finish.tv_sec + ts_finish.tv_nsec * 1e-9) * 1e-9,
355 for (i = 0; i < CPUSTATES; i++) {
356 cp_time_finish[i] -= cp_time_start[i];
357 ticks += cp_time_finish[i];
359 printf("user%% %lu nice%% %lu sys%% %lu intr%% %lu "
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);