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 $
38 #include <sys/types.h>
40 #if defined(_UWIN) || defined(__linux__)
41 # include <sys/param.h>
45 # include <sys/time.h>
61 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
64 * Each test run will work with one or more separate file descriptors for the
65 * same file. This allows testing cache coherency across multiple mountpoints
66 * of the same network filesystem on a single client.
74 enum fd_iteration_policy {
79 int fd_policy = FD_RANDOM;
83 * A log entry is an operation and a bunch of arguments.
90 const struct test_file *tf;
93 #define LOGSIZE 100000
95 struct log_entry oplog[LOGSIZE]; /* the log */
96 int logptr = 0; /* current position in log */
97 int logcount = 0; /* total ops */
105 #define OP_TRUNCATE 3
106 #define OP_CLOSEOPEN 4
108 #define OP_MAPWRITE 6
114 char *original_buf; /* a pointer to the original data */
115 char *good_buf; /* a pointer to the correct data */
116 char *temp_buf; /* a pointer to the current data */
117 char *fname; /* name of our test file */
118 char logfile[PATH_MAX]; /* name of our log file */
119 char goodfile[PATH_MAX]; /* name of our test file */
124 unsigned long testcalls = 0; /* calls to function "test" */
126 long simulatedopcount = 0; /* -b flag */
127 int closeprob = 0; /* -c flag */
128 int debug = 0; /* -d flag */
129 long debugstart = 0; /* -D flag */
130 long maxfilelen = 256 * 1024; /* -l flag */
131 int sizechecks = 1; /* -n flag disables them */
132 int maxoplen = 64 * 1024; /* -o flag */
133 int quiet = 0; /* -q flag */
134 long progressinterval = 0; /* -p flag */
135 int readbdy = 1; /* -r flag */
136 int style = 0; /* -s flag */
137 int truncbdy = 1; /* -t flag */
138 int writebdy = 1; /* -w flag */
139 long monitorstart = -1; /* -m flag */
140 long monitorend = -1; /* -m flag */
141 int lite = 0; /* -L flag */
142 long numops = -1; /* -N flag */
143 int randomoplen = 1; /* -O flag disables it */
144 int seed = 1; /* -S flag */
145 int mapped_writes = 1; /* -W flag disables */
146 int mapped_reads = 1; /* -R flag disables it */
148 int o_direct; /* -Z */
149 FILE * fsxlogf = NULL;
153 vwarnc(code, fmt, ap)
158 fprintf(stderr, "fsx: ");
160 vfprintf(stderr, fmt, ap);
161 fprintf(stderr, ": ");
163 fprintf(stderr, "%s\n", strerror(code));
168 warn(const char * fmt, ...)
172 vwarnc(errno, fmt, ap);
178 __attribute__((format(printf, 1, 2)))
184 vfprintf(stdout, fmt, args);
189 vfprintf(fsxlogf, fmt, args);
197 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
202 log4(int operation, int arg0, int arg1, int arg2, struct timeval *tv,
203 const struct test_file *tf)
205 struct log_entry *le;
208 le->operation = operation;
216 if (logptr >= LOGSIZE)
221 fill_tf_buf(const struct test_file *tf)
223 static int max_tf_len;
224 static char tf_buf[32];
226 if (fd_policy == FD_SINGLE)
230 max_tf_len = snprintf(tf_buf, sizeof(tf_buf) - 1,
231 "%u", num_test_files - 1);
233 sprintf(tf_buf, "[%0*lu]", max_tf_len,
234 (unsigned long)(tf - test_files));
243 struct log_entry *lp;
245 prt("LOG DUMP (%d total operations):\n", logcount);
246 if (logcount < LOGSIZE) {
253 for ( ; count > 0; count--) {
256 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
258 prt("%d%s: %lu.%06lu ", opnum, fill_tf_buf(lp->tf),
259 lp->tv.tv_sec, lp->tv.tv_usec);
261 switch (lp->operation) {
263 prt("MAPREAD 0x%x thru 0x%x (0x%x bytes)",
264 lp->args[0], lp->args[0] + lp->args[1] - 1,
266 if (badoff >= lp->args[0] && badoff <
267 lp->args[0] + lp->args[1])
271 prt("MAPWRITE 0x%x thru 0x%x (0x%x bytes)",
272 lp->args[0], lp->args[0] + lp->args[1] - 1,
274 if (badoff >= lp->args[0] && badoff <
275 lp->args[0] + lp->args[1])
279 prt("READ 0x%x thru 0x%x (0x%x bytes)",
280 lp->args[0], lp->args[0] + lp->args[1] - 1,
282 if (badoff >= lp->args[0] &&
283 badoff < lp->args[0] + lp->args[1])
287 prt("WRITE 0x%x thru 0x%x (0x%x bytes)",
288 lp->args[0], lp->args[0] + lp->args[1] - 1,
290 if (lp->args[0] > lp->args[2])
292 else if (lp->args[0] + lp->args[1] > lp->args[2])
294 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
295 badoff < lp->args[0] + lp->args[1])
299 down = lp->args[0] < lp->args[1];
300 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
301 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
302 if (badoff >= lp->args[!down] &&
303 badoff < lp->args[!!down])
310 prt("SKIPPED (no operation)");
313 prt("BOGUS LOG ENTRY (operation code = %d)!",
325 save_buffer(char *buffer, off_t bufferlength, int fd)
328 ssize_t byteswritten;
330 if (fd <= 0 || bufferlength == 0)
333 if (bufferlength > SSIZE_MAX) {
334 prt("fsx flaw: overflow in save_buffer\n");
338 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
339 if (size_by_seek == (off_t)-1)
340 prterr("save_buffer: lseek eof");
341 else if (bufferlength > size_by_seek) {
342 warn("save_buffer: .fsxgood file too short... will"
343 "save 0x%llx bytes instead of 0x%llx\n",
344 (unsigned long long)size_by_seek,
345 (unsigned long long)bufferlength);
346 bufferlength = size_by_seek;
350 ret = lseek(fd, (off_t)0, SEEK_SET);
351 if (ret == (off_t)-1)
352 prterr("save_buffer: lseek 0");
354 byteswritten = write(fd, buffer, (size_t)bufferlength);
355 if (byteswritten != bufferlength) {
356 if (byteswritten == -1)
357 prterr("save_buffer write");
359 warn("save_buffer: short write, 0x%x bytes instead"
361 (unsigned)byteswritten,
362 (unsigned long long)bufferlength);
368 report_failure(int status)
374 save_buffer(good_buf, file_size, fsxgoodfd);
375 prt("Correct content saved for comparison\n");
376 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
385 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
386 *(((unsigned char *)(cp)) + 1)))
389 check_buffers(unsigned offset, unsigned size)
397 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
398 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
400 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
402 c = good_buf[offset];
406 bad = short_at(&temp_buf[i]);
407 prt("%#07x\t%#06x\t%#06x", offset,
408 short_at(&good_buf[offset]), bad);
409 op = temp_buf[offset & 1 ? i+1 : i];
421 prt("operation# (mod 256) for the bad data"
422 "may be %u\n", ((unsigned)op & 0xff));
424 prt("operation# (mod 256) for the bad data"
425 "unknown, check HOLE and EXTEND ops\n");
427 prt("????????????????\n");
448 prt("unknown policy");
452 return &test_files[ index % num_test_files ];
456 assign_fd_policy(char *policy)
458 if (!strcmp(policy, "random"))
459 fd_policy = FD_RANDOM;
460 else if (!strcmp(policy, "rotate"))
461 fd_policy = FD_ROTATE;
463 prt("unknown -I policy: '%s'\n", policy);
471 struct test_file *tf = get_tf();
475 static const char *my_basename(const char *path)
477 char *c = strrchr(path, '/');
479 return c ? c++ : path;
483 open_test_files(char **argv, int argc)
485 struct test_file *tf;
488 num_test_files = argc;
489 if (num_test_files == 1)
490 fd_policy = FD_SINGLE;
492 test_files = calloc(num_test_files, sizeof(*test_files));
493 if (test_files == NULL) {
494 prterr("reallocating space for test files");
498 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
501 tf->fd = open(tf->path, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|
509 if (quiet || fd_policy == FD_SINGLE)
512 for (i = 0, tf = test_files; i < num_test_files; i++, tf++)
513 prt("fd %d: %s\n", i, tf->path);
517 close_test_files(void)
520 struct test_file *tf;
522 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
538 if (fstat(fd, &statbuf)) {
539 prterr("check_size: fstat");
540 statbuf.st_size = -1;
542 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
543 if (file_size != statbuf.st_size || file_size != size_by_seek) {
544 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
545 (unsigned long long)file_size,
546 (unsigned long long)statbuf.st_size,
547 (unsigned long long)size_by_seek);
554 check_trunc_hack(void)
559 /* should not ignore ftruncate(2)'s return value */
560 if (ftruncate(fd, (off_t)0) < 0) {
561 prterr("trunc_hack: ftruncate(0)");
564 if (ftruncate(fd, (off_t)100000) < 0) {
565 prterr("trunc_hack: ftruncate(100000)");
568 if (fstat(fd, &statbuf)) {
569 prterr("trunc_hack: fstat");
570 statbuf.st_size = -1;
572 if (statbuf.st_size != (off_t)100000) {
573 prt("no extend on truncate! not posix!\n");
576 if (ftruncate(fd, 0) < 0) {
577 prterr("trunc_hack: ftruncate(0) (2nd call)");
583 output_line(struct test_file *tf, int op, unsigned offset,
584 unsigned size, struct timeval *tv)
588 [OP_WRITE] = "write",
589 [OP_TRUNCATE] = "trunc from",
590 [OP_MAPREAD] = "mapread",
591 [OP_MAPWRITE] = "mapwrite",
595 if (!(!quiet && ((progressinterval &&
596 testcalls % progressinterval == 0) ||
598 (monitorstart == -1 ||
599 (offset + size > monitorstart &&
600 (monitorend == -1 || offset <= monitorend)))))))
603 prt("%06lu%s %lu.%06lu %-10s %#08x %s %#08x\t(0x%x bytes)\n",
604 testcalls, fill_tf_buf(tf), tv->tv_sec, tv->tv_usec,
606 offset, op == OP_TRUNCATE ? " to " : "thru",
607 offset + size - 1, size);
611 doread(unsigned offset, unsigned size)
616 struct test_file *tf = get_tf();
619 offset -= offset % readbdy;
621 size -= size % readbdy;
622 gettimeofday(&t, NULL);
624 if (!quiet && testcalls > simulatedopcount && !o_direct)
625 prt("skipping zero size read\n");
626 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
629 if (size + offset > file_size) {
630 if (!quiet && testcalls > simulatedopcount)
631 prt("skipping seek/read past end of file\n");
632 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
636 log4(OP_READ, offset, size, 0, &t, tf);
638 if (testcalls <= simulatedopcount)
641 output_line(tf, OP_READ, offset, size, &t);
643 ret = lseek(fd, (off_t)offset, SEEK_SET);
644 if (ret == (off_t)-1) {
645 prterr("doread: lseek");
648 iret = read(fd, temp_buf, size);
649 if (!quiet && (debug > 1 &&
650 (monitorstart == -1 ||
651 (offset + size > monitorstart &&
652 (monitorend == -1 || offset <= monitorend))))) {
653 gettimeofday(&t, NULL);
654 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
658 prterr("doread: read");
660 prt("short read: 0x%x bytes instead of 0x%x\n",
664 check_buffers(offset, size);
669 domapread(unsigned offset, unsigned size)
675 struct test_file *tf = get_tf();
678 offset -= offset % readbdy;
679 gettimeofday(&t, NULL);
681 if (!quiet && testcalls > simulatedopcount)
682 prt("skipping zero size read\n");
683 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
686 if (size + offset > file_size) {
687 if (!quiet && testcalls > simulatedopcount)
688 prt("skipping seek/read past end of file\n");
689 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
693 log4(OP_MAPREAD, offset, size, 0, &t, tf);
695 if (testcalls <= simulatedopcount)
698 output_line(tf, OP_MAPREAD, offset, size, &t);
700 pg_offset = offset & page_mask;
701 map_size = pg_offset + size;
703 if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
704 (off_t)(offset - pg_offset))) == MAP_FAILED) {
705 prterr("domapread: mmap");
708 if (!quiet && (debug > 1 &&
709 (monitorstart == -1 ||
710 (offset + size > monitorstart &&
711 (monitorend == -1 || offset <= monitorend))))) {
712 gettimeofday(&t, NULL);
713 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
715 memcpy(temp_buf, p + pg_offset, size);
716 if (!quiet && (debug > 1 &&
717 (monitorstart == -1 ||
718 (offset + size > monitorstart &&
719 (monitorend == -1 || offset <= monitorend))))) {
720 gettimeofday(&t, NULL);
721 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
723 if (munmap(p, map_size) != 0) {
724 prterr("domapread: munmap");
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 munmap done\n", t.tv_sec, t.tv_usec);
735 check_buffers(offset, size);
740 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
743 good_buf[offset] = testcalls % 256;
745 good_buf[offset] += original_buf[offset];
752 dowrite(unsigned offset, unsigned size)
757 struct test_file *tf = get_tf();
760 offset -= offset % writebdy;
762 size -= size % writebdy;
763 gettimeofday(&t, NULL);
765 if (!quiet && testcalls > simulatedopcount && !o_direct)
766 prt("skipping zero size write\n");
767 log4(OP_SKIPPED, OP_WRITE, offset, size, &t, tf);
771 log4(OP_WRITE, offset, size, file_size, &t, tf);
773 gendata(original_buf, good_buf, offset, size);
774 if (file_size < offset + size) {
775 if (file_size < offset)
776 memset(good_buf + file_size, '\0', offset - file_size);
777 file_size = offset + size;
779 warn("Lite file size bug in fsx!");
784 if (testcalls <= simulatedopcount)
787 output_line(tf, OP_WRITE, offset, size, &t);
789 ret = lseek(fd, (off_t)offset, SEEK_SET);
790 if (ret == (off_t)-1) {
791 prterr("dowrite: lseek");
794 iret = write(fd, good_buf + offset, size);
795 if (!quiet && (debug > 1 &&
796 (monitorstart == -1 ||
797 (offset + size > monitorstart &&
798 (monitorend == -1 || offset <= monitorend))))) {
799 gettimeofday(&t, NULL);
800 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
804 prterr("dowrite: write");
806 prt("short write: 0x%x bytes instead of 0x%x\n",
814 domapwrite(unsigned offset, unsigned size)
821 struct test_file *tf = get_tf();
824 offset -= offset % writebdy;
825 gettimeofday(&t, NULL);
827 if (!quiet && testcalls > simulatedopcount)
828 prt("skipping zero size write\n");
829 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t, tf);
832 cur_filesize = file_size;
834 log4(OP_MAPWRITE, offset, size, 0, &t, tf);
836 gendata(original_buf, good_buf, offset, size);
837 if (file_size < offset + size) {
838 if (file_size < offset)
839 memset(good_buf + file_size, '\0', offset - file_size);
840 file_size = offset + size;
842 warn("Lite file size bug in fsx!");
847 if (testcalls <= simulatedopcount)
850 output_line(tf, OP_MAPWRITE, offset, size, &t);
852 if (file_size > cur_filesize) {
853 if (ftruncate(fd, file_size) == -1) {
854 prterr("domapwrite: ftruncate");
857 if (!quiet && (debug > 1 &&
858 (monitorstart == -1 ||
859 (offset + size > monitorstart &&
860 (monitorend == -1 || offset <= monitorend))))) {
861 gettimeofday(&t, NULL);
862 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
865 pg_offset = offset & page_mask;
866 map_size = pg_offset + size;
868 if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
869 fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
870 prterr("domapwrite: mmap");
873 if (!quiet && (debug > 1 &&
874 (monitorstart == -1 ||
875 (offset + size > monitorstart &&
876 (monitorend == -1 || offset <= monitorend))))) {
877 gettimeofday(&t, NULL);
878 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
880 memcpy(p + pg_offset, good_buf + offset, size);
881 if (!quiet && (debug > 1 &&
882 (monitorstart == -1 ||
883 (offset + size > monitorstart &&
884 (monitorend == -1 || offset <= monitorend))))) {
885 gettimeofday(&t, NULL);
886 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
888 if (msync(p, map_size, 0) != 0) {
889 prterr("domapwrite: msync");
892 if (!quiet && (debug > 1 &&
893 (monitorstart == -1 ||
894 (offset + size > monitorstart &&
895 (monitorend == -1 || offset <= monitorend))))) {
896 gettimeofday(&t, NULL);
897 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
899 if (munmap(p, map_size) != 0) {
900 prterr("domapwrite: munmap");
903 if (!quiet && (debug > 1 &&
904 (monitorstart == -1 ||
905 (offset + size > monitorstart &&
906 (monitorend == -1 || offset <= monitorend))))) {
907 gettimeofday(&t, NULL);
908 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
914 dotruncate(unsigned size)
917 int oldsize = file_size;
918 struct test_file *tf = get_tf();
921 size -= size % truncbdy;
922 gettimeofday(&t, NULL);
923 if (size > biggest) {
925 if (!quiet && testcalls > simulatedopcount)
926 prt("truncating to largest ever: 0x%x\n", size);
929 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t, tf);
931 if (size > file_size)
932 memset(good_buf + file_size, '\0', size - file_size);
935 if (testcalls <= simulatedopcount)
938 output_line(tf, OP_TRUNCATE, oldsize, size, &t);
940 if (ftruncate(fd, (off_t)size) == -1) {
941 prt("ftruncate1: %x\n", size);
942 prterr("dotruncate: ftruncate");
945 if (!quiet && debug > 1) {
946 gettimeofday(&t, NULL);
947 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
958 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
959 prterr("writefileimage: lseek");
962 iret = write(fd, good_buf, file_size);
963 if ((off_t)iret != file_size) {
965 prterr("writefileimage: write");
967 prt("short write: 0x%lx bytes instead of 0x%llx\n",
969 (unsigned long long)file_size);
972 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
973 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
974 prterr("writefileimage: ftruncate");
984 struct test_file *tf = get_tf();
986 if (testcalls <= simulatedopcount)
989 gettimeofday(&t, NULL);
990 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t, tf);
993 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
996 prterr("docloseopen: close");
999 if (!quiet && debug > 1) {
1000 gettimeofday(&t, NULL);
1001 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
1003 tf->fd = open(tf->path, O_RDWR|o_direct, 0);
1005 prterr("docloseopen: open");
1006 report_failure(181);
1008 if (!quiet && debug > 1) {
1009 gettimeofday(&t, NULL);
1010 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
1018 unsigned long offset;
1019 unsigned long size = maxoplen;
1020 unsigned long rv = random();
1021 unsigned long op = rv % (3 + !lite + mapped_writes);
1023 /* turn off the map read if necessary */
1025 if (op == 2 && !mapped_reads)
1028 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1033 if (debugstart > 0 && testcalls >= debugstart)
1036 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1037 prt("%lu...\n", testcalls);
1044 * MAPWRITE: op = 3 or 4
1046 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
1047 dotruncate(random() % maxfilelen);
1050 size = random() % (maxoplen+1);
1051 if (lite ? 0 : op == 3)
1055 if (op == 1 || op == (lite ? 3 : 4)) {
1056 offset %= maxfilelen;
1057 if (offset + size > maxfilelen)
1058 size = maxfilelen - offset;
1060 domapwrite(offset, size);
1062 dowrite(offset, size);
1065 offset %= file_size;
1068 if (offset + size > file_size)
1069 size = file_size - offset;
1071 domapread(offset, size);
1073 doread(offset, size);
1077 if (sizechecks && testcalls > simulatedopcount)
1079 if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
1089 prt("signal %d\n", sig);
1090 prt("testcalls = %lu\n", testcalls);
1098 fprintf(stdout, "usage: %s",
1099 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m "
1100 "start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t "
1101 "truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] "
1102 "[ -I random|rotate ] fname [additional paths to fname..]\n"
1103 " -b opnum: beginning operation number (default 1)\n"
1104 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1105 " -d: debug output for all operations [-d -d = more debugging]\n"
1106 " -l flen: the upper bound on file size (default 262144)\n"
1107 " -m startop:endop: monitor (print debug output) specified byte rang"
1108 "(default 0:infinity)\n"
1109 " -n: no verifications of file size\n"
1110 " -o oplen: the upper bound on operation size (default 65536)\n"
1111 " -p progressinterval: debug output at specified operation interval\n"
1112 " -q: quieter operation\n"
1113 " -r readbdy: 4096 would make reads page aligned (default 1)\n"
1114 " -s style: 1 gives smaller truncates (default 0)\n"
1115 " -t truncbdy: 4096 would make truncates page aligned (default 1)\n"
1116 " -w writebdy: 4096 would make writes page aligned (default 1)\n"
1117 " -D startingop: debug output starting at specified operation\n"
1118 " -L: fsxLite - no file creations & no file size changes\n"
1119 " -N numops: total # operations to do (default infinity)\n"
1120 " -O: use oplen (see -o flag) for every op (default random)\n"
1121 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1122 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1123 " -W: mapped write operations DISabled\n"
1124 " -R: read() system calls only (mapped reads disabled)\n"
1125 " -Z: O_DIRECT (use -R, -W, -r and -w too)\n"
1126 " -I: When multiple paths to the file are given each operation uses\n"
1127 " a different path. Iterate through them in order with 'rotate'\n"
1128 " or chose then at 'random'. (defaults to random)\n"
1129 " fname: this filename is REQUIRED (no default)\n");
1135 getnum(char *s, char **e)
1140 ret = strtol(s, e, 0);
1168 main(int argc, char **argv)
1177 page_size = getpagesize();
1178 page_mask = page_size - 1;
1180 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1182 while ((ch = getopt(argc, argv,
1183 "b:c:dl:m:no:p:qr:s:t:w:D:I:LN:OP:RS:WZ"))
1187 simulatedopcount = getnum(optarg, &endp);
1189 fprintf(stdout, "Will begin at operation"
1192 if (simulatedopcount == 0)
1194 simulatedopcount -= 1;
1197 closeprob = getnum(optarg, &endp);
1200 "Chance of close/open is 1 in %d\n",
1209 maxfilelen = getnum(optarg, &endp);
1210 if (maxfilelen <= 0)
1214 monitorstart = getnum(optarg, &endp);
1215 if (monitorstart < 0)
1217 if (!endp || *endp++ != ':')
1219 monitorend = getnum(endp, &endp);
1222 if (monitorend == 0)
1223 monitorend = -1; /* aka infinity */
1229 maxoplen = getnum(optarg, &endp);
1234 progressinterval = getnum(optarg, &endp);
1235 if (progressinterval < 0)
1242 readbdy = getnum(optarg, &endp);
1247 style = getnum(optarg, &endp);
1248 if (style < 0 || style > 1)
1252 truncbdy = getnum(optarg, &endp);
1257 writebdy = getnum(optarg, &endp);
1262 debugstart = getnum(optarg, &endp);
1267 assign_fd_policy(optarg);
1273 numops = getnum(optarg, &endp);
1281 strncpy(goodfile, optarg, sizeof(goodfile) - 1);
1282 strcat(goodfile, "/");
1283 strncpy(logfile, optarg, sizeof(logfile) - 1);
1284 strcat(logfile, "/");
1291 seed = getnum(optarg, &endp);
1293 seed = time(0) % 10000;
1295 fprintf(stdout, "Seed set to %d\n", seed);
1302 fprintf(stdout, "mapped writes DISABLED\n");
1305 o_direct = O_DIRECT;
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 ? my_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 ? my_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;
1370 ret = posix_memalign((void **)&good_buf, writebdy, maxfilelen);
1372 prt("main: posix_memalign failed: %s\n", strerror(ret));
1376 ret = posix_memalign((void **)&temp_buf, readbdy, maxoplen);
1378 prt("main: posix_memalign failed: %s\n", strerror(ret));
1382 good_buf = malloc(maxfilelen);
1384 prt("malloc failed.\n");
1388 temp_buf = malloc(maxoplen);
1390 prt("malloc failed.\n");
1394 memset(good_buf, 0, maxfilelen);
1395 memset(temp_buf, 0, maxoplen);
1397 if (lite) { /* zero entire existing file */
1401 written = write(fd, good_buf, (size_t)maxfilelen);
1402 if (written != maxfilelen) {
1403 if (written == -1) {
1405 warn("main: error on write");
1407 warn("main: short write, 0x%x bytes instead"
1409 (unsigned)written, maxfilelen);
1415 while (numops == -1 || numops--)
1419 prt("All operations completed A-OK!\n");