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 * benchmark for close
33 */
34
35#include <unistd.h>
36#include <stdlib.h>
37#include <stdio.h>
38#include <fcntl.h>
39
40#include "libmicro.h"
41
42#define	DEFF			"/dev/null"
43static char			*optf = DEFF;
44static int 			optb = 0;
45
46typedef struct {
47	int			*ts_fds;
48} tsd_t;
49
50int
51benchmark_init()
52{
53	lm_tsdsize = sizeof (tsd_t);
54
55	lm_defB = 256;
56
57	(void) sprintf(lm_optstr, "f:b");
58
59	(void) sprintf(lm_usage,
60	    "       [-f file-to-close (default %s)]\n"
61	    "       [-b] (try to close an unopened fd)\n"
62	    "notes: measures close()",
63	    DEFF);
64
65	return (0);
66}
67
68int
69benchmark_optswitch(int opt, char *optarg)
70{
71	switch (opt) {
72	case 'f':
73		optf = optarg;
74		break;
75	case 'b':
76		optb = 1;
77		break;
78	default:
79		return (-1);
80	}
81
82	return (0);
83}
84
85int
86benchmark_initrun()
87{
88	(void) setfdlimit(lm_optB * lm_optT + 10);
89
90	return (0);
91}
92
93int
94benchmark_initworker(void *tsd)
95{
96	tsd_t			*ts = (tsd_t *)tsd;
97
98	ts->ts_fds = (int *)malloc(lm_optB * sizeof (int));
99	if (ts->ts_fds == NULL) {
100		return (1);
101	}
102	return (0);
103}
104
105/*
106 * don't need a finiworker; we're exiting anyway
107 */
108
109int
110benchmark_initbatch(void *tsd)
111{
112	tsd_t			*ts = (tsd_t *)tsd;
113	int			i;
114	int			errors = 0;
115
116	for (i = 0; i < lm_optB; i++) {
117		ts->ts_fds[i] = ((optb == 0) ?
118		    open(optf, O_RDONLY) : i + 1024);
119		if (ts->ts_fds[i] == -1) {
120			errors++;
121		}
122	}
123
124	return (errors);
125}
126
127int
128benchmark(void *tsd, result_t *res)
129{
130	tsd_t			*ts = (tsd_t *)tsd;
131	int			i;
132
133	for (i = 0; i < lm_optB; i++) {
134		if (close(ts->ts_fds[i]) == -1 && !optb) {
135			res->re_errors++;
136		}
137	}
138	res->re_count = i;
139
140	return (0);
141}
142