]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/close_tcp.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 to measure time to close a local tcp connection
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
56 struct sockaddr_in
*ts_adds
;
62 lm_tsdsize
= sizeof (tsd_t
);
66 (void) sprintf(lm_usage
,
67 "notes: measures close() on local TCP connections");
75 (void) setfdlimit(3 * lm_optB
* lm_optT
+ 10);
81 benchmark_initworker(void *tsd
)
83 tsd_t
*ts
= (tsd_t
*)tsd
;
89 ts
->ts_lsns
= (int *)malloc(lm_optB
* sizeof (int));
90 if (ts
->ts_lsns
== NULL
) {
93 ts
->ts_accs
= (int *)malloc(lm_optB
* sizeof (int));
94 if (ts
->ts_accs
== NULL
) {
97 ts
->ts_cons
= (int *)malloc(lm_optB
* sizeof (int));
98 if (ts
->ts_cons
== NULL
) {
101 ts
->ts_adds
= (struct sockaddr_in
*)malloc(lm_optB
*
102 sizeof (struct sockaddr_in
));
103 if (ts
->ts_adds
== NULL
) {
108 for (i
= 0; i
< lm_optB
; i
++) {
109 ts
->ts_lsns
[i
] = socket(AF_INET
, SOCK_STREAM
, 0);
110 if (ts
->ts_lsns
[i
] == -1) {
115 if (setsockopt(ts
->ts_lsns
[i
], SOL_SOCKET
, SO_REUSEADDR
,
116 &opt
, sizeof (int)) == -1) {
117 perror("setsockopt");
121 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
= htons(j
++);
130 (void) memcpy(&ts
->ts_adds
[i
].sin_addr
.s_addr
,
131 host
->h_addr_list
[0], sizeof (struct in_addr
));
133 if (bind(ts
->ts_lsns
[i
],
134 (struct sockaddr
*)&ts
->ts_adds
[i
],
135 sizeof (struct sockaddr_in
)) == 0) {
139 if (errno
!= EADDRINUSE
) {
145 if (listen(ts
->ts_lsns
[i
], 5) == -1) {
155 benchmark_initbatch(void *tsd
)
157 tsd_t
*ts
= (tsd_t
*)tsd
;
160 struct sockaddr_in addr
;
164 for (i
= 0; i
< lm_optB
; i
++) {
165 ts
->ts_cons
[i
] = socket(AF_INET
, SOCK_STREAM
, 0);
166 if (ts
->ts_cons
[i
] == -1) {
172 if (fcntl(ts
->ts_cons
[i
], F_SETFL
, O_NDELAY
) == -1) {
178 result
= connect(ts
->ts_cons
[i
],
179 (struct sockaddr
*)&ts
->ts_adds
[i
],
180 sizeof (struct sockaddr_in
));
182 if ((result
== -1) && (errno
!= EINPROGRESS
)) {
188 size
= sizeof (struct sockaddr
);
189 result
= accept(ts
->ts_lsns
[i
], (struct sockaddr
*)&addr
,
196 ts
->ts_accs
[i
] = result
;
203 benchmark(void *tsd
, result_t
*res
)
205 tsd_t
*ts
= (tsd_t
*)tsd
;
208 for (i
= 0; i
< lm_optB
; i
++) {
209 if (close(ts
->ts_accs
[i
]) == -1) {
219 benchmark_finibatch(void *tsd
)
221 tsd_t
*ts
= (tsd_t
*)tsd
;
224 for (i
= 0; i
< lm_optB
; i
++) {
225 (void) close(ts
->ts_cons
[i
]);
232 benchmark_finiworker(void *tsd
)
234 tsd_t
*ts
= (tsd_t
*)tsd
;
237 for (i
= 0; i
< lm_optB
; i
++) {
238 (void) close(ts
->ts_lsns
[i
]);