Home | History | Annotate | Download | only in libmicro
      1 /*
      2  * CDDL HEADER START
      3  *
      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.
      8  *
      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.
     14  *
     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]
     22  *
     23  * CDDL HEADER END
     24  */
     25 
     26 /*
     27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
     28  * Use is subject to license terms.
     29  */
     30 
     31 /*
     32  * listen benchmark
     33  */
     34 
     35 
     36 #include <sys/types.h>
     37 #include <sys/socket.h>
     38 #include <netinet/in.h>
     39 #include <netinet/tcp.h>
     40 #include <arpa/inet.h>
     41 #include <netdb.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 #include <stdlib.h>
     45 #include <stdio.h>
     46 #include <fcntl.h>
     47 #include <errno.h>
     48 
     49 #include "libmicro.h"
     50 
     51 #define	FIRSTPORT		12345
     52 
     53 static struct sockaddr_in	adds;
     54 static int			sock = -1;
     55 
     56 int
     57 benchmark_init()
     58 {
     59 	(void) sprintf(lm_usage, "notes: measures listen()()\n");
     60 
     61 	lm_tsdsize = 0;
     62 
     63 	return (0);
     64 }
     65 
     66 int
     67 benchmark_initrun()
     68 {
     69 	int			j;
     70 	int			opt = 1;
     71 	struct hostent	*host;
     72 
     73 	sock = socket(AF_INET, SOCK_STREAM, 0);
     74 	if (sock == -1) {
     75 		perror("socket");
     76 		exit(1);
     77 	}
     78 
     79 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
     80 	    &opt, sizeof (int)) == -1) {
     81 		perror("setsockopt");
     82 		exit(1);
     83 	}
     84 
     85 	if ((host = gethostbyname("localhost")) == NULL) {
     86 		perror("gethostbyname");
     87 		exit(1);
     88 	}
     89 
     90 	j = FIRSTPORT;
     91 	for (;;) {
     92 		(void) memset(&adds, 0, sizeof (struct sockaddr_in));
     93 		adds.sin_family = AF_INET;
     94 		adds.sin_port = htons(j++);
     95 		(void) memcpy(&adds.sin_addr.s_addr, host->h_addr_list[0],
     96 		    sizeof (struct in_addr));
     97 
     98 		if (bind(sock, (struct sockaddr *)&adds,
     99 		    sizeof (struct sockaddr_in)) == 0) {
    100 			break;
    101 		}
    102 
    103 		if (errno != EADDRINUSE) {
    104 			perror("bind");
    105 			exit(1);
    106 		}
    107 	}
    108 
    109 	return (0);
    110 }
    111 
    112 /*ARGSUSED*/
    113 int
    114 benchmark(void *tsd, result_t *res)
    115 {
    116 	int			i;
    117 
    118 	for (i = 0; i < lm_optB; i += 2) {
    119 		if (listen(sock, 4) == -1)
    120 			res->re_errors++;
    121 		if (listen(sock, 5) == -1)
    122 			res->re_errors++;
    123 	}
    124 	res->re_count = i;
    125 
    126 	return (0);
    127 }
    128