2 * Copyright 2002 Silicon Graphics, Inc. ALL RIGHTS RESERVED
4 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
6 * Use, duplication or disclosure by the Government is subject to restrictions
7 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the
8 * Rights in Technical Data and Computer Software clause at DFARS 252.227-7013
9 * and/or in similar or successor clauses in the FAR, or the DOD or NASA FAR
10 * Supplement. Unpublished -- rights reserved under the Copyright Laws
11 * of the United States. Contractor/manufacturer is Silicon Graphics, Inc.,
12 * 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
14 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
16 * The copyright notice above does not evidence any actual or intended
17 * publication or disclosure of this source code, which includes information
18 * that is the confidential and/or proprietary, and is a trade secret,
19 * of Silicon Graphics, Inc. Any use, duplication or disclosure not
20 * specifically authorized in writing by Silicon Graphics is strictly
21 * prohibited. ANY DUPLICATION, MODIFICATION, DISTRIBUTION,PUBLIC PERFORMANCE,
22 * OR PUBLIC DISPLAY OF THIS SOURCE CODE WITHOUT THE EXPRESS WRITTEN CONSENT
23 * OF SILICON GRAPHICS, INC. IS STRICTLY PROHIBITED. THE RECEIPT OR POSSESSION
24 * OF THIS SOURCE CODE AND/OR INFORMATION DOES NOT CONVEY ANY RIGHTS
25 * TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE,
26 * OR SELL ANYTHING THAT IT MAY DESCRIBE, IN WHOLE OR IN PART.
38 #include <sys/socket.h>
41 #include <sys/types.h>
47 #define PERROR(a,b) perror(a)
48 #define GET_LAST_ERROR errno
51 #define INVALID_HANDLE -1
52 #define TRUNCATE_ERROR -1
53 #define FLUSH_ERROR EOF
55 #define FILE_BEGIN SEEK_SET
56 #define FILE_CURRENT SEEK_CUR
58 #define OPEN(N, F) open(N, O_CREAT|O_RDWR|F, 0644); fp = fdopen(f, "r+")
59 #define SEEK(H, O, F) (lseek(H, O, F))
60 #define READ(H, B, L) (read(H, B, L))
61 #define WRITE(H, B, L) (write(H, B, L))
62 #define CLOSE(H) (close(H))
63 #define DELETE_FILE(F) (unlink(F))
64 #define FLUSH(F) (fflush(fp))
65 #define TRUNCATE(F) (ftruncate(F, 0))
67 #define ALLOC_ALIGNED(S) (memalign(65536, S))
68 #define FREE_ALIGNED(P) (free(P))
70 #define DIRECT_IO_FLAG O_DIRECT
88 "Usage: %s [switches] <filename>\n"
89 " -i <count> = repeat count (default forever)\n"
95 " -b <size> = buffer size\n"
108 main(int argc, char *argv[])
110 HANDLE f = INVALID_HANDLE;
120 while ((c = getopt(argc, argv, "i:orwb:svthfFDd?")) != EOF) {
123 count = atoi(optarg);
126 flags |= FLAG_OPENCLOSE;
135 flags |= FLAG_TRUNCATE;
138 flags |= FLAG_VERBOSE;
141 bufsize = atoi(optarg);
144 flags |= FLAG_SEQUENTIAL;
150 flags |= FLAG_DIRECT;
153 flags |= FLAG_DELETE;
162 if (optind != argc - 1) {
167 filename = argv[optind];
169 fprintf(stderr, "nothing to do!\n");
173 if (flags & FLAG_DIRECT)
174 buf = (char *)ALLOC_ALIGNED(bufsize);
176 buf = (char *)malloc(bufsize);
179 PERROR("malloc", GET_LAST_ERROR);
181 for (i = 0; i < bufsize; i++) {
185 for (i = 0; count < 0 || i < count; i++) {
187 if ((flags & FLAG_OPENCLOSE) || !i) {
190 if (flags & FLAG_VERBOSE)
191 printf("open %s\n", filename);
194 if (flags & FLAG_DIRECT)
195 fileflags |= DIRECT_IO_FLAG;
197 f = OPEN(filename, fileflags);
198 if (f == INVALID_HANDLE)
199 PERROR("OPEN", GET_LAST_ERROR);
203 if ((flags & FLAG_OPENCLOSE) && (flags & FLAG_SEQUENTIAL)) {
204 if (flags & FLAG_VERBOSE)
205 printf("seek %" LL "d\n", seek_to);
206 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
207 PERROR("SEEK", GET_LAST_ERROR);
210 if (flags & FLAG_WRITE) {
213 if (!(flags & FLAG_SEQUENTIAL)) {
214 if (flags & FLAG_VERBOSE)
215 printf("seek %" LL "d\n", seek_to);
216 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
217 PERROR("SEEK", GET_LAST_ERROR);
220 if (flags & FLAG_VERBOSE)
221 printf("write %d\n", bufsize);
222 if ((sizewritten = WRITE(f, buf, bufsize)) != bufsize) {
224 PERROR("WRITE", GET_LAST_ERROR);
226 fprintf(stderr, "short write: %d of %d\n",
227 sizewritten, bufsize);
231 if (flags & FLAG_READ) {
234 if (!(flags & FLAG_SEQUENTIAL) || (flags & FLAG_WRITE)) {
235 if (flags & FLAG_VERBOSE)
236 printf("seek %" LL "d\n", seek_to);
237 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
238 PERROR("SEEK", GET_LAST_ERROR);
241 if (flags & FLAG_VERBOSE)
242 printf("read %d\n", bufsize);
243 if ((sizeread = READ(f, buf, bufsize)) != bufsize) {
245 PERROR("READ", GET_LAST_ERROR);
247 fprintf(stderr, "short read: %d of %d\n",
250 fprintf(stderr, "Read past EOF\n");
256 if (flags & FLAG_TRUNCATE) {
257 if (flags & FLAG_VERBOSE)
260 if (SEEK(f, 0, FILE_BEGIN) < 0)
261 PERROR("SEEK", GET_LAST_ERROR);
263 if (flags & FLAG_VERBOSE)
264 printf("truncate\n");
266 if (TRUNCATE(f) == TRUNCATE_ERROR)
267 PERROR("TRUNCATE", GET_LAST_ERROR);
270 if (flags & FLAG_FLUSH) {
271 if (flags & FLAG_VERBOSE)
274 if (FLUSH(f) == FLUSH_ERROR)
275 PERROR("FLUSH", GET_LAST_ERROR);
278 if (flags & FLAG_SEQUENTIAL) {
280 if (flags & FLAG_TRUNCATE) {
281 if (flags & FLAG_VERBOSE)
282 printf("seek %" LL "d\n", seek_to);
283 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
284 PERROR("SEEK", GET_LAST_ERROR);
288 if (flags & FLAG_OPENCLOSE) {
289 if (flags & FLAG_VERBOSE)
290 printf("close %s\n", filename);
294 if (flags & FLAG_DELETE) {
295 if (flags & FLAG_VERBOSE)
296 printf("delete %s\n", filename);
297 DELETE_FILE(filename);
302 if (flags & FLAG_DIRECT)