11 #include <sys/types.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
23 static int nr_clients = 8;
24 static int net_port = 8888;
25 static int client_loops = 10;
27 static int write_to_null;
29 static int splice_size = SPLICE_SIZE;
30 static char *filename = "splice-file";
31 static int max_client_run = 15;
32 static int run_rw = 1;
33 static int run_splice = 1;
34 static int run_mmap = 1;
38 static int usage(char *name)
40 fprintf(stderr, "Usage %s [options] [filename]:\n", name);
41 fprintf(stderr, "\t[-n] (number of clients]\n");
42 fprintf(stderr, "\t[-p] (port number)\n");
43 fprintf(stderr, "\t[-l] (number of loops)\n");
44 fprintf(stderr, "\t[-z] (write to /dev/null)\n");
45 fprintf(stderr, "\t[-s] (use 1 file for all)\n");
46 fprintf(stderr, "\t[-a] (set CPU affinity)\n");
47 fprintf(stderr, "\t[-b] (splice chunk size)\n");
48 fprintf(stderr, "\t[-t] (max client runtime in seconds)\n");
49 fprintf(stderr, "\t[-c] (clients to run (rw/mmap/splice)\n");
53 static int parse_options(int argc, char *argv[])
57 while ((c = getopt(argc, argv, "n:p:l:azsb:t:c:")) != -1) {
60 nr_clients = atoi(optarg);
64 net_port = atoi(optarg);
68 client_loops = atoi(optarg);
84 splice_size = atoi(optarg);
88 max_client_run = atoi(optarg);
92 if (!strstr(optarg, "rw"))
94 if (!strstr(optarg, "splice"))
96 if (!strstr(optarg, "mmap"))
108 static int bind_to_cpu(int index)
114 if (!bind_cpu || nr_cpus == 1)
117 cpu = index % nr_cpus;
120 CPU_SET((cpu), &cpu_mask);
123 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1)
124 return error("set affinity");
129 static int accept_loop(int listen_sk)
131 struct sockaddr addr;
132 unsigned int len = sizeof(addr);
136 sk = accept(listen_sk, &addr, &len);
138 return error("accept");
142 int ret = recv(sk, NULL, 128*1024*1024, MSG_TRUNC);
147 if (errno == EAGAIN || errno == EINTR)
156 static int server(int offset)
158 struct sockaddr_in saddr_in;
159 struct sockaddr addr;
166 sk = socket(PF_INET, SOCK_STREAM, 0);
168 return error("socket");
170 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
171 saddr_in.sin_port = htons(net_port + offset);
173 if (bind(sk, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) < 0)
174 return error("bind");
176 if (listen(sk, 1) < 0)
177 return error("listen");
180 if (getsockname(sk, &addr, &len) < 0)
181 return error("getsockname");
183 return accept_loop(sk);
186 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
190 sec = e->tv_sec - s->tv_sec;
191 usec = e->tv_usec - s->tv_usec;
192 if (sec > 0 && usec < 0) {
197 sec *= (double) 1000;
198 usec /= (double) 1000;
203 static unsigned long mtime_since_now(struct timeval *s)
207 gettimeofday(&t, NULL);
208 return mtime_since(s, &t);
211 static int client_rw(int out_fd, int file_fd, int offset)
213 int loops = client_loops;
214 struct timeval start;
217 unsigned long long size;
220 if (fstat(file_fd, &sb) < 0)
221 return error("fstat");
223 buf = malloc(splice_size);
225 gettimeofday(&start, NULL);
227 if (lseek(file_fd, 0, SEEK_SET) < 0)
228 return error("lseek");
232 int this_len = min(size, (unsigned long long) splice_size);
233 int ret = read(file_fd, buf, this_len);
236 return error("read");
240 int written = write(out_fd, buf, ret);
243 return error("write");
251 if ((mtime_since_now(&start) < max_client_run * 1000) && loops)
255 size = sb.st_size >> 10;
256 size *= (client_loops - loops);
257 msecs = mtime_since_now(&start);
258 fprintf(stdout, "Client%d (rw): %Lu MiB/sec (%LuMiB in %lu msecs)\n", offset, size / (unsigned long long) msecs, size >> 10, msecs);
262 static int client_mmap(int out_fd, int file_fd, int offset)
264 int loops = client_loops;
265 struct timeval start;
267 void *mmap_area, *buf;
268 unsigned long long size;
271 if (fstat(file_fd, &sb) < 0)
272 return error("fstat");
274 mmap_area = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, file_fd, 0);
275 if (mmap_area == MAP_FAILED)
276 return error("mmap");
278 if (madvise(mmap_area, sb.st_size, MADV_WILLNEED) < 0)
279 return error("madvise");
281 gettimeofday(&start, NULL);
286 int this_len = min(size, (unsigned long long) splice_size);
287 int ret = write(out_fd, buf, this_len);
290 return error("write");
298 if ((mtime_since_now(&start) < max_client_run * 1000) && loops)
301 size = sb.st_size >> 10;
302 size *= (client_loops - loops);
303 msecs = mtime_since_now(&start);
304 fprintf(stdout, "Client%d (mmap): %Lu MiB/sec (%LuMiB in %lu msecs)\n", offset, size / (unsigned long long) msecs, size >> 10, msecs);
305 munmap(mmap_area, sb.st_size);
310 static int client_splice_loop(int out_fd, int fd, int *pfd, int offset)
312 struct timeval start;
313 unsigned long long size;
316 int loops = client_loops;
319 if (fstat(fd, &sb) < 0)
320 return error("fstat");
322 gettimeofday(&start, NULL);
328 int ret = splice(fd, &off, pfd[1], NULL, min(size, (unsigned long long) splice_size), 0);
331 return error("splice-in");
335 int flags = size ? SPLICE_F_MORE : 0;
336 int written = splice(pfd[0], NULL, out_fd, NULL, ret, flags);
339 return error("splice-out");
347 if ((mtime_since_now(&start) < max_client_run * 1000) && loops)
350 size = sb.st_size >> 10;
351 size *= (client_loops - loops);
352 msecs = mtime_since_now(&start);
353 fprintf(stdout, "Client%d (splice): %Lu MiB/sec (%LuMiB in %lu msecs)\n", offset, size / (unsigned long long) msecs, size >> 10, msecs);
357 static int client_splice(int out_fd, int file_fd, int offset)
362 return error("pipe");
364 ret = client_splice_loop(out_fd, file_fd, pfd, offset);
370 static int do_client(int out_fd, int file_fd, int offset)
375 ret = client_splice(out_fd, file_fd, offset);
380 ret = client_mmap(out_fd, file_fd, offset);
385 ret = client_rw(out_fd, file_fd, offset);
392 static int client_open_net(int offset)
394 int sk = socket(PF_INET, SOCK_STREAM, 0);
395 struct sockaddr_in s_to;
398 hp = gethostbyname("localhost");
400 return error("gethostbyname");
402 bzero((char *) &s_to, sizeof (s_to));
403 bcopy((char *) hp->h_addr, (char *) &(s_to.sin_addr), hp->h_length);
404 s_to.sin_family = hp->h_addrtype;
405 s_to.sin_port = htons(net_port + offset);
407 if (connect(sk, (struct sockaddr *)&s_to, sizeof(s_to)) < 0)
408 return error("connect");
413 static int client(int offset)
422 out_fd = client_open_net(offset);
424 out_fd = open("/dev/null", O_WRONLY);
427 return error("socket");
429 sprintf(fname, "%s%d", filename, same_file ? 0 : offset);
430 file_fd = open(fname, O_RDONLY);
432 return error("open");
434 return do_client(out_fd, file_fd, offset);
437 int main(int argc, char *argv[])
439 pid_t *spids, *cpids;
442 index = parse_options(argc, argv);
444 return usage(argv[0]);
447 filename = argv[index];
449 spids = malloc(nr_clients * sizeof(pid_t));
450 cpids = malloc(nr_clients * sizeof(pid_t));
451 memset(spids, 0, nr_clients * sizeof(pid_t));
452 memset(cpids, 0, nr_clients * sizeof(pid_t));
454 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
456 return error("_SC_NPROCESSORS_ONLN");
461 if (!write_to_null) {
462 for (i = 0; i < nr_clients; i++) {
473 sleep(1); /* should have servers started now */
479 for (i = 0; i < nr_clients; i++) {
492 * wait for clients to exit
494 fprintf(stdout, "Waiting for clients\n");
495 for (i = 0; i < nr_clients; i++) {
497 waitpid(cpids[i], NULL, 0);
505 for (i = 0; i < nr_clients; i++) {
507 kill(spids[i], SIGKILL);
508 waitpid(spids[i], NULL, 0);