]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/poll.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
;
50 static int target
= 0;
52 typedef struct pollfd pfd_t
;
62 lm_tsdsize
= sizeof (tsd_t
);
64 (void) sprintf(lm_optstr
, "n:r:w:x");
66 (void) sprintf(lm_usage
,
67 " [-n fds-per-thread (default %d)]\n"
68 " [-r readable-fds (default 0)]\n"
69 " [-w writeable-fds (default 0)]\n"
70 " [-x] (start -r option with highest fd first; "
71 "default is lowest first)\n"
72 "notes: measures poll()\n",
75 (void) sprintf(lm_header
, "%8s %5s", "nfds", "flags");
81 benchmark_optswitch(int opt
, char *optarg
)
110 (void) printf("ERROR: -n value must be even\n");
111 optn
= optr
= optw
= 0;
115 if (optn
< 0 || optr
< 0 || optw
< 0) {
116 (void) printf("ERROR: -n, -r and -w values must be > 0\n");
117 optn
= optr
= optw
= 0;
121 if (optr
> optn
|| optw
> optn
) {
122 (void) printf("ERROR: -r and -w values must be <= maxfd\n");
123 optn
= optr
= optw
= 0;
127 fds
= (int *)malloc(optn
* sizeof (int));
129 (void) printf("ERROR: malloc() failed\n");
130 optn
= optr
= optw
= 0;
134 (void) setfdlimit(optn
+ 10);
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]);
148 target
= MIN(optr
+ optw
, optn
);
149 for (i
= 0, j
= optn
- 1; i
< optr
; i
++, j
--) {
150 (void) write(fds
[j
+1 - (2*(j%2
))], "", 1);
153 target
= MAX(optr
, optw
);
154 for (i
= 0; i
< optr
; i
++) {
155 (void) write(fds
[i
+1 - (2*(i%2
))], "", 1);
163 benchmark_initbatch(void *tsd
)
165 tsd_t
*ts
= (tsd_t
*)tsd
;
169 if (ts
->ts_once
++ == 0) {
170 ts
->ts_pfds
= (pfd_t
*)malloc(optn
* sizeof (pfd_t
));
171 if (ts
->ts_pfds
== NULL
) {
175 for (i
= 0; i
< optn
; i
++) {
176 ts
->ts_pfds
[i
].fd
= fds
[i
];
177 ts
->ts_pfds
[i
].events
= POLLIN
;
180 for (i
= 0; i
< optw
; i
++) {
181 ts
->ts_pfds
[i
].events
|= POLLOUT
;
189 benchmark(void *tsd
, result_t
*res
)
191 tsd_t
*ts
= (tsd_t
*)tsd
;
194 for (i
= 0; i
< lm_optB
; i
++) {
195 if (poll(ts
->ts_pfds
, optn
, 0) != target
) {
207 static char result
[256];
210 flags
[0] = optr
? 'r' : '-';
211 flags
[1] = optw
? 'w' : '-';
212 flags
[2] = optx
? 'x' : '-';
215 (void) sprintf(result
, "%8d %5s", optn
, flags
);