2 * Copyright (C) 1991, NeXT Computer, Inc. All Rights Reserverd.
5 * Author: Avadis Tevanian, Jr.
7 * File system exerciser.
9 * Rewritten 8/98 by Conrad Minshall.
12 #include <sys/types.h>
14 #if defined(sun) || defined(_UWIN) || defined(__linux)
15 # include <sys/param.h>
21 # include <sys/dirent.h>
37 # define L_SET SEEK_SET
40 # define L_INCR SEEK_CUR
43 # define L_XTND SEEK_END
46 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
49 * A log entry is an operation and a bunch of arguments.
59 struct log_entry oplog[LOGSIZE]; /* the log */
60 int logptr = 0; /* current position in log */
61 int logcount = 0; /* total ops */
70 #define OP_CLOSEOPEN 4
76 #define PAGE_SIZE 4096
78 #define PAGE_MASK (PAGE_SIZE - 1)
80 char *original_buf; /* a pointer to the original data */
81 char *good_buf; /* a pointer to the correct data */
82 char *temp_buf; /* a pointer to the current data */
83 char *fname; /* name of our test file */
84 int fd; /* fd for our test file */
89 unsigned long testcalls = 0; /* calls to function "test" */
91 unsigned long simulatedopcount = 0; /* -b flag */
92 int closeprob = 0; /* -c flag */
93 int debug = 0; /* -d flag */
94 unsigned long debugstart = 0; /* -D flag */
95 unsigned long maxfilelen = 256 * 1024; /* -l flag */
96 int sizechecks = 1; /* -n flag disables them */
97 int maxoplen = 64 * 1024; /* -o flag */
98 int quiet = 0; /* -q flag */
99 unsigned long progressinterval = 0; /* -p flag */
100 int readbdy = 1; /* -r flag */
101 int style = 0; /* -s flag */
102 int truncbdy = 1; /* -t flag */
103 int writebdy = 1; /* -w flag */
104 long monitorstart = -1; /* -m flag */
105 long monitorend = -1; /* -m flag */
106 int lite = 0; /* -L flag */
107 long numops = -1; /* -N flag */
108 int randomoplen = 1; /* -O flag disables it */
109 int seed = 1; /* -S flag */
110 int mapped_writes = 1; /* -W flag disables */
111 int mapped_reads = 1; /* -R flag disables it */
113 FILE *fsxlogf = NULL;
124 vfprintf(stdout, fmt, args);
126 vfprintf(fsxlogf, fmt, args);
133 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
138 log4(int operation, int arg0, int arg1, int arg2)
140 struct log_entry *le;
143 le->operation = operation;
145 le->operation = ~le->operation;
151 if (logptr >= LOGSIZE)
160 struct log_entry *lp;
162 prt("LOG DUMP (%d total operations):\n", logcount);
163 if (logcount < LOGSIZE) {
170 for (; count > 0; count--) {
173 opnum = i + 1 + (logcount / LOGSIZE) * LOGSIZE;
174 prt("%d(%d mod 256): ", opnum, opnum % 256);
176 if ((closeopen = lp->operation < 0))
177 lp->operation = ~lp->operation;
179 switch (lp->operation) {
181 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
182 lp->args[0] + lp->args[1] - 1, lp->args[1]);
183 if (badoff >= lp->args[0] && badoff < lp->args[0] + lp->args[1])
187 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
188 lp->args[0] + lp->args[1] - 1, lp->args[1]);
189 if (badoff >= lp->args[0] && badoff < lp->args[0] + lp->args[1])
193 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
194 lp->args[0] + lp->args[1] - 1, lp->args[1]);
195 if (badoff >= lp->args[0] && badoff < lp->args[0] + lp->args[1])
199 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
200 lp->args[0] + lp->args[1] - 1, lp->args[1]);
201 if (lp->args[0] > lp->args[2])
203 else if (lp->args[0] + lp->args[1] > lp->args[2])
205 if ((badoff >= lp->args[0] || badoff >= lp->args[2])
206 && badoff < lp->args[0] + lp->args[1])
210 down = lp->args[0] < lp->args[1];
211 prt("TRUNCATE %s\tfrom 0x%x to 0x%x", down ? "DOWN" : "UP",
212 lp->args[1], lp->args[0]);
213 if (badoff >= lp->args[!down] && badoff < lp->args[!!down])
217 prt("SKIPPED (no operation)");
220 prt("BOGUS LOG ENTRY (operation code = %d)!", lp->operation);
223 prt("\n\t\tCLOSE/OPEN");
233 save_buffer(char *buffer, off_t bufferlength, int fd)
236 ssize_t byteswritten;
238 if (fd <= 0 || bufferlength == 0)
241 if (bufferlength > SSIZE_MAX) {
242 prt("fsx flaw: overflow in save_buffer\n");
246 off_t size_by_seek = lseek(fd, (off_t) 0, L_XTND);
247 if (size_by_seek == (off_t) - 1)
248 prterr("save_buffer: lseek eof");
249 else if (bufferlength > size_by_seek) {
250 warn("save_buffer: .fsxgood file too short... will save 0x%qx bytes instead of 0x%qx\n", (unsigned long long)size_by_seek, (unsigned long long)bufferlength);
251 bufferlength = size_by_seek;
255 ret = lseek(fd, (off_t) 0, SEEK_SET);
256 if (ret == (off_t) - 1)
257 prterr("save_buffer: lseek 0");
259 byteswritten = write(fd, buffer, (size_t) bufferlength);
260 if (byteswritten != bufferlength) {
261 if (byteswritten == -1)
262 prterr("save_buffer write");
264 warn("save_buffer: short write, 0x%x bytes instead of 0x%qx\n",
265 (unsigned)byteswritten, (unsigned long long)bufferlength);
271 report_failure(int status)
277 save_buffer(good_buf, file_size, fsxgoodfd);
278 prt("Correct content saved for comparison\n");
279 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n", fname, fname);
287 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
288 *(((unsigned char *)(cp)) + 1)))
291 check_buffers(unsigned offset, unsigned size)
299 if (bcmp(good_buf + offset, temp_buf, size) != 0) {
300 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n", offset, size);
301 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
303 c = good_buf[offset];
307 bad = short_at(&temp_buf[i]);
308 prt("0x%5x\t0x%04x\t0x%04x", offset,
309 short_at(&good_buf[offset]), bad);
310 op = temp_buf[offset & 1 ? i + 1 : i];
322 prt("operation# (mod 256) for the bad data may be %u\n",
323 ((unsigned)op & 0xff));
325 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
327 prt("????????????????\n");
339 if (fstat(fd, &statbuf)) {
340 prterr("check_size: fstat");
341 statbuf.st_size = -1;
343 size_by_seek = lseek(fd, (off_t) 0, L_XTND);
344 if (file_size != statbuf.st_size || file_size != size_by_seek) {
345 prt("Size error: expected 0x%qx stat 0x%qx seek 0x%qx\n",
346 (unsigned long long)file_size,
347 (unsigned long long)statbuf.st_size,
348 (unsigned long long)size_by_seek);
355 check_trunc_hack(void)
359 ftruncate(fd, (off_t) 0);
360 ftruncate(fd, (off_t) 100000);
362 if (statbuf.st_size != (off_t) 100000) {
363 prt("no extend on truncate! not posix!\n");
371 doread(unsigned offset, unsigned size)
376 offset -= offset % readbdy;
378 if (!quiet && testcalls > simulatedopcount)
379 prt("skipping zero size read\n");
380 log4(OP_SKIPPED, OP_READ, offset, size);
383 if (size + offset > file_size) {
384 if (!quiet && testcalls > simulatedopcount)
385 prt("skipping seek/read past end of file\n");
386 log4(OP_SKIPPED, OP_READ, offset, size);
390 log4(OP_READ, offset, size, 0);
392 if (testcalls <= simulatedopcount)
396 && (progressinterval && testcalls % progressinterval == 0 || debug
397 && (monitorstart == -1 || offset + size > monitorstart
398 && (monitorend == -1 || offset <= monitorend))))
399 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, offset,
400 offset + size - 1, size);
401 ret = lseek(fd, (off_t) offset, SEEK_SET);
402 if (ret == (off_t) - 1) {
403 prterr("doread: lseek");
406 iret = read(fd, temp_buf, size);
409 prterr("doread: read");
411 prt("short read: 0x%x bytes instead of 0x%x\n", iret, size);
414 check_buffers(offset, size);
419 domapread(unsigned offset, unsigned size)
425 offset -= offset % readbdy;
427 if (!quiet && testcalls > simulatedopcount)
428 prt("skipping zero size read\n");
429 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
432 if (size + offset > file_size) {
433 if (!quiet && testcalls > simulatedopcount)
434 prt("skipping seek/read past end of file\n");
435 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
439 log4(OP_MAPREAD, offset, size, 0);
441 if (testcalls <= simulatedopcount)
445 && (progressinterval && testcalls % progressinterval == 0 || debug
446 && (monitorstart == -1 || offset + size > monitorstart
447 && (monitorend == -1 || offset <= monitorend))))
448 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, offset,
449 offset + size - 1, size);
451 pg_offset = offset & PAGE_MASK;
452 map_size = pg_offset + size;
455 (char *)mmap(0, map_size, PROT_READ, MAP_FILE, fd,
456 (off_t) (offset - pg_offset))) == (char *)-1) {
457 prterr("domapread: mmap");
460 memcpy(temp_buf, p + pg_offset, size);
461 if (munmap(p, map_size) != 0) {
462 prterr("domapread: munmap");
466 check_buffers(offset, size);
471 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
474 good_buf[offset] = testcalls % 256;
476 good_buf[offset] += original_buf[offset];
483 dowrite(unsigned offset, unsigned size)
488 offset -= offset % writebdy;
490 if (!quiet && testcalls > simulatedopcount)
491 prt("skipping zero size write\n");
492 log4(OP_SKIPPED, OP_WRITE, offset, size);
496 log4(OP_WRITE, offset, size, file_size);
498 gendata(original_buf, good_buf, offset, size);
499 if (file_size < offset + size) {
500 if (file_size < offset)
501 bzero(good_buf + file_size, offset - file_size);
502 file_size = offset + size;
504 warn("Lite file size bug in fsx!");
509 if (testcalls <= simulatedopcount)
513 && (progressinterval && testcalls % progressinterval == 0 || debug
514 && (monitorstart == -1 || offset + size > monitorstart
515 && (monitorend == -1 || offset <= monitorend))))
516 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, offset,
517 offset + size - 1, size);
518 ret = lseek(fd, (off_t) offset, SEEK_SET);
519 if (ret == (off_t) - 1) {
520 prterr("dowrite: lseek");
523 iret = write(fd, good_buf + offset, size);
526 prterr("dowrite: write");
528 prt("short write: 0x%x bytes instead of 0x%x\n", iret, size);
535 domapwrite(unsigned offset, unsigned size)
542 offset -= offset % writebdy;
544 if (!quiet && testcalls > simulatedopcount)
545 prt("skipping zero size write\n");
546 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
549 cur_filesize = file_size;
551 log4(OP_MAPWRITE, offset, size, 0);
553 gendata(original_buf, good_buf, offset, size);
554 if (file_size < offset + size) {
555 if (file_size < offset)
556 bzero(good_buf + file_size, offset - file_size);
557 file_size = offset + size;
559 warn("Lite file size bug in fsx!");
564 if (testcalls <= simulatedopcount)
568 && (progressinterval && testcalls % progressinterval == 0 || debug
569 && (monitorstart == -1 || offset + size > monitorstart
570 && (monitorend == -1 || offset <= monitorend))))
571 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
572 offset, offset + size - 1, size);
574 if (file_size > cur_filesize) {
575 if (ftruncate(fd, file_size) == -1) {
576 prterr("domapwrite: ftruncate");
580 pg_offset = offset & PAGE_MASK;
581 map_size = pg_offset + size;
584 (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
585 MAP_FILE | MAP_SHARED, fd,
586 (off_t) (offset - pg_offset))) == (char *)-1) {
587 prterr("domapwrite: mmap");
590 memcpy(p + pg_offset, good_buf + offset, size);
591 if (msync(p, map_size, 0) != 0) {
592 prterr("domapwrite: msync");
595 if (munmap(p, map_size) != 0) {
596 prterr("domapwrite: munmap");
603 dotruncate(unsigned size)
605 int oldsize = file_size;
607 size -= size % truncbdy;
608 if (size > biggest) {
610 if (!quiet && testcalls > simulatedopcount)
611 prt("truncating to largest ever: 0x%x\n", size);
614 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
616 if (size > file_size)
617 bzero(good_buf + file_size, size - file_size);
620 if (testcalls <= simulatedopcount)
623 if (progressinterval && testcalls % progressinterval == 0 || debug
624 && (monitorstart == -1 || monitorend == -1 || size <= monitorend))
625 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
626 if (ftruncate(fd, (off_t) size) == -1) {
627 prt("ftruncate1: %x\n", size);
628 prterr("dotruncate: ftruncate");
639 if (lseek(fd, (off_t) 0, SEEK_SET) == (off_t) - 1) {
640 prterr("writefileimage: lseek");
643 iret = write(fd, good_buf, file_size);
644 if ((off_t) iret != file_size) {
646 prterr("writefileimage: write");
648 prt("short write: 0x%x bytes instead of 0x%qx\n", iret,
649 (unsigned long long)file_size);
652 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
653 prt("ftruncate2: %qx\n", (unsigned long long)file_size);
654 prterr("writefileimage: ftruncate");
663 if (testcalls <= simulatedopcount)
667 prt("%lu close/open\n", testcalls);
669 prterr("docloseopen: close");
672 fd = open(fname, O_RDWR, 0);
674 prterr("docloseopen: open");
683 unsigned long offset;
684 unsigned long size = maxoplen;
685 unsigned long rv = random();
686 unsigned long op = rv % (3 + !lite + mapped_writes);
688 /* turn off the map read if necessary */
690 if (op == 2 && !mapped_reads)
693 if (simulatedopcount > 0 && testcalls == simulatedopcount)
698 closeopen = (rv >> 3) < (1 << 28) / closeprob;
700 if (debugstart > 0 && testcalls >= debugstart)
703 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
704 prt("%lu...\n", testcalls);
711 * MAPWRITE: op = 3 or 4
713 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
714 dotruncate(random() % maxfilelen);
717 size = random() % (maxoplen + 1);
718 if (lite ? 0 : op == 3)
722 if (op == 1 || op == (lite ? 3 : 4)) {
723 offset %= maxfilelen;
724 if (offset + size > maxfilelen)
725 size = maxfilelen - offset;
727 domapwrite(offset, size);
729 dowrite(offset, size);
735 if (offset + size > file_size)
736 size = file_size - offset;
738 domapread(offset, size);
740 doread(offset, size);
744 if (sizechecks && testcalls > simulatedopcount)
756 prt("signal %d\n", sig);
757 prt("testcalls = %lu\n", testcalls);
765 fprintf(stdout, "usage: %s",
766 "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\
767 -b opnum: beginning operation number (default 1)\n\
768 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
769 -d: debug output for all operations\n\
770 -l flen: the upper bound on file size (default 262144)\n\
771 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
772 -n: no verifications of file size\n\
773 -o oplen: the upper bound on operation size (default 65536)\n\
774 -p progressinterval: debug output at specified operation interval\n\
775 -q: quieter operation\n\
776 -r readbdy: 4096 would make reads page aligned (default 1)\n\
777 -s style: 1 gives smaller truncates (default 0)\n\
778 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
779 -w writebdy: 4096 would make writes page aligned (default 1)\n\
780 -D startingop: debug output starting at specified operation\n\
781 -L: fsxLite - no file creations & no file size changes\n\
782 -N numops: total # operations to do (default infinity)\n\
783 -O: use oplen (see -o flag) for every op (default random)\n\
784 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
785 -S seed: for random # generator (default 1) 0 gets timestamp\n\
786 -W: mapped write operations DISabled\n\
787 -R: read() system calls only (mapped reads disabled)\n\
788 fname: this filename is REQUIRED (no default)\n");
794 getnum(char *s, char **e)
799 ret = strtol(s, e, 0);
828 main(int argc, char **argv)
838 setvbuf(stdout, NULL, _IOLBF, 0); /* line buffered stdout */
840 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
844 simulatedopcount = getnum(optarg, &endp);
846 fprintf(stdout, "Will begin at operation %ld\n",
848 if (simulatedopcount == 0)
850 simulatedopcount -= 1;
853 closeprob = getnum(optarg, &endp);
855 fprintf(stdout, "Chance of close/open is 1 in %d\n",
864 maxfilelen = getnum(optarg, &endp);
869 monitorstart = getnum(optarg, &endp);
870 if (monitorstart < 0)
872 if (!endp || *endp++ != ':')
874 monitorend = getnum(endp, &endp);
878 monitorend = -1; /* aka infinity */
884 maxoplen = getnum(optarg, &endp);
889 progressinterval = getnum(optarg, &endp);
890 if (progressinterval < 0)
897 readbdy = getnum(optarg, &endp);
902 style = getnum(optarg, &endp);
903 if (style < 0 || style > 1)
907 truncbdy = getnum(optarg, &endp);
912 writebdy = getnum(optarg, &endp);
917 debugstart = getnum(optarg, &endp);
925 numops = getnum(optarg, &endp);
933 strncpy(goodfile, optarg, sizeof(goodfile));
934 strcat(goodfile, "/");
935 strncpy(logfile, optarg, sizeof(logfile));
936 strcat(logfile, "/");
942 seed = getnum(optarg, &endp);
944 seed = time(0) % 10000;
946 fprintf(stdout, "Seed set to %d\n", seed);
953 fprintf(stdout, "mapped writes DISABLED\n");
966 signal(SIGHUP, cleanup);
967 signal(SIGINT, cleanup);
968 signal(SIGPIPE, cleanup);
969 signal(SIGALRM, cleanup);
970 signal(SIGTERM, cleanup);
971 signal(SIGXCPU, cleanup);
972 signal(SIGXFSZ, cleanup);
973 signal(SIGVTALRM, cleanup);
974 signal(SIGUSR1, cleanup);
975 signal(SIGUSR2, cleanup);
977 initstate(seed, state, 256);
979 fd = open(fname, O_RDWR | (lite ? 0 : O_CREAT | O_TRUNC), 0666);
984 strncat(goodfile, fname, 256);
985 strcat(goodfile, ".fsxgood");
986 fsxgoodfd = open(goodfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
991 strncat(logfile, fname, 256);
992 strcat(logfile, ".fsxlog");
993 fsxlogf = fopen(logfile, "w");
994 if (fsxlogf == NULL) {
1000 file_size = maxfilelen = lseek(fd, (off_t) 0, L_XTND);
1001 if (file_size == (off_t) - 1) {
1003 warn("main: lseek eof");
1006 ret = lseek(fd, (off_t) 0, SEEK_SET);
1007 if (ret == (off_t) - 1) {
1009 warn("main: lseek 0");
1013 original_buf = (char *)malloc(maxfilelen);
1014 for (i = 0; i < maxfilelen; i++)
1015 original_buf[i] = random() % 256;
1016 good_buf = (char *)malloc(maxfilelen);
1017 bzero(good_buf, maxfilelen);
1018 temp_buf = (char *)malloc(maxoplen);
1019 bzero(temp_buf, maxoplen);
1020 if (lite) { /* zero entire existing file */
1023 written = write(fd, good_buf, (size_t) maxfilelen);
1024 if (written != maxfilelen) {
1025 if (written == -1) {
1027 warn("main: error on write");
1029 warn("main: short write, 0x%x bytes instead of 0x%x\n",
1030 (unsigned)written, maxfilelen);
1036 while (numops == -1 || numops--)
1043 prt("All operations completed A-OK!\n");