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.
72 #define LOGSIZE 100000
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 */
103 unsigned long testcalls = 0; /* calls to function "test" */
105 unsigned long simulatedopcount = 0; /* -b flag */
106 int closeprob = 0; /* -c flag */
107 int debug = 0; /* -d flag */
108 unsigned long debugstart = 0; /* -D flag */
109 unsigned long maxfilelen = 256 * 1024; /* -l flag */
110 int sizechecks = 1; /* -n flag disables them */
111 int maxoplen = 64 * 1024; /* -o flag */
112 int quiet = 0; /* -q flag */
113 unsigned long progressinterval = 0; /* -p flag */
114 int readbdy = 1; /* -r flag */
115 int style = 0; /* -s flag */
116 int truncbdy = 1; /* -t flag */
117 int writebdy = 1; /* -w flag */
118 long monitorstart = -1; /* -m flag */
119 long monitorend = -1; /* -m flag */
120 int lite = 0; /* -L flag */
121 long numops = -1; /* -N flag */
122 int randomoplen = 1; /* -O flag disables it */
123 int seed = 1; /* -S flag */
124 int mapped_writes = 1; /* -W flag disables */
125 int mapped_reads = 1; /* -R flag disables it */
127 FILE * fsxlogf = NULL;
132 vwarnc(code, fmt, ap)
137 fprintf(stderr, "fsx: ");
139 vfprintf(stderr, fmt, ap);
140 fprintf(stderr, ": ");
142 fprintf(stderr, "%s\n", strerror(code));
147 warn(const char * fmt, ...)
151 vwarnc(errno, fmt, ap);
157 __attribute__((format(printf, 1, 2)))
163 vfprintf(stdout, fmt, args);
168 vfprintf(fsxlogf, fmt, args);
176 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
181 log4(int operation, int arg0, int arg1, int arg2, struct timeval *tv)
183 struct log_entry *le;
187 le->operation = operation;
193 if (logptr >= LOGSIZE)
202 struct log_entry *lp;
204 prt("LOG DUMP (%d total operations):\n", logcount);
205 if (logcount < LOGSIZE) {
212 for ( ; count > 0; count--) {
215 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
217 prt("%d: %lu.%06lu ", opnum,
218 lp->tv.tv_sec, lp->tv.tv_usec);
220 switch (lp->operation) {
222 prt("MAPREAD 0x%x thru 0x%x (0x%x bytes)",
223 lp->args[0], lp->args[0] + lp->args[1] - 1,
225 if (badoff >= lp->args[0] && badoff <
226 lp->args[0] + lp->args[1])
230 prt("MAPWRITE 0x%x thru 0x%x (0x%x bytes)",
231 lp->args[0], lp->args[0] + lp->args[1] - 1,
233 if (badoff >= lp->args[0] && badoff <
234 lp->args[0] + lp->args[1])
238 prt("READ 0x%x thru 0x%x (0x%x bytes)",
239 lp->args[0], lp->args[0] + lp->args[1] - 1,
241 if (badoff >= lp->args[0] &&
242 badoff < lp->args[0] + lp->args[1])
246 prt("WRITE 0x%x thru 0x%x (0x%x bytes)",
247 lp->args[0], lp->args[0] + lp->args[1] - 1,
249 if (lp->args[0] > lp->args[2])
251 else if (lp->args[0] + lp->args[1] > lp->args[2])
253 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
254 badoff < lp->args[0] + lp->args[1])
258 down = lp->args[0] < lp->args[1];
259 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
260 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
261 if (badoff >= lp->args[!down] &&
262 badoff < lp->args[!!down])
269 prt("SKIPPED (no operation)");
272 prt("BOGUS LOG ENTRY (operation code = %d)!",
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",
303 (unsigned long long)size_by_seek,
304 (unsigned long long)bufferlength);
305 bufferlength = size_by_seek;
309 ret = lseek(fd, (off_t)0, SEEK_SET);
310 if (ret == (off_t)-1)
311 prterr("save_buffer: lseek 0");
313 byteswritten = write(fd, buffer, (size_t)bufferlength);
314 if (byteswritten != bufferlength) {
315 if (byteswritten == -1)
316 prterr("save_buffer write");
318 warn("save_buffer: short write, 0x%x bytes instead"
320 (unsigned)byteswritten,
321 (unsigned long long)bufferlength);
327 report_failure(int status)
333 save_buffer(good_buf, file_size, fsxgoodfd);
334 prt("Correct content saved for comparison\n");
335 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
344 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
345 *(((unsigned char *)(cp)) + 1)))
348 check_buffers(unsigned offset, unsigned size)
356 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
357 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
359 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
361 c = good_buf[offset];
365 bad = short_at(&temp_buf[i]);
366 prt("%#07x\t%#06x\t%#06x", offset,
367 short_at(&good_buf[offset]), bad);
368 op = temp_buf[offset & 1 ? i+1 : i];
380 prt("operation# (mod 256) for the bad data"
381 "may be %u\n", ((unsigned)op & 0xff));
383 prt("operation# (mod 256) for the bad data"
384 "unknown, check HOLE and EXTEND ops\n");
386 prt("????????????????\n");
394 } *test_files = NULL;
396 int num_test_files = 0;
397 enum fd_iteration_policy {
402 int fd_policy = FD_RANDOM;
421 prt("unknown policy");
425 return &test_files[ index % num_test_files ];
429 assign_fd_policy(char *policy)
431 if (!strcmp(policy, "random"))
432 fd_policy = FD_RANDOM;
433 else if (!strcmp(policy, "rotate"))
434 fd_policy = FD_ROTATE;
436 prt("unknown -I policy: '%s'\n", policy);
444 struct test_file *tf = get_tf();
448 static const char *my_basename(const char *path)
450 char *c = strrchr(path, '/');
452 return c ? c++ : path;
456 open_test_files(char **argv, int argc)
458 struct test_file *tf;
461 num_test_files = argc;
462 if (num_test_files == 1)
463 fd_policy = FD_SINGLE;
465 test_files = calloc(num_test_files, sizeof(*test_files));
466 if (test_files == NULL) {
467 prterr("reallocating space for test files");
471 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
474 tf->fd = open(tf->path, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC),
482 if (quiet || fd_policy == FD_SINGLE)
485 for (i = 0, tf = test_files; i < num_test_files; i++, tf++)
486 prt("fd %d: %s\n", i, tf->path);
490 close_test_files(void)
493 struct test_file *tf;
495 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
511 if (fstat(fd, &statbuf)) {
512 prterr("check_size: fstat");
513 statbuf.st_size = -1;
515 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
516 if (file_size != statbuf.st_size || file_size != size_by_seek) {
517 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
518 (unsigned long long)file_size,
519 (unsigned long long)statbuf.st_size,
520 (unsigned long long)size_by_seek);
527 check_trunc_hack(void)
532 /* should not ignore ftruncate(2)'s return value */
533 if (ftruncate(fd, (off_t)0) < 0) {
534 prterr("trunc_hack: ftruncate(0)");
537 if (ftruncate(fd, (off_t)100000) < 0) {
538 prterr("trunc_hack: ftruncate(100000)");
541 if (fstat(fd, &statbuf)) {
542 prterr("trunc_hack: fstat");
543 statbuf.st_size = -1;
545 if (statbuf.st_size != (off_t)100000) {
546 prt("no extend on truncate! not posix!\n");
549 if (ftruncate(fd, 0) < 0) {
550 prterr("trunc_hack: ftruncate(0) (2nd call)");
555 static char *tf_buf = NULL;
556 static int max_tf_len = 0;
562 int highest = num_test_files - 1;
565 len = snprintf(&dummy, 0, "%u ", highest);
567 prterr("finding max tf_buf");
571 tf_buf = malloc(len);
572 if (tf_buf == NULL) {
573 prterr("allocating tf_buf");
576 max_tf_len = snprintf(tf_buf, len, "%u ", highest);
577 if (max_tf_len < 1) {
578 prterr("fiding max_tv_len\n");
581 if (max_tf_len != len - 1) {
582 warn("snprintf() gave %d instead of %d?\n",
583 max_tf_len, len - 1);
589 fill_tf_buf(struct test_file *tf)
594 sprintf(tf_buf,"%lu ", (unsigned long)(tf - test_files));
599 output_line(struct test_file *tf, int op, unsigned offset,
600 unsigned size, struct timeval *tv)
606 [OP_WRITE] = "write",
607 [OP_TRUNCATE] = "trunc from",
608 [OP_MAPREAD] = "mapread",
609 [OP_MAPWRITE] = "mapwrite",
612 if (fd_policy != FD_SINGLE)
613 tf_num = fill_tf_buf(tf);
616 if (!(!quiet && ((progressinterval &&
617 testcalls % progressinterval == 0) ||
619 (monitorstart == -1 ||
620 (offset + size > monitorstart &&
621 (monitorend == -1 || offset <= monitorend)))))))
624 prt("%06lu %lu.%06lu %.*s%-10s %#08x %s %#08x\t(0x%x bytes)\n",
625 testcalls, tv->tv_sec, tv->tv_usec, max_tf_len,
627 offset, op == OP_TRUNCATE ? " to " : "thru",
628 offset + size - 1, size);
632 doread(unsigned offset, unsigned size)
637 struct test_file *tf = get_tf();
640 offset -= offset % readbdy;
641 gettimeofday(&t, NULL);
643 if (!quiet && testcalls > simulatedopcount)
644 prt("skipping zero size read\n");
645 log4(OP_SKIPPED, OP_READ, offset, size, &t);
648 if (size + offset > file_size) {
649 if (!quiet && testcalls > simulatedopcount)
650 prt("skipping seek/read past end of file\n");
651 log4(OP_SKIPPED, OP_READ, offset, size, &t);
655 log4(OP_READ, offset, size, 0, &t);
657 if (testcalls <= simulatedopcount)
660 output_line(tf, OP_READ, offset, size, &t);
662 ret = lseek(fd, (off_t)offset, SEEK_SET);
663 if (ret == (off_t)-1) {
664 prterr("doread: lseek");
667 iret = read(fd, temp_buf, size);
668 if (!quiet && (debug > 1 &&
669 (monitorstart == -1 ||
670 (offset + size > monitorstart &&
671 (monitorend == -1 || offset <= monitorend))))) {
672 gettimeofday(&t, NULL);
673 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
677 prterr("doread: read");
679 prt("short read: 0x%x bytes instead of 0x%x\n",
683 check_buffers(offset, size);
688 domapread(unsigned offset, unsigned size)
694 struct test_file *tf = get_tf();
697 offset -= offset % readbdy;
698 gettimeofday(&t, NULL);
700 if (!quiet && testcalls > simulatedopcount)
701 prt("skipping zero size read\n");
702 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
705 if (size + offset > file_size) {
706 if (!quiet && testcalls > simulatedopcount)
707 prt("skipping seek/read past end of file\n");
708 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
712 log4(OP_MAPREAD, offset, size, 0, &t);
714 if (testcalls <= simulatedopcount)
717 output_line(tf, OP_MAPREAD, offset, size, &t);
719 pg_offset = offset & page_mask;
720 map_size = pg_offset + size;
722 if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
723 (off_t)(offset - pg_offset))) == MAP_FAILED) {
724 prterr("domapread: mmap");
727 if (!quiet && (debug > 1 &&
728 (monitorstart == -1 ||
729 (offset + size > monitorstart &&
730 (monitorend == -1 || offset <= monitorend))))) {
731 gettimeofday(&t, NULL);
732 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
734 memcpy(temp_buf, p + pg_offset, size);
735 if (!quiet && (debug > 1 &&
736 (monitorstart == -1 ||
737 (offset + size > monitorstart &&
738 (monitorend == -1 || offset <= monitorend))))) {
739 gettimeofday(&t, NULL);
740 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
742 if (munmap(p, map_size) != 0) {
743 prterr("domapread: munmap");
746 if (!quiet && (debug > 1 &&
747 (monitorstart == -1 ||
748 (offset + size > monitorstart &&
749 (monitorend == -1 || offset <= monitorend))))) {
750 gettimeofday(&t, NULL);
751 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
754 check_buffers(offset, size);
759 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
762 good_buf[offset] = testcalls % 256;
764 good_buf[offset] += original_buf[offset];
771 dowrite(unsigned offset, unsigned size)
776 struct test_file *tf = get_tf();
779 offset -= offset % writebdy;
780 gettimeofday(&t, NULL);
782 if (!quiet && testcalls > simulatedopcount)
783 prt("skipping zero size write\n");
784 log4(OP_SKIPPED, OP_WRITE, offset, size, &t);
788 log4(OP_WRITE, offset, size, file_size, &t);
790 gendata(original_buf, good_buf, offset, size);
791 if (file_size < offset + size) {
792 if (file_size < offset)
793 memset(good_buf + file_size, '\0', offset - file_size);
794 file_size = offset + size;
796 warn("Lite file size bug in fsx!");
801 if (testcalls <= simulatedopcount)
804 output_line(tf, OP_WRITE, offset, size, &t);
806 ret = lseek(fd, (off_t)offset, SEEK_SET);
807 if (ret == (off_t)-1) {
808 prterr("dowrite: lseek");
811 iret = write(fd, good_buf + offset, size);
812 if (!quiet && (debug > 1 &&
813 (monitorstart == -1 ||
814 (offset + size > monitorstart &&
815 (monitorend == -1 || offset <= monitorend))))) {
816 gettimeofday(&t, NULL);
817 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
821 prterr("dowrite: write");
823 prt("short write: 0x%x bytes instead of 0x%x\n",
831 domapwrite(unsigned offset, unsigned size)
838 struct test_file *tf = get_tf();
841 offset -= offset % writebdy;
842 gettimeofday(&t, NULL);
844 if (!quiet && testcalls > simulatedopcount)
845 prt("skipping zero size write\n");
846 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t);
849 cur_filesize = file_size;
851 log4(OP_MAPWRITE, offset, size, 0, &t);
853 gendata(original_buf, good_buf, offset, size);
854 if (file_size < offset + size) {
855 if (file_size < offset)
856 memset(good_buf + file_size, '\0', offset - file_size);
857 file_size = offset + size;
859 warn("Lite file size bug in fsx!");
864 if (testcalls <= simulatedopcount)
867 output_line(tf, OP_MAPWRITE, offset, size, &t);
869 if (file_size > cur_filesize) {
870 if (ftruncate(fd, file_size) == -1) {
871 prterr("domapwrite: ftruncate");
874 if (!quiet && (debug > 1 &&
875 (monitorstart == -1 ||
876 (offset + size > monitorstart &&
877 (monitorend == -1 || offset <= monitorend))))) {
878 gettimeofday(&t, NULL);
879 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
882 pg_offset = offset & page_mask;
883 map_size = pg_offset + size;
885 if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
886 fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
887 prterr("domapwrite: mmap");
890 if (!quiet && (debug > 1 &&
891 (monitorstart == -1 ||
892 (offset + size > monitorstart &&
893 (monitorend == -1 || offset <= monitorend))))) {
894 gettimeofday(&t, NULL);
895 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
897 memcpy(p + pg_offset, good_buf + offset, size);
898 if (!quiet && (debug > 1 &&
899 (monitorstart == -1 ||
900 (offset + size > monitorstart &&
901 (monitorend == -1 || offset <= monitorend))))) {
902 gettimeofday(&t, NULL);
903 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
905 if (msync(p, map_size, 0) != 0) {
906 prterr("domapwrite: msync");
909 if (!quiet && (debug > 1 &&
910 (monitorstart == -1 ||
911 (offset + size > monitorstart &&
912 (monitorend == -1 || offset <= monitorend))))) {
913 gettimeofday(&t, NULL);
914 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
916 if (munmap(p, map_size) != 0) {
917 prterr("domapwrite: munmap");
920 if (!quiet && (debug > 1 &&
921 (monitorstart == -1 ||
922 (offset + size > monitorstart &&
923 (monitorend == -1 || offset <= monitorend))))) {
924 gettimeofday(&t, NULL);
925 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
931 dotruncate(unsigned size)
934 int oldsize = file_size;
935 struct test_file *tf = get_tf();
938 size -= size % truncbdy;
939 gettimeofday(&t, NULL);
940 if (size > biggest) {
942 if (!quiet && testcalls > simulatedopcount)
943 prt("truncating to largest ever: 0x%x\n", size);
946 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t);
948 if (size > file_size)
949 memset(good_buf + file_size, '\0', size - file_size);
952 if (testcalls <= simulatedopcount)
955 output_line(tf, OP_TRUNCATE, oldsize, size, &t);
957 if (ftruncate(fd, (off_t)size) == -1) {
958 prt("ftruncate1: %x\n", size);
959 prterr("dotruncate: ftruncate");
962 if (!quiet && debug > 1) {
963 gettimeofday(&t, NULL);
964 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
975 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
976 prterr("writefileimage: lseek");
979 iret = write(fd, good_buf, file_size);
980 if ((off_t)iret != file_size) {
982 prterr("writefileimage: write");
984 prt("short write: 0x%lx bytes instead of 0x%llx\n",
986 (unsigned long long)file_size);
989 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
990 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
991 prterr("writefileimage: ftruncate");
1001 struct test_file *tf = get_tf();
1003 if (testcalls <= simulatedopcount)
1006 gettimeofday(&t, NULL);
1007 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t);
1010 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
1012 if (close(tf->fd)) {
1013 prterr("docloseopen: close");
1014 report_failure(180);
1016 if (!quiet && debug > 1) {
1017 gettimeofday(&t, NULL);
1018 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
1020 tf->fd = open(tf->path, O_RDWR, 0);
1022 prterr("docloseopen: open");
1023 report_failure(181);
1025 if (!quiet && debug > 1) {
1026 gettimeofday(&t, NULL);
1027 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
1035 unsigned long offset;
1036 unsigned long size = maxoplen;
1037 unsigned long rv = random();
1038 unsigned long op = rv % (3 + !lite + mapped_writes);
1040 /* turn off the map read if necessary */
1042 if (op == 2 && !mapped_reads)
1045 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1050 if (debugstart > 0 && testcalls >= debugstart)
1053 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1054 prt("%lu...\n", testcalls);
1061 * MAPWRITE: op = 3 or 4
1063 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
1064 dotruncate(random() % maxfilelen);
1067 size = random() % (maxoplen+1);
1068 if (lite ? 0 : op == 3)
1072 if (op == 1 || op == (lite ? 3 : 4)) {
1073 offset %= maxfilelen;
1074 if (offset + size > maxfilelen)
1075 size = maxfilelen - offset;
1077 domapwrite(offset, size);
1079 dowrite(offset, size);
1082 offset %= file_size;
1085 if (offset + size > file_size)
1086 size = file_size - offset;
1088 domapread(offset, size);
1090 doread(offset, size);
1094 if (sizechecks && testcalls > simulatedopcount)
1096 if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
1106 prt("signal %d\n", sig);
1107 prt("testcalls = %lu\n", testcalls);
1115 fprintf(stdout, "usage: %s",
1116 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m "
1117 "start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t "
1118 "truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] "
1119 "[ -I random|rotate ] fname [additional paths to fname..]\n"
1120 " -b opnum: beginning operation number (default 1)\n"
1121 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1122 " -d: debug output for all operations [-d -d = more debugging]\n"
1123 " -l flen: the upper bound on file size (default 262144)\n"
1124 " -m startop:endop: monitor (print debug output) specified byte rang"
1125 "(default 0:infinity)\n"
1126 " -n: no verifications of file size\n"
1127 " -o oplen: the upper bound on operation size (default 65536)\n"
1128 " -p progressinterval: debug output at specified operation interval\n"
1129 " -q: quieter operation\n"
1130 " -r readbdy: 4096 would make reads page aligned (default 1)\n"
1131 " -s style: 1 gives smaller truncates (default 0)\n"
1132 " -t truncbdy: 4096 would make truncates page aligned (default 1)\n"
1133 " -w writebdy: 4096 would make writes page aligned (default 1)\n"
1134 " -D startingop: debug output starting at specified operation\n"
1135 " -L: fsxLite - no file creations & no file size changes\n"
1136 " -N numops: total # operations to do (default infinity)\n"
1137 " -O: use oplen (see -o flag) for every op (default random)\n"
1138 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1139 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1140 " -W: mapped write operations DISabled\n"
1141 " -R: read() system calls only (mapped reads disabled)\n"
1142 " -I: When multiple paths to the file are given each operation uses"
1143 " a different path. Iterate through them in order with 'rotate'"
1144 " or chose then at 'random'. (defaults to random)\n"
1145 " fname: this filename is REQUIRED (no default)\n");
1151 getnum(char *s, char **e)
1156 ret = strtol(s, e, 0);
1184 main(int argc, char **argv)
1193 page_size = getpagesize();
1194 page_mask = page_size - 1;
1196 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1198 while ((ch = getopt(argc, argv,
1199 "b:c:dl:m:no:p:qr:s:t:w:D:I:LN:OP:RS:W"))
1203 simulatedopcount = getnum(optarg, &endp);
1205 fprintf(stdout, "Will begin at operation"
1208 if (simulatedopcount == 0)
1210 simulatedopcount -= 1;
1213 closeprob = getnum(optarg, &endp);
1216 "Chance of close/open is 1 in %d\n",
1225 maxfilelen = getnum(optarg, &endp);
1226 if (maxfilelen <= 0)
1230 monitorstart = getnum(optarg, &endp);
1231 if (monitorstart < 0)
1233 if (!endp || *endp++ != ':')
1235 monitorend = getnum(endp, &endp);
1238 if (monitorend == 0)
1239 monitorend = -1; /* aka infinity */
1245 maxoplen = getnum(optarg, &endp);
1250 progressinterval = getnum(optarg, &endp);
1251 if (progressinterval < 0)
1258 readbdy = getnum(optarg, &endp);
1263 style = getnum(optarg, &endp);
1264 if (style < 0 || style > 1)
1268 truncbdy = getnum(optarg, &endp);
1273 writebdy = getnum(optarg, &endp);
1278 debugstart = getnum(optarg, &endp);
1283 assign_fd_policy(optarg);
1289 numops = getnum(optarg, &endp);
1297 strncpy(goodfile, optarg, sizeof(goodfile));
1298 strcat(goodfile, "/");
1299 strncpy(logfile, optarg, sizeof(logfile));
1300 strcat(logfile, "/");
1307 seed = getnum(optarg, &endp);
1309 seed = time(0) % 10000;
1311 fprintf(stdout, "Seed set to %d\n", seed);
1318 fprintf(stdout, "mapped writes DISABLED\n");
1331 signal(SIGHUP, cleanup);
1332 signal(SIGINT, cleanup);
1333 signal(SIGPIPE, cleanup);
1334 signal(SIGALRM, cleanup);
1335 signal(SIGTERM, cleanup);
1336 signal(SIGXCPU, cleanup);
1337 signal(SIGXFSZ, cleanup);
1338 signal(SIGVTALRM, cleanup);
1339 signal(SIGUSR1, cleanup);
1340 signal(SIGUSR2, cleanup);
1342 initstate(seed, state, 256);
1345 open_test_files(argv, argc);
1347 strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1348 strcat (goodfile, ".fsxgood");
1349 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1350 if (fsxgoodfd < 0) {
1354 strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1355 strcat (logfile, ".fsxlog");
1356 fsxlogf = fopen(logfile, "w");
1357 if (fsxlogf == NULL) {
1364 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1365 if (file_size == (off_t)-1) {
1367 warn("main: lseek eof");
1370 ret = lseek(fd, (off_t)0, SEEK_SET);
1371 if (ret == (off_t)-1) {
1373 warn("main: lseek 0");
1377 original_buf = (char *) malloc(maxfilelen);
1378 for (i = 0; i < maxfilelen; i++)
1379 original_buf[i] = random() % 256;
1380 good_buf = (char *) malloc(maxfilelen);
1381 memset(good_buf, '\0', maxfilelen);
1382 temp_buf = (char *) malloc(maxoplen);
1383 memset(temp_buf, '\0', maxoplen);
1384 if (lite) { /* zero entire existing file */
1388 written = write(fd, good_buf, (size_t)maxfilelen);
1389 if (written != maxfilelen) {
1390 if (written == -1) {
1392 warn("main: error on write");
1394 warn("main: short write, 0x%x bytes instead"
1396 (unsigned)written, maxfilelen);
1402 while (numops == -1 || numops--)
1406 prt("All operations completed A-OK!\n");