]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/pipe.c
4 * The contents of this file are subject to the terms
5 * of the Common Development and Distribution License
6 * (the "License"). You may not use this file except
7 * in compliance with the License.
9 * You can obtain a copy of the license at
10 * src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing
13 * permissions and limitations under the License.
15 * When distributing Covered Code, include this CDDL
16 * HEADER in each file and include the License file at
17 * usr/src/OPENSOLARIS.LICENSE. If applicable,
18 * add the following below this CDDL HEADER, with the
19 * fields enclosed by brackets "[]" replaced with your
20 * own identifying information: Portions Copyright [yyyy]
21 * [name of copyright owner]
27 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
37 #include <arpa/inet.h>
59 struct sockaddr_in ts_add
;
62 #define FIRSTPORT 12345
64 static char *modes
[] = {"st", "mt", "mp", NULL
};
66 #define MD_MULTITHREAD 1
67 #define MD_MULTIPROCESS 2
69 static char *xports
[] = {"pipe", "fifo", "sock", "tcp",
73 #define XP_SOCKETPAIR 2
76 #define DEFM MD_SINGLE
80 static int optm
= DEFM
;
81 static size_t opts
= DEFS
;
82 static int optx
= DEFX
;
83 static void *rbuf
= NULL
;
84 static void *wbuf
= NULL
;
86 int readall(int s
, void *buf
, size_t len
);
87 void *loopback(void *arg
);
88 int prepare_pipes(tsd_t
*tsd
);
89 int prepare_fifos(tsd_t
*tsd
);
90 int cleanup_fifos(tsd_t
*tsd
);
91 int prepare_socketpair(tsd_t
*tsd
);
92 int prepare_localtcp(tsd_t
*tsd
);
93 int prepare_localtcp_once(tsd_t
*tsd
);
94 char *lookupa(int x
, char *names
[]);
95 int lookup(char *x
, char *names
[]);
100 lm_tsdsize
= sizeof (tsd_t
);
102 (void) sprintf(lm_optstr
, "m:s:x:");
104 (void) sprintf(lm_usage
,
105 " [-m mode (st|mt|mp, default %s)]\n"
106 " [-s buffer-size (default %d)]\n"
107 " [-x transport (pipe|fifo|sock|tcp, default %s)]\n"
108 "notes: measures write()/read() across various transports\n",
109 lookupa(DEFM
, modes
), DEFS
, lookupa(DEFX
, xports
));
111 (void) sprintf(lm_header
, "%2s %4s", "md", "xprt");
117 benchmark_optswitch(int opt
, char *optarg
)
123 x
= lookup(optarg
, modes
);
129 opts
= sizetoll(optarg
);
132 x
= lookup(optarg
, xports
);
146 if (optx
== XP_FIFOS
) {
147 if (geteuid() != 0) {
148 (void) printf("sorry, must be root to create fifos\n");
153 (void) setfdlimit(4 * lm_optT
+ 10);
162 benchmark_initbatch(void *tsd
)
164 tsd_t
*ts
= (tsd_t
*)tsd
;
170 result
= prepare_socketpair(ts
);
173 result
= prepare_localtcp(ts
);
176 result
= prepare_fifos(ts
);
180 result
= prepare_pipes(ts
);
189 result
= pthread_create(&ts
->ts_thread
, NULL
, loopback
, tsd
);
194 case MD_MULTIPROCESS
:
198 (void) loopback(tsd
);
213 /* Prime the loopback */
214 if (write(ts
->ts_out
, wbuf
, opts
) != opts
) {
217 if (readall(ts
->ts_in
, rbuf
, opts
) != opts
) {
225 benchmark(void *tsd
, result_t
*res
)
227 tsd_t
*ts
= (tsd_t
*)tsd
;
231 for (i
= 0; i
< lm_optB
; i
++) {
232 if (write(ts
->ts_out
, wbuf
, opts
) != opts
) {
237 n
= readall(ts
->ts_in
, rbuf
, opts
);
249 benchmark_finibatch(void *tsd
)
251 tsd_t
*ts
= (tsd_t
*)tsd
;
253 /* Terminate the loopback */
254 (void) write(ts
->ts_out
, wbuf
, opts
);
255 (void) readall(ts
->ts_in
, rbuf
, opts
);
259 (void) close(ts
->ts_in2
);
260 (void) close(ts
->ts_out2
);
261 (void) pthread_join(ts
->ts_thread
, NULL
);
263 case MD_MULTIPROCESS
:
264 (void) close(ts
->ts_in2
);
265 (void) close(ts
->ts_out2
);
266 (void) waitpid(ts
->ts_child
, NULL
, 0);
273 (void) close(ts
->ts_in
);
274 (void) close(ts
->ts_out
);
276 if (optx
== XP_FIFOS
) {
277 (void) cleanup_fifos(ts
);
286 static char result
[256];
288 (void) sprintf(result
, "%2s %4s",
289 lookupa(optm
, modes
), lookupa(optx
, xports
));
295 readall(int s
, void *buf
, size_t len
)
301 n
= read(s
, (void *)((long)buf
+ total
), len
- total
);
315 tsd_t
*ts
= (tsd_t
*)arg
;
318 /* Include priming and termination */
321 for (i
= 0; i
< m
; i
++) {
322 n
= readall(ts
->ts_in2
, rbuf
, opts
);
326 if (write(ts
->ts_out2
, wbuf
, opts
) != opts
) {
335 prepare_localtcp_once(tsd_t
*ts
)
339 struct hostent
*host
;
343 ts
->ts_lsn
= socket(AF_INET
, SOCK_STREAM
, 0);
344 if (ts
->ts_lsn
== -1) {
348 if (setsockopt(ts
->ts_lsn
, SOL_SOCKET
, SO_REUSEADDR
,
349 &opt
, sizeof (int)) == -1) {
353 if ((host
= gethostbyname("localhost")) == NULL
) {
358 (void) memset(&ts
->ts_add
, 0,
359 sizeof (struct sockaddr_in
));
360 ts
->ts_add
.sin_family
= AF_INET
;
361 ts
->ts_add
.sin_port
= htons(j
++);
362 (void) memcpy(&ts
->ts_add
.sin_addr
.s_addr
,
363 host
->h_addr_list
[0], sizeof (struct in_addr
));
366 (struct sockaddr
*)&ts
->ts_add
,
367 sizeof (struct sockaddr_in
)) == 0) {
371 if (errno
!= EADDRINUSE
) {
376 if (listen(ts
->ts_lsn
, 5) == -1) {
384 prepare_localtcp(tsd_t
*ts
)
387 struct sockaddr_in addr
;
391 if (ts
->ts_once
++ == 0) {
392 if (prepare_localtcp_once(ts
) == -1) {
397 ts
->ts_out
= socket(AF_INET
, SOCK_STREAM
, 0);
398 if (ts
->ts_out
== -1) {
402 if (fcntl(ts
->ts_out
, F_SETFL
, O_NDELAY
) == -1) {
406 result
= connect(ts
->ts_out
, (struct sockaddr
*)&ts
->ts_add
,
407 sizeof (struct sockaddr_in
));
408 if ((result
== -1) && (errno
!= EINPROGRESS
)) {
412 if (fcntl(ts
->ts_out
, F_SETFL
, 0) == -1) {
416 size
= sizeof (struct sockaddr
);
417 result
= accept(ts
->ts_lsn
, (struct sockaddr
*)&addr
, &size
);
421 ts
->ts_out2
= result
;
423 if (setsockopt(ts
->ts_out
, IPPROTO_TCP
, TCP_NODELAY
,
424 &opt
, sizeof (int)) == -1) {
428 if (setsockopt(ts
->ts_out2
, IPPROTO_TCP
, TCP_NODELAY
,
429 &opt
, sizeof (int)) == -1) {
433 if (optm
== MD_SINGLE
) {
434 ts
->ts_in
= ts
->ts_out2
;
436 ts
->ts_in
= ts
->ts_out
;
437 ts
->ts_in2
= ts
->ts_out2
;
444 prepare_socketpair(tsd_t
*ts
)
448 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, s
) == -1) {
452 if (optm
== MD_SINGLE
) {
466 prepare_fifos(tsd_t
*ts
)
470 (void) sprintf(path
, "/private/tmp/pipe_%ld.%dA",
471 getpid(), pthread_self());
472 if (mknod(path
, 0600, S_IFIFO
) == -1) {
476 if (optm
== MD_SINGLE
) {
477 ts
->ts_in
= open(path
, O_RDONLY
);
478 ts
->ts_out
= open(path
, O_WRONLY
);
480 ts
->ts_in
= open(path
, O_RDONLY
);
481 ts
->ts_out2
= open(path
, O_WRONLY
);
483 (void) sprintf(path
, "/private/tmp/pipe_%ld.%dB",
484 getpid(), pthread_self());
485 if (mknod(path
, 0600, S_IFIFO
) == -1) {
489 ts
->ts_in2
= open(path
, O_RDONLY
);
490 ts
->ts_out
= open(path
, O_WRONLY
);
498 cleanup_fifos(tsd_t
*ts
)
502 (void) sprintf(path
, "/private/tmp/pipe_%ld.%dA", getpid(), pthread_self());
504 (void) sprintf(path
, "/private/tmp/pipe_%ld.%dB", getpid(), pthread_self());
511 prepare_pipes(tsd_t
*ts
)
515 if (optm
== MD_SINGLE
) {
540 lookupa(int x
, char *names
[])
544 while (names
[i
] != NULL
) {
554 lookup(char *x
, char *names
[])
558 while (names
[i
] != NULL
) {
559 if (strcmp(names
[i
], x
) == 0) {