]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/bind.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 * benchmark for bind... keep in mind tcp hash chain effects
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/tcp.h>
39 #include <arpa/inet.h>
50 #define FIRSTPORT 12345
54 struct sockaddr_in
*ts_adds
;
62 lm_tsdsize
= sizeof (tsd_t
);
65 (void) sprintf(lm_optstr
, "z");
67 (void) sprintf(lm_usage
,
68 " [-z bind to port 0 rather than seq. number\n"
69 "notes: measures bind() on TCP");
77 (void) setfdlimit(lm_optB
* lm_optT
+ 10);
84 benchmark_optswitch(int opt
, char *optarg
)
97 benchmark_initbatch(void *tsd
)
99 tsd_t
*ts
= (tsd_t
*)tsd
;
102 struct hostent
*host
;
105 ts
->ts_lsns
= (int *)malloc(lm_optB
* sizeof (int));
106 if (ts
->ts_lsns
== NULL
)
109 ts
->ts_adds
= (struct sockaddr_in
*)malloc(lm_optB
*
110 sizeof (struct sockaddr_in
));
111 if (ts
->ts_adds
== NULL
)
115 for (i
= 0; i
< lm_optB
; i
++) {
116 if ((ts
->ts_lsns
[i
] = socket(PF_INET
, SOCK_STREAM
, 0)) == -1)
119 if (setsockopt(ts
->ts_lsns
[i
], SOL_SOCKET
, SO_REUSEADDR
,
120 &opt
, sizeof (int)) == -1)
123 if ((host
= gethostbyname("localhost")) == NULL
)
126 (void) memset(&ts
->ts_adds
[i
], 0,
127 sizeof (struct sockaddr_in
));
128 ts
->ts_adds
[i
].sin_family
= AF_INET
;
129 ts
->ts_adds
[i
].sin_port
= (optz
? 0 : htons(j
++));
130 (void) memcpy(&ts
->ts_adds
[i
].sin_addr
.s_addr
,
131 host
->h_addr_list
[0], sizeof (struct in_addr
));
137 benchmark(void *tsd
, result_t
*res
)
139 tsd_t
*ts
= (tsd_t
*)tsd
;
142 for (i
= 0; i
< lm_optB
; i
++) {
143 if ((bind(ts
->ts_lsns
[i
],
144 (struct sockaddr
*)&ts
->ts_adds
[i
],
145 sizeof (struct sockaddr_in
)) != 0) &&
146 (errno
!= EADDRINUSE
))
155 benchmark_finibatch(void *tsd
)
157 tsd_t
*ts
= (tsd_t
*)tsd
;
160 for (i
= 0; i
< lm_optB
; i
++)
161 (void) close(ts
->ts_lsns
[i
]);