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 */
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("0x%5x\t0x%04x\t0x%04x", 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 *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 ftruncate(fd, (off_t)0);
533 ftruncate(fd, (off_t)100000);
535 if (statbuf.st_size != (off_t)100000) {
536 prt("no extend on truncate! not posix!\n");
542 static char *tf_buf = NULL;
543 static int max_tf_len = 0;
549 int highest = num_test_files - 1;
552 len = snprintf(&dummy, 0, "%u ", highest);
554 prterr("finding max tf_buf");
558 tf_buf = malloc(len);
559 if (tf_buf == NULL) {
560 prterr("allocating tf_buf");
563 max_tf_len = snprintf(tf_buf, len, "%u ", highest);
564 if (max_tf_len < 1) {
565 prterr("fiding max_tv_len\n");
568 if (max_tf_len != len - 1) {
569 warn("snprintf() gave %d instead of %d?\n",
570 max_tf_len, len - 1);
576 fill_tf_buf(struct test_file *tf)
581 sprintf(tf_buf,"%lu ", (unsigned long)(tf - test_files));
586 output_line(struct test_file *tf, int op, unsigned offset,
587 unsigned size, struct timeval *tv)
593 [OP_WRITE] = "write",
594 [OP_TRUNCATE] = "trunc from",
595 [OP_MAPREAD] = "mapread",
596 [OP_MAPWRITE] = "mapwrite",
599 if (fd_policy != FD_SINGLE)
600 tf_num = fill_tf_buf(tf);
603 if (!(!quiet && ((progressinterval &&
604 testcalls % progressinterval == 0) ||
606 (monitorstart == -1 ||
607 (offset + size > monitorstart &&
608 (monitorend == -1 || offset <= monitorend)))))))
611 prt("%06lu %lu.%06lu %.*s%-10s %#08x %s %#08x\t(0x%x bytes)\n",
612 testcalls, tv->tv_sec, tv->tv_usec, max_tf_len,
614 offset, op == OP_TRUNCATE ? " to " : "thru",
615 offset + size - 1, size);
619 doread(unsigned offset, unsigned size)
624 struct test_file *tf = get_tf();
627 offset -= offset % readbdy;
628 gettimeofday(&t, NULL);
630 if (!quiet && testcalls > simulatedopcount)
631 prt("skipping zero size read\n");
632 log4(OP_SKIPPED, OP_READ, offset, size, &t);
635 if (size + offset > file_size) {
636 if (!quiet && testcalls > simulatedopcount)
637 prt("skipping seek/read past end of file\n");
638 log4(OP_SKIPPED, OP_READ, offset, size, &t);
642 log4(OP_READ, offset, size, 0, &t);
644 if (testcalls <= simulatedopcount)
647 output_line(tf, OP_READ, offset, size, &t);
649 ret = lseek(fd, (off_t)offset, SEEK_SET);
650 if (ret == (off_t)-1) {
651 prterr("doread: lseek");
654 iret = read(fd, temp_buf, size);
655 if (!quiet && (debug > 1 &&
656 (monitorstart == -1 ||
657 (offset + size > monitorstart &&
658 (monitorend == -1 || offset <= monitorend))))) {
659 gettimeofday(&t, NULL);
660 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
664 prterr("doread: read");
666 prt("short read: 0x%x bytes instead of 0x%x\n",
670 check_buffers(offset, size);
675 domapread(unsigned offset, unsigned size)
681 struct test_file *tf = get_tf();
684 offset -= offset % readbdy;
685 gettimeofday(&t, NULL);
687 if (!quiet && testcalls > simulatedopcount)
688 prt("skipping zero size read\n");
689 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
692 if (size + offset > file_size) {
693 if (!quiet && testcalls > simulatedopcount)
694 prt("skipping seek/read past end of file\n");
695 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t);
699 log4(OP_MAPREAD, offset, size, 0, &t);
701 if (testcalls <= simulatedopcount)
704 output_line(tf, OP_MAPREAD, offset, size, &t);
706 pg_offset = offset & page_mask;
707 map_size = pg_offset + size;
709 if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
710 (off_t)(offset - pg_offset))) == MAP_FAILED) {
711 prterr("domapread: mmap");
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 mmap done\n", t.tv_sec, t.tv_usec);
721 memcpy(temp_buf, p + pg_offset, size);
722 if (!quiet && (debug > 1 &&
723 (monitorstart == -1 ||
724 (offset + size > monitorstart &&
725 (monitorend == -1 || offset <= monitorend))))) {
726 gettimeofday(&t, NULL);
727 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
729 if (munmap(p, map_size) != 0) {
730 prterr("domapread: munmap");
733 if (!quiet && (debug > 1 &&
734 (monitorstart == -1 ||
735 (offset + size > monitorstart &&
736 (monitorend == -1 || offset <= monitorend))))) {
737 gettimeofday(&t, NULL);
738 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
741 check_buffers(offset, size);
746 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
749 good_buf[offset] = testcalls % 256;
751 good_buf[offset] += original_buf[offset];
758 dowrite(unsigned offset, unsigned size)
763 struct test_file *tf = get_tf();
766 offset -= offset % writebdy;
767 gettimeofday(&t, NULL);
769 if (!quiet && testcalls > simulatedopcount)
770 prt("skipping zero size write\n");
771 log4(OP_SKIPPED, OP_WRITE, offset, size, &t);
775 log4(OP_WRITE, offset, size, file_size, &t);
777 gendata(original_buf, good_buf, offset, size);
778 if (file_size < offset + size) {
779 if (file_size < offset)
780 memset(good_buf + file_size, '\0', offset - file_size);
781 file_size = offset + size;
783 warn("Lite file size bug in fsx!");
788 if (testcalls <= simulatedopcount)
791 output_line(tf, OP_WRITE, offset, size, &t);
793 ret = lseek(fd, (off_t)offset, SEEK_SET);
794 if (ret == (off_t)-1) {
795 prterr("dowrite: lseek");
798 iret = write(fd, good_buf + offset, size);
799 if (!quiet && (debug > 1 &&
800 (monitorstart == -1 ||
801 (offset + size > monitorstart &&
802 (monitorend == -1 || offset <= monitorend))))) {
803 gettimeofday(&t, NULL);
804 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
808 prterr("dowrite: write");
810 prt("short write: 0x%x bytes instead of 0x%x\n",
818 domapwrite(unsigned offset, unsigned size)
825 struct test_file *tf = get_tf();
828 offset -= offset % writebdy;
829 gettimeofday(&t, NULL);
831 if (!quiet && testcalls > simulatedopcount)
832 prt("skipping zero size write\n");
833 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t);
836 cur_filesize = file_size;
838 log4(OP_MAPWRITE, offset, size, 0, &t);
840 gendata(original_buf, good_buf, offset, size);
841 if (file_size < offset + size) {
842 if (file_size < offset)
843 memset(good_buf + file_size, '\0', offset - file_size);
844 file_size = offset + size;
846 warn("Lite file size bug in fsx!");
851 if (testcalls <= simulatedopcount)
854 output_line(tf, OP_MAPWRITE, offset, size, &t);
856 if (file_size > cur_filesize) {
857 if (ftruncate(fd, file_size) == -1) {
858 prterr("domapwrite: ftruncate");
861 if (!quiet && (debug > 1 &&
862 (monitorstart == -1 ||
863 (offset + size > monitorstart &&
864 (monitorend == -1 || offset <= monitorend))))) {
865 gettimeofday(&t, NULL);
866 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
869 pg_offset = offset & page_mask;
870 map_size = pg_offset + size;
872 if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
873 fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
874 prterr("domapwrite: mmap");
877 if (!quiet && (debug > 1 &&
878 (monitorstart == -1 ||
879 (offset + size > monitorstart &&
880 (monitorend == -1 || offset <= monitorend))))) {
881 gettimeofday(&t, NULL);
882 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
884 memcpy(p + pg_offset, good_buf + offset, size);
885 if (!quiet && (debug > 1 &&
886 (monitorstart == -1 ||
887 (offset + size > monitorstart &&
888 (monitorend == -1 || offset <= monitorend))))) {
889 gettimeofday(&t, NULL);
890 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
892 if (msync(p, map_size, 0) != 0) {
893 prterr("domapwrite: msync");
896 if (!quiet && (debug > 1 &&
897 (monitorstart == -1 ||
898 (offset + size > monitorstart &&
899 (monitorend == -1 || offset <= monitorend))))) {
900 gettimeofday(&t, NULL);
901 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
903 if (munmap(p, map_size) != 0) {
904 prterr("domapwrite: munmap");
907 if (!quiet && (debug > 1 &&
908 (monitorstart == -1 ||
909 (offset + size > monitorstart &&
910 (monitorend == -1 || offset <= monitorend))))) {
911 gettimeofday(&t, NULL);
912 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
918 dotruncate(unsigned size)
921 int oldsize = file_size;
922 struct test_file *tf = get_tf();
925 size -= size % truncbdy;
926 gettimeofday(&t, NULL);
927 if (size > biggest) {
929 if (!quiet && testcalls > simulatedopcount)
930 prt("truncating to largest ever: 0x%x\n", size);
933 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t);
935 if (size > file_size)
936 memset(good_buf + file_size, '\0', size - file_size);
939 if (testcalls <= simulatedopcount)
942 output_line(tf, OP_TRUNCATE, oldsize, size, &t);
944 if (ftruncate(fd, (off_t)size) == -1) {
945 prt("ftruncate1: %x\n", size);
946 prterr("dotruncate: ftruncate");
949 if (!quiet && debug > 1) {
950 gettimeofday(&t, NULL);
951 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
962 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
963 prterr("writefileimage: lseek");
966 iret = write(fd, good_buf, file_size);
967 if ((off_t)iret != file_size) {
969 prterr("writefileimage: write");
971 prt("short write: 0x%lx bytes instead of 0x%llx\n",
973 (unsigned long long)file_size);
976 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
977 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
978 prterr("writefileimage: ftruncate");
988 struct test_file *tf = get_tf();
990 if (testcalls <= simulatedopcount)
993 gettimeofday(&t, NULL);
994 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t);
997 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
1000 prterr("docloseopen: close");
1001 report_failure(180);
1003 if (!quiet && debug > 1) {
1004 gettimeofday(&t, NULL);
1005 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
1007 tf->fd = open(tf->path, O_RDWR, 0);
1009 prterr("docloseopen: open");
1010 report_failure(181);
1012 if (!quiet && debug > 1) {
1013 gettimeofday(&t, NULL);
1014 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
1022 unsigned long offset;
1023 unsigned long size = maxoplen;
1024 unsigned long rv = random();
1025 unsigned long op = rv % (3 + !lite + mapped_writes);
1027 /* turn off the map read if necessary */
1029 if (op == 2 && !mapped_reads)
1032 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1037 if (debugstart > 0 && testcalls >= debugstart)
1040 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1041 prt("%lu...\n", testcalls);
1048 * MAPWRITE: op = 3 or 4
1050 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
1051 dotruncate(random() % maxfilelen);
1054 size = random() % (maxoplen+1);
1055 if (lite ? 0 : op == 3)
1059 if (op == 1 || op == (lite ? 3 : 4)) {
1060 offset %= maxfilelen;
1061 if (offset + size > maxfilelen)
1062 size = maxfilelen - offset;
1064 domapwrite(offset, size);
1066 dowrite(offset, size);
1069 offset %= file_size;
1072 if (offset + size > file_size)
1073 size = file_size - offset;
1075 domapread(offset, size);
1077 doread(offset, size);
1081 if (sizechecks && testcalls > simulatedopcount)
1083 if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
1093 prt("signal %d\n", sig);
1094 prt("testcalls = %lu\n", testcalls);
1102 fprintf(stdout, "usage: %s",
1103 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m "
1104 "start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t "
1105 "truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] "
1106 "[ -I random|rotate ] fname [additional paths to fname..]\n"
1107 " -b opnum: beginning operation number (default 1)\n"
1108 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1109 " -d: debug output for all operations [-d -d = more debugging]\n"
1110 " -l flen: the upper bound on file size (default 262144)\n"
1111 " -m startop:endop: monitor (print debug output) specified byte rang"
1112 "(default 0:infinity)\n"
1113 " -n: no verifications of file size\n"
1114 " -o oplen: the upper bound on operation size (default 65536)\n"
1115 " -p progressinterval: debug output at specified operation interval\n"
1116 " -q: quieter operation\n"
1117 " -r readbdy: 4096 would make reads page aligned (default 1)\n"
1118 " -s style: 1 gives smaller truncates (default 0)\n"
1119 " -t truncbdy: 4096 would make truncates page aligned (default 1)\n"
1120 " -w writebdy: 4096 would make writes page aligned (default 1)\n"
1121 " -D startingop: debug output starting at specified operation\n"
1122 " -L: fsxLite - no file creations & no file size changes\n"
1123 " -N numops: total # operations to do (default infinity)\n"
1124 " -O: use oplen (see -o flag) for every op (default random)\n"
1125 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1126 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1127 " -W: mapped write operations DISabled\n"
1128 " -R: read() system calls only (mapped reads disabled)\n"
1129 " -I: When multiple paths to the file are given each operation uses"
1130 " a different path. Iterate through them in order with 'rotate'"
1131 " or chose then at 'random'. (defaults to random)\n"
1132 " fname: this filename is REQUIRED (no default)\n");
1138 getnum(char *s, char **e)
1143 ret = strtol(s, e, 0);
1171 main(int argc, char **argv)
1180 page_size = getpagesize();
1181 page_mask = page_size - 1;
1183 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1185 while ((ch = getopt(argc, argv,
1186 "b:c:dl:m:no:p:qr:s:t:w:D:I:LN:OP:RS:W"))
1190 simulatedopcount = getnum(optarg, &endp);
1192 fprintf(stdout, "Will begin at operation"
1195 if (simulatedopcount == 0)
1197 simulatedopcount -= 1;
1200 closeprob = getnum(optarg, &endp);
1203 "Chance of close/open is 1 in %d\n",
1212 maxfilelen = getnum(optarg, &endp);
1213 if (maxfilelen <= 0)
1217 monitorstart = getnum(optarg, &endp);
1218 if (monitorstart < 0)
1220 if (!endp || *endp++ != ':')
1222 monitorend = getnum(endp, &endp);
1225 if (monitorend == 0)
1226 monitorend = -1; /* aka infinity */
1232 maxoplen = getnum(optarg, &endp);
1237 progressinterval = getnum(optarg, &endp);
1238 if (progressinterval < 0)
1245 readbdy = getnum(optarg, &endp);
1250 style = getnum(optarg, &endp);
1251 if (style < 0 || style > 1)
1255 truncbdy = getnum(optarg, &endp);
1260 writebdy = getnum(optarg, &endp);
1265 debugstart = getnum(optarg, &endp);
1270 assign_fd_policy(optarg);
1276 numops = getnum(optarg, &endp);
1284 strncpy(goodfile, optarg, sizeof(goodfile));
1285 strcat(goodfile, "/");
1286 strncpy(logfile, optarg, sizeof(logfile));
1287 strcat(logfile, "/");
1294 seed = getnum(optarg, &endp);
1296 seed = time(0) % 10000;
1298 fprintf(stdout, "Seed set to %d\n", seed);
1305 fprintf(stdout, "mapped writes DISABLED\n");
1318 signal(SIGHUP, cleanup);
1319 signal(SIGINT, cleanup);
1320 signal(SIGPIPE, cleanup);
1321 signal(SIGALRM, cleanup);
1322 signal(SIGTERM, cleanup);
1323 signal(SIGXCPU, cleanup);
1324 signal(SIGXFSZ, cleanup);
1325 signal(SIGVTALRM, cleanup);
1326 signal(SIGUSR1, cleanup);
1327 signal(SIGUSR2, cleanup);
1329 initstate(seed, state, 256);
1332 open_test_files(argv, argc);
1334 strncat(goodfile, dirpath ? basename(fname) : fname, 256);
1335 strcat (goodfile, ".fsxgood");
1336 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1337 if (fsxgoodfd < 0) {
1341 strncat(logfile, dirpath ? basename(fname) : fname, 256);
1342 strcat (logfile, ".fsxlog");
1343 fsxlogf = fopen(logfile, "w");
1344 if (fsxlogf == NULL) {
1351 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1352 if (file_size == (off_t)-1) {
1354 warn("main: lseek eof");
1357 ret = lseek(fd, (off_t)0, SEEK_SET);
1358 if (ret == (off_t)-1) {
1360 warn("main: lseek 0");
1364 original_buf = (char *) malloc(maxfilelen);
1365 for (i = 0; i < maxfilelen; i++)
1366 original_buf[i] = random() % 256;
1367 good_buf = (char *) malloc(maxfilelen);
1368 memset(good_buf, '\0', maxfilelen);
1369 temp_buf = (char *) malloc(maxoplen);
1370 memset(temp_buf, '\0', maxoplen);
1371 if (lite) { /* zero entire existing file */
1375 written = write(fd, good_buf, (size_t)maxfilelen);
1376 if (written != maxfilelen) {
1377 if (written == -1) {
1379 warn("main: error on write");
1381 warn("main: short write, 0x%x bytes instead"
1383 (unsigned)written, maxfilelen);
1389 while (numops == -1 || numops--)
1393 prt("All operations completed A-OK!\n");