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#include <unistd.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <sys/socket.h>
36
37#include "libmicro.h"
38
39typedef struct {
40	int			ts_once;
41	int			*ts_fds;
42} tsd_t;
43
44#define	DEFF		"PF_UNIX"
45
46static char			*optf = DEFF;
47static int			family;
48
49int
50lookup_family(char *name)
51{
52	if (strcmp("PF_UNIX", name) == 0) {
53		return (PF_UNIX);
54	} else if (strcmp("PF_INET", name) == 0) {
55		return (PF_INET);
56	} else if (strcmp("PF_INET6", name) == 0) {
57		return (PF_INET6);
58	}
59
60	return (-1);
61}
62
63int
64benchmark_init()
65{
66	lm_tsdsize = sizeof (tsd_t);
67
68	lm_defB = 256;
69
70	(void) sprintf(lm_optstr, "f:n");
71
72	(void) sprintf(lm_usage,
73	    "       [-f socket-family (default %s)]\n"
74	    "notes: measures socket\n",
75	    DEFF);
76
77	return (0);
78}
79
80int
81benchmark_optswitch(int opt, char *optarg)
82{
83	switch (opt) {
84	case 'f':
85		optf = optarg;
86		break;
87	default:
88		return (-1);
89	}
90
91	return (0);
92}
93
94
95int
96benchmark_initrun()
97{
98	(void) setfdlimit(lm_optB * lm_optT + 10);
99	family = lookup_family(optf);
100
101	return (0);
102}
103
104int
105benchmark_finirun()
106{
107	return (0);
108}
109
110int
111benchmark_initbatch(void *tsd)
112{
113	int			i;
114	tsd_t			*ts = (tsd_t *)tsd;
115
116	if (ts->ts_once++ == 0) {
117		ts->ts_fds = (int *)malloc(lm_optB * sizeof (int));
118		if (ts->ts_fds == NULL) {
119			return (1);
120		}
121		for (i = 0; i < lm_optB; i++) {
122			ts->ts_fds[i] = -1;
123		}
124	}
125
126	return (0);
127}
128
129int
130benchmark(void *tsd, result_t *res)
131{
132	int			i;
133	tsd_t			*ts = (tsd_t *)tsd;
134
135	for (i = 0; i < lm_optB; i++) {
136		ts->ts_fds[i] = socket(family, SOCK_STREAM, 0);
137		if (ts->ts_fds[i] == -1) {
138			res->re_errors++;
139		}
140	}
141	res->re_count += lm_optB;
142
143	return (0);
144}
145
146int
147benchmark_finibatch(void *tsd)
148{
149	int			i;
150	tsd_t			*ts = (tsd_t *)tsd;
151
152	for (i = 0; i < lm_optB; i++) {
153		(void) close(ts->ts_fds[i]);
154	}
155
156	return (0);
157}
158