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#ifdef linux
32#define	_XOPEN_SOURCE 500
33#endif
34
35#include <unistd.h>
36#include <stdlib.h>
37#include <stdio.h>
38#include <fcntl.h>
39
40#include "libmicro.h"
41
42typedef struct {
43	char			*ts_buf;
44	int			ts_fd;
45} tsd_t;
46
47#define	DEFF			"/dev/null"
48#define	DEFS			1024
49
50static int			optc = 0;
51static char			*optf = DEFF;
52static long long		opts = DEFS;
53static int			optd;
54
55int
56benchmark_init()
57{
58	lm_tsdsize = sizeof (tsd_t);
59
60	(void) sprintf(lm_optstr, "cdf:s:");
61
62	(void) sprintf(lm_usage,
63	    "       [-f file-to-write (default %s)]\n"
64	    "       [-s buffer-size (default %d)]\n"
65	    "       [-c ] (make sure buffer is in cache)\n"
66#ifdef __sun
67	    "       [-d ] use directio"
68#endif
69	    "notes: measures write()\n",
70	    DEFF, DEFS);
71
72	(void) sprintf(lm_header, "%8s", "size");
73
74	lm_defB = 1;
75
76	return (0);
77}
78
79int
80benchmark_optswitch(int opt, char *optarg)
81{
82	switch (opt) {
83
84	case 'd':
85		optd++;
86		break;
87	case 'c':
88		optc++;
89		break;
90	case 'f':
91		optf = optarg;
92		break;
93	case 's':
94		opts = sizetoll(optarg);
95		break;
96	default:
97		return (-1);
98	}
99	return (0);
100}
101
102int
103benchmark_initbatch(void *tsd)
104{
105	tsd_t			*ts = (tsd_t *)tsd;
106	int			i;
107
108	if (ts->ts_buf == NULL) {
109		ts->ts_buf = malloc(opts);
110		ts->ts_fd = open(optf, O_WRONLY);
111
112#ifdef __sun
113		if (optd)
114			(void) directio(ts->ts_fd, DIRECTIO_ON);
115#endif
116		/*
117		 * bring buf into cache if specified.
118		 */
119
120		if (optc)
121			for (i = 0; i < opts; i++)
122				ts->ts_buf[i] = 0;
123	}
124
125	(void) lseek(ts->ts_fd, 0, SEEK_SET);
126
127	return (0);
128}
129
130int
131benchmark(void *tsd, result_t *res)
132{
133	tsd_t			*ts = (tsd_t *)tsd;
134	int			i;
135
136	for (i = 0; i < lm_optB; i++) {
137		if (write(ts->ts_fd, ts->ts_buf, opts) != opts) {
138			res->re_errors++;
139		}
140	}
141	res->re_count = i;
142
143	return (0);
144}
145
146char *
147benchmark_result()
148{
149	static char		result[256];
150
151	(void) sprintf(result, "%8lld", opts);
152
153	return (result);
154}
155