]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/select.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 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #define MAX(x, y) ((x) > (y) ? (x) : (y))
32 #define MIN(x, y) ((x) > (y) ? (y) : (x))
39 #include <sys/socket.h>
45 static int optn
= DEFN
;
53 static int target
= 0;
58 (void) sprintf(lm_optstr
, "n:r:w:x");
62 (void) sprintf(lm_usage
,
63 " [-n fds-per-thread (default %d)]\n"
64 " [-r readable-fds (default 0)]\n"
65 " [-w writeable-fds (default 0)]\n"
66 " [-x] (start -r option with highest fd first; "
67 "default is lowest first)\n"
68 "notes: measures select()\n",
71 (void) sprintf(lm_header
, "%8s %5s", "maxfd", "flags");
77 benchmark_optswitch(int opt
, char *optarg
)
106 (void) printf("ERROR: -n value must be even\n");
107 optn
= optr
= optw
= 0;
111 if (optn
< 0 || optr
< 0 || optw
< 0) {
112 (void) printf("ERROR: -n, -r and -w values must be > 0\n");
113 optn
= optr
= optw
= 0;
117 if (optr
> optn
|| optw
> optn
) {
118 (void) printf("ERROR: -r and -w values must be <= maxfd\n");
119 optn
= optr
= optw
= 0;
123 fds
= (int *)malloc(optn
* sizeof (int));
125 (void) printf("ERROR: malloc() failed\n");
126 optn
= optr
= optw
= 0;
130 (void) setfdlimit(optn
+ 10);
132 target
= optr
+ optw
;
137 for (i
= 0; i
< optn
; i
+= 2) {
138 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, pair
) == -1) {
139 (void) printf("ERROR: socketpair() failed\n");
143 fds
[i
] = MIN(pair
[0], pair
[1]);
144 fds
[i
+1] = MAX(pair
[0], pair
[1]);
145 maxfd
= fds
[i
+1] + 1;
147 if (maxfd
> FD_SETSIZE
) {
148 (void) printf("WARNING: FD_SETSIZE is too small!\n");
152 FD_SET(fds
[i
], &iset
);
153 FD_SET(fds
[i
+1], &iset
);
156 for (i
= 0; i
< optw
; i
++) {
157 FD_SET(fds
[i
], &oset
);
160 for (i
= 0, j
= optn
- 1; i
< optr
; i
++, j
--) {
161 (void) write(fds
[j
+1 - (2*(j%2
))], "", 1);
164 for (i
= 0; i
< optr
; i
++) {
165 (void) write(fds
[i
+1 - (2*(i%2
))], "", 1);
174 benchmark(void *tsd
, result_t
*res
)
179 fd_set
*my_iset
= &set1
;
180 fd_set
*my_oset
= NULL
;
181 struct timeval tv
= {0, 0};
187 for (i
= 0; i
< lm_optB
; i
++) {
188 (void) memcpy(&set1
, &iset
, sizeof (fd_set
));
189 (void) memcpy(&set2
, &oset
, sizeof (fd_set
));
191 if (select(maxfd
, my_iset
, my_oset
, NULL
, &tv
) != target
) {
203 static char result
[256];
206 flags
[0] = optr
? 'r' : '-';
207 flags
[1] = optw
? 'w' : '-';
208 flags
[2] = optx
? 'x' : '-';
211 (void) sprintf(result
, "%8d %5s", optn
, flags
);