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.
40 #include <sys/socket.h>
43 #include <sys/types.h>
49 #define PERROR(a,b) perror(a)
50 #define GET_LAST_ERROR errno
53 #define INVALID_HANDLE -1
54 #define TRUNCATE_ERROR -1
55 #define FLUSH_ERROR EOF
57 #define FILE_BEGIN SEEK_SET
58 #define FILE_CURRENT SEEK_CUR
60 #define OPEN(N, F) open(N, O_CREAT|O_RDWR|F, 0644); fp = fdopen(f, "r+")
61 #define SEEK(H, O, F) (lseek(H, O, F))
62 #define READ(H, B, L) (read(H, B, L))
63 #define WRITE(H, B, L) (write(H, B, L))
64 #define CLOSE(H) (close(H))
65 #define DELETE_FILE(F) (unlink(F))
66 #define FLUSH(F) (fflush(fp))
67 #define TRUNCATE(F) (ftruncate(F, 0))
69 #define ALLOC_ALIGNED(S) (memalign(65536, S))
70 #define FREE_ALIGNED(P) (free(P))
72 #define DIRECT_IO_FLAG O_DIRECT
90 "Usage: %s [switches] <filename>\n"
91 " -i <count> = repeat count (default forever)\n"
97 " -b <size> = buffer size\n"
110 main(int argc, char *argv[])
112 HANDLE f = INVALID_HANDLE;
122 while ((c = getopt(argc, argv, "i:orwb:svthfFDd?")) != EOF) {
125 count = atoi(optarg);
128 flags |= FLAG_OPENCLOSE;
137 flags |= FLAG_TRUNCATE;
140 flags |= FLAG_VERBOSE;
143 bufsize = atoi(optarg);
146 flags |= FLAG_SEQUENTIAL;
152 flags |= FLAG_DIRECT;
155 flags |= FLAG_DELETE;
164 if (optind != argc - 1) {
169 filename = argv[optind];
171 fprintf(stderr, "nothing to do!\n");
175 if (flags & FLAG_DIRECT)
176 buf = (char *)ALLOC_ALIGNED(bufsize);
178 buf = (char *)malloc(bufsize);
181 PERROR("malloc", GET_LAST_ERROR);
183 for (i = 0; i < bufsize; i++) {
187 for (i = 0; count < 0 || i < count; i++) {
189 if ((flags & FLAG_OPENCLOSE) || !i) {
192 if (flags & FLAG_VERBOSE)
193 printf("open %s\n", filename);
196 if (flags & FLAG_DIRECT)
197 fileflags |= DIRECT_IO_FLAG;
199 f = OPEN(filename, fileflags);
200 if (f == INVALID_HANDLE)
201 PERROR("OPEN", GET_LAST_ERROR);
205 if ((flags & FLAG_OPENCLOSE) && (flags & FLAG_SEQUENTIAL)) {
206 if (flags & FLAG_VERBOSE)
207 printf("seek %" LL "d\n", seek_to);
208 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
209 PERROR("SEEK", GET_LAST_ERROR);
212 if (flags & FLAG_WRITE) {
215 if (!(flags & FLAG_SEQUENTIAL)) {
216 if (flags & FLAG_VERBOSE)
217 printf("seek %" LL "d\n", seek_to);
218 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
219 PERROR("SEEK", GET_LAST_ERROR);
222 if (flags & FLAG_VERBOSE)
223 printf("write %d\n", bufsize);
224 if ((sizewritten = WRITE(f, buf, bufsize)) != bufsize) {
226 PERROR("WRITE", GET_LAST_ERROR);
228 fprintf(stderr, "short write: %d of %d\n",
229 sizewritten, bufsize);
233 if (flags & FLAG_READ) {
236 if (!(flags & FLAG_SEQUENTIAL) || (flags & FLAG_WRITE)) {
237 if (flags & FLAG_VERBOSE)
238 printf("seek %" LL "d\n", seek_to);
239 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
240 PERROR("SEEK", GET_LAST_ERROR);
243 if (flags & FLAG_VERBOSE)
244 printf("read %d\n", bufsize);
245 if ((sizeread = READ(f, buf, bufsize)) != bufsize) {
247 PERROR("READ", GET_LAST_ERROR);
249 fprintf(stderr, "short read: %d of %d\n",
252 fprintf(stderr, "Read past EOF\n");
258 if (flags & FLAG_TRUNCATE) {
259 if (flags & FLAG_VERBOSE)
262 if (SEEK(f, 0, FILE_BEGIN) < 0)
263 PERROR("SEEK", GET_LAST_ERROR);
265 if (flags & FLAG_VERBOSE)
266 printf("truncate\n");
268 if (TRUNCATE(f) == TRUNCATE_ERROR)
269 PERROR("TRUNCATE", GET_LAST_ERROR);
272 if (flags & FLAG_FLUSH) {
273 if (flags & FLAG_VERBOSE)
276 if (FLUSH(f) == FLUSH_ERROR)
277 PERROR("FLUSH", GET_LAST_ERROR);
280 if (flags & FLAG_SEQUENTIAL) {
282 if (flags & FLAG_TRUNCATE) {
283 if (flags & FLAG_VERBOSE)
284 printf("seek %" LL "d\n", seek_to);
285 if (SEEK(f, seek_to, FILE_BEGIN) < 0)
286 PERROR("SEEK", GET_LAST_ERROR);
290 if (flags & FLAG_OPENCLOSE) {
291 if (flags & FLAG_VERBOSE)
292 printf("close %s\n", filename);
296 if (flags & FLAG_DELETE) {
297 if (flags & FLAG_VERBOSE)
298 printf("delete %s\n", filename);
299 DELETE_FILE(filename);
304 if (flags & FLAG_DIRECT)