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;
133 vwarnc(code, fmt, ap)
138 fprintf(stderr, "fsx: ");
140 vfprintf(stderr, fmt, ap);
141 fprintf(stderr, ": ");
143 fprintf(stderr, "%s\n", strerror(code));
148 warn(const char * fmt, ...)
152 vwarnc(errno, fmt, ap);
163 vfprintf(stdout, fmt, args);
165 vfprintf(fsxlogf, fmt, args);
172 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
177 log4(int operation, int arg0, int arg1, int arg2, struct timeval *tv)
179 struct log_entry *le;
183 le->operation = operation;
189 if (logptr >= LOGSIZE)
198 struct log_entry *lp;
200 prt("LOG DUMP (%d total operations):\n", logcount);
201 if (logcount < LOGSIZE) {
208 for ( ; count > 0; count--) {
211 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
213 prt("%d: %lu.%06lu ", opnum,
214 lp->tv.tv_sec, lp->tv.tv_usec);
216 switch (lp->operation) {
218 prt("MAPREAD 0x%x thru 0x%x (0x%x bytes)",
219 lp->args[0], lp->args[0] + lp->args[1] - 1,
221 if (badoff >= lp->args[0] && badoff <
222 lp->args[0] + lp->args[1])
226 prt("MAPWRITE 0x%x thru 0x%x (0x%x bytes)",
227 lp->args[0], lp->args[0] + lp->args[1] - 1,
229 if (badoff >= lp->args[0] && badoff <
230 lp->args[0] + lp->args[1])
234 prt("READ 0x%x thru 0x%x (0x%x bytes)",
235 lp->args[0], lp->args[0] + lp->args[1] - 1,
237 if (badoff >= lp->args[0] &&
238 badoff < lp->args[0] + lp->args[1])
242 prt("WRITE 0x%x thru 0x%x (0x%x bytes)",
243 lp->args[0], lp->args[0] + lp->args[1] - 1,
245 if (lp->args[0] > lp->args[2])
247 else if (lp->args[0] + lp->args[1] > lp->args[2])
249 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
250 badoff < lp->args[0] + lp->args[1])
254 down = lp->args[0] < lp->args[1];
255 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
256 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
257 if (badoff >= lp->args[!down] &&
258 badoff < lp->args[!!down])
265 prt("SKIPPED (no operation)");
268 prt("BOGUS LOG ENTRY (operation code = %d)!",
280 save_buffer(char *buffer, off_t bufferlength, int fd)
283 ssize_t byteswritten;
285 if (fd <= 0 || bufferlength == 0)
288 if (bufferlength > SSIZE_MAX) {
289 prt("fsx flaw: overflow in save_buffer\n");
293 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
294 if (size_by_seek == (off_t)-1)
295 prterr("save_buffer: lseek eof");
296 else if (bufferlength > size_by_seek) {
297 warn("save_buffer: .fsxgood file too short... will
298 save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
299 (unsigned long long)bufferlength);
300 bufferlength = size_by_seek;
304 ret = lseek(fd, (off_t)0, SEEK_SET);
305 if (ret == (off_t)-1)
306 prterr("save_buffer: lseek 0");
308 byteswritten = write(fd, buffer, (size_t)bufferlength);
309 if (byteswritten != bufferlength) {
310 if (byteswritten == -1)
311 prterr("save_buffer write");
313 warn("save_buffer: short write, 0x%x bytes instead
315 (unsigned)byteswritten,
316 (unsigned long long)bufferlength);
322 report_failure(int status)
328 save_buffer(good_buf, file_size, fsxgoodfd);
329 prt("Correct content saved for comparison\n");
330 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
339 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
340 *(((unsigned char *)(cp)) + 1)))
343 check_buffers(unsigned offset, unsigned size)
351 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
352 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
354 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
356 c = good_buf[offset];
360 bad = short_at(&temp_buf[i]);
361 prt("0x%5x\t0x%04x\t0x%04x", offset,
362 short_at(&good_buf[offset]), bad);
363 op = temp_buf[offset & 1 ? i+1 : i];
375 prt("operation# (mod 256) for the bad data
376 may be %u\n", ((unsigned)op & 0xff));
378 prt("operation# (mod 256) for the bad data
379 unknown, check HOLE and EXTEND ops\n");
381 prt("????????????????\n");
393 if (fstat(fd, &statbuf)) {
394 prterr("check_size: fstat");
395 statbuf.st_size = -1;
397 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
398 if (file_size != statbuf.st_size || file_size != size_by_seek) {
399 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
400 (unsigned long long)file_size,
401 (unsigned long long)statbuf.st_size,
402 (unsigned long long)size_by_seek);
409 check_trunc_hack(void)
413 ftruncate(fd, (off_t)0);
414 ftruncate(fd, (off_t)100000);
416 if (statbuf.st_size != (off_t)100000) {
417 prt("no extend on truncate! not posix!\n");
425 doread(unsigned offset, unsigned size)
431 offset -= offset % readbdy;
432 gettimeofday(&t, NULL);
434 if (!quiet && testcalls > simulatedopcount)
435 prt("skipping zero size read\n");
436 log4(OP_SKIPPED, OP_READ, offset, size, &t);
439 if (size + offset > file_size) {
440 if (!quiet && testcalls > simulatedopcount)
441 prt("skipping seek/read past end of file\n");
442 log4(OP_SKIPPED, OP_READ, offset, size, &t);
446 log4(OP_READ, offset, size, 0, &t);
448 if (testcalls <= simulatedopcount)
451 if (!quiet && ((progressinterval &&
452 testcalls % progressinterval == 0) ||
454 (monitorstart == -1 ||
455 (offset + size > monitorstart &&
456 (monitorend == -1 || offset <= monitorend))))))
457 prt("%06lu %lu.%06lu read %#08x thru %#08x\t(0x%x bytes)\n",
458 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
460 ret = lseek(fd, (off_t)offset, SEEK_SET);
461 if (ret == (off_t)-1) {
462 prterr("doread: lseek");
465 iret = read(fd, temp_buf, size);
466 if (!quiet && (debug > 1 &&
467 (monitorstart == -1 ||
468 (offset + size > monitorstart &&
469 (monitorend == -1 || offset <= monitorend))))) {
470 gettimeofday(&t, NULL);
471 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
475 prterr("doread: read");
477 prt("short read: 0x%x bytes instead of 0x%x\n",
481 check_buffers(offset, size);
486 domapread(unsigned offset, unsigned size)
493 offset -= offset % readbdy;
494 gettimeofday(&t, NULL);
496 if (!quiet && testcalls > simulatedopcount)
497 prt("skipping zero size read\n");
498 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
501 if (size + offset > file_size) {
502 if (!quiet && testcalls > simulatedopcount)
503 prt("skipping seek/read past end of file\n");
504 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
508 log4(OP_MAPREAD, offset, size, 0, &t);
510 if (testcalls <= simulatedopcount)
513 if (!quiet && ((progressinterval &&
514 testcalls % progressinterval == 0) ||
516 (monitorstart == -1 ||
517 (offset + size > monitorstart &&
518 (monitorend == -1 || offset <= monitorend))))))
519 prt("%06lu %lu.%06lu mapread %#08x thru %#08x\t(0x%x bytes)\n",
520 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
523 pg_offset = offset & page_mask;
524 map_size = pg_offset + size;
526 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
527 (off_t)(offset - pg_offset))) == (char *)-1) {
528 prterr("domapread: mmap");
531 if (!quiet && (debug > 1 &&
532 (monitorstart == -1 ||
533 (offset + size > monitorstart &&
534 (monitorend == -1 || offset <= monitorend))))) {
535 gettimeofday(&t, NULL);
536 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
538 memcpy(temp_buf, p + pg_offset, size);
539 if (!quiet && (debug > 1 &&
540 (monitorstart == -1 ||
541 (offset + size > monitorstart &&
542 (monitorend == -1 || offset <= monitorend))))) {
543 gettimeofday(&t, NULL);
544 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
546 if (munmap(p, map_size) != 0) {
547 prterr("domapread: munmap");
550 if (!quiet && (debug > 1 &&
551 (monitorstart == -1 ||
552 (offset + size > monitorstart &&
553 (monitorend == -1 || offset <= monitorend))))) {
554 gettimeofday(&t, NULL);
555 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
558 check_buffers(offset, size);
563 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
566 good_buf[offset] = testcalls % 256;
568 good_buf[offset] += original_buf[offset];
575 dowrite(unsigned offset, unsigned size)
581 offset -= offset % writebdy;
582 gettimeofday(&t, NULL);
584 if (!quiet && testcalls > simulatedopcount)
585 prt("skipping zero size write\n");
586 log4(OP_SKIPPED, OP_WRITE, offset, size, &t);
590 log4(OP_WRITE, offset, size, file_size, &t);
592 gendata(original_buf, good_buf, offset, size);
593 if (file_size < offset + size) {
594 if (file_size < offset)
595 memset(good_buf + file_size, '\0', offset - file_size);
596 file_size = offset + size;
598 warn("Lite file size bug in fsx!");
603 if (testcalls <= simulatedopcount)
606 if (!quiet && ((progressinterval &&
607 testcalls % progressinterval == 0) ||
609 (monitorstart == -1 ||
610 (offset + size > monitorstart &&
611 (monitorend == -1 || offset <= monitorend))))))
612 prt("%06lu %lu.%06lu write %#08x thru %#08x\t(0x%x bytes)\n",
613 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
615 ret = lseek(fd, (off_t)offset, SEEK_SET);
616 if (ret == (off_t)-1) {
617 prterr("dowrite: lseek");
620 iret = write(fd, good_buf + offset, size);
621 if (!quiet && (debug > 1 &&
622 (monitorstart == -1 ||
623 (offset + size > monitorstart &&
624 (monitorend == -1 || offset <= monitorend))))) {
625 gettimeofday(&t, NULL);
626 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
630 prterr("dowrite: write");
632 prt("short write: 0x%x bytes instead of 0x%x\n",
640 domapwrite(unsigned offset, unsigned size)
648 offset -= offset % writebdy;
649 gettimeofday(&t, NULL);
651 if (!quiet && testcalls > simulatedopcount)
652 prt("skipping zero size write\n");
653 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t);
656 cur_filesize = file_size;
658 log4(OP_MAPWRITE, offset, size, 0, &t);
660 gendata(original_buf, good_buf, offset, size);
661 if (file_size < offset + size) {
662 if (file_size < offset)
663 memset(good_buf + file_size, '\0', offset - file_size);
664 file_size = offset + size;
666 warn("Lite file size bug in fsx!");
671 if (testcalls <= simulatedopcount)
674 if (!quiet && ((progressinterval &&
675 testcalls % progressinterval == 0) ||
677 (monitorstart == -1 ||
678 (offset + size > monitorstart &&
679 (monitorend == -1 || offset <= monitorend))))))
680 prt("%06lu %lu.%06lu mapwrite %#08x thru %#08x\t(0x%x bytes)\n",
681 testcalls, t.tv_sec, t.tv_usec, offset, offset + size - 1,
684 if (file_size > cur_filesize) {
685 if (ftruncate(fd, file_size) == -1) {
686 prterr("domapwrite: ftruncate");
689 if (!quiet && (debug > 1 &&
690 (monitorstart == -1 ||
691 (offset + size > monitorstart &&
692 (monitorend == -1 || offset <= monitorend))))) {
693 gettimeofday(&t, NULL);
694 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
697 pg_offset = offset & page_mask;
698 map_size = pg_offset + size;
700 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
701 MAP_FILE | MAP_SHARED, fd,
702 (off_t)(offset - pg_offset))) == (char *)-1) {
703 prterr("domapwrite: mmap");
706 if (!quiet && (debug > 1 &&
707 (monitorstart == -1 ||
708 (offset + size > monitorstart &&
709 (monitorend == -1 || offset <= monitorend))))) {
710 gettimeofday(&t, NULL);
711 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
713 memcpy(p + pg_offset, good_buf + offset, size);
714 if (!quiet && (debug > 1 &&
715 (monitorstart == -1 ||
716 (offset + size > monitorstart &&
717 (monitorend == -1 || offset <= monitorend))))) {
718 gettimeofday(&t, NULL);
719 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
721 if (msync(p, map_size, 0) != 0) {
722 prterr("domapwrite: msync");
725 if (!quiet && (debug > 1 &&
726 (monitorstart == -1 ||
727 (offset + size > monitorstart &&
728 (monitorend == -1 || offset <= monitorend))))) {
729 gettimeofday(&t, NULL);
730 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
732 if (munmap(p, map_size) != 0) {
733 prterr("domapwrite: munmap");
736 if (!quiet && (debug > 1 &&
737 (monitorstart == -1 ||
738 (offset + size > monitorstart &&
739 (monitorend == -1 || offset <= monitorend))))) {
740 gettimeofday(&t, NULL);
741 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
747 dotruncate(unsigned size)
750 int oldsize = file_size;
752 size -= size % truncbdy;
753 gettimeofday(&t, NULL);
754 if (size > biggest) {
756 if (!quiet && testcalls > simulatedopcount)
757 prt("truncating to largest ever: 0x%x\n", size);
760 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t);
762 if (size > file_size)
763 memset(good_buf + file_size, '\0', size - file_size);
766 if (testcalls <= simulatedopcount)
769 if ((progressinterval && testcalls % progressinterval == 0) ||
770 (debug && (monitorstart == -1 || monitorend == -1 ||
771 size <= monitorend)))
772 prt("%06lu %lu.%06lu trunc from %#08x to %#08x\n",
773 testcalls, t.tv_sec, t.tv_usec, oldsize, size);
774 if (ftruncate(fd, (off_t)size) == -1) {
775 prt("ftruncate1: %x\n", size);
776 prterr("dotruncate: ftruncate");
779 if (!quiet && debug > 1) {
780 gettimeofday(&t, NULL);
781 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
791 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
792 prterr("writefileimage: lseek");
795 iret = write(fd, good_buf, file_size);
796 if ((off_t)iret != file_size) {
798 prterr("writefileimage: write");
800 prt("short write: 0x%x bytes instead of 0x%llx\n",
801 iret, (unsigned long long)file_size);
804 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
805 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
806 prterr("writefileimage: ftruncate");
817 if (testcalls <= simulatedopcount)
820 gettimeofday(&t, NULL);
821 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t);
824 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
827 prterr("docloseopen: close");
830 if (!quiet && debug > 1) {
831 gettimeofday(&t, NULL);
832 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
834 fd = open(fname, O_RDWR, 0);
836 prterr("docloseopen: open");
839 if (!quiet && debug > 1) {
840 gettimeofday(&t, NULL);
841 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
849 unsigned long offset;
850 unsigned long size = maxoplen;
851 unsigned long rv = random();
852 unsigned long op = rv % (3 + !lite + mapped_writes);
854 /* turn off the map read if necessary */
856 if (op == 2 && !mapped_reads)
859 if (simulatedopcount > 0 && testcalls == simulatedopcount)
864 if (debugstart > 0 && testcalls >= debugstart)
867 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
868 prt("%lu...\n", testcalls);
875 * MAPWRITE: op = 3 or 4
877 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
878 dotruncate(random() % maxfilelen);
881 size = random() % (maxoplen+1);
882 if (lite ? 0 : op == 3)
886 if (op == 1 || op == (lite ? 3 : 4)) {
887 offset %= maxfilelen;
888 if (offset + size > maxfilelen)
889 size = maxfilelen - offset;
891 domapwrite(offset, size);
893 dowrite(offset, size);
899 if (offset + size > file_size)
900 size = file_size - offset;
902 domapread(offset, size);
904 doread(offset, size);
908 if (sizechecks && testcalls > simulatedopcount)
910 if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
920 prt("signal %d\n", sig);
921 prt("testcalls = %lu\n", testcalls);
929 fprintf(stdout, "usage: %s",
930 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m
931 start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t
932 truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed]
934 -b opnum: beginning operation number (default 1)\n\
935 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
936 -d: debug output for all operations [-d -d = more debugging]\n\
937 -l flen: the upper bound on file size (default 262144)\n\
938 -m startop:endop: monitor (print debug output) specified byte range
939 (default 0:infinity)\n\
940 -n: no verifications of file size\n\
941 -o oplen: the upper bound on operation size (default 65536)\n\
942 -p progressinterval: debug output at specified operation interval\n\
943 -q: quieter operation\n\
944 -r readbdy: 4096 would make reads page aligned (default 1)\n\
945 -s style: 1 gives smaller truncates (default 0)\n\
946 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
947 -w writebdy: 4096 would make writes page aligned (default 1)\n\
948 -D startingop: debug output starting at specified operation\n\
949 -L: fsxLite - no file creations & no file size changes\n\
950 -N numops: total # operations to do (default infinity)\n\
951 -O: use oplen (see -o flag) for every op (default random)\n\
952 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
953 -S seed: for random # generator (default 1) 0 gets timestamp\n\
954 -W: mapped write operations DISabled\n\
955 -R: read() system calls only (mapped reads disabled)\n\
956 fname: this filename is REQUIRED (no default)\n");
962 getnum(char *s, char **e)
967 ret = strtol(s, e, 0);
995 static const char *basename(const char *path)
997 char *c = strrchr(path, '/');
999 return c ? c++ : path;
1003 main(int argc, char **argv)
1012 page_size = getpagesize();
1013 page_mask = page_size - 1;
1015 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1017 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
1021 simulatedopcount = getnum(optarg, &endp);
1023 fprintf(stdout, "Will begin at operation
1026 if (simulatedopcount == 0)
1028 simulatedopcount -= 1;
1031 closeprob = getnum(optarg, &endp);
1034 "Chance of close/open is 1 in %d\n",
1043 maxfilelen = getnum(optarg, &endp);
1044 if (maxfilelen <= 0)
1048 monitorstart = getnum(optarg, &endp);
1049 if (monitorstart < 0)
1051 if (!endp || *endp++ != ':')
1053 monitorend = getnum(endp, &endp);
1056 if (monitorend == 0)
1057 monitorend = -1; /* aka infinity */
1063 maxoplen = getnum(optarg, &endp);
1068 progressinterval = getnum(optarg, &endp);
1069 if (progressinterval < 0)
1076 readbdy = getnum(optarg, &endp);
1081 style = getnum(optarg, &endp);
1082 if (style < 0 || style > 1)
1086 truncbdy = getnum(optarg, &endp);
1091 writebdy = getnum(optarg, &endp);
1096 debugstart = getnum(optarg, &endp);
1104 numops = getnum(optarg, &endp);
1112 strncpy(goodfile, optarg, sizeof(goodfile));
1113 strcat(goodfile, "/");
1114 strncpy(logfile, optarg, sizeof(logfile));
1115 strcat(logfile, "/");
1122 seed = getnum(optarg, &endp);
1124 seed = time(0) % 10000;
1126 fprintf(stdout, "Seed set to %d\n", seed);
1133 fprintf(stdout, "mapped writes DISABLED\n");
1146 signal(SIGHUP, cleanup);
1147 signal(SIGINT, cleanup);
1148 signal(SIGPIPE, cleanup);
1149 signal(SIGALRM, cleanup);
1150 signal(SIGTERM, cleanup);
1151 signal(SIGXCPU, cleanup);
1152 signal(SIGXFSZ, cleanup);
1153 signal(SIGVTALRM, cleanup);
1154 signal(SIGUSR1, cleanup);
1155 signal(SIGUSR2, cleanup);
1157 initstate(seed, state, 256);
1159 fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1164 strncat(goodfile, dirpath ? basename(fname) : fname, 256);
1165 strcat (goodfile, ".fsxgood");
1166 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1167 if (fsxgoodfd < 0) {
1171 strncat(logfile, dirpath ? basename(fname) : fname, 256);
1172 strcat (logfile, ".fsxlog");
1173 fsxlogf = fopen(logfile, "w");
1174 if (fsxlogf == NULL) {
1180 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1181 if (file_size == (off_t)-1) {
1183 warn("main: lseek eof");
1186 ret = lseek(fd, (off_t)0, SEEK_SET);
1187 if (ret == (off_t)-1) {
1189 warn("main: lseek 0");
1193 original_buf = (char *) malloc(maxfilelen);
1194 for (i = 0; i < maxfilelen; i++)
1195 original_buf[i] = random() % 256;
1196 good_buf = (char *) malloc(maxfilelen);
1197 memset(good_buf, '\0', maxfilelen);
1198 temp_buf = (char *) malloc(maxoplen);
1199 memset(temp_buf, '\0', maxoplen);
1200 if (lite) { /* zero entire existing file */
1203 written = write(fd, good_buf, (size_t)maxfilelen);
1204 if (written != maxfilelen) {
1205 if (written == -1) {
1207 warn("main: error on write");
1209 warn("main: short write, 0x%x bytes instead
1211 (unsigned)written, maxfilelen);
1217 while (numops == -1 || numops--)
1224 prt("All operations completed A-OK!\n");