2 * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved.
4 * Copyright (c) 2012, Intel Corporation.
6 * @APPLE_LICENSE_HEADER_START@
8 * The contents of this file constitute Original Code as defined in and
9 * are subject to the Apple Public Source License Version 1.1 (the
10 * "License"). You may not use this file except in compliance with the
11 * License. Please obtain a copy of the License at
12 * http://www.apple.com/publicsource and read it before using this file.
14 * This Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Author: Avadis Tevanian, Jr.
27 * File system exerciser.
29 * Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com
31 * Various features from Joe Sokol, Pat Dirks, and Clark Warner.
33 * Small changes to work under Linux -- davej@suse.de
35 * Sundry porting patches from Guy Harris 12/2001
36 * $FreeBSD: src/tools/regression/fsx/fsx.c,v 1.1 2001/12/20 04:15:57 jkh Exp $
39 #include <sys/types.h>
41 #if defined(_UWIN) || defined(__linux__)
42 # include <sys/param.h>
46 # include <sys/time.h>
62 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
65 * Each test run will work with one or more separate file descriptors for the
66 * same file. This allows testing cache coherency across multiple mountpoints
67 * of the same network filesystem on a single client.
75 enum fd_iteration_policy {
80 int fd_policy = FD_RANDOM;
84 * A log entry is an operation and a bunch of arguments.
91 const struct test_file *tf;
94 #define LOGSIZE 100000
96 struct log_entry oplog[LOGSIZE]; /* the log */
97 int logptr = 0; /* current position in log */
98 int logcount = 0; /* total ops */
106 #define OP_TRUNCATE 3
107 #define OP_CLOSEOPEN 4
109 #define OP_MAPWRITE 6
115 char *original_buf; /* a pointer to the original data */
116 char *good_buf; /* a pointer to the correct data */
117 char *temp_buf; /* a pointer to the current data */
118 char *fname; /* name of our test file */
119 char logfile[1024]; /* name of our log file */
120 char goodfile[1024]; /* name of our test file */
125 unsigned long testcalls = 0; /* calls to function "test" */
127 long simulatedopcount = 0; /* -b flag */
128 int closeprob = 0; /* -c flag */
129 int debug = 0; /* -d flag */
130 long debugstart = 0; /* -D flag */
131 long maxfilelen = 256 * 1024; /* -l flag */
132 int sizechecks = 1; /* -n flag disables them */
133 int maxoplen = 64 * 1024; /* -o flag */
134 int quiet = 0; /* -q flag */
135 long progressinterval = 0; /* -p flag */
136 int readbdy = 1; /* -r flag */
137 int style = 0; /* -s flag */
138 int truncbdy = 1; /* -t flag */
139 int writebdy = 1; /* -w flag */
140 long monitorstart = -1; /* -m flag */
141 long monitorend = -1; /* -m flag */
142 int lite = 0; /* -L flag */
143 long numops = -1; /* -N flag */
144 int randomoplen = 1; /* -O flag disables it */
145 int seed = 1; /* -S flag */
146 int mapped_writes = 1; /* -W flag disables */
147 int mapped_reads = 1; /* -R flag disables it */
149 FILE * fsxlogf = NULL;
154 vwarnc(code, fmt, ap)
159 fprintf(stderr, "fsx: ");
161 vfprintf(stderr, fmt, ap);
162 fprintf(stderr, ": ");
164 fprintf(stderr, "%s\n", strerror(code));
169 warn(const char * fmt, ...)
173 vwarnc(errno, fmt, ap);
179 __attribute__((format(printf, 1, 2)))
185 vfprintf(stdout, fmt, args);
190 vfprintf(fsxlogf, fmt, args);
198 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
203 log4(int operation, int arg0, int arg1, int arg2, struct timeval *tv,
204 const struct test_file *tf)
206 struct log_entry *le;
209 le->operation = operation;
217 if (logptr >= LOGSIZE)
222 fill_tf_buf(const struct test_file *tf)
224 static int max_tf_len;
225 static char tf_buf[32];
227 if (fd_policy == FD_SINGLE)
231 max_tf_len = snprintf(tf_buf, sizeof(tf_buf) - 1,
232 "%u", num_test_files - 1);
234 sprintf(tf_buf, "[%0*lu]", max_tf_len,
235 (unsigned long)(tf - test_files));
244 struct log_entry *lp;
246 prt("LOG DUMP (%d total operations):\n", logcount);
247 if (logcount < LOGSIZE) {
254 for ( ; count > 0; count--) {
257 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
259 prt("%d%s: %lu.%06lu ", opnum, fill_tf_buf(lp->tf),
260 lp->tv.tv_sec, lp->tv.tv_usec);
262 switch (lp->operation) {
264 prt("MAPREAD 0x%x thru 0x%x (0x%x bytes)",
265 lp->args[0], lp->args[0] + lp->args[1] - 1,
267 if (badoff >= lp->args[0] && badoff <
268 lp->args[0] + lp->args[1])
272 prt("MAPWRITE 0x%x thru 0x%x (0x%x bytes)",
273 lp->args[0], lp->args[0] + lp->args[1] - 1,
275 if (badoff >= lp->args[0] && badoff <
276 lp->args[0] + lp->args[1])
280 prt("READ 0x%x thru 0x%x (0x%x bytes)",
281 lp->args[0], lp->args[0] + lp->args[1] - 1,
283 if (badoff >= lp->args[0] &&
284 badoff < lp->args[0] + lp->args[1])
288 prt("WRITE 0x%x thru 0x%x (0x%x bytes)",
289 lp->args[0], lp->args[0] + lp->args[1] - 1,
291 if (lp->args[0] > lp->args[2])
293 else if (lp->args[0] + lp->args[1] > lp->args[2])
295 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
296 badoff < lp->args[0] + lp->args[1])
300 down = lp->args[0] < lp->args[1];
301 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
302 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
303 if (badoff >= lp->args[!down] &&
304 badoff < lp->args[!!down])
311 prt("SKIPPED (no operation)");
314 prt("BOGUS LOG ENTRY (operation code = %d)!",
326 save_buffer(char *buffer, off_t bufferlength, int fd)
329 ssize_t byteswritten;
331 if (fd <= 0 || bufferlength == 0)
334 if (bufferlength > SSIZE_MAX) {
335 prt("fsx flaw: overflow in save_buffer\n");
339 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
340 if (size_by_seek == (off_t)-1)
341 prterr("save_buffer: lseek eof");
342 else if (bufferlength > size_by_seek) {
343 warn("save_buffer: .fsxgood file too short... will"
344 "save 0x%llx bytes instead of 0x%llx\n",
345 (unsigned long long)size_by_seek,
346 (unsigned long long)bufferlength);
347 bufferlength = size_by_seek;
351 ret = lseek(fd, (off_t)0, SEEK_SET);
352 if (ret == (off_t)-1)
353 prterr("save_buffer: lseek 0");
355 byteswritten = write(fd, buffer, (size_t)bufferlength);
356 if (byteswritten != bufferlength) {
357 if (byteswritten == -1)
358 prterr("save_buffer write");
360 warn("save_buffer: short write, 0x%x bytes instead"
362 (unsigned)byteswritten,
363 (unsigned long long)bufferlength);
369 report_failure(int status)
375 save_buffer(good_buf, file_size, fsxgoodfd);
376 prt("Correct content saved for comparison\n");
377 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
386 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
387 *(((unsigned char *)(cp)) + 1)))
390 check_buffers(unsigned offset, unsigned size)
398 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
399 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
401 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
403 c = good_buf[offset];
407 bad = short_at(&temp_buf[i]);
408 prt("%#07x\t%#06x\t%#06x", offset,
409 short_at(&good_buf[offset]), bad);
410 op = temp_buf[offset & 1 ? i+1 : i];
422 prt("operation# (mod 256) for the bad data"
423 "may be %u\n", ((unsigned)op & 0xff));
425 prt("operation# (mod 256) for the bad data"
426 "unknown, check HOLE and EXTEND ops\n");
428 prt("????????????????\n");
449 prt("unknown policy");
453 return &test_files[ index % num_test_files ];
457 assign_fd_policy(char *policy)
459 if (!strcmp(policy, "random"))
460 fd_policy = FD_RANDOM;
461 else if (!strcmp(policy, "rotate"))
462 fd_policy = FD_ROTATE;
464 prt("unknown -I policy: '%s'\n", policy);
472 struct test_file *tf = get_tf();
476 static const char *my_basename(const char *path)
478 char *c = strrchr(path, '/');
480 return c ? c++ : path;
484 open_test_files(char **argv, int argc)
486 struct test_file *tf;
489 num_test_files = argc;
490 if (num_test_files == 1)
491 fd_policy = FD_SINGLE;
493 test_files = calloc(num_test_files, sizeof(*test_files));
494 if (test_files == NULL) {
495 prterr("reallocating space for test files");
499 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
502 tf->fd = open(tf->path, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC),
510 if (quiet || fd_policy == FD_SINGLE)
513 for (i = 0, tf = test_files; i < num_test_files; i++, tf++)
514 prt("fd %d: %s\n", i, tf->path);
518 close_test_files(void)
521 struct test_file *tf;
523 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
539 if (fstat(fd, &statbuf)) {
540 prterr("check_size: fstat");
541 statbuf.st_size = -1;
543 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
544 if (file_size != statbuf.st_size || file_size != size_by_seek) {
545 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
546 (unsigned long long)file_size,
547 (unsigned long long)statbuf.st_size,
548 (unsigned long long)size_by_seek);
555 check_trunc_hack(void)
560 /* should not ignore ftruncate(2)'s return value */
561 if (ftruncate(fd, (off_t)0) < 0) {
562 prterr("trunc_hack: ftruncate(0)");
565 if (ftruncate(fd, (off_t)100000) < 0) {
566 prterr("trunc_hack: ftruncate(100000)");
569 if (fstat(fd, &statbuf)) {
570 prterr("trunc_hack: fstat");
571 statbuf.st_size = -1;
573 if (statbuf.st_size != (off_t)100000) {
574 prt("no extend on truncate! not posix!\n");
577 if (ftruncate(fd, 0) < 0) {
578 prterr("trunc_hack: ftruncate(0) (2nd call)");
584 output_line(struct test_file *tf, int op, unsigned offset,
585 unsigned size, struct timeval *tv)
589 [OP_WRITE] = "write",
590 [OP_TRUNCATE] = "trunc from",
591 [OP_MAPREAD] = "mapread",
592 [OP_MAPWRITE] = "mapwrite",
596 if (!(!quiet && ((progressinterval &&
597 testcalls % progressinterval == 0) ||
599 (monitorstart == -1 ||
600 (offset + size > monitorstart &&
601 (monitorend == -1 || offset <= monitorend)))))))
604 prt("%06lu%s %lu.%06lu %-10s %#08x %s %#08x\t(0x%x bytes)\n",
605 testcalls, fill_tf_buf(tf), tv->tv_sec, tv->tv_usec,
607 offset, op == OP_TRUNCATE ? " to " : "thru",
608 offset + size - 1, size);
612 doread(unsigned offset, unsigned size)
617 struct test_file *tf = get_tf();
620 offset -= offset % readbdy;
621 gettimeofday(&t, NULL);
623 if (!quiet && testcalls > simulatedopcount)
624 prt("skipping zero size read\n");
625 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
628 if (size + offset > file_size) {
629 if (!quiet && testcalls > simulatedopcount)
630 prt("skipping seek/read past end of file\n");
631 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
635 log4(OP_READ, offset, size, 0, &t, tf);
637 if (testcalls <= simulatedopcount)
640 output_line(tf, OP_READ, offset, size, &t);
642 ret = lseek(fd, (off_t)offset, SEEK_SET);
643 if (ret == (off_t)-1) {
644 prterr("doread: lseek");
647 iret = read(fd, temp_buf, size);
648 if (!quiet && (debug > 1 &&
649 (monitorstart == -1 ||
650 (offset + size > monitorstart &&
651 (monitorend == -1 || offset <= monitorend))))) {
652 gettimeofday(&t, NULL);
653 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
657 prterr("doread: read");
659 prt("short read: 0x%x bytes instead of 0x%x\n",
663 check_buffers(offset, size);
668 domapread(unsigned offset, unsigned size)
674 struct test_file *tf = get_tf();
677 offset -= offset % readbdy;
678 gettimeofday(&t, NULL);
680 if (!quiet && testcalls > simulatedopcount)
681 prt("skipping zero size read\n");
682 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
685 if (size + offset > file_size) {
686 if (!quiet && testcalls > simulatedopcount)
687 prt("skipping seek/read past end of file\n");
688 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
692 log4(OP_MAPREAD, offset, size, 0, &t, tf);
694 if (testcalls <= simulatedopcount)
697 output_line(tf, OP_MAPREAD, offset, size, &t);
699 pg_offset = offset & page_mask;
700 map_size = pg_offset + size;
702 if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
703 (off_t)(offset - pg_offset))) == MAP_FAILED) {
704 prterr("domapread: mmap");
707 if (!quiet && (debug > 1 &&
708 (monitorstart == -1 ||
709 (offset + size > monitorstart &&
710 (monitorend == -1 || offset <= monitorend))))) {
711 gettimeofday(&t, NULL);
712 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
714 memcpy(temp_buf, p + pg_offset, size);
715 if (!quiet && (debug > 1 &&
716 (monitorstart == -1 ||
717 (offset + size > monitorstart &&
718 (monitorend == -1 || offset <= monitorend))))) {
719 gettimeofday(&t, NULL);
720 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
722 if (munmap(p, map_size) != 0) {
723 prterr("domapread: munmap");
726 if (!quiet && (debug > 1 &&
727 (monitorstart == -1 ||
728 (offset + size > monitorstart &&
729 (monitorend == -1 || offset <= monitorend))))) {
730 gettimeofday(&t, NULL);
731 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
734 check_buffers(offset, size);
739 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
742 good_buf[offset] = testcalls % 256;
744 good_buf[offset] += original_buf[offset];
751 dowrite(unsigned offset, unsigned size)
756 struct test_file *tf = get_tf();
759 offset -= offset % writebdy;
760 gettimeofday(&t, NULL);
762 if (!quiet && testcalls > simulatedopcount)
763 prt("skipping zero size write\n");
764 log4(OP_SKIPPED, OP_WRITE, offset, size, &t, tf);
768 log4(OP_WRITE, offset, size, file_size, &t, tf);
770 gendata(original_buf, good_buf, offset, size);
771 if (file_size < offset + size) {
772 if (file_size < offset)
773 memset(good_buf + file_size, '\0', offset - file_size);
774 file_size = offset + size;
776 warn("Lite file size bug in fsx!");
781 if (testcalls <= simulatedopcount)
784 output_line(tf, OP_WRITE, offset, size, &t);
786 ret = lseek(fd, (off_t)offset, SEEK_SET);
787 if (ret == (off_t)-1) {
788 prterr("dowrite: lseek");
791 iret = write(fd, good_buf + offset, size);
792 if (!quiet && (debug > 1 &&
793 (monitorstart == -1 ||
794 (offset + size > monitorstart &&
795 (monitorend == -1 || offset <= monitorend))))) {
796 gettimeofday(&t, NULL);
797 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
801 prterr("dowrite: write");
803 prt("short write: 0x%x bytes instead of 0x%x\n",
811 domapwrite(unsigned offset, unsigned size)
818 struct test_file *tf = get_tf();
821 offset -= offset % writebdy;
822 gettimeofday(&t, NULL);
824 if (!quiet && testcalls > simulatedopcount)
825 prt("skipping zero size write\n");
826 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t, tf);
829 cur_filesize = file_size;
831 log4(OP_MAPWRITE, offset, size, 0, &t, tf);
833 gendata(original_buf, good_buf, offset, size);
834 if (file_size < offset + size) {
835 if (file_size < offset)
836 memset(good_buf + file_size, '\0', offset - file_size);
837 file_size = offset + size;
839 warn("Lite file size bug in fsx!");
844 if (testcalls <= simulatedopcount)
847 output_line(tf, OP_MAPWRITE, offset, size, &t);
849 if (file_size > cur_filesize) {
850 if (ftruncate(fd, file_size) == -1) {
851 prterr("domapwrite: ftruncate");
854 if (!quiet && (debug > 1 &&
855 (monitorstart == -1 ||
856 (offset + size > monitorstart &&
857 (monitorend == -1 || offset <= monitorend))))) {
858 gettimeofday(&t, NULL);
859 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
862 pg_offset = offset & page_mask;
863 map_size = pg_offset + size;
865 if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
866 fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
867 prterr("domapwrite: mmap");
870 if (!quiet && (debug > 1 &&
871 (monitorstart == -1 ||
872 (offset + size > monitorstart &&
873 (monitorend == -1 || offset <= monitorend))))) {
874 gettimeofday(&t, NULL);
875 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
877 memcpy(p + pg_offset, good_buf + offset, size);
878 if (!quiet && (debug > 1 &&
879 (monitorstart == -1 ||
880 (offset + size > monitorstart &&
881 (monitorend == -1 || offset <= monitorend))))) {
882 gettimeofday(&t, NULL);
883 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
885 if (msync(p, map_size, 0) != 0) {
886 prterr("domapwrite: msync");
889 if (!quiet && (debug > 1 &&
890 (monitorstart == -1 ||
891 (offset + size > monitorstart &&
892 (monitorend == -1 || offset <= monitorend))))) {
893 gettimeofday(&t, NULL);
894 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
896 if (munmap(p, map_size) != 0) {
897 prterr("domapwrite: munmap");
900 if (!quiet && (debug > 1 &&
901 (monitorstart == -1 ||
902 (offset + size > monitorstart &&
903 (monitorend == -1 || offset <= monitorend))))) {
904 gettimeofday(&t, NULL);
905 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
911 dotruncate(unsigned size)
914 int oldsize = file_size;
915 struct test_file *tf = get_tf();
918 size -= size % truncbdy;
919 gettimeofday(&t, NULL);
920 if (size > biggest) {
922 if (!quiet && testcalls > simulatedopcount)
923 prt("truncating to largest ever: 0x%x\n", size);
926 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t, tf);
928 if (size > file_size)
929 memset(good_buf + file_size, '\0', size - file_size);
932 if (testcalls <= simulatedopcount)
935 output_line(tf, OP_TRUNCATE, oldsize, size, &t);
937 if (ftruncate(fd, (off_t)size) == -1) {
938 prt("ftruncate1: %x\n", size);
939 prterr("dotruncate: ftruncate");
942 if (!quiet && debug > 1) {
943 gettimeofday(&t, NULL);
944 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
955 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
956 prterr("writefileimage: lseek");
959 iret = write(fd, good_buf, file_size);
960 if ((off_t)iret != file_size) {
962 prterr("writefileimage: write");
964 prt("short write: 0x%lx bytes instead of 0x%llx\n",
966 (unsigned long long)file_size);
969 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
970 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
971 prterr("writefileimage: ftruncate");
981 struct test_file *tf = get_tf();
983 if (testcalls <= simulatedopcount)
986 gettimeofday(&t, NULL);
987 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t, tf);
990 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
993 prterr("docloseopen: close");
996 if (!quiet && debug > 1) {
997 gettimeofday(&t, NULL);
998 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
1000 tf->fd = open(tf->path, O_RDWR, 0);
1002 prterr("docloseopen: open");
1003 report_failure(181);
1005 if (!quiet && debug > 1) {
1006 gettimeofday(&t, NULL);
1007 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
1015 unsigned long offset;
1016 unsigned long size = maxoplen;
1017 unsigned long rv = random();
1018 unsigned long op = rv % (3 + !lite + mapped_writes);
1020 /* turn off the map read if necessary */
1022 if (op == 2 && !mapped_reads)
1025 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1030 if (debugstart > 0 && testcalls >= debugstart)
1033 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1034 prt("%lu...\n", testcalls);
1041 * MAPWRITE: op = 3 or 4
1043 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
1044 dotruncate(random() % maxfilelen);
1047 size = random() % (maxoplen+1);
1048 if (lite ? 0 : op == 3)
1052 if (op == 1 || op == (lite ? 3 : 4)) {
1053 offset %= maxfilelen;
1054 if (offset + size > maxfilelen)
1055 size = maxfilelen - offset;
1057 domapwrite(offset, size);
1059 dowrite(offset, size);
1062 offset %= file_size;
1065 if (offset + size > file_size)
1066 size = file_size - offset;
1068 domapread(offset, size);
1070 doread(offset, size);
1074 if (sizechecks && testcalls > simulatedopcount)
1076 if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
1086 prt("signal %d\n", sig);
1087 prt("testcalls = %lu\n", testcalls);
1095 fprintf(stdout, "usage: %s",
1096 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m "
1097 "start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t "
1098 "truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] "
1099 "[ -I random|rotate ] fname [additional paths to fname..]\n"
1100 " -b opnum: beginning operation number (default 1)\n"
1101 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1102 " -d: debug output for all operations [-d -d = more debugging]\n"
1103 " -l flen: the upper bound on file size (default 262144)\n"
1104 " -m startop:endop: monitor (print debug output) specified byte rang"
1105 "(default 0:infinity)\n"
1106 " -n: no verifications of file size\n"
1107 " -o oplen: the upper bound on operation size (default 65536)\n"
1108 " -p progressinterval: debug output at specified operation interval\n"
1109 " -q: quieter operation\n"
1110 " -r readbdy: 4096 would make reads page aligned (default 1)\n"
1111 " -s style: 1 gives smaller truncates (default 0)\n"
1112 " -t truncbdy: 4096 would make truncates page aligned (default 1)\n"
1113 " -w writebdy: 4096 would make writes page aligned (default 1)\n"
1114 " -D startingop: debug output starting at specified operation\n"
1115 " -L: fsxLite - no file creations & no file size changes\n"
1116 " -N numops: total # operations to do (default infinity)\n"
1117 " -O: use oplen (see -o flag) for every op (default random)\n"
1118 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1119 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1120 " -W: mapped write operations DISabled\n"
1121 " -R: read() system calls only (mapped reads disabled)\n"
1122 " -I: When multiple paths to the file are given each operation uses"
1123 " a different path. Iterate through them in order with 'rotate'"
1124 " or chose then at 'random'. (defaults to random)\n"
1125 " fname: this filename is REQUIRED (no default)\n");
1131 getnum(char *s, char **e)
1136 ret = strtol(s, e, 0);
1164 main(int argc, char **argv)
1173 page_size = getpagesize();
1174 page_mask = page_size - 1;
1176 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1178 while ((ch = getopt(argc, argv,
1179 "b:c:dl:m:no:p:qr:s:t:w:D:I:LN:OP:RS:W"))
1183 simulatedopcount = getnum(optarg, &endp);
1185 fprintf(stdout, "Will begin at operation"
1188 if (simulatedopcount == 0)
1190 simulatedopcount -= 1;
1193 closeprob = getnum(optarg, &endp);
1196 "Chance of close/open is 1 in %d\n",
1205 maxfilelen = getnum(optarg, &endp);
1206 if (maxfilelen <= 0)
1210 monitorstart = getnum(optarg, &endp);
1211 if (monitorstart < 0)
1213 if (!endp || *endp++ != ':')
1215 monitorend = getnum(endp, &endp);
1218 if (monitorend == 0)
1219 monitorend = -1; /* aka infinity */
1225 maxoplen = getnum(optarg, &endp);
1230 progressinterval = getnum(optarg, &endp);
1231 if (progressinterval < 0)
1238 readbdy = getnum(optarg, &endp);
1243 style = getnum(optarg, &endp);
1244 if (style < 0 || style > 1)
1248 truncbdy = getnum(optarg, &endp);
1253 writebdy = getnum(optarg, &endp);
1258 debugstart = getnum(optarg, &endp);
1263 assign_fd_policy(optarg);
1269 numops = getnum(optarg, &endp);
1277 strncpy(goodfile, optarg, sizeof(goodfile));
1278 strcat(goodfile, "/");
1279 strncpy(logfile, optarg, sizeof(logfile));
1280 strcat(logfile, "/");
1287 seed = getnum(optarg, &endp);
1289 seed = time(0) % 10000;
1291 fprintf(stdout, "Seed set to %d\n", seed);
1298 fprintf(stdout, "mapped writes DISABLED\n");
1311 signal(SIGHUP, cleanup);
1312 signal(SIGINT, cleanup);
1313 signal(SIGPIPE, cleanup);
1314 signal(SIGALRM, cleanup);
1315 signal(SIGTERM, cleanup);
1316 signal(SIGXCPU, cleanup);
1317 signal(SIGXFSZ, cleanup);
1318 signal(SIGVTALRM, cleanup);
1319 signal(SIGUSR1, cleanup);
1320 signal(SIGUSR2, cleanup);
1322 initstate(seed, state, 256);
1325 open_test_files(argv, argc);
1327 strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1328 strcat (goodfile, ".fsxgood");
1329 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1330 if (fsxgoodfd < 0) {
1334 strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1335 strcat (logfile, ".fsxlog");
1336 fsxlogf = fopen(logfile, "w");
1337 if (fsxlogf == NULL) {
1344 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1345 if (file_size == (off_t)-1) {
1347 warn("main: lseek eof");
1350 ret = lseek(fd, (off_t)0, SEEK_SET);
1351 if (ret == (off_t)-1) {
1353 warn("main: lseek 0");
1357 original_buf = (char *) malloc(maxfilelen);
1358 for (i = 0; i < maxfilelen; i++)
1359 original_buf[i] = random() % 256;
1360 good_buf = (char *) malloc(maxfilelen);
1361 memset(good_buf, '\0', maxfilelen);
1362 temp_buf = (char *) malloc(maxoplen);
1363 memset(temp_buf, '\0', maxoplen);
1364 if (lite) { /* zero entire existing file */
1368 written = write(fd, good_buf, (size_t)maxfilelen);
1369 if (written != maxfilelen) {
1370 if (written == -1) {
1372 warn("main: error on write");
1374 warn("main: short write, 0x%x bytes instead"
1376 (unsigned)written, maxfilelen);
1382 while (numops == -1 || numops--)
1386 prt("All operations completed A-OK!\n");