]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/connection.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.
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <arpa/inet.h>
48 #define FIRSTPORT 12345
55 struct sockaddr_in
*ts_adds
;
60 static struct hostent
*host
;
66 lm_tsdsize
= sizeof (tsd_t
);
68 (void) sprintf(lm_optstr
, "ac");
70 (void) sprintf(lm_usage
,
71 " [-a] (measure accept() only)\n"
72 " [-c] (measure connect() only)\n"
73 "notes: measures connect()/accept()\n");
80 benchmark_optswitch(int opt
, char *optarg
)
94 (void) printf("warning: -a overrides -c\n");
104 (void) setfdlimit(3 * lm_optB
* lm_optT
+ 10);
110 benchmark_initbatch_once(void *tsd
)
112 tsd_t
*ts
= (tsd_t
*)tsd
;
117 ts
->ts_lsns
= (int *)malloc(lm_optB
* sizeof (int));
118 if (ts
->ts_lsns
== NULL
) {
121 ts
->ts_accs
= (int *)malloc(lm_optB
* sizeof (int));
122 if (ts
->ts_accs
== NULL
) {
125 ts
->ts_cons
= (int *)malloc(lm_optB
* sizeof (int));
126 if (ts
->ts_cons
== NULL
) {
130 (struct sockaddr_in
*)malloc(lm_optB
*
131 sizeof (struct sockaddr_in
));
132 if (ts
->ts_accs
== NULL
) {
137 for (i
= 0; i
< lm_optB
; i
++) {
138 ts
->ts_lsns
[i
] = socket(AF_INET
, SOCK_STREAM
, 0);
139 if (ts
->ts_lsns
[i
] == -1) {
145 * make accept socket non-blocking so in case of errors
149 if (fcntl(ts
->ts_lsns
[i
], F_SETFL
, O_NDELAY
) == -1) {
155 if ((host
= gethostbyname("localhost")) == NULL
) {
160 (void) memset(&ts
->ts_adds
[i
], 0,
161 sizeof (struct sockaddr_in
));
162 ts
->ts_adds
[i
].sin_family
= AF_INET
;
163 ts
->ts_adds
[i
].sin_port
= htons(j
++);
164 (void) memcpy(&ts
->ts_adds
[i
].sin_addr
.s_addr
,
165 host
->h_addr_list
[0], sizeof (struct in_addr
));
167 if (bind(ts
->ts_lsns
[i
],
168 (struct sockaddr
*)&ts
->ts_adds
[i
],
169 sizeof (struct sockaddr_in
)) == 0) {
173 if (errno
!= EADDRINUSE
) {
178 if (listen(ts
->ts_lsns
[i
], 5) == -1) {
187 benchmark_initbatch(void *tsd
)
189 tsd_t
*ts
= (tsd_t
*)tsd
;
194 if (ts
->ts_once
++ == 0) {
195 if (errors
+= benchmark_initbatch_once(tsd
) == -1) {
201 for (i
= 0; i
< lm_optB
; i
++) {
202 ts
->ts_cons
[i
] = socket(AF_INET
, SOCK_STREAM
, 0);
203 if (ts
->ts_cons
[i
] == -1) {
204 perror("init:socket");
208 if (fcntl(ts
->ts_cons
[i
], F_SETFL
, O_NDELAY
) == -1) {
209 perror("init:fcntl");
214 result
= connect(ts
->ts_cons
[i
],
215 (struct sockaddr
*)&ts
->ts_adds
[i
],
216 sizeof (struct sockaddr_in
));
217 if ((result
== -1) && (errno
!= EINPROGRESS
)) {
218 perror("init:connect");
228 benchmark(void *tsd
, result_t
*res
)
233 tsd_t
*ts
= (tsd_t
*)tsd
;
236 struct sockaddr_in addr
;
239 for (i
= 0; i
< lm_optB
; i
++) {
242 result
= connect(ts
->ts_cons
[i
],
243 (struct sockaddr
*)&ts
->ts_adds
[i
],
244 sizeof (struct sockaddr_in
));
245 if (result
!= 0 && errno
!= EISCONN
) {
246 if (errno
== EINPROGRESS
) {
247 struct pollfd pollfd
;
250 pollfd
.fd
= ts
->ts_cons
[i
];
251 pollfd
.events
= POLLOUT
;
252 if (poll(&pollfd
, 1, -1) == 1)
257 perror("benchmark:connect");
263 size
= sizeof (struct sockaddr
);
265 struct pollfd pollfd
;
266 result
= accept(ts
->ts_lsns
[i
],
267 (struct sockaddr
*)&addr
, &size
);
268 if (result
> 0 || (result
== -1 &&
271 pollfd
.fd
= ts
->ts_lsns
[i
];
272 pollfd
.events
= POLLIN
;
273 if (poll(&pollfd
, 1, -1) != 1)
277 ts
->ts_accs
[i
] = result
;
280 perror("benchmark:accept");
291 benchmark_finibatch(void *tsd
)
293 tsd_t
*ts
= (tsd_t
*)tsd
;
296 for (i
= 0; i
< lm_optB
; i
++) {
299 (void) close(ts
->ts_accs
[i
]);
301 (void) close(ts
->ts_cons
[i
]);