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.2 (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
35 * Checks for mmap last-page zero fill.
39 #include <sys/types.h>
42 # include <sys/param.h>
61 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
64 * A log entry is an operation and a bunch of arguments.
74 struct log_entry oplog[LOGSIZE]; /* the log */
75 int logptr = 0; /* current position in log */
76 int logcount = 0; /* total ops */
85 #define OP_CLOSEOPEN 4
93 char *original_buf; /* a pointer to the original data */
94 char *good_buf; /* a pointer to the correct data */
95 char *temp_buf; /* a pointer to the current data */
96 char *fname; /* name of our test file */
97 int fd; /* fd for our test file */
102 unsigned long testcalls = 0; /* calls to function "test" */
104 unsigned long simulatedopcount = 0; /* -b flag */
105 int closeprob = 0; /* -c flag */
106 int debug = 0; /* -d flag */
107 unsigned long debugstart = 0; /* -D flag */
108 unsigned long maxfilelen = 256 * 1024; /* -l flag */
109 int sizechecks = 1; /* -n flag disables them */
110 int maxoplen = 64 * 1024; /* -o flag */
111 int quiet = 0; /* -q flag */
112 unsigned long progressinterval = 0; /* -p flag */
113 int readbdy = 1; /* -r flag */
114 int style = 0; /* -s flag */
115 int truncbdy = 1; /* -t flag */
116 int writebdy = 1; /* -w flag */
117 long monitorstart = -1; /* -m flag */
118 long monitorend = -1; /* -m flag */
119 int lite = 0; /* -L flag */
120 long numops = -1; /* -N flag */
121 int randomoplen = 1; /* -O flag disables it */
122 int seed = 1; /* -S flag */
123 int mapped_writes = 1; /* -W flag disables */
124 int mapped_reads = 1; /* -R flag disables it */
126 FILE * fsxlogf = NULL;
132 vwarnc(code, fmt, ap)
137 fprintf(stderr, "fsx: ");
139 vfprintf(stderr, fmt, ap);
140 fprintf(stderr, ": ");
142 fprintf(stderr, "%s\n", strerror(code));
147 warn(const char * fmt, ...)
151 vwarnc(errno, fmt, ap);
162 vfprintf(stdout, fmt, args);
164 vfprintf(fsxlogf, fmt, args);
171 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
176 log4(int operation, int arg0, int arg1, int arg2)
178 struct log_entry *le;
181 le->operation = operation;
183 le->operation = ~ le->operation;
189 if (logptr >= LOGSIZE)
198 struct log_entry *lp;
200 prt("LOG DUMP (%d total operations):\n", logcount);
201 if (logcount < LOGSIZE) {
208 for ( ; count > 0; count--) {
211 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
212 prt("%d(%d mod 256): ", opnum, opnum%256);
214 if ((closeopen = lp->operation < 0))
215 lp->operation = ~ lp->operation;
217 switch (lp->operation) {
219 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
220 lp->args[0], lp->args[0] + lp->args[1] - 1,
222 if (badoff >= lp->args[0] && badoff <
223 lp->args[0] + lp->args[1])
227 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
228 lp->args[0], lp->args[0] + lp->args[1] - 1,
230 if (badoff >= lp->args[0] && badoff <
231 lp->args[0] + lp->args[1])
235 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
236 lp->args[0], lp->args[0] + lp->args[1] - 1,
238 if (badoff >= lp->args[0] &&
239 badoff < lp->args[0] + lp->args[1])
243 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
244 lp->args[0], lp->args[0] + lp->args[1] - 1,
246 if (lp->args[0] > lp->args[2])
248 else if (lp->args[0] + lp->args[1] > lp->args[2])
250 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
251 badoff < lp->args[0] + lp->args[1])
255 down = lp->args[0] < lp->args[1];
256 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
257 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
258 if (badoff >= lp->args[!down] &&
259 badoff < lp->args[!!down])
263 prt("SKIPPED (no operation)");
266 prt("BOGUS LOG ENTRY (operation code = %d)!",
270 prt("\n\t\tCLOSE/OPEN");
280 save_buffer(char *buffer, off_t bufferlength, int fd)
283 ssize_t byteswritten;
285 if (fd <= 0 || bufferlength == 0)
288 if (bufferlength > SSIZE_MAX) {
289 prt("fsx flaw: overflow in save_buffer\n");
293 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
294 if (size_by_seek == (off_t)-1)
295 prterr("save_buffer: lseek eof");
296 else if (bufferlength > size_by_seek) {
297 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
298 (unsigned long long)bufferlength);
299 bufferlength = size_by_seek;
303 ret = lseek(fd, (off_t)0, SEEK_SET);
304 if (ret == (off_t)-1)
305 prterr("save_buffer: lseek 0");
307 byteswritten = write(fd, buffer, (size_t)bufferlength);
308 if (byteswritten != bufferlength) {
309 if (byteswritten == -1)
310 prterr("save_buffer write");
312 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
313 (unsigned)byteswritten,
314 (unsigned long long)bufferlength);
320 report_failure(int status)
326 save_buffer(good_buf, file_size, fsxgoodfd);
327 prt("Correct content saved for comparison\n");
328 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
337 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
338 *(((unsigned char *)(cp)) + 1)))
341 check_buffers(unsigned offset, unsigned size)
349 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
350 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
352 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
354 c = good_buf[offset];
358 bad = short_at(&temp_buf[i]);
359 prt("0x%5x\t0x%04x\t0x%04x", offset,
360 short_at(&good_buf[offset]), bad);
361 op = temp_buf[offset & 1 ? i+1 : i];
373 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
375 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
377 prt("????????????????\n");
389 if (fstat(fd, &statbuf)) {
390 prterr("check_size: fstat");
391 statbuf.st_size = -1;
393 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
394 if (file_size != statbuf.st_size || file_size != size_by_seek) {
395 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
396 (unsigned long long)file_size,
397 (unsigned long long)statbuf.st_size,
398 (unsigned long long)size_by_seek);
405 check_trunc_hack(void)
409 ftruncate(fd, (off_t)0);
410 ftruncate(fd, (off_t)100000);
412 if (statbuf.st_size != (off_t)100000) {
413 prt("no extend on truncate! not posix!\n");
416 ftruncate(fd, (off_t)0);
421 doread(unsigned offset, unsigned size)
426 offset -= offset % readbdy;
428 if (!quiet && testcalls > simulatedopcount)
429 prt("skipping zero size read\n");
430 log4(OP_SKIPPED, OP_READ, offset, size);
433 if (size + offset > file_size) {
434 if (!quiet && testcalls > simulatedopcount)
435 prt("skipping seek/read past end of file\n");
436 log4(OP_SKIPPED, OP_READ, offset, size);
440 log4(OP_READ, offset, size, 0);
442 if (testcalls <= simulatedopcount)
445 if (!quiet && ((progressinterval &&
446 testcalls % progressinterval == 0) ||
448 (monitorstart == -1 ||
449 (offset + size > monitorstart &&
450 (monitorend == -1 || offset <= monitorend))))))
451 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
452 offset, offset + size - 1, size);
453 ret = lseek(fd, (off_t)offset, SEEK_SET);
454 if (ret == (off_t)-1) {
455 prterr("doread: lseek");
458 iret = read(fd, temp_buf, size);
461 prterr("doread: read");
463 prt("short read: 0x%x bytes instead of 0x%x\n",
467 check_buffers(offset, size);
472 check_eofpage(char *s, unsigned offset, char *p, int size)
474 unsigned last_page, should_be_zero;
476 if (offset + size <= (file_size & ~page_mask))
479 * we landed in the last page of the file
480 * test to make sure the VM system provided 0's
481 * beyond the true end of the file mapping
482 * (as required by mmap def in 1996 posix 1003.1)
484 last_page = ((int)p + (offset & page_mask) + size) & ~page_mask;
486 for (should_be_zero = last_page + (file_size & page_mask);
487 should_be_zero < last_page + page_size;
489 if (*(char *)should_be_zero) {
490 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
491 s, file_size - 1, should_be_zero & page_mask,
492 short_at(should_be_zero));
499 domapread(unsigned offset, unsigned size)
505 offset -= offset % readbdy;
507 if (!quiet && testcalls > simulatedopcount)
508 prt("skipping zero size read\n");
509 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
512 if (size + offset > file_size) {
513 if (!quiet && testcalls > simulatedopcount)
514 prt("skipping seek/read past end of file\n");
515 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
519 log4(OP_MAPREAD, offset, size, 0);
521 if (testcalls <= simulatedopcount)
524 if (!quiet && ((progressinterval &&
525 testcalls % progressinterval == 0) ||
527 (monitorstart == -1 ||
528 (offset + size > monitorstart &&
529 (monitorend == -1 || offset <= monitorend))))))
530 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
531 offset, offset + size - 1, size);
533 pg_offset = offset & page_mask;
534 map_size = pg_offset + size;
536 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
537 (off_t)(offset - pg_offset))) == (char *)-1) {
538 prterr("domapread: mmap");
541 memcpy(temp_buf, p + pg_offset, size);
543 check_eofpage("Read", offset, p, size);
545 if (munmap(p, map_size) != 0) {
546 prterr("domapread: munmap");
550 check_buffers(offset, size);
555 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
558 good_buf[offset] = testcalls % 256;
560 good_buf[offset] += original_buf[offset];
567 dowrite(unsigned offset, unsigned size)
572 offset -= offset % writebdy;
574 if (!quiet && testcalls > simulatedopcount)
575 prt("skipping zero size write\n");
576 log4(OP_SKIPPED, OP_WRITE, offset, size);
580 log4(OP_WRITE, offset, size, file_size);
582 gendata(original_buf, good_buf, offset, size);
583 if (file_size < offset + size) {
584 if (file_size < offset)
585 memset(good_buf + file_size, '\0', offset - file_size);
586 file_size = offset + size;
588 warn("Lite file size bug in fsx!");
593 if (testcalls <= simulatedopcount)
596 if (!quiet && ((progressinterval &&
597 testcalls % progressinterval == 0) ||
599 (monitorstart == -1 ||
600 (offset + size > monitorstart &&
601 (monitorend == -1 || offset <= monitorend))))))
602 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
603 offset, offset + size - 1, size);
604 ret = lseek(fd, (off_t)offset, SEEK_SET);
605 if (ret == (off_t)-1) {
606 prterr("dowrite: lseek");
609 iret = write(fd, good_buf + offset, size);
612 prterr("dowrite: write");
614 prt("short write: 0x%x bytes instead of 0x%x\n",
622 domapwrite(unsigned offset, unsigned size)
629 offset -= offset % writebdy;
631 if (!quiet && testcalls > simulatedopcount)
632 prt("skipping zero size write\n");
633 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
636 cur_filesize = file_size;
638 log4(OP_MAPWRITE, offset, size, 0);
640 gendata(original_buf, good_buf, offset, size);
641 if (file_size < offset + size) {
642 if (file_size < offset)
643 memset(good_buf + file_size, '\0', offset - file_size);
644 file_size = offset + size;
646 warn("Lite file size bug in fsx!");
651 if (testcalls <= simulatedopcount)
654 if (!quiet && ((progressinterval &&
655 testcalls % progressinterval == 0) ||
657 (monitorstart == -1 ||
658 (offset + size > monitorstart &&
659 (monitorend == -1 || offset <= monitorend))))))
660 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
661 offset, offset + size - 1, size);
663 if (file_size > cur_filesize) {
664 if (ftruncate(fd, file_size) == -1) {
665 prterr("domapwrite: ftruncate");
669 pg_offset = offset & page_mask;
670 map_size = pg_offset + size;
672 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
673 MAP_FILE | MAP_SHARED, fd,
674 (off_t)(offset - pg_offset))) == (char *)-1) {
675 prterr("domapwrite: mmap");
678 memcpy(p + pg_offset, good_buf + offset, size);
679 if (msync(p, map_size, 0) != 0) {
680 prterr("domapwrite: msync");
684 check_eofpage("Write", offset, p, size);
686 if (munmap(p, map_size) != 0) {
687 prterr("domapwrite: munmap");
694 dotruncate(unsigned size)
696 int oldsize = file_size;
698 size -= size % truncbdy;
699 if (size > biggest) {
701 if (!quiet && testcalls > simulatedopcount)
702 prt("truncating to largest ever: 0x%x\n", size);
705 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
707 if (size > file_size)
708 memset(good_buf + file_size, '\0', size - file_size);
711 if (testcalls <= simulatedopcount)
714 if ((progressinterval && testcalls % progressinterval == 0) ||
715 (debug && (monitorstart == -1 || monitorend == -1 ||
716 size <= monitorend)))
717 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
718 if (ftruncate(fd, (off_t)size) == -1) {
719 prt("ftruncate1: %x\n", size);
720 prterr("dotruncate: ftruncate");
731 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
732 prterr("writefileimage: lseek");
735 iret = write(fd, good_buf, file_size);
736 if ((off_t)iret != file_size) {
738 prterr("writefileimage: write");
740 prt("short write: 0x%x bytes instead of 0x%llx\n",
741 iret, (unsigned long long)file_size);
744 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
745 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
746 prterr("writefileimage: ftruncate");
755 if (testcalls <= simulatedopcount)
759 prt("%lu close/open\n", testcalls);
761 prterr("docloseopen: close");
764 fd = open(fname, O_RDWR, 0);
766 prterr("docloseopen: open");
775 unsigned long offset;
776 unsigned long size = maxoplen;
777 unsigned long rv = random();
778 unsigned long op = rv % (3 + !lite + mapped_writes);
780 /* turn off the map read if necessary */
782 if (op == 2 && !mapped_reads)
785 if (simulatedopcount > 0 && testcalls == simulatedopcount)
791 closeopen = (rv >> 3) < (1 << 28) / closeprob;
793 if (debugstart > 0 && testcalls >= debugstart)
796 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
797 prt("%lu...\n", testcalls);
804 * MAPWRITE: op = 3 or 4
806 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
807 dotruncate(random() % maxfilelen);
810 size = random() % (maxoplen+1);
811 if (lite ? 0 : op == 3)
815 if (op == 1 || op == (lite ? 3 : 4)) {
816 offset %= maxfilelen;
817 if (offset + size > maxfilelen)
818 size = maxfilelen - offset;
820 domapwrite(offset, size);
822 dowrite(offset, size);
828 if (offset + size > file_size)
829 size = file_size - offset;
831 domapread(offset, size);
833 doread(offset, size);
837 if (sizechecks && testcalls > simulatedopcount)
849 prt("signal %d\n", sig);
850 prt("testcalls = %lu\n", testcalls);
858 fprintf(stdout, "usage: %s",
859 "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\
860 -b opnum: beginning operation number (default 1)\n\
861 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
862 -d: debug output for all operations\n\
863 -l flen: the upper bound on file size (default 262144)\n\
864 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
865 -n: no verifications of file size\n\
866 -o oplen: the upper bound on operation size (default 65536)\n\
867 -p progressinterval: debug output at specified operation interval\n\
868 -q: quieter operation\n\
869 -r readbdy: 4096 would make reads page aligned (default 1)\n\
870 -s style: 1 gives smaller truncates (default 0)\n\
871 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
872 -w writebdy: 4096 would make writes page aligned (default 1)\n\
873 -D startingop: debug output starting at specified operation\n\
874 -L: fsxLite - no file creations & no file size changes\n\
875 -N numops: total # operations to do (default infinity)\n\
876 -O: use oplen (see -o flag) for every op (default random)\n\
877 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
878 -S seed: for random # generator (default 1) 0 gets timestamp\n\
879 -W: mapped write operations DISabled\n\
880 -R: mapped read operations DISabled)\n\
881 fname: this filename is REQUIRED (no default)\n");
887 getnum(char *s, char **e)
892 ret = strtol(s, e, 0);
921 main(int argc, char **argv)
931 page_size = getpagesize();
932 page_mask = page_size - 1;
934 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
936 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
940 simulatedopcount = getnum(optarg, &endp);
942 fprintf(stdout, "Will begin at operation %ld\n",
944 if (simulatedopcount == 0)
946 simulatedopcount -= 1;
949 closeprob = getnum(optarg, &endp);
952 "Chance of close/open is 1 in %d\n",
961 maxfilelen = getnum(optarg, &endp);
966 monitorstart = getnum(optarg, &endp);
967 if (monitorstart < 0)
969 if (!endp || *endp++ != ':')
971 monitorend = getnum(endp, &endp);
975 monitorend = -1; /* aka infinity */
981 maxoplen = getnum(optarg, &endp);
986 progressinterval = getnum(optarg, &endp);
987 if (progressinterval < 0)
994 readbdy = getnum(optarg, &endp);
999 style = getnum(optarg, &endp);
1000 if (style < 0 || style > 1)
1004 truncbdy = getnum(optarg, &endp);
1009 writebdy = getnum(optarg, &endp);
1014 debugstart = getnum(optarg, &endp);
1022 numops = getnum(optarg, &endp);
1030 strncpy(goodfile, optarg, sizeof(goodfile));
1031 strcat(goodfile, "/");
1032 strncpy(logfile, optarg, sizeof(logfile));
1033 strcat(logfile, "/");
1039 seed = getnum(optarg, &endp);
1041 seed = time(0) % 10000;
1043 fprintf(stdout, "Seed set to %d\n", seed);
1050 fprintf(stdout, "mapped writes DISABLED\n");
1063 signal(SIGHUP, cleanup);
1064 signal(SIGINT, cleanup);
1065 signal(SIGPIPE, cleanup);
1066 signal(SIGALRM, cleanup);
1067 signal(SIGTERM, cleanup);
1068 signal(SIGXCPU, cleanup);
1069 signal(SIGXFSZ, cleanup);
1070 signal(SIGVTALRM, cleanup);
1071 signal(SIGUSR1, cleanup);
1072 signal(SIGUSR2, cleanup);
1074 initstate(seed, state, 256);
1076 fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1081 strncat(goodfile, fname, 256);
1082 strcat (goodfile, ".fsxgood");
1083 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1084 if (fsxgoodfd < 0) {
1088 strncat(logfile, fname, 256);
1089 strcat (logfile, ".fsxlog");
1090 fsxlogf = fopen(logfile, "w");
1091 if (fsxlogf == NULL) {
1097 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1098 if (file_size == (off_t)-1) {
1100 warn("main: lseek eof");
1103 ret = lseek(fd, (off_t)0, SEEK_SET);
1104 if (ret == (off_t)-1) {
1106 warn("main: lseek 0");
1110 original_buf = (char *) malloc(maxfilelen);
1111 for (i = 0; i < maxfilelen; i++)
1112 original_buf[i] = random() % 256;
1113 good_buf = (char *) malloc(maxfilelen);
1114 memset(good_buf, '\0', maxfilelen);
1115 temp_buf = (char *) malloc(maxoplen);
1116 memset(temp_buf, '\0', maxoplen);
1117 if (lite) { /* zero entire existing file */
1120 written = write(fd, good_buf, (size_t)maxfilelen);
1121 if (written != maxfilelen) {
1122 if (written == -1) {
1124 warn("main: error on write");
1126 warn("main: short write, 0x%x bytes instead of 0x%x\n",
1127 (unsigned)written, maxfilelen);
1133 while (numops == -1 || numops--)
1140 prt("All operations completed A-OK!\n");