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>
60 #include <libcfs/util/string.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[PATH_MAX]; /* name of our log file */
120 char goodfile[PATH_MAX]; /* 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 int o_direct; /* -Z */
150 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 = scnprintf(tf_buf, sizeof(tf_buf) - 1,
232 "%u", num_test_files - 1);
234 snprintf(tf_buf, sizeof(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;
622 size -= size % readbdy;
623 gettimeofday(&t, NULL);
625 if (!quiet && testcalls > simulatedopcount && !o_direct)
626 prt("skipping zero size read\n");
627 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
630 if (size + offset > file_size) {
631 if (!quiet && testcalls > simulatedopcount)
632 prt("skipping seek/read past end of file\n");
633 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
637 log4(OP_READ, offset, size, 0, &t, tf);
639 if (testcalls <= simulatedopcount)
642 output_line(tf, OP_READ, offset, size, &t);
644 ret = lseek(fd, (off_t)offset, SEEK_SET);
645 if (ret == (off_t)-1) {
646 prterr("doread: lseek");
649 iret = read(fd, temp_buf, size);
650 if (!quiet && (debug > 1 &&
651 (monitorstart == -1 ||
652 (offset + size > monitorstart &&
653 (monitorend == -1 || offset <= monitorend))))) {
654 gettimeofday(&t, NULL);
655 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
659 prterr("doread: read");
661 prt("short read: 0x%x bytes instead of 0x%x\n",
665 check_buffers(offset, size);
670 domapread(unsigned offset, unsigned size)
676 struct test_file *tf = get_tf();
679 offset -= offset % readbdy;
680 gettimeofday(&t, NULL);
682 if (!quiet && testcalls > simulatedopcount)
683 prt("skipping zero size read\n");
684 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
687 if (size + offset > file_size) {
688 if (!quiet && testcalls > simulatedopcount)
689 prt("skipping seek/read past end of file\n");
690 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
694 log4(OP_MAPREAD, offset, size, 0, &t, tf);
696 if (testcalls <= simulatedopcount)
699 output_line(tf, OP_MAPREAD, offset, size, &t);
701 pg_offset = offset & page_mask;
702 map_size = pg_offset + size;
704 if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
705 (off_t)(offset - pg_offset))) == MAP_FAILED) {
706 prterr("domapread: mmap");
709 if (!quiet && (debug > 1 &&
710 (monitorstart == -1 ||
711 (offset + size > monitorstart &&
712 (monitorend == -1 || offset <= monitorend))))) {
713 gettimeofday(&t, NULL);
714 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
716 memcpy(temp_buf, p + pg_offset, size);
717 if (!quiet && (debug > 1 &&
718 (monitorstart == -1 ||
719 (offset + size > monitorstart &&
720 (monitorend == -1 || offset <= monitorend))))) {
721 gettimeofday(&t, NULL);
722 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
724 if (munmap(p, map_size) != 0) {
725 prterr("domapread: munmap");
728 if (!quiet && (debug > 1 &&
729 (monitorstart == -1 ||
730 (offset + size > monitorstart &&
731 (monitorend == -1 || offset <= monitorend))))) {
732 gettimeofday(&t, NULL);
733 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
736 check_buffers(offset, size);
741 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
744 good_buf[offset] = testcalls % 256;
746 good_buf[offset] += original_buf[offset];
753 dowrite(unsigned offset, unsigned size)
758 struct test_file *tf = get_tf();
761 offset -= offset % writebdy;
763 size -= size % writebdy;
764 gettimeofday(&t, NULL);
766 if (!quiet && testcalls > simulatedopcount && !o_direct)
767 prt("skipping zero size write\n");
768 log4(OP_SKIPPED, OP_WRITE, offset, size, &t, tf);
772 log4(OP_WRITE, offset, size, file_size, &t, tf);
774 gendata(original_buf, good_buf, offset, size);
775 if (file_size < offset + size) {
776 if (file_size < offset)
777 memset(good_buf + file_size, '\0', offset - file_size);
778 file_size = offset + size;
780 warn("Lite file size bug in fsx!");
785 if (testcalls <= simulatedopcount)
788 output_line(tf, OP_WRITE, offset, size, &t);
790 ret = lseek(fd, (off_t)offset, SEEK_SET);
791 if (ret == (off_t)-1) {
792 prterr("dowrite: lseek");
795 iret = write(fd, good_buf + offset, size);
796 if (!quiet && (debug > 1 &&
797 (monitorstart == -1 ||
798 (offset + size > monitorstart &&
799 (monitorend == -1 || offset <= monitorend))))) {
800 gettimeofday(&t, NULL);
801 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
805 prterr("dowrite: write");
807 prt("short write: 0x%x bytes instead of 0x%x\n",
815 domapwrite(unsigned offset, unsigned size)
822 struct test_file *tf = get_tf();
825 offset -= offset % writebdy;
826 gettimeofday(&t, NULL);
828 if (!quiet && testcalls > simulatedopcount)
829 prt("skipping zero size write\n");
830 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t, tf);
833 cur_filesize = file_size;
835 log4(OP_MAPWRITE, offset, size, 0, &t, tf);
837 gendata(original_buf, good_buf, offset, size);
838 if (file_size < offset + size) {
839 if (file_size < offset)
840 memset(good_buf + file_size, '\0', offset - file_size);
841 file_size = offset + size;
843 warn("Lite file size bug in fsx!");
848 if (testcalls <= simulatedopcount)
851 output_line(tf, OP_MAPWRITE, offset, size, &t);
853 if (file_size > cur_filesize) {
854 if (ftruncate(fd, file_size) == -1) {
855 prterr("domapwrite: ftruncate");
858 if (!quiet && (debug > 1 &&
859 (monitorstart == -1 ||
860 (offset + size > monitorstart &&
861 (monitorend == -1 || offset <= monitorend))))) {
862 gettimeofday(&t, NULL);
863 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
866 pg_offset = offset & page_mask;
867 map_size = pg_offset + size;
869 if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
870 fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
871 prterr("domapwrite: mmap");
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 mmap done\n", t.tv_sec, t.tv_usec);
881 memcpy(p + pg_offset, good_buf + offset, size);
882 if (!quiet && (debug > 1 &&
883 (monitorstart == -1 ||
884 (offset + size > monitorstart &&
885 (monitorend == -1 || offset <= monitorend))))) {
886 gettimeofday(&t, NULL);
887 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
889 if (msync(p, map_size, 0) != 0) {
890 prterr("domapwrite: msync");
893 if (!quiet && (debug > 1 &&
894 (monitorstart == -1 ||
895 (offset + size > monitorstart &&
896 (monitorend == -1 || offset <= monitorend))))) {
897 gettimeofday(&t, NULL);
898 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
900 if (munmap(p, map_size) != 0) {
901 prterr("domapwrite: munmap");
904 if (!quiet && (debug > 1 &&
905 (monitorstart == -1 ||
906 (offset + size > monitorstart &&
907 (monitorend == -1 || offset <= monitorend))))) {
908 gettimeofday(&t, NULL);
909 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
915 dotruncate(unsigned size)
918 int oldsize = file_size;
919 struct test_file *tf = get_tf();
922 size -= size % truncbdy;
923 gettimeofday(&t, NULL);
924 if (size > biggest) {
926 if (!quiet && testcalls > simulatedopcount)
927 prt("truncating to largest ever: 0x%x\n", size);
930 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t, tf);
932 if (size > file_size)
933 memset(good_buf + file_size, '\0', size - file_size);
936 if (testcalls <= simulatedopcount)
939 output_line(tf, OP_TRUNCATE, oldsize, size, &t);
941 if (ftruncate(fd, (off_t)size) == -1) {
942 prt("ftruncate1: %x\n", size);
943 prterr("dotruncate: ftruncate");
946 if (!quiet && debug > 1) {
947 gettimeofday(&t, NULL);
948 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
959 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
960 prterr("writefileimage: lseek");
963 iret = write(fd, good_buf, file_size);
964 if ((off_t)iret != file_size) {
966 prterr("writefileimage: write");
968 prt("short write: 0x%lx bytes instead of 0x%llx\n",
970 (unsigned long long)file_size);
973 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
974 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
975 prterr("writefileimage: ftruncate");
985 struct test_file *tf = get_tf();
987 if (testcalls <= simulatedopcount)
990 gettimeofday(&t, NULL);
991 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t, tf);
994 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
997 prterr("docloseopen: close");
1000 if (!quiet && debug > 1) {
1001 gettimeofday(&t, NULL);
1002 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
1004 tf->fd = open(tf->path, O_RDWR|o_direct, 0);
1006 prterr("docloseopen: open");
1007 report_failure(181);
1009 if (!quiet && debug > 1) {
1010 gettimeofday(&t, NULL);
1011 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
1019 unsigned long offset;
1020 unsigned long size = maxoplen;
1021 unsigned long rv = random();
1022 unsigned long op = rv % (3 + !lite + mapped_writes);
1024 /* turn off the map read if necessary */
1026 if (op == 2 && !mapped_reads)
1029 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1034 if (debugstart > 0 && testcalls >= debugstart)
1037 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1038 prt("%lu...\n", testcalls);
1045 * MAPWRITE: op = 3 or 4
1047 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
1048 dotruncate(random() % maxfilelen);
1051 size = random() % (maxoplen+1);
1052 if (lite ? 0 : op == 3)
1056 if (op == 1 || op == (lite ? 3 : 4)) {
1057 offset %= maxfilelen;
1058 if (offset + size > maxfilelen)
1059 size = maxfilelen - offset;
1061 domapwrite(offset, size);
1063 dowrite(offset, size);
1066 offset %= file_size;
1069 if (offset + size > file_size)
1070 size = file_size - offset;
1072 domapread(offset, size);
1074 doread(offset, size);
1078 if (sizechecks && testcalls > simulatedopcount)
1080 if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
1090 prt("signal %d\n", sig);
1091 prt("testcalls = %lu\n", testcalls);
1099 fprintf(stdout, "usage: %s",
1100 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m "
1101 "start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t "
1102 "truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] "
1103 "[ -I random|rotate ] fname [additional paths to fname..]\n"
1104 " -b opnum: beginning operation number (default 1)\n"
1105 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1106 " -d: debug output for all operations [-d -d = more debugging]\n"
1107 " -l flen: the upper bound on file size (default 262144)\n"
1108 " -m startop:endop: monitor (print debug output) specified byte rang"
1109 "(default 0:infinity)\n"
1110 " -n: no verifications of file size\n"
1111 " -o oplen: the upper bound on operation size (default 65536)\n"
1112 " -p progressinterval: debug output at specified operation interval\n"
1113 " -q: quieter operation\n"
1114 " -r readbdy: 4096 would make reads page aligned (default 1)\n"
1115 " -s style: 1 gives smaller truncates (default 0)\n"
1116 " -t truncbdy: 4096 would make truncates page aligned (default 1)\n"
1117 " -w writebdy: 4096 would make writes page aligned (default 1)\n"
1118 " -D startingop: debug output starting at specified operation\n"
1119 " -L: fsxLite - no file creations & no file size changes\n"
1120 " -N numops: total # operations to do (default infinity)\n"
1121 " -O: use oplen (see -o flag) for every op (default random)\n"
1122 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1123 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1124 " -W: mapped write operations DISabled\n"
1125 " -R: read() system calls only (mapped reads disabled)\n"
1126 " -Z: O_DIRECT (use -R, -W, -r and -w too)\n"
1127 " -I: When multiple paths to the file are given each operation uses\n"
1128 " a different path. Iterate through them in order with 'rotate'\n"
1129 " or chose then at 'random'. (defaults to random)\n"
1130 " fname: this filename is REQUIRED (no default)\n");
1136 getnum(char *s, char **e)
1141 ret = strtol(s, e, 0);
1169 main(int argc, char **argv)
1178 page_size = getpagesize();
1179 page_mask = page_size - 1;
1181 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1183 while ((ch = getopt(argc, argv,
1184 "b:c:dl:m:no:p:qr:s:t:w:D:I:LN:OP:RS:WZ"))
1188 simulatedopcount = getnum(optarg, &endp);
1190 fprintf(stdout, "Will begin at operation"
1193 if (simulatedopcount == 0)
1195 simulatedopcount -= 1;
1198 closeprob = getnum(optarg, &endp);
1201 "Chance of close/open is 1 in %d\n",
1210 maxfilelen = getnum(optarg, &endp);
1211 if (maxfilelen <= 0)
1215 monitorstart = getnum(optarg, &endp);
1216 if (monitorstart < 0)
1218 if (!endp || *endp++ != ':')
1220 monitorend = getnum(endp, &endp);
1223 if (monitorend == 0)
1224 monitorend = -1; /* aka infinity */
1230 maxoplen = getnum(optarg, &endp);
1235 progressinterval = getnum(optarg, &endp);
1236 if (progressinterval < 0)
1243 readbdy = getnum(optarg, &endp);
1248 style = getnum(optarg, &endp);
1249 if (style < 0 || style > 1)
1253 truncbdy = getnum(optarg, &endp);
1258 writebdy = getnum(optarg, &endp);
1263 debugstart = getnum(optarg, &endp);
1268 assign_fd_policy(optarg);
1274 numops = getnum(optarg, &endp);
1282 strncpy(goodfile, optarg, sizeof(goodfile) - 1);
1283 strcat(goodfile, "/");
1284 strncpy(logfile, optarg, sizeof(logfile) - 1);
1285 strcat(logfile, "/");
1292 seed = getnum(optarg, &endp);
1294 seed = time(0) % 10000;
1296 fprintf(stdout, "Seed set to %d\n", seed);
1303 fprintf(stdout, "mapped writes DISABLED\n");
1306 o_direct = O_DIRECT;
1319 signal(SIGHUP, cleanup);
1320 signal(SIGINT, cleanup);
1321 signal(SIGPIPE, cleanup);
1322 signal(SIGALRM, cleanup);
1323 signal(SIGTERM, cleanup);
1324 signal(SIGXCPU, cleanup);
1325 signal(SIGXFSZ, cleanup);
1326 signal(SIGVTALRM, cleanup);
1327 signal(SIGUSR1, cleanup);
1328 signal(SIGUSR2, cleanup);
1330 initstate(seed, state, 256);
1333 open_test_files(argv, argc);
1335 strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1336 strcat (goodfile, ".fsxgood");
1337 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1338 if (fsxgoodfd < 0) {
1342 strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1343 strcat (logfile, ".fsxlog");
1344 fsxlogf = fopen(logfile, "w");
1345 if (fsxlogf == NULL) {
1352 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1353 if (file_size == (off_t)-1) {
1355 warn("main: lseek eof");
1358 ret = lseek(fd, (off_t)0, SEEK_SET);
1359 if (ret == (off_t)-1) {
1361 warn("main: lseek 0");
1365 original_buf = (char *) malloc(maxfilelen);
1366 for (i = 0; i < maxfilelen; i++)
1367 original_buf[i] = random() % 256;
1371 ret = posix_memalign((void **)&good_buf, writebdy, maxfilelen);
1373 prt("main: posix_memalign failed: %s\n", strerror(ret));
1377 ret = posix_memalign((void **)&temp_buf, readbdy, maxoplen);
1379 prt("main: posix_memalign failed: %s\n", strerror(ret));
1383 good_buf = malloc(maxfilelen);
1385 prt("malloc failed.\n");
1389 temp_buf = malloc(maxoplen);
1391 prt("malloc failed.\n");
1395 memset(good_buf, 0, maxfilelen);
1396 memset(temp_buf, 0, maxoplen);
1398 if (lite) { /* zero entire existing file */
1402 written = write(fd, good_buf, (size_t)maxfilelen);
1403 if (written != maxfilelen) {
1404 if (written == -1) {
1406 warn("main: error on write");
1408 warn("main: short write, 0x%x bytes instead"
1410 (unsigned)written, maxfilelen);
1416 while (numops == -1 || numops--)
1420 prt("All operations completed A-OK!\n");