4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include "file_common.h"
31 #include <sys/types.h>
37 static unsigned char bigbuffer
[BIGBUFFERSIZE
];
40 * Writes (or appends) a given value to a file repeatedly.
41 * See header file for defaults.
44 static void usage(char *);
47 * pseudo-randomize the buffer
49 static void randomize_buffer(int block_size
) {
51 char rnd
= rand() & 0xff;
52 for (i
= 0; i
< block_size
; i
++)
57 main(int argc
, char **argv
)
65 int64_t good_writes
= 0;
71 int write_count
= BIGFILESIZE
;
72 uchar_t fillchar
= DATA
;
73 int block_size
= BLOCKSZ
;
74 char *filename
= NULL
;
75 char *operation
= NULL
;
76 offset_t noffset
, offset
= 0;
84 while ((c
= getopt(argc
, argv
, "b:c:d:s:f:o:vwr")) != -1) {
87 block_size
= atoi(optarg
);
90 write_count
= atoi(optarg
);
94 fillchar
= 'R'; /* R = random data */
96 fillchar
= atoi(optarg
);
99 offset
= atoll(optarg
);
117 (void) printf("unknown arg %c\n", optopt
);
124 * Validate Parameters
127 (void) printf("Filename not specified (-f <file>)\n");
132 (void) printf("Operation not specified (-o <operation>).\n");
136 if (block_size
> BIGBUFFERSIZE
) {
137 (void) printf("block_size is too large max==%d.\n",
143 usage(prog
); /* no return */
148 * Prepare the buffer and determine the requested operation
150 nxtfillchar
= fillchar
;
156 for (i
= 0; i
< block_size
; i
++) {
157 bigbuffer
[i
] = nxtfillchar
;
160 if ((k
% DATA_RANGE
) == 0) {
164 } else if (fillchar
== 'R') {
165 nxtfillchar
= rand() & 0xff;
170 * using the strncmp of operation will make the operation match the
171 * first shortest match - as the operations are unique from the first
172 * character this means that we match single character operations
174 if ((strncmp(operation
, "create", strlen(operation
) + 1)) == 0 ||
175 (strncmp(operation
, "overwrite", strlen(operation
) + 1)) == 0) {
176 oflag
= (O_RDWR
|O_CREAT
);
177 } else if ((strncmp(operation
, "append", strlen(operation
) + 1)) == 0) {
178 oflag
= (O_RDWR
|O_APPEND
);
180 (void) printf("valid operations are <create|append> not '%s'\n",
186 oflag
= oflag
| O_RSYNC
;
190 oflag
= oflag
| O_SYNC
;
194 * Given an operation (create/overwrite/append), open the file
195 * accordingly and perform a write of the appropriate type.
197 if ((bigfd
= open(filename
, oflag
, 0666)) == -1) {
198 (void) printf("open %s: failed [%s]%d. Aborting!\n", filename
,
199 strerror(errno
), errno
);
202 noffset
= lseek64(bigfd
, offset
, SEEK_SET
);
203 if (noffset
!= offset
) {
204 (void) printf("llseek %s (%lld/%lld) failed [%s]%d.Aborting!\n",
205 filename
, offset
, noffset
, strerror(errno
), errno
);
210 (void) printf("%s: block_size = %d, write_count = %d, "
211 "offset = %lld, ", filename
, block_size
,
212 write_count
, offset
);
213 if (fillchar
== 'R') {
214 (void) printf("data = [random]\n");
216 (void) printf("data = %s%d\n",
217 (fillchar
== 0) ? "0->" : "",
218 (fillchar
== 0) ? DATA_RANGE
: fillchar
);
222 for (i
= 0; i
< write_count
; i
++) {
225 randomize_buffer(block_size
);
227 if ((n
= write(bigfd
, &bigbuffer
, block_size
)) == -1) {
228 (void) printf("write failed (%ld), good_writes = %"
229 PRId64
", " "error: %s[%d]\n",
230 (long)n
, good_writes
,
239 (void) printf("Success: good_writes = %" PRId64
"(%"
240 PRId64
")\n", good_writes
, (good_writes
* block_size
));
249 (void) printf("Usage: %s [-v] -o {create,overwrite,append} -f file_name"
251 "\t[-s offset] [-c write_count] [-d data]\n\n"
252 "Where [data] equal to zero causes chars "
253 "0->%d to be repeated throughout, or [data]\n"
254 "equal to 'R' for pseudorandom data.\n",