Cross Reference: select.c
xref
: /
libmicro
/
select.c
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
0
N/A
/*
0
N/A
* CDDL HEADER START
0
N/A
*
0
N/A
* The contents of this file are subject to the terms
0
N/A
* of the Common Development and Distribution License
0
N/A
* (the "License"). You may not use this file except
0
N/A
* in compliance with the License.
0
N/A
*
0
N/A
* You can obtain a copy of the license at
0
N/A
*
src
/
OPENSOLARIS.LICENSE
0
N/A
* or
http://www.opensolaris.org/os/licensing
.
0
N/A
* See the License for the specific language governing
0
N/A
* permissions and limitations under the License.
0
N/A
*
0
N/A
* When distributing Covered Code, include this CDDL
0
N/A
* HEADER in each file and include the License file at
0
N/A
*
usr
/
src
/
OPENSOLARIS.LICENSE
. If applicable,
0
N/A
* add the following below this CDDL HEADER, with the
0
N/A
* fields enclosed by brackets "[]" replaced with your
0
N/A
* own identifying information: Portions Copyright [yyyy]
0
N/A
* [name of copyright owner]
0
N/A
*
0
N/A
* CDDL HEADER END
0
N/A
*/
0
N/A
0
N/A
/*
0
N/A
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
0
N/A
* Use is subject to license terms.
0
N/A
*/
0
N/A
0
N/A
#
define
MAX
(x, y) ((x) > (y) ? (x) : (y))
0
N/A
#
define
MIN
(x, y) ((x) > (y) ? (y) : (x))
0
N/A
0
N/A
#
include
<
unistd.h
>
0
N/A
#
include
<
stdlib.h
>
0
N/A
#
include
<
stdio.h
>
0
N/A
#
include
<
string.h
>
0
N/A
#
include
<
sys
/
time.h
>
0
N/A
#
include
<
sys
/
socket.h
>
0
N/A
0
N/A
#
include
"
libmicro.h
"
0
N/A
0
N/A
#
define
DEFN
256
0
N/A
0
N/A
static
int
optn
=
DEFN
;
0
N/A
static
int
optr
= 0;
0
N/A
static
int
optw
= 0;
0
N/A
static
int
optx
= 0;
0
N/A
static
int
*
fds
;
0
N/A
static
fd_set
iset
;
0
N/A
static
fd_set
oset
;
0
N/A
static
int
maxfd
= 0;
0
N/A
static
int
target
= 0;
0
N/A
0
N/A
int
0
N/A
benchmark_init
()
0
N/A
{
0
N/A
(
void
)
sprintf
(
lm_optstr
,
"n:r:w:x"
);
0
N/A
0
N/A
lm_tsdsize
= 0;
0
N/A
0
N/A
(
void
)
sprintf
(
lm_usage
,
0
N/A
" [-n fds-per-thread (default %d)]\n"
0
N/A
" [-r readable-fds (default 0)]\n"
0
N/A
" [-w writeable-fds (default 0)]\n"
0
N/A
" [-x] (start -r option with highest fd first; "
0
N/A
"default is lowest first)\n"
0
N/A
"notes: measures select()\n"
,
0
N/A
DEFN
);
0
N/A
0
N/A
(
void
)
sprintf
(
lm_header
,
"%8s %5s"
,
"maxfd"
,
"flags"
);
0
N/A
0
N/A
return
(0);
0
N/A
}
0
N/A
0
N/A
int
0
N/A
benchmark_optswitch
(
int
opt
,
char
*
optarg
)
0
N/A
{
0
N/A
switch
(
opt
) {
0
N/A
case
'n'
:
0
N/A
optn
=
atoi
(
optarg
);
0
N/A
break
;
0
N/A
case
'r'
:
0
N/A
optr
=
atoi
(
optarg
);
0
N/A
break
;
0
N/A
case
'w'
:
0
N/A
optw
=
atoi
(
optarg
);
0
N/A
break
;
0
N/A
case
'x'
:
0
N/A
optx
=
1
;
0
N/A
break
;
0
N/A
default
:
0
N/A
return
(-
1
);
0
N/A
}
0
N/A
return
(0);
0
N/A
}
0
N/A
0
N/A
int
0
N/A
benchmark_initrun
()
0
N/A
{
0
N/A
int
i;
0
N/A
int
j;
0
N/A
int
pair
[
2
];
0
N/A
0
N/A
if
(
optn
%
2
!= 0) {
0
N/A
(
void
)
printf
(
"ERROR: -n value must be even\n"
);
0
N/A
optn
=
optr
=
optw
= 0;
0
N/A
return
(-
1
);
0
N/A
}
0
N/A
0
N/A
if
(
optn
< 0 ||
optr
< 0 ||
optw
< 0) {
0
N/A
(
void
)
printf
(
"ERROR: -n, -r and -w values must be > 0\n"
);
0
N/A
optn
=
optr
=
optw
= 0;
0
N/A
return
(-
1
);
0
N/A
}
0
N/A
0
N/A
if
(
optr
>
optn
||
optw
>
optn
) {
0
N/A
(
void
)
printf
(
"ERROR: -r and -w values must be <= maxfd\n"
);
0
N/A
optn
=
optr
=
optw
= 0;
0
N/A
return
(-
1
);
0
N/A
}
0
N/A
0
N/A
fds
= (
int
*)
malloc
(
optn
*
sizeof
(
int
));
0
N/A
if
(
fds
==
NULL
) {
0
N/A
(
void
)
printf
(
"ERROR: malloc() failed\n"
);
0
N/A
optn
=
optr
=
optw
= 0;
0
N/A
return
(-
1
);
0
N/A
}
0
N/A
0
N/A
(
void
)
setfdlimit
(
optn
+
10
);
0
N/A
0
N/A
target
=
optr
+
optw
;
0
N/A
0
N/A
FD_ZERO
(&
iset
);
0
N/A
FD_ZERO
(&
oset
);
0
N/A
0
N/A
for
(i = 0; i <
optn
; i +=
2
) {
0
N/A
if
(
socketpair
(
PF_UNIX
,
SOCK_STREAM
, 0,
pair
) == -
1
) {
0
N/A
(
void
)
printf
(
"ERROR: socketpair() failed\n"
);
0
N/A
return
(-
1
);
0
N/A
}
0
N/A
0
N/A
fds
[i] =
MIN
(
pair
[0],
pair
[
1
]);
0
N/A
fds
[i+
1
] =
MAX
(
pair
[0],
pair
[
1
]);
0
N/A
maxfd
=
fds
[i+
1
] +
1
;
0
N/A
0
N/A
if
(
maxfd
>
FD_SETSIZE
) {
0
N/A
(
void
)
printf
(
"WARNING: FD_SETSIZE is too small!\n"
);
0
N/A
return
(-
1
);
0
N/A
}
0
N/A
0
N/A
FD_SET
(
fds
[i], &
iset
);
0
N/A
FD_SET
(
fds
[i+
1
], &
iset
);
0
N/A
}
0
N/A
0
N/A
for
(i = 0; i <
optw
; i++) {
0
N/A
FD_SET
(
fds
[i], &
oset
);
0
N/A
}
0
N/A
if
(
optx
) {
0
N/A
for
(i = 0, j =
optn
-
1
; i <
optr
; i++, j--) {
0
N/A
(
void
)
write
(
fds
[j+
1
- (
2
*(j%
2
))],
""
,
1
);
0
N/A
}
0
N/A
}
else
{
0
N/A
for
(i = 0; i <
optr
; i++) {
0
N/A
(
void
)
write
(
fds
[i+
1
- (
2
*(i%
2
))],
""
,
1
);
0
N/A
}
0
N/A
}
0
N/A
0
N/A
return
(0);
0
N/A
}
0
N/A
0
N/A
/*ARGSUSED*/
0
N/A
int
0
N/A
benchmark
(
void
*
tsd
,
result_t
*
res
)
0
N/A
{
0
N/A
int
i;
0
N/A
fd_set
set1
;
0
N/A
fd_set
set2
;
0
N/A
fd_set
*
my_iset
= &
set1
;
0
N/A
fd_set
*
my_oset
=
NULL
;
0
N/A
struct
timeval
tv
= {0, 0};
0
N/A
0
N/A
if
(
optw
) {
0
N/A
my_oset
= &
set2
;
0
N/A
}
0
N/A
0
N/A
for
(i = 0; i <
lm_optB
; i++) {
0
N/A
(
void
)
memcpy
(&
set1
, &
iset
,
sizeof
(
fd_set
));
0
N/A
(
void
)
memcpy
(&
set2
, &
oset
,
sizeof
(
fd_set
));
0
N/A
0
N/A
if
(
select
(
maxfd
,
my_iset
,
my_oset
,
NULL
, &
tv
) !=
target
) {
0
N/A
res
->
re_errors
++;
0
N/A
}
0
N/A
}
0
N/A
res
->
re_count
= i;
0
N/A
0
N/A
return
(0);
0
N/A
}
0
N/A
0
N/A
char
*
0
N/A
benchmark_result
()
0
N/A
{
0
N/A
static
char
result
[
256
];
0
N/A
char
flags
[
4
];
0
N/A
0
N/A
flags
[0] =
optr
?
'r'
:
'-'
;
0
N/A
flags
[
1
] =
optw
?
'w'
:
'-'
;
0
N/A
flags
[
2
] =
optx
?
'x'
:
'-'
;
0
N/A
flags
[
3
] = 0;
0
N/A
0
N/A
(
void
)
sprintf
(
result
,
"%8d %5s"
,
optn
,
flags
);
0
N/A
0
N/A
return
(
result
);
0
N/A
}