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 2.0 (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.opensource.apple.com/apsl/ 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
35 * Checks for mmap last-page zero fill.
37 * Updated license to APSL 2.0, 2004/7/27 - Jordan Hubbard
43 #include <sys/types.h>
46 # include <sys/param.h>
65 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
68 * A log entry is an operation and a bunch of arguments.
78 struct log_entry oplog[LOGSIZE]; /* the log */
79 int logptr = 0; /* current position in log */
80 int logcount = 0; /* total ops */
89 #define OP_CLOSEOPEN 4
97 char *original_buf; /* a pointer to the original data */
98 char *good_buf; /* a pointer to the correct data */
99 char *temp_buf; /* a pointer to the current data */
100 char *fname; /* name of our test file */
101 int fd; /* fd for our test file */
106 unsigned long testcalls = 0; /* calls to function "test" */
108 unsigned long simulatedopcount = 0; /* -b flag */
109 int closeprob = 0; /* -c flag */
110 int debug = 0; /* -d flag */
111 unsigned long debugstart = 0; /* -D flag */
112 unsigned long maxfilelen = 256 * 1024; /* -l flag */
113 int sizechecks = 1; /* -n flag disables them */
114 int maxoplen = 64 * 1024; /* -o flag */
115 int quiet = 0; /* -q flag */
116 unsigned long progressinterval = 0; /* -p flag */
117 int readbdy = 1; /* -r flag */
118 int style = 0; /* -s flag */
119 int truncbdy = 1; /* -t flag */
120 int writebdy = 1; /* -w flag */
121 long monitorstart = -1; /* -m flag */
122 long monitorend = -1; /* -m flag */
123 int lite = 0; /* -L flag */
124 long numops = -1; /* -N flag */
125 int randomoplen = 1; /* -O flag disables it */
126 int seed = 1; /* -S flag */
127 int mapped_writes = 1; /* -W flag disables */
128 int mapped_reads = 1; /* -R flag disables it */
130 FILE * fsxlogf = NULL;
136 vwarnc(code, fmt, ap)
141 fprintf(stderr, "fsx: ");
143 vfprintf(stderr, fmt, ap);
144 fprintf(stderr, ": ");
146 fprintf(stderr, "%s\n", strerror(code));
151 warn(const char * fmt, ...)
155 vwarnc(errno, fmt, ap);
166 vfprintf(stdout, fmt, args);
168 vfprintf(fsxlogf, fmt, args);
175 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
180 log4(int operation, int arg0, int arg1, int arg2)
182 struct log_entry *le;
185 le->operation = operation;
187 le->operation = ~ le->operation;
193 if (logptr >= LOGSIZE)
202 struct log_entry *lp;
204 prt("LOG DUMP (%d total operations):\n", logcount);
205 if (logcount < LOGSIZE) {
212 for ( ; count > 0; count--) {
215 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
216 prt("%d(%d mod 256): ", opnum, opnum%256);
218 if ((closeopen = lp->operation < 0))
219 lp->operation = ~ lp->operation;
221 switch (lp->operation) {
223 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
224 lp->args[0], lp->args[0] + lp->args[1] - 1,
226 if (badoff >= lp->args[0] && badoff <
227 lp->args[0] + lp->args[1])
231 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
232 lp->args[0], lp->args[0] + lp->args[1] - 1,
234 if (badoff >= lp->args[0] && badoff <
235 lp->args[0] + lp->args[1])
239 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
240 lp->args[0], lp->args[0] + lp->args[1] - 1,
242 if (badoff >= lp->args[0] &&
243 badoff < lp->args[0] + lp->args[1])
247 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
248 lp->args[0], lp->args[0] + lp->args[1] - 1,
250 if (lp->args[0] > lp->args[2])
252 else if (lp->args[0] + lp->args[1] > lp->args[2])
254 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
255 badoff < lp->args[0] + lp->args[1])
259 down = lp->args[0] < lp->args[1];
260 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
261 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
262 if (badoff >= lp->args[!down] &&
263 badoff < lp->args[!!down])
267 prt("SKIPPED (no operation)");
270 prt("BOGUS LOG ENTRY (operation code = %d)!",
274 prt("\n\t\tCLOSE/OPEN");
284 save_buffer(char *buffer, off_t bufferlength, int fd)
287 ssize_t byteswritten;
289 if (fd <= 0 || bufferlength == 0)
292 if (bufferlength > SSIZE_MAX) {
293 prt("fsx flaw: overflow in save_buffer\n");
297 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
298 if (size_by_seek == (off_t)-1)
299 prterr("save_buffer: lseek eof");
300 else if (bufferlength > size_by_seek) {
301 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
302 (unsigned long long)bufferlength);
303 bufferlength = size_by_seek;
307 ret = lseek(fd, (off_t)0, SEEK_SET);
308 if (ret == (off_t)-1)
309 prterr("save_buffer: lseek 0");
311 byteswritten = write(fd, buffer, (size_t)bufferlength);
312 if (byteswritten != bufferlength) {
313 if (byteswritten == -1)
314 prterr("save_buffer write");
316 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
317 (unsigned)byteswritten,
318 (unsigned long long)bufferlength);
324 report_failure(int status)
330 save_buffer(good_buf, file_size, fsxgoodfd);
331 prt("Correct content saved for comparison\n");
332 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
341 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
342 *(((unsigned char *)(cp)) + 1)))
345 check_buffers(unsigned offset, unsigned size)
353 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
354 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
356 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
358 c = good_buf[offset];
362 bad = short_at(&temp_buf[i]);
363 prt("0x%5x\t0x%04x\t0x%04x", offset,
364 short_at(&good_buf[offset]), bad);
365 op = temp_buf[offset & 1 ? i+1 : i];
377 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
379 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
381 prt("????????????????\n");
393 if (fstat(fd, &statbuf)) {
394 prterr("check_size: fstat");
395 statbuf.st_size = -1;
397 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
398 if (file_size != statbuf.st_size || file_size != size_by_seek) {
399 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
400 (unsigned long long)file_size,
401 (unsigned long long)statbuf.st_size,
402 (unsigned long long)size_by_seek);
409 check_trunc_hack(void)
413 ftruncate(fd, (off_t)0);
414 ftruncate(fd, (off_t)100000);
416 if (statbuf.st_size != (off_t)100000) {
417 prt("no extend on truncate! not posix!\n");
420 ftruncate(fd, (off_t)0);
425 doread(unsigned offset, unsigned size)
430 offset -= offset % readbdy;
432 if (!quiet && testcalls > simulatedopcount)
433 prt("skipping zero size read\n");
434 log4(OP_SKIPPED, OP_READ, offset, size);
437 if (size + offset > file_size) {
438 if (!quiet && testcalls > simulatedopcount)
439 prt("skipping seek/read past end of file\n");
440 log4(OP_SKIPPED, OP_READ, offset, size);
444 log4(OP_READ, offset, size, 0);
446 if (testcalls <= simulatedopcount)
449 if (!quiet && ((progressinterval &&
450 testcalls % progressinterval == 0) ||
452 (monitorstart == -1 ||
453 (offset + size > monitorstart &&
454 (monitorend == -1 || offset <= monitorend))))))
455 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
456 offset, offset + size - 1, size);
457 ret = lseek(fd, (off_t)offset, SEEK_SET);
458 if (ret == (off_t)-1) {
459 prterr("doread: lseek");
462 iret = read(fd, temp_buf, size);
465 prterr("doread: read");
467 prt("short read: 0x%x bytes instead of 0x%x\n",
471 check_buffers(offset, size);
476 check_eofpage(char *s, unsigned offset, char *p, int size)
478 unsigned last_page, should_be_zero;
480 if (offset + size <= (file_size & ~page_mask))
483 * we landed in the last page of the file
484 * test to make sure the VM system provided 0's
485 * beyond the true end of the file mapping
486 * (as required by mmap def in 1996 posix 1003.1)
488 last_page = ((int)p + (offset & page_mask) + size) & ~page_mask;
490 for (should_be_zero = last_page + (file_size & page_mask);
491 should_be_zero < last_page + page_size;
493 if (*(char *)should_be_zero) {
494 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
495 s, file_size - 1, should_be_zero & page_mask,
496 short_at(should_be_zero));
503 domapread(unsigned offset, unsigned size)
509 offset -= offset % readbdy;
511 if (!quiet && testcalls > simulatedopcount)
512 prt("skipping zero size read\n");
513 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
516 if (size + offset > file_size) {
517 if (!quiet && testcalls > simulatedopcount)
518 prt("skipping seek/read past end of file\n");
519 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
523 log4(OP_MAPREAD, offset, size, 0);
525 if (testcalls <= simulatedopcount)
528 if (!quiet && ((progressinterval &&
529 testcalls % progressinterval == 0) ||
531 (monitorstart == -1 ||
532 (offset + size > monitorstart &&
533 (monitorend == -1 || offset <= monitorend))))))
534 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
535 offset, offset + size - 1, size);
537 pg_offset = offset & page_mask;
538 map_size = pg_offset + size;
540 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
541 (off_t)(offset - pg_offset))) == (char *)-1) {
542 prterr("domapread: mmap");
545 memcpy(temp_buf, p + pg_offset, size);
547 check_eofpage("Read", offset, p, size);
549 if (munmap(p, map_size) != 0) {
550 prterr("domapread: munmap");
554 check_buffers(offset, size);
559 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
562 good_buf[offset] = testcalls % 256;
564 good_buf[offset] += original_buf[offset];
571 dowrite(unsigned offset, unsigned size)
576 offset -= offset % writebdy;
578 if (!quiet && testcalls > simulatedopcount)
579 prt("skipping zero size write\n");
580 log4(OP_SKIPPED, OP_WRITE, offset, size);
584 log4(OP_WRITE, offset, size, file_size);
586 gendata(original_buf, good_buf, offset, size);
587 if (file_size < offset + size) {
588 if (file_size < offset)
589 memset(good_buf + file_size, '\0', offset - file_size);
590 file_size = offset + size;
592 warn("Lite file size bug in fsx!");
597 if (testcalls <= simulatedopcount)
600 if (!quiet && ((progressinterval &&
601 testcalls % progressinterval == 0) ||
603 (monitorstart == -1 ||
604 (offset + size > monitorstart &&
605 (monitorend == -1 || offset <= monitorend))))))
606 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
607 offset, offset + size - 1, size);
608 ret = lseek(fd, (off_t)offset, SEEK_SET);
609 if (ret == (off_t)-1) {
610 prterr("dowrite: lseek");
613 iret = write(fd, good_buf + offset, size);
616 prterr("dowrite: write");
618 prt("short write: 0x%x bytes instead of 0x%x\n",
626 domapwrite(unsigned offset, unsigned size)
633 offset -= offset % writebdy;
635 if (!quiet && testcalls > simulatedopcount)
636 prt("skipping zero size write\n");
637 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
640 cur_filesize = file_size;
642 log4(OP_MAPWRITE, offset, size, 0);
644 gendata(original_buf, good_buf, offset, size);
645 if (file_size < offset + size) {
646 if (file_size < offset)
647 memset(good_buf + file_size, '\0', offset - file_size);
648 file_size = offset + size;
650 warn("Lite file size bug in fsx!");
655 if (testcalls <= simulatedopcount)
658 if (!quiet && ((progressinterval &&
659 testcalls % progressinterval == 0) ||
661 (monitorstart == -1 ||
662 (offset + size > monitorstart &&
663 (monitorend == -1 || offset <= monitorend))))))
664 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
665 offset, offset + size - 1, size);
667 if (file_size > cur_filesize) {
668 if (ftruncate(fd, file_size) == -1) {
669 prterr("domapwrite: ftruncate");
673 pg_offset = offset & page_mask;
674 map_size = pg_offset + size;
676 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
677 MAP_FILE | MAP_SHARED, fd,
678 (off_t)(offset - pg_offset))) == (char *)-1) {
679 prterr("domapwrite: mmap");
682 memcpy(p + pg_offset, good_buf + offset, size);
683 if (msync(p, map_size, 0) != 0) {
684 prterr("domapwrite: msync");
688 check_eofpage("Write", offset, p, size);
690 if (munmap(p, map_size) != 0) {
691 prterr("domapwrite: munmap");
698 dotruncate(unsigned size)
700 int oldsize = file_size;
702 size -= size % truncbdy;
703 if (size > biggest) {
705 if (!quiet && testcalls > simulatedopcount)
706 prt("truncating to largest ever: 0x%x\n", size);
709 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
711 if (size > file_size)
712 memset(good_buf + file_size, '\0', size - file_size);
715 if (testcalls <= simulatedopcount)
718 if ((progressinterval && testcalls % progressinterval == 0) ||
719 (debug && (monitorstart == -1 || monitorend == -1 ||
720 size <= monitorend)))
721 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
722 if (ftruncate(fd, (off_t)size) == -1) {
723 prt("ftruncate1: %x\n", size);
724 prterr("dotruncate: ftruncate");
735 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
736 prterr("writefileimage: lseek");
739 iret = write(fd, good_buf, file_size);
740 if ((off_t)iret != file_size) {
742 prterr("writefileimage: write");
744 prt("short write: 0x%x bytes instead of 0x%llx\n",
745 iret, (unsigned long long)file_size);
748 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
749 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
750 prterr("writefileimage: ftruncate");
759 if (testcalls <= simulatedopcount)
763 prt("%lu close/open\n", testcalls);
765 prterr("docloseopen: close");
768 fd = open(fname, O_RDWR, 0);
770 prterr("docloseopen: open");
779 unsigned long offset;
780 unsigned long size = maxoplen;
781 unsigned long rv = random();
782 unsigned long op = rv % (3 + !lite + mapped_writes);
784 /* turn off the map read if necessary */
786 if (op == 2 && !mapped_reads)
789 if (simulatedopcount > 0 && testcalls == simulatedopcount)
795 closeopen = (rv >> 3) < (1 << 28) / closeprob;
797 if (debugstart > 0 && testcalls >= debugstart)
800 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
801 prt("%lu...\n", testcalls);
808 * MAPWRITE: op = 3 or 4
810 if (lite ? 0 : op == 3 && style == 0) /* vanilla truncate? */
811 dotruncate(random() % maxfilelen);
814 size = random() % (maxoplen+1);
815 if (lite ? 0 : op == 3)
819 if (op == 1 || op == (lite ? 3 : 4)) {
820 offset %= maxfilelen;
821 if (offset + size > maxfilelen)
822 size = maxfilelen - offset;
824 domapwrite(offset, size);
826 dowrite(offset, size);
832 if (offset + size > file_size)
833 size = file_size - offset;
835 domapread(offset, size);
837 doread(offset, size);
841 if (sizechecks && testcalls > simulatedopcount)
853 prt("signal %d\n", sig);
854 prt("testcalls = %lu\n", testcalls);
862 fprintf(stdout, "usage: %s",
863 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
864 -b opnum: beginning operation number (default 1)\n\
865 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
866 -d: debug output for all operations\n\
867 -l flen: the upper bound on file size (default 262144)\n\
868 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
869 -n: no verifications of file size\n\
870 -o oplen: the upper bound on operation size (default 65536)\n\
871 -p progressinterval: debug output at specified operation interval\n\
872 -q: quieter operation\n\
873 -r readbdy: 4096 would make reads page aligned (default 1)\n\
874 -s style: 1 gives smaller truncates (default 0)\n\
875 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
876 -w writebdy: 4096 would make writes page aligned (default 1)\n\
877 -D startingop: debug output starting at specified operation\n\
878 -L: fsxLite - no file creations & no file size changes\n\
879 -N numops: total # operations to do (default infinity)\n\
880 -O: use oplen (see -o flag) for every op (default random)\n\
881 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
882 -S seed: for random # generator (default 1) 0 gets timestamp\n\
883 -W: mapped write operations DISabled\n\
884 -R: mapped read operations DISabled)\n\
885 fname: this filename is REQUIRED (no default)\n");
891 getnum(char *s, char **e)
896 ret = strtol(s, e, 0);
925 main(int argc, char **argv)
935 page_size = getpagesize();
936 page_mask = page_size - 1;
938 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
940 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
944 simulatedopcount = getnum(optarg, &endp);
946 fprintf(stdout, "Will begin at operation %ld\n",
948 if (simulatedopcount == 0)
950 simulatedopcount -= 1;
953 closeprob = getnum(optarg, &endp);
956 "Chance of close/open is 1 in %d\n",
965 maxfilelen = getnum(optarg, &endp);
970 monitorstart = getnum(optarg, &endp);
971 if (monitorstart < 0)
973 if (!endp || *endp++ != ':')
975 monitorend = getnum(endp, &endp);
979 monitorend = -1; /* aka infinity */
985 maxoplen = getnum(optarg, &endp);
990 progressinterval = getnum(optarg, &endp);
991 if (progressinterval < 0)
998 readbdy = getnum(optarg, &endp);
1003 style = getnum(optarg, &endp);
1004 if (style < 0 || style > 1)
1008 truncbdy = getnum(optarg, &endp);
1013 writebdy = getnum(optarg, &endp);
1018 debugstart = getnum(optarg, &endp);
1026 numops = getnum(optarg, &endp);
1034 strncpy(goodfile, optarg, sizeof(goodfile));
1035 strcat(goodfile, "/");
1036 strncpy(logfile, optarg, sizeof(logfile));
1037 strcat(logfile, "/");
1043 seed = getnum(optarg, &endp);
1045 seed = time(0) % 10000;
1047 fprintf(stdout, "Seed set to %d\n", seed);
1054 fprintf(stdout, "mapped writes DISABLED\n");
1067 signal(SIGHUP, cleanup);
1068 signal(SIGINT, cleanup);
1069 signal(SIGPIPE, cleanup);
1070 signal(SIGALRM, cleanup);
1071 signal(SIGTERM, cleanup);
1072 signal(SIGXCPU, cleanup);
1073 signal(SIGXFSZ, cleanup);
1074 signal(SIGVTALRM, cleanup);
1075 signal(SIGUSR1, cleanup);
1076 signal(SIGUSR2, cleanup);
1078 initstate(seed, state, 256);
1080 fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1085 strncat(goodfile, fname, 256);
1086 strcat (goodfile, ".fsxgood");
1087 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1088 if (fsxgoodfd < 0) {
1092 strncat(logfile, fname, 256);
1093 strcat (logfile, ".fsxlog");
1094 fsxlogf = fopen(logfile, "w");
1095 if (fsxlogf == NULL) {
1101 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1102 if (file_size == (off_t)-1) {
1104 warn("main: lseek eof");
1107 ret = lseek(fd, (off_t)0, SEEK_SET);
1108 if (ret == (off_t)-1) {
1110 warn("main: lseek 0");
1114 original_buf = (char *) malloc(maxfilelen);
1115 for (i = 0; i < maxfilelen; i++)
1116 original_buf[i] = random() % 256;
1117 good_buf = (char *) malloc(maxfilelen);
1118 memset(good_buf, '\0', maxfilelen);
1119 temp_buf = (char *) malloc(maxoplen);
1120 memset(temp_buf, '\0', maxoplen);
1121 if (lite) { /* zero entire existing file */
1124 written = write(fd, good_buf, (size_t)maxfilelen);
1125 if (written != maxfilelen) {
1126 if (written == -1) {
1128 warn("main: error on write");
1130 warn("main: short write, 0x%x bytes instead of 0x%x\n",
1131 (unsigned)written, maxfilelen);
1137 while (numops == -1 || numops--)
1144 prt("All operations completed A-OK!\n");