2 * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Author: Avadis Tevanian, Jr.
25 * File system exerciser.
27 * Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com
29 * Various features from Joe Sokol, Pat Dirks, and Clark Warner.
31 * Small changes to work under Linux -- davej@suse.de
33 * Sundry porting patches from Guy Harris 12/2001
34 * $FreeBSD: src/tools/regression/fsx/fsx.c,v 1.1 2001/12/20 04:15:57 jkh Exp $
37 #include <sys/types.h>
39 #if defined(_UWIN) || defined(__linux__)
40 # include <sys/param.h>
44 # include <sys/time.h>
60 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
63 * A log entry is an operation and a bunch of arguments.
74 struct log_entry oplog[LOGSIZE]; /* the log */
75 int logptr = 0; /* current position in log */
76 int logcount = 0; /* total ops */
85 #define OP_CLOSEOPEN 4
93 char *original_buf; /* a pointer to the original data */
94 char *good_buf; /* a pointer to the correct data */
95 char *temp_buf; /* a pointer to the current data */
96 char *fname; /* name of our test file */
97 char logfile[1024]; /* name of our log file */
98 char goodfile[1024]; /* name of our test file */
99 int fd; /* fd for our test file */
104 unsigned long testcalls = 0; /* calls to function "test" */
106 unsigned long simulatedopcount = 0; /* -b flag */
107 int closeprob = 0; /* -c flag */
108 int debug = 0; /* -d flag */
109 unsigned long debugstart = 0; /* -D flag */
110 unsigned long maxfilelen = 256 * 1024; /* -l flag */
111 int sizechecks = 1; /* -n flag disables them */
112 int maxoplen = 64 * 1024; /* -o flag */
113 int quiet = 0; /* -q flag */
114 unsigned long progressinterval = 0; /* -p flag */
115 int readbdy = 1; /* -r flag */
116 int style = 0; /* -s flag */
117 int truncbdy = 1; /* -t flag */
118 int writebdy = 1; /* -w flag */
119 long monitorstart = -1; /* -m flag */
120 long monitorend = -1; /* -m flag */
121 int lite = 0; /* -L flag */
122 long numops = -1; /* -N flag */
123 int randomoplen = 1; /* -O flag disables it */
124 int seed = 1; /* -S flag */
125 int mapped_writes = 1; /* -W flag disables */
126 int mapped_reads = 1; /* -R flag disables it */
128 FILE * fsxlogf = NULL;
134 vwarnc(code, fmt, ap)
139 fprintf(stderr, "fsx: ");
141 vfprintf(stderr, fmt, ap);
142 fprintf(stderr, ": ");
144 fprintf(stderr, "%s\n", strerror(code));
149 warn(const char * fmt, ...)
153 vwarnc(errno, fmt, ap);
164 vfprintf(stdout, fmt, args);
166 vfprintf(fsxlogf, fmt, args);
173 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
178 log4(int operation, int arg0, int arg1, int arg2, struct timeval *tv)
180 struct log_entry *le;
184 le->operation = operation;
186 le->operation = ~ le->operation;
192 if (logptr >= LOGSIZE)
201 struct log_entry *lp;
203 prt("LOG DUMP (%d total operations):\n", logcount);
204 if (logcount < LOGSIZE) {
211 for ( ; count > 0; count--) {
214 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
216 prt("%d(%d mod 256): %lu.%06lu ", opnum, opnum%256,
217 lp->tv.tv_sec, lp->tv.tv_usec);
218 if ((closeopen = lp->operation < 0))
219 lp->operation = ~ lp->operation;
221 switch (lp->operation) {
223 prt("MAPREAD\t0x%x thru 0x%x (0x%x bytes)",
224 lp->args[0], lp->args[0] + lp->args[1] - 1,
226 if (badoff >= lp->args[0] && badoff <
227 lp->args[0] + lp->args[1])
231 prt("MAPWRITE 0x%x thru 0x%x (0x%x bytes)",
232 lp->args[0], lp->args[0] + lp->args[1] - 1,
234 if (badoff >= lp->args[0] && badoff <
235 lp->args[0] + lp->args[1])
239 prt("READ\t0x%x thru 0x%x (0x%x bytes)",
240 lp->args[0], lp->args[0] + lp->args[1] - 1,
242 if (badoff >= lp->args[0] &&
243 badoff < lp->args[0] + lp->args[1])
247 prt("WRITE\t0x%x thru 0x%x (0x%x bytes)",
248 lp->args[0], lp->args[0] + lp->args[1] - 1,
250 if (lp->args[0] > lp->args[2])
252 else if (lp->args[0] + lp->args[1] > lp->args[2])
254 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
255 badoff < lp->args[0] + lp->args[1])
259 down = lp->args[0] < lp->args[1];
260 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
261 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
262 if (badoff >= lp->args[!down] &&
263 badoff < lp->args[!!down])
267 prt("SKIPPED (no operation)");
270 prt("BOGUS LOG ENTRY (operation code = %d)!",
274 prt("\n\t\tCLOSE/OPEN");
284 save_buffer(char *buffer, off_t bufferlength, int fd)
287 ssize_t byteswritten;
289 if (fd <= 0 || bufferlength == 0)
292 if (bufferlength > SSIZE_MAX) {
293 prt("fsx flaw: overflow in save_buffer\n");
297 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
298 if (size_by_seek == (off_t)-1)
299 prterr("save_buffer: lseek eof");
300 else if (bufferlength > size_by_seek) {
301 warn("save_buffer: .fsxgood file too short... will
302 save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
303 (unsigned long long)bufferlength);
304 bufferlength = size_by_seek;
308 ret = lseek(fd, (off_t)0, SEEK_SET);
309 if (ret == (off_t)-1)
310 prterr("save_buffer: lseek 0");
312 byteswritten = write(fd, buffer, (size_t)bufferlength);
313 if (byteswritten != bufferlength) {
314 if (byteswritten == -1)
315 prterr("save_buffer write");
317 warn("save_buffer: short write, 0x%x bytes instead
319 (unsigned)byteswritten,
320 (unsigned long long)bufferlength);
326 report_failure(int status)
332 save_buffer(good_buf, file_size, fsxgoodfd);
333 prt("Correct content saved for comparison\n");
334 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
343 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
344 *(((unsigned char *)(cp)) + 1)))
347 check_buffers(unsigned offset, unsigned size)
355 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
356 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
358 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
360 c = good_buf[offset];
364 bad = short_at(&temp_buf[i]);
365 prt("0x%5x\t0x%04x\t0x%04x", offset,
366 short_at(&good_buf[offset]), bad);
367 op = temp_buf[offset & 1 ? i+1 : i];
379 prt("operation# (mod 256) for the bad data
380 may be %u\n", ((unsigned)op & 0xff));
382 prt("operation# (mod 256) for the bad data
383 unknown, check HOLE and EXTEND ops\n");
385 prt("????????????????\n");
397 if (fstat(fd, &statbuf)) {
398 prterr("check_size: fstat");
399 statbuf.st_size = -1;
401 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
402 if (file_size != statbuf.st_size || file_size != size_by_seek) {
403 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
404 (unsigned long long)file_size,
405 (unsigned long long)statbuf.st_size,
406 (unsigned long long)size_by_seek);
413 check_trunc_hack(void)
417 ftruncate(fd, (off_t)0);
418 ftruncate(fd, (off_t)100000);
420 if (statbuf.st_size != (off_t)100000) {
421 prt("no extend on truncate! not posix!\n");
429 doread(unsigned offset, unsigned size)
435 offset -= offset % readbdy;
436 gettimeofday(&t, NULL);
438 if (!quiet && testcalls > simulatedopcount)
439 prt("skipping zero size read\n");
440 log4(OP_SKIPPED, OP_READ, offset, size, &t);
443 if (size + offset > file_size) {
444 if (!quiet && testcalls > simulatedopcount)
445 prt("skipping seek/read past end of file\n");
446 log4(OP_SKIPPED, OP_READ, offset, size, &t);
450 log4(OP_READ, offset, size, 0, &t);
452 if (testcalls <= simulatedopcount)
455 if (!quiet && ((progressinterval &&
456 testcalls % progressinterval == 0) ||
458 (monitorstart == -1 ||
459 (offset + size > monitorstart &&
460 (monitorend == -1 || offset <= monitorend))))))
461 prt("%06lu %lu.%06lu read %#08x thru %#08x\t(0x%x bytes)\n",
462 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
464 ret = lseek(fd, (off_t)offset, SEEK_SET);
465 if (ret == (off_t)-1) {
466 prterr("doread: lseek");
469 iret = read(fd, temp_buf, size);
470 if (!quiet && (debug > 1 &&
471 (monitorstart == -1 ||
472 (offset + size > monitorstart &&
473 (monitorend == -1 || offset <= monitorend))))) {
474 gettimeofday(&t, NULL);
475 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
479 prterr("doread: read");
481 prt("short read: 0x%x bytes instead of 0x%x\n",
485 check_buffers(offset, size);
490 domapread(unsigned offset, unsigned size)
497 offset -= offset % readbdy;
498 gettimeofday(&t, NULL);
500 if (!quiet && testcalls > simulatedopcount)
501 prt("skipping zero size read\n");
502 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
505 if (size + offset > file_size) {
506 if (!quiet && testcalls > simulatedopcount)
507 prt("skipping seek/read past end of file\n");
508 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
512 log4(OP_MAPREAD, offset, size, 0, &t);
514 if (testcalls <= simulatedopcount)
517 if (!quiet && ((progressinterval &&
518 testcalls % progressinterval == 0) ||
520 (monitorstart == -1 ||
521 (offset + size > monitorstart &&
522 (monitorend == -1 || offset <= monitorend))))))
523 prt("%06lu %lu.%06lu mapread %#08x thru %#08x\t(0x%x bytes)\n",
524 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
527 pg_offset = offset & page_mask;
528 map_size = pg_offset + size;
530 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
531 (off_t)(offset - pg_offset))) == (char *)-1) {
532 prterr("domapread: mmap");
535 if (!quiet && (debug > 1 &&
536 (monitorstart == -1 ||
537 (offset + size > monitorstart &&
538 (monitorend == -1 || offset <= monitorend))))) {
539 gettimeofday(&t, NULL);
540 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
542 memcpy(temp_buf, p + pg_offset, size);
543 if (!quiet && (debug > 1 &&
544 (monitorstart == -1 ||
545 (offset + size > monitorstart &&
546 (monitorend == -1 || offset <= monitorend))))) {
547 gettimeofday(&t, NULL);
548 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
550 if (munmap(p, map_size) != 0) {
551 prterr("domapread: munmap");
554 if (!quiet && (debug > 1 &&
555 (monitorstart == -1 ||
556 (offset + size > monitorstart &&
557 (monitorend == -1 || offset <= monitorend))))) {
558 gettimeofday(&t, NULL);
559 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
562 check_buffers(offset, size);
567 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
570 good_buf[offset] = testcalls % 256;
572 good_buf[offset] += original_buf[offset];
579 dowrite(unsigned offset, unsigned size)
585 offset -= offset % writebdy;
586 gettimeofday(&t, NULL);
588 if (!quiet && testcalls > simulatedopcount)
589 prt("skipping zero size write\n");
590 log4(OP_SKIPPED, OP_WRITE, offset, size, &t);
594 log4(OP_WRITE, offset, size, file_size, &t);
596 gendata(original_buf, good_buf, offset, size);
597 if (file_size < offset + size) {
598 if (file_size < offset)
599 memset(good_buf + file_size, '\0', offset - file_size);
600 file_size = offset + size;
602 warn("Lite file size bug in fsx!");
607 if (testcalls <= simulatedopcount)
610 if (!quiet && ((progressinterval &&
611 testcalls % progressinterval == 0) ||
613 (monitorstart == -1 ||
614 (offset + size > monitorstart &&
615 (monitorend == -1 || offset <= monitorend))))))
616 prt("%06lu %lu.%06lu write %#08x thru %#08x\t(0x%x bytes)\n",
617 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
619 ret = lseek(fd, (off_t)offset, SEEK_SET);
620 if (ret == (off_t)-1) {
621 prterr("dowrite: lseek");
624 iret = write(fd, good_buf + offset, size);
625 if (!quiet && (debug > 1 &&
626 (monitorstart == -1 ||
627 (offset + size > monitorstart &&
628 (monitorend == -1 || offset <= monitorend))))) {
629 gettimeofday(&t, NULL);
630 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
634 prterr("dowrite: write");
636 prt("short write: 0x%x bytes instead of 0x%x\n",
644 domapwrite(unsigned offset, unsigned size)
652 offset -= offset % writebdy;
653 gettimeofday(&t, NULL);
655 if (!quiet && testcalls > simulatedopcount)
656 prt("skipping zero size write\n");
657 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t);
660 cur_filesize = file_size;
662 log4(OP_MAPWRITE, offset, size, 0, &t);
664 gendata(original_buf, good_buf, offset, size);
665 if (file_size < offset + size) {
666 if (file_size < offset)
667 memset(good_buf + file_size, '\0', offset - file_size);
668 file_size = offset + size;
670 warn("Lite file size bug in fsx!");
675 if (testcalls <= simulatedopcount)
678 if (!quiet && ((progressinterval &&
679 testcalls % progressinterval == 0) ||
681 (monitorstart == -1 ||
682 (offset + size > monitorstart &&
683 (monitorend == -1 || offset <= monitorend))))))
684 prt("%06lu %lu.%06lu mapwrite %#08x thru %#08x\t(0x%x bytes)\n",
685 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
688 if (file_size > cur_filesize) {
689 if (ftruncate(fd, file_size) == -1) {
690 prterr("domapwrite: ftruncate");
693 if (!quiet && (debug > 1 &&
694 (monitorstart == -1 ||
695 (offset + size > monitorstart &&
696 (monitorend == -1 || offset <= monitorend))))) {
697 gettimeofday(&t, NULL);
698 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
701 pg_offset = offset & page_mask;
702 map_size = pg_offset + size;
704 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
705 MAP_FILE | MAP_SHARED, fd,
706 (off_t)(offset - pg_offset))) == (char *)-1) {
707 prterr("domapwrite: mmap");
710 if (!quiet && (debug > 1 &&
711 (monitorstart == -1 ||
712 (offset + size > monitorstart &&
713 (monitorend == -1 || offset <= monitorend))))) {
714 gettimeofday(&t, NULL);
715 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
717 memcpy(p + pg_offset, good_buf + offset, size);
718 if (!quiet && (debug > 1 &&
719 (monitorstart == -1 ||
720 (offset + size > monitorstart &&
721 (monitorend == -1 || offset <= monitorend))))) {
722 gettimeofday(&t, NULL);
723 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
725 if (msync(p, map_size, 0) != 0) {
726 prterr("domapwrite: msync");
729 if (!quiet && (debug > 1 &&
730 (monitorstart == -1 ||
731 (offset + size > monitorstart &&
732 (monitorend == -1 || offset <= monitorend))))) {
733 gettimeofday(&t, NULL);
734 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
736 if (munmap(p, map_size) != 0) {
737 prterr("domapwrite: munmap");
740 if (!quiet && (debug > 1 &&
741 (monitorstart == -1 ||
742 (offset + size > monitorstart &&
743 (monitorend == -1 || offset <= monitorend))))) {
744 gettimeofday(&t, NULL);
745 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
751 dotruncate(unsigned size)
754 int oldsize = file_size;
756 size -= size % truncbdy;
757 gettimeofday(&t, NULL);
758 if (size > biggest) {
760 if (!quiet && testcalls > simulatedopcount)
761 prt("truncating to largest ever: 0x%x\n", size);
764 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t);
766 if (size > file_size)
767 memset(good_buf + file_size, '\0', size - file_size);
770 if (testcalls <= simulatedopcount)
773 if ((progressinterval && testcalls % progressinterval == 0) ||
774 (debug && (monitorstart == -1 || monitorend == -1 ||
775 size <= monitorend)))
776 prt("%06lu %lu.%06lu trunc from %#08x to %#08x\n",
777 testcalls, t.tv_sec, t.tv_usec, oldsize, size);
778 if (ftruncate(fd, (off_t)size) == -1) {
779 prt("ftruncate1: %x\n", size);
780 prterr("dotruncate: ftruncate");
783 if (!quiet && debug > 1) {
784 gettimeofday(&t, NULL);
785 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
795 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
796 prterr("writefileimage: lseek");
799 iret = write(fd, good_buf, file_size);
800 if ((off_t)iret != file_size) {
802 prterr("writefileimage: write");
804 prt("short write: 0x%x bytes instead of 0x%llx\n",
805 iret, (unsigned long long)file_size);
808 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
809 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
810 prterr("writefileimage: ftruncate");
821 if (testcalls <= simulatedopcount)
824 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t);
826 gettimeofday(&t, NULL);
828 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
831 prterr("docloseopen: close");
834 if (!quiet && debug > 1) {
835 gettimeofday(&t, NULL);
836 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
838 fd = open(fname, O_RDWR, 0);
840 prterr("docloseopen: open");
843 if (!quiet && debug > 1) {
844 gettimeofday(&t, NULL);
845 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
853 unsigned long offset;
854 unsigned long size = maxoplen;
855 unsigned long rv = random();
856 unsigned long op = rv % (3 + !lite + mapped_writes);
858 /* turn off the map read if necessary */
860 if (op == 2 && !mapped_reads)
863 if (simulatedopcount > 0 && testcalls == simulatedopcount)
869 closeopen = (rv >> 3) < (1 << 28) / closeprob;
871 if (debugstart > 0 && testcalls >= debugstart)
874 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
875 prt("%lu...\n", testcalls);
882 * MAPWRITE: op = 3 or 4
884 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
885 dotruncate(random() % maxfilelen);
888 size = random() % (maxoplen+1);
889 if (lite ? 0 : op == 3)
893 if (op == 1 || op == (lite ? 3 : 4)) {
894 offset %= maxfilelen;
895 if (offset + size > maxfilelen)
896 size = maxfilelen - offset;
898 domapwrite(offset, size);
900 dowrite(offset, size);
906 if (offset + size > file_size)
907 size = file_size - offset;
909 domapread(offset, size);
911 doread(offset, size);
915 if (sizechecks && testcalls > simulatedopcount)
927 prt("signal %d\n", sig);
928 prt("testcalls = %lu\n", testcalls);
936 fprintf(stdout, "usage: %s",
937 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m
938 start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t
939 truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed]
941 -b opnum: beginning operation number (default 1)\n\
942 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
943 -d: debug output for all operations [-d -d = more debugging]\n\
944 -l flen: the upper bound on file size (default 262144)\n\
945 -m startop:endop: monitor (print debug output) specified byte range
946 (default 0:infinity)\n\
947 -n: no verifications of file size\n\
948 -o oplen: the upper bound on operation size (default 65536)\n\
949 -p progressinterval: debug output at specified operation interval\n\
950 -q: quieter operation\n\
951 -r readbdy: 4096 would make reads page aligned (default 1)\n\
952 -s style: 1 gives smaller truncates (default 0)\n\
953 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
954 -w writebdy: 4096 would make writes page aligned (default 1)\n\
955 -D startingop: debug output starting at specified operation\n\
956 -L: fsxLite - no file creations & no file size changes\n\
957 -N numops: total # operations to do (default infinity)\n\
958 -O: use oplen (see -o flag) for every op (default random)\n\
959 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
960 -S seed: for random # generator (default 1) 0 gets timestamp\n\
961 -W: mapped write operations DISabled\n\
962 -R: read() system calls only (mapped reads disabled)\n\
963 fname: this filename is REQUIRED (no default)\n");
969 getnum(char *s, char **e)
974 ret = strtol(s, e, 0);
1002 static const char *basename(const char *path)
1004 char *c = strrchr(path, '/');
1006 return c ? c++ : path;
1010 main(int argc, char **argv)
1019 page_size = getpagesize();
1020 page_mask = page_size - 1;
1022 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1024 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
1028 simulatedopcount = getnum(optarg, &endp);
1030 fprintf(stdout, "Will begin at operation
1033 if (simulatedopcount == 0)
1035 simulatedopcount -= 1;
1038 closeprob = getnum(optarg, &endp);
1041 "Chance of close/open is 1 in %d\n",
1050 maxfilelen = getnum(optarg, &endp);
1051 if (maxfilelen <= 0)
1055 monitorstart = getnum(optarg, &endp);
1056 if (monitorstart < 0)
1058 if (!endp || *endp++ != ':')
1060 monitorend = getnum(endp, &endp);
1063 if (monitorend == 0)
1064 monitorend = -1; /* aka infinity */
1070 maxoplen = getnum(optarg, &endp);
1075 progressinterval = getnum(optarg, &endp);
1076 if (progressinterval < 0)
1083 readbdy = getnum(optarg, &endp);
1088 style = getnum(optarg, &endp);
1089 if (style < 0 || style > 1)
1093 truncbdy = getnum(optarg, &endp);
1098 writebdy = getnum(optarg, &endp);
1103 debugstart = getnum(optarg, &endp);
1111 numops = getnum(optarg, &endp);
1119 strncpy(goodfile, optarg, sizeof(goodfile));
1120 strcat(goodfile, "/");
1121 strncpy(logfile, optarg, sizeof(logfile));
1122 strcat(logfile, "/");
1129 seed = getnum(optarg, &endp);
1131 seed = time(0) % 10000;
1133 fprintf(stdout, "Seed set to %d\n", seed);
1140 fprintf(stdout, "mapped writes DISABLED\n");
1153 signal(SIGHUP, cleanup);
1154 signal(SIGINT, cleanup);
1155 signal(SIGPIPE, cleanup);
1156 signal(SIGALRM, cleanup);
1157 signal(SIGTERM, cleanup);
1158 signal(SIGXCPU, cleanup);
1159 signal(SIGXFSZ, cleanup);
1160 signal(SIGVTALRM, cleanup);
1161 signal(SIGUSR1, cleanup);
1162 signal(SIGUSR2, cleanup);
1164 initstate(seed, state, 256);
1166 fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1171 strncat(goodfile, dirpath ? basename(fname) : fname, 256);
1172 strcat (goodfile, ".fsxgood");
1173 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1174 if (fsxgoodfd < 0) {
1178 strncat(logfile, dirpath ? basename(fname) : fname, 256);
1179 strcat (logfile, ".fsxlog");
1180 fsxlogf = fopen(logfile, "w");
1181 if (fsxlogf == NULL) {
1187 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1188 if (file_size == (off_t)-1) {
1190 warn("main: lseek eof");
1193 ret = lseek(fd, (off_t)0, SEEK_SET);
1194 if (ret == (off_t)-1) {
1196 warn("main: lseek 0");
1200 original_buf = (char *) malloc(maxfilelen);
1201 for (i = 0; i < maxfilelen; i++)
1202 original_buf[i] = random() % 256;
1203 good_buf = (char *) malloc(maxfilelen);
1204 memset(good_buf, '\0', maxfilelen);
1205 temp_buf = (char *) malloc(maxoplen);
1206 memset(temp_buf, '\0', maxoplen);
1207 if (lite) { /* zero entire existing file */
1210 written = write(fd, good_buf, (size_t)maxfilelen);
1211 if (written != maxfilelen) {
1212 if (written == -1) {
1214 warn("main: error on write");
1216 warn("main: short write, 0x%x bytes instead
1218 (unsigned)written, maxfilelen);
1224 while (numops == -1 || numops--)
1231 prt("All operations completed A-OK!\n");