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 * Each test run will work with one or more separate file descriptors for the
64 * same file. This allows testing cache coherency across multiple mountpoints
65 * of the same network filesystem on a single client.
73 enum fd_iteration_policy {
78 int fd_policy = FD_RANDOM;
82 * A log entry is an operation and a bunch of arguments.
89 const struct test_file *tf;
92 #define LOGSIZE 100000
94 struct log_entry oplog[LOGSIZE]; /* the log */
95 int logptr = 0; /* current position in log */
96 int logcount = 0; /* total ops */
104 #define OP_TRUNCATE 3
105 #define OP_CLOSEOPEN 4
107 #define OP_MAPWRITE 6
113 char *original_buf; /* a pointer to the original data */
114 char *good_buf; /* a pointer to the correct data */
115 char *temp_buf; /* a pointer to the current data */
116 char *fname; /* name of our test file */
117 char logfile[1024]; /* name of our log file */
118 char goodfile[1024]; /* name of our test file */
123 unsigned long testcalls = 0; /* calls to function "test" */
125 long simulatedopcount = 0; /* -b flag */
126 int closeprob = 0; /* -c flag */
127 int debug = 0; /* -d flag */
128 long debugstart = 0; /* -D flag */
129 long maxfilelen = 256 * 1024; /* -l flag */
130 int sizechecks = 1; /* -n flag disables them */
131 int maxoplen = 64 * 1024; /* -o flag */
132 int quiet = 0; /* -q flag */
133 long progressinterval = 0; /* -p flag */
134 int readbdy = 1; /* -r flag */
135 int style = 0; /* -s flag */
136 int truncbdy = 1; /* -t flag */
137 int writebdy = 1; /* -w flag */
138 long monitorstart = -1; /* -m flag */
139 long monitorend = -1; /* -m flag */
140 int lite = 0; /* -L flag */
141 long numops = -1; /* -N flag */
142 int randomoplen = 1; /* -O flag disables it */
143 int seed = 1; /* -S flag */
144 int mapped_writes = 1; /* -W flag disables */
145 int mapped_reads = 1; /* -R flag disables it */
147 FILE * fsxlogf = NULL;
152 vwarnc(code, fmt, ap)
157 fprintf(stderr, "fsx: ");
159 vfprintf(stderr, fmt, ap);
160 fprintf(stderr, ": ");
162 fprintf(stderr, "%s\n", strerror(code));
167 warn(const char * fmt, ...)
171 vwarnc(errno, fmt, ap);
177 __attribute__((format(printf, 1, 2)))
183 vfprintf(stdout, fmt, args);
188 vfprintf(fsxlogf, fmt, args);
196 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
201 log4(int operation, int arg0, int arg1, int arg2, struct timeval *tv,
202 const struct test_file *tf)
204 struct log_entry *le;
207 le->operation = operation;
215 if (logptr >= LOGSIZE)
220 fill_tf_buf(const struct test_file *tf)
222 static int max_tf_len;
223 static char tf_buf[32];
225 if (fd_policy == FD_SINGLE)
229 max_tf_len = snprintf(tf_buf, sizeof(tf_buf) - 1,
230 "%u", num_test_files - 1);
232 sprintf(tf_buf, "[%0*lu]", max_tf_len,
233 (unsigned long)(tf - test_files));
242 struct log_entry *lp;
244 prt("LOG DUMP (%d total operations):\n", logcount);
245 if (logcount < LOGSIZE) {
252 for ( ; count > 0; count--) {
255 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
257 prt("%d%s: %lu.%06lu ", opnum, fill_tf_buf(lp->tf),
258 lp->tv.tv_sec, lp->tv.tv_usec);
260 switch (lp->operation) {
262 prt("MAPREAD 0x%x thru 0x%x (0x%x bytes)",
263 lp->args[0], lp->args[0] + lp->args[1] - 1,
265 if (badoff >= lp->args[0] && badoff <
266 lp->args[0] + lp->args[1])
270 prt("MAPWRITE 0x%x thru 0x%x (0x%x bytes)",
271 lp->args[0], lp->args[0] + lp->args[1] - 1,
273 if (badoff >= lp->args[0] && badoff <
274 lp->args[0] + lp->args[1])
278 prt("READ 0x%x thru 0x%x (0x%x bytes)",
279 lp->args[0], lp->args[0] + lp->args[1] - 1,
281 if (badoff >= lp->args[0] &&
282 badoff < lp->args[0] + lp->args[1])
286 prt("WRITE 0x%x thru 0x%x (0x%x bytes)",
287 lp->args[0], lp->args[0] + lp->args[1] - 1,
289 if (lp->args[0] > lp->args[2])
291 else if (lp->args[0] + lp->args[1] > lp->args[2])
293 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
294 badoff < lp->args[0] + lp->args[1])
298 down = lp->args[0] < lp->args[1];
299 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
300 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
301 if (badoff >= lp->args[!down] &&
302 badoff < lp->args[!!down])
309 prt("SKIPPED (no operation)");
312 prt("BOGUS LOG ENTRY (operation code = %d)!",
324 save_buffer(char *buffer, off_t bufferlength, int fd)
327 ssize_t byteswritten;
329 if (fd <= 0 || bufferlength == 0)
332 if (bufferlength > SSIZE_MAX) {
333 prt("fsx flaw: overflow in save_buffer\n");
337 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
338 if (size_by_seek == (off_t)-1)
339 prterr("save_buffer: lseek eof");
340 else if (bufferlength > size_by_seek) {
341 warn("save_buffer: .fsxgood file too short... will"
342 "save 0x%llx bytes instead of 0x%llx\n",
343 (unsigned long long)size_by_seek,
344 (unsigned long long)bufferlength);
345 bufferlength = size_by_seek;
349 ret = lseek(fd, (off_t)0, SEEK_SET);
350 if (ret == (off_t)-1)
351 prterr("save_buffer: lseek 0");
353 byteswritten = write(fd, buffer, (size_t)bufferlength);
354 if (byteswritten != bufferlength) {
355 if (byteswritten == -1)
356 prterr("save_buffer write");
358 warn("save_buffer: short write, 0x%x bytes instead"
360 (unsigned)byteswritten,
361 (unsigned long long)bufferlength);
367 report_failure(int status)
373 save_buffer(good_buf, file_size, fsxgoodfd);
374 prt("Correct content saved for comparison\n");
375 prt("(maybe hexdump \"%s\" vs \"%s\")\n",
384 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
385 *(((unsigned char *)(cp)) + 1)))
388 check_buffers(unsigned offset, unsigned size)
396 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
397 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
399 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
401 c = good_buf[offset];
405 bad = short_at(&temp_buf[i]);
406 prt("%#07x\t%#06x\t%#06x", offset,
407 short_at(&good_buf[offset]), bad);
408 op = temp_buf[offset & 1 ? i+1 : i];
420 prt("operation# (mod 256) for the bad data"
421 "may be %u\n", ((unsigned)op & 0xff));
423 prt("operation# (mod 256) for the bad data"
424 "unknown, check HOLE and EXTEND ops\n");
426 prt("????????????????\n");
447 prt("unknown policy");
451 return &test_files[ index % num_test_files ];
455 assign_fd_policy(char *policy)
457 if (!strcmp(policy, "random"))
458 fd_policy = FD_RANDOM;
459 else if (!strcmp(policy, "rotate"))
460 fd_policy = FD_ROTATE;
462 prt("unknown -I policy: '%s'\n", policy);
470 struct test_file *tf = get_tf();
474 static const char *my_basename(const char *path)
476 char *c = strrchr(path, '/');
478 return c ? c++ : path;
482 open_test_files(char **argv, int argc)
484 struct test_file *tf;
487 num_test_files = argc;
488 if (num_test_files == 1)
489 fd_policy = FD_SINGLE;
491 test_files = calloc(num_test_files, sizeof(*test_files));
492 if (test_files == NULL) {
493 prterr("reallocating space for test files");
497 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
500 tf->fd = open(tf->path, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC),
508 if (quiet || fd_policy == FD_SINGLE)
511 for (i = 0, tf = test_files; i < num_test_files; i++, tf++)
512 prt("fd %d: %s\n", i, tf->path);
516 close_test_files(void)
519 struct test_file *tf;
521 for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
537 if (fstat(fd, &statbuf)) {
538 prterr("check_size: fstat");
539 statbuf.st_size = -1;
541 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
542 if (file_size != statbuf.st_size || file_size != size_by_seek) {
543 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
544 (unsigned long long)file_size,
545 (unsigned long long)statbuf.st_size,
546 (unsigned long long)size_by_seek);
553 check_trunc_hack(void)
558 /* should not ignore ftruncate(2)'s return value */
559 if (ftruncate(fd, (off_t)0) < 0) {
560 prterr("trunc_hack: ftruncate(0)");
563 if (ftruncate(fd, (off_t)100000) < 0) {
564 prterr("trunc_hack: ftruncate(100000)");
567 if (fstat(fd, &statbuf)) {
568 prterr("trunc_hack: fstat");
569 statbuf.st_size = -1;
571 if (statbuf.st_size != (off_t)100000) {
572 prt("no extend on truncate! not posix!\n");
575 if (ftruncate(fd, 0) < 0) {
576 prterr("trunc_hack: ftruncate(0) (2nd call)");
582 output_line(struct test_file *tf, int op, unsigned offset,
583 unsigned size, struct timeval *tv)
587 [OP_WRITE] = "write",
588 [OP_TRUNCATE] = "trunc from",
589 [OP_MAPREAD] = "mapread",
590 [OP_MAPWRITE] = "mapwrite",
594 if (!(!quiet && ((progressinterval &&
595 testcalls % progressinterval == 0) ||
597 (monitorstart == -1 ||
598 (offset + size > monitorstart &&
599 (monitorend == -1 || offset <= monitorend)))))))
602 prt("%06lu%s %lu.%06lu %-10s %#08x %s %#08x\t(0x%x bytes)\n",
603 testcalls, fill_tf_buf(tf), tv->tv_sec, tv->tv_usec,
605 offset, op == OP_TRUNCATE ? " to " : "thru",
606 offset + size - 1, size);
610 doread(unsigned offset, unsigned size)
615 struct test_file *tf = get_tf();
618 offset -= offset % readbdy;
619 gettimeofday(&t, NULL);
621 if (!quiet && testcalls > simulatedopcount)
622 prt("skipping zero size read\n");
623 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
626 if (size + offset > file_size) {
627 if (!quiet && testcalls > simulatedopcount)
628 prt("skipping seek/read past end of file\n");
629 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
633 log4(OP_READ, offset, size, 0, &t, tf);
635 if (testcalls <= simulatedopcount)
638 output_line(tf, OP_READ, offset, size, &t);
640 ret = lseek(fd, (off_t)offset, SEEK_SET);
641 if (ret == (off_t)-1) {
642 prterr("doread: lseek");
645 iret = read(fd, temp_buf, size);
646 if (!quiet && (debug > 1 &&
647 (monitorstart == -1 ||
648 (offset + size > monitorstart &&
649 (monitorend == -1 || offset <= monitorend))))) {
650 gettimeofday(&t, NULL);
651 prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
655 prterr("doread: read");
657 prt("short read: 0x%x bytes instead of 0x%x\n",
661 check_buffers(offset, size);
666 domapread(unsigned offset, unsigned size)
672 struct test_file *tf = get_tf();
675 offset -= offset % readbdy;
676 gettimeofday(&t, NULL);
678 if (!quiet && testcalls > simulatedopcount)
679 prt("skipping zero size read\n");
680 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
683 if (size + offset > file_size) {
684 if (!quiet && testcalls > simulatedopcount)
685 prt("skipping seek/read past end of file\n");
686 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
690 log4(OP_MAPREAD, offset, size, 0, &t, tf);
692 if (testcalls <= simulatedopcount)
695 output_line(tf, OP_MAPREAD, offset, size, &t);
697 pg_offset = offset & page_mask;
698 map_size = pg_offset + size;
700 if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
701 (off_t)(offset - pg_offset))) == MAP_FAILED) {
702 prterr("domapread: mmap");
705 if (!quiet && (debug > 1 &&
706 (monitorstart == -1 ||
707 (offset + size > monitorstart &&
708 (monitorend == -1 || offset <= monitorend))))) {
709 gettimeofday(&t, NULL);
710 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
712 memcpy(temp_buf, p + pg_offset, size);
713 if (!quiet && (debug > 1 &&
714 (monitorstart == -1 ||
715 (offset + size > monitorstart &&
716 (monitorend == -1 || offset <= monitorend))))) {
717 gettimeofday(&t, NULL);
718 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
720 if (munmap(p, map_size) != 0) {
721 prterr("domapread: munmap");
724 if (!quiet && (debug > 1 &&
725 (monitorstart == -1 ||
726 (offset + size > monitorstart &&
727 (monitorend == -1 || offset <= monitorend))))) {
728 gettimeofday(&t, NULL);
729 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
732 check_buffers(offset, size);
737 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
740 good_buf[offset] = testcalls % 256;
742 good_buf[offset] += original_buf[offset];
749 dowrite(unsigned offset, unsigned size)
754 struct test_file *tf = get_tf();
757 offset -= offset % writebdy;
758 gettimeofday(&t, NULL);
760 if (!quiet && testcalls > simulatedopcount)
761 prt("skipping zero size write\n");
762 log4(OP_SKIPPED, OP_WRITE, offset, size, &t, tf);
766 log4(OP_WRITE, offset, size, file_size, &t, tf);
768 gendata(original_buf, good_buf, offset, size);
769 if (file_size < offset + size) {
770 if (file_size < offset)
771 memset(good_buf + file_size, '\0', offset - file_size);
772 file_size = offset + size;
774 warn("Lite file size bug in fsx!");
779 if (testcalls <= simulatedopcount)
782 output_line(tf, OP_WRITE, offset, size, &t);
784 ret = lseek(fd, (off_t)offset, SEEK_SET);
785 if (ret == (off_t)-1) {
786 prterr("dowrite: lseek");
789 iret = write(fd, good_buf + offset, size);
790 if (!quiet && (debug > 1 &&
791 (monitorstart == -1 ||
792 (offset + size > monitorstart &&
793 (monitorend == -1 || offset <= monitorend))))) {
794 gettimeofday(&t, NULL);
795 prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
799 prterr("dowrite: write");
801 prt("short write: 0x%x bytes instead of 0x%x\n",
809 domapwrite(unsigned offset, unsigned size)
816 struct test_file *tf = get_tf();
819 offset -= offset % writebdy;
820 gettimeofday(&t, NULL);
822 if (!quiet && testcalls > simulatedopcount)
823 prt("skipping zero size write\n");
824 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t, tf);
827 cur_filesize = file_size;
829 log4(OP_MAPWRITE, offset, size, 0, &t, tf);
831 gendata(original_buf, good_buf, offset, size);
832 if (file_size < offset + size) {
833 if (file_size < offset)
834 memset(good_buf + file_size, '\0', offset - file_size);
835 file_size = offset + size;
837 warn("Lite file size bug in fsx!");
842 if (testcalls <= simulatedopcount)
845 output_line(tf, OP_MAPWRITE, offset, size, &t);
847 if (file_size > cur_filesize) {
848 if (ftruncate(fd, file_size) == -1) {
849 prterr("domapwrite: ftruncate");
852 if (!quiet && (debug > 1 &&
853 (monitorstart == -1 ||
854 (offset + size > monitorstart &&
855 (monitorend == -1 || offset <= monitorend))))) {
856 gettimeofday(&t, NULL);
857 prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
860 pg_offset = offset & page_mask;
861 map_size = pg_offset + size;
863 if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
864 fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
865 prterr("domapwrite: mmap");
868 if (!quiet && (debug > 1 &&
869 (monitorstart == -1 ||
870 (offset + size > monitorstart &&
871 (monitorend == -1 || offset <= monitorend))))) {
872 gettimeofday(&t, NULL);
873 prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
875 memcpy(p + pg_offset, good_buf + offset, size);
876 if (!quiet && (debug > 1 &&
877 (monitorstart == -1 ||
878 (offset + size > monitorstart &&
879 (monitorend == -1 || offset <= monitorend))))) {
880 gettimeofday(&t, NULL);
881 prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
883 if (msync(p, map_size, 0) != 0) {
884 prterr("domapwrite: msync");
887 if (!quiet && (debug > 1 &&
888 (monitorstart == -1 ||
889 (offset + size > monitorstart &&
890 (monitorend == -1 || offset <= monitorend))))) {
891 gettimeofday(&t, NULL);
892 prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
894 if (munmap(p, map_size) != 0) {
895 prterr("domapwrite: munmap");
898 if (!quiet && (debug > 1 &&
899 (monitorstart == -1 ||
900 (offset + size > monitorstart &&
901 (monitorend == -1 || offset <= monitorend))))) {
902 gettimeofday(&t, NULL);
903 prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
909 dotruncate(unsigned size)
912 int oldsize = file_size;
913 struct test_file *tf = get_tf();
916 size -= size % truncbdy;
917 gettimeofday(&t, NULL);
918 if (size > biggest) {
920 if (!quiet && testcalls > simulatedopcount)
921 prt("truncating to largest ever: 0x%x\n", size);
924 log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t, tf);
926 if (size > file_size)
927 memset(good_buf + file_size, '\0', size - file_size);
930 if (testcalls <= simulatedopcount)
933 output_line(tf, OP_TRUNCATE, oldsize, size, &t);
935 if (ftruncate(fd, (off_t)size) == -1) {
936 prt("ftruncate1: %x\n", size);
937 prterr("dotruncate: ftruncate");
940 if (!quiet && debug > 1) {
941 gettimeofday(&t, NULL);
942 prt(" %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
953 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
954 prterr("writefileimage: lseek");
957 iret = write(fd, good_buf, file_size);
958 if ((off_t)iret != file_size) {
960 prterr("writefileimage: write");
962 prt("short write: 0x%lx bytes instead of 0x%llx\n",
964 (unsigned long long)file_size);
967 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
968 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
969 prterr("writefileimage: ftruncate");
979 struct test_file *tf = get_tf();
981 if (testcalls <= simulatedopcount)
984 gettimeofday(&t, NULL);
985 log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t, tf);
988 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
991 prterr("docloseopen: close");
994 if (!quiet && debug > 1) {
995 gettimeofday(&t, NULL);
996 prt(" %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
998 tf->fd = open(tf->path, O_RDWR, 0);
1000 prterr("docloseopen: open");
1001 report_failure(181);
1003 if (!quiet && debug > 1) {
1004 gettimeofday(&t, NULL);
1005 prt(" %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
1013 unsigned long offset;
1014 unsigned long size = maxoplen;
1015 unsigned long rv = random();
1016 unsigned long op = rv % (3 + !lite + mapped_writes);
1018 /* turn off the map read if necessary */
1020 if (op == 2 && !mapped_reads)
1023 if (simulatedopcount > 0 && testcalls == simulatedopcount)
1028 if (debugstart > 0 && testcalls >= debugstart)
1031 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1032 prt("%lu...\n", testcalls);
1039 * MAPWRITE: op = 3 or 4
1041 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
1042 dotruncate(random() % maxfilelen);
1045 size = random() % (maxoplen+1);
1046 if (lite ? 0 : op == 3)
1050 if (op == 1 || op == (lite ? 3 : 4)) {
1051 offset %= maxfilelen;
1052 if (offset + size > maxfilelen)
1053 size = maxfilelen - offset;
1055 domapwrite(offset, size);
1057 dowrite(offset, size);
1060 offset %= file_size;
1063 if (offset + size > file_size)
1064 size = file_size - offset;
1066 domapread(offset, size);
1068 doread(offset, size);
1072 if (sizechecks && testcalls > simulatedopcount)
1074 if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
1084 prt("signal %d\n", sig);
1085 prt("testcalls = %lu\n", testcalls);
1093 fprintf(stdout, "usage: %s",
1094 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m "
1095 "start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t "
1096 "truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] "
1097 "[ -I random|rotate ] fname [additional paths to fname..]\n"
1098 " -b opnum: beginning operation number (default 1)\n"
1099 " -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1100 " -d: debug output for all operations [-d -d = more debugging]\n"
1101 " -l flen: the upper bound on file size (default 262144)\n"
1102 " -m startop:endop: monitor (print debug output) specified byte rang"
1103 "(default 0:infinity)\n"
1104 " -n: no verifications of file size\n"
1105 " -o oplen: the upper bound on operation size (default 65536)\n"
1106 " -p progressinterval: debug output at specified operation interval\n"
1107 " -q: quieter operation\n"
1108 " -r readbdy: 4096 would make reads page aligned (default 1)\n"
1109 " -s style: 1 gives smaller truncates (default 0)\n"
1110 " -t truncbdy: 4096 would make truncates page aligned (default 1)\n"
1111 " -w writebdy: 4096 would make writes page aligned (default 1)\n"
1112 " -D startingop: debug output starting at specified operation\n"
1113 " -L: fsxLite - no file creations & no file size changes\n"
1114 " -N numops: total # operations to do (default infinity)\n"
1115 " -O: use oplen (see -o flag) for every op (default random)\n"
1116 " -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1117 " -S seed: for random # generator (default 1) 0 gets timestamp\n"
1118 " -W: mapped write operations DISabled\n"
1119 " -R: read() system calls only (mapped reads disabled)\n"
1120 " -I: When multiple paths to the file are given each operation uses"
1121 " a different path. Iterate through them in order with 'rotate'"
1122 " or chose then at 'random'. (defaults to random)\n"
1123 " fname: this filename is REQUIRED (no default)\n");
1129 getnum(char *s, char **e)
1134 ret = strtol(s, e, 0);
1162 main(int argc, char **argv)
1171 page_size = getpagesize();
1172 page_mask = page_size - 1;
1174 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1176 while ((ch = getopt(argc, argv,
1177 "b:c:dl:m:no:p:qr:s:t:w:D:I:LN:OP:RS:W"))
1181 simulatedopcount = getnum(optarg, &endp);
1183 fprintf(stdout, "Will begin at operation"
1186 if (simulatedopcount == 0)
1188 simulatedopcount -= 1;
1191 closeprob = getnum(optarg, &endp);
1194 "Chance of close/open is 1 in %d\n",
1203 maxfilelen = getnum(optarg, &endp);
1204 if (maxfilelen <= 0)
1208 monitorstart = getnum(optarg, &endp);
1209 if (monitorstart < 0)
1211 if (!endp || *endp++ != ':')
1213 monitorend = getnum(endp, &endp);
1216 if (monitorend == 0)
1217 monitorend = -1; /* aka infinity */
1223 maxoplen = getnum(optarg, &endp);
1228 progressinterval = getnum(optarg, &endp);
1229 if (progressinterval < 0)
1236 readbdy = getnum(optarg, &endp);
1241 style = getnum(optarg, &endp);
1242 if (style < 0 || style > 1)
1246 truncbdy = getnum(optarg, &endp);
1251 writebdy = getnum(optarg, &endp);
1256 debugstart = getnum(optarg, &endp);
1261 assign_fd_policy(optarg);
1267 numops = getnum(optarg, &endp);
1275 strncpy(goodfile, optarg, sizeof(goodfile));
1276 strcat(goodfile, "/");
1277 strncpy(logfile, optarg, sizeof(logfile));
1278 strcat(logfile, "/");
1285 seed = getnum(optarg, &endp);
1287 seed = time(0) % 10000;
1289 fprintf(stdout, "Seed set to %d\n", seed);
1296 fprintf(stdout, "mapped writes DISABLED\n");
1309 signal(SIGHUP, cleanup);
1310 signal(SIGINT, cleanup);
1311 signal(SIGPIPE, cleanup);
1312 signal(SIGALRM, cleanup);
1313 signal(SIGTERM, cleanup);
1314 signal(SIGXCPU, cleanup);
1315 signal(SIGXFSZ, cleanup);
1316 signal(SIGVTALRM, cleanup);
1317 signal(SIGUSR1, cleanup);
1318 signal(SIGUSR2, cleanup);
1320 initstate(seed, state, 256);
1323 open_test_files(argv, argc);
1325 strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1326 strcat (goodfile, ".fsxgood");
1327 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1328 if (fsxgoodfd < 0) {
1332 strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1333 strcat (logfile, ".fsxlog");
1334 fsxlogf = fopen(logfile, "w");
1335 if (fsxlogf == NULL) {
1342 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1343 if (file_size == (off_t)-1) {
1345 warn("main: lseek eof");
1348 ret = lseek(fd, (off_t)0, SEEK_SET);
1349 if (ret == (off_t)-1) {
1351 warn("main: lseek 0");
1355 original_buf = (char *) malloc(maxfilelen);
1356 for (i = 0; i < maxfilelen; i++)
1357 original_buf[i] = random() % 256;
1358 good_buf = (char *) malloc(maxfilelen);
1359 memset(good_buf, '\0', maxfilelen);
1360 temp_buf = (char *) malloc(maxoplen);
1361 memset(temp_buf, '\0', maxoplen);
1362 if (lite) { /* zero entire existing file */
1366 written = write(fd, good_buf, (size_t)maxfilelen);
1367 if (written != maxfilelen) {
1368 if (written == -1) {
1370 warn("main: error on write");
1372 warn("main: short write, 0x%x bytes instead"
1374 (unsigned)written, maxfilelen);
1380 while (numops == -1 || numops--)
1384 prt("All operations completed A-OK!\n");