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>
40 # include <sys/param.h>
59 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
62 * A log entry is an operation and a bunch of arguments.
72 struct log_entry oplog[LOGSIZE]; /* the log */
73 int logptr = 0; /* current position in log */
74 int logcount = 0; /* total ops */
83 #define OP_CLOSEOPEN 4
91 char *original_buf; /* a pointer to the original data */
92 char *good_buf; /* a pointer to the correct data */
93 char *temp_buf; /* a pointer to the current data */
94 char *fname; /* name of our test file */
95 int fd; /* fd for our test file */
100 unsigned long testcalls = 0; /* calls to function "test" */
102 unsigned long simulatedopcount = 0; /* -b flag */
103 int closeprob = 0; /* -c flag */
104 int debug = 0; /* -d flag */
105 unsigned long debugstart = 0; /* -D flag */
106 unsigned long maxfilelen = 256 * 1024; /* -l flag */
107 int sizechecks = 1; /* -n flag disables them */
108 int maxoplen = 64 * 1024; /* -o flag */
109 int quiet = 0; /* -q flag */
110 unsigned long progressinterval = 0; /* -p flag */
111 int readbdy = 1; /* -r flag */
112 int style = 0; /* -s flag */
113 int truncbdy = 1; /* -t flag */
114 int writebdy = 1; /* -w flag */
115 long monitorstart = -1; /* -m flag */
116 long monitorend = -1; /* -m flag */
117 int lite = 0; /* -L flag */
118 long numops = -1; /* -N flag */
119 int randomoplen = 1; /* -O flag disables it */
120 int seed = 1; /* -S flag */
121 int mapped_writes = 1; /* -W flag disables */
122 int mapped_reads = 1; /* -R flag disables it */
124 FILE * fsxlogf = NULL;
130 vwarnc(code, fmt, ap)
135 fprintf(stderr, "fsx: ");
137 vfprintf(stderr, fmt, ap);
138 fprintf(stderr, ": ");
140 fprintf(stderr, "%s\n", strerror(code));
145 warn(const char * fmt, ...)
149 vwarnc(errno, fmt, ap);
160 vfprintf(stdout, fmt, args);
162 vfprintf(fsxlogf, fmt, args);
169 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
174 log4(int operation, int arg0, int arg1, int arg2)
176 struct log_entry *le;
179 le->operation = operation;
181 le->operation = ~ le->operation;
187 if (logptr >= LOGSIZE)
196 struct log_entry *lp;
198 prt("LOG DUMP (%d total operations):\n", logcount);
199 if (logcount < LOGSIZE) {
206 for ( ; count > 0; count--) {
209 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
210 prt("%d(%d mod 256): ", opnum, opnum%256);
212 if ((closeopen = lp->operation < 0))
213 lp->operation = ~ lp->operation;
215 switch (lp->operation) {
217 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
218 lp->args[0], lp->args[0] + lp->args[1] - 1,
220 if (badoff >= lp->args[0] && badoff <
221 lp->args[0] + lp->args[1])
225 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
226 lp->args[0], lp->args[0] + lp->args[1] - 1,
228 if (badoff >= lp->args[0] && badoff <
229 lp->args[0] + lp->args[1])
233 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
234 lp->args[0], lp->args[0] + lp->args[1] - 1,
236 if (badoff >= lp->args[0] &&
237 badoff < lp->args[0] + lp->args[1])
241 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
242 lp->args[0], lp->args[0] + lp->args[1] - 1,
244 if (lp->args[0] > lp->args[2])
246 else if (lp->args[0] + lp->args[1] > lp->args[2])
248 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
249 badoff < lp->args[0] + lp->args[1])
253 down = lp->args[0] < lp->args[1];
254 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
255 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
256 if (badoff >= lp->args[!down] &&
257 badoff < lp->args[!!down])
261 prt("SKIPPED (no operation)");
264 prt("BOGUS LOG ENTRY (operation code = %d)!",
268 prt("\n\t\tCLOSE/OPEN");
278 save_buffer(char *buffer, off_t bufferlength, int fd)
281 ssize_t byteswritten;
283 if (fd <= 0 || bufferlength == 0)
286 if (bufferlength > SSIZE_MAX) {
287 prt("fsx flaw: overflow in save_buffer\n");
291 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
292 if (size_by_seek == (off_t)-1)
293 prterr("save_buffer: lseek eof");
294 else if (bufferlength > size_by_seek) {
295 warn("save_buffer: .fsxgood file too short... will
296 save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
297 (unsigned long long)bufferlength);
298 bufferlength = size_by_seek;
302 ret = lseek(fd, (off_t)0, SEEK_SET);
303 if (ret == (off_t)-1)
304 prterr("save_buffer: lseek 0");
306 byteswritten = write(fd, buffer, (size_t)bufferlength);
307 if (byteswritten != bufferlength) {
308 if (byteswritten == -1)
309 prterr("save_buffer write");
311 warn("save_buffer: short write, 0x%x bytes instead
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
374 may be %u\n", ((unsigned)op & 0xff));
376 prt("operation# (mod 256) for the bad data
377 unknown, check HOLE and EXTEND ops\n");
379 prt("????????????????\n");
391 if (fstat(fd, &statbuf)) {
392 prterr("check_size: fstat");
393 statbuf.st_size = -1;
395 size_by_seek = lseek(fd, (off_t)0, SEEK_END);
396 if (file_size != statbuf.st_size || file_size != size_by_seek) {
397 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
398 (unsigned long long)file_size,
399 (unsigned long long)statbuf.st_size,
400 (unsigned long long)size_by_seek);
407 check_trunc_hack(void)
411 ftruncate(fd, (off_t)0);
412 ftruncate(fd, (off_t)100000);
414 if (statbuf.st_size != (off_t)100000) {
415 prt("no extend on truncate! not posix!\n");
423 doread(unsigned offset, unsigned size)
428 offset -= offset % readbdy;
430 if (!quiet && testcalls > simulatedopcount)
431 prt("skipping zero size read\n");
432 log4(OP_SKIPPED, OP_READ, offset, size);
435 if (size + offset > file_size) {
436 if (!quiet && testcalls > simulatedopcount)
437 prt("skipping seek/read past end of file\n");
438 log4(OP_SKIPPED, OP_READ, offset, size);
442 log4(OP_READ, offset, size, 0);
444 if (testcalls <= simulatedopcount)
447 if (!quiet && ((progressinterval &&
448 testcalls % progressinterval == 0) ||
450 (monitorstart == -1 ||
451 (offset + size > monitorstart &&
452 (monitorend == -1 || offset <= monitorend))))))
453 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
454 offset, offset + size - 1, size);
455 ret = lseek(fd, (off_t)offset, SEEK_SET);
456 if (ret == (off_t)-1) {
457 prterr("doread: lseek");
460 iret = read(fd, temp_buf, size);
463 prterr("doread: read");
465 prt("short read: 0x%x bytes instead of 0x%x\n",
469 check_buffers(offset, size);
474 domapread(unsigned offset, unsigned size)
480 offset -= offset % readbdy;
482 if (!quiet && testcalls > simulatedopcount)
483 prt("skipping zero size read\n");
484 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
487 if (size + offset > file_size) {
488 if (!quiet && testcalls > simulatedopcount)
489 prt("skipping seek/read past end of file\n");
490 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
494 log4(OP_MAPREAD, offset, size, 0);
496 if (testcalls <= simulatedopcount)
499 if (!quiet && ((progressinterval &&
500 testcalls % progressinterval == 0) ||
502 (monitorstart == -1 ||
503 (offset + size > monitorstart &&
504 (monitorend == -1 || offset <= monitorend))))))
505 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
506 offset, offset + size - 1, size);
508 pg_offset = offset & page_mask;
509 map_size = pg_offset + size;
511 if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_FILE |
513 (off_t)(offset - pg_offset))) == (char *)-1) {
514 prterr("domapread: mmap");
517 memcpy(temp_buf, p + pg_offset, size);
518 if (munmap(p, map_size) != 0) {
519 prterr("domapread: munmap");
523 check_buffers(offset, size);
528 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
531 good_buf[offset] = testcalls % 256;
533 good_buf[offset] += original_buf[offset];
540 dowrite(unsigned offset, unsigned size)
545 offset -= offset % writebdy;
547 if (!quiet && testcalls > simulatedopcount)
548 prt("skipping zero size write\n");
549 log4(OP_SKIPPED, OP_WRITE, offset, size);
553 log4(OP_WRITE, offset, size, file_size);
555 gendata(original_buf, good_buf, offset, size);
556 if (file_size < offset + size) {
557 if (file_size < offset)
558 memset(good_buf + file_size, '\0', offset - file_size);
559 file_size = offset + size;
561 warn("Lite file size bug in fsx!");
566 if (testcalls <= simulatedopcount)
569 if (!quiet && ((progressinterval &&
570 testcalls % progressinterval == 0) ||
572 (monitorstart == -1 ||
573 (offset + size > monitorstart &&
574 (monitorend == -1 || offset <= monitorend))))))
575 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
576 offset, offset + size - 1, size);
577 ret = lseek(fd, (off_t)offset, SEEK_SET);
578 if (ret == (off_t)-1) {
579 prterr("dowrite: lseek");
582 iret = write(fd, good_buf + offset, size);
585 prterr("dowrite: write");
587 prt("short write: 0x%x bytes instead of 0x%x\n",
595 domapwrite(unsigned offset, unsigned size)
602 offset -= offset % writebdy;
604 if (!quiet && testcalls > simulatedopcount)
605 prt("skipping zero size write\n");
606 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
609 cur_filesize = file_size;
611 log4(OP_MAPWRITE, offset, size, 0);
613 gendata(original_buf, good_buf, offset, size);
614 if (file_size < offset + size) {
615 if (file_size < offset)
616 memset(good_buf + file_size, '\0', offset - file_size);
617 file_size = offset + size;
619 warn("Lite file size bug in fsx!");
624 if (testcalls <= simulatedopcount)
627 if (!quiet && ((progressinterval &&
628 testcalls % progressinterval == 0) ||
630 (monitorstart == -1 ||
631 (offset + size > monitorstart &&
632 (monitorend == -1 || offset <= monitorend))))))
633 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
634 offset, offset + size - 1, size);
636 if (file_size > cur_filesize) {
637 if (ftruncate(fd, file_size) == -1) {
638 prterr("domapwrite: ftruncate");
642 pg_offset = offset & page_mask;
643 map_size = pg_offset + size;
645 if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
646 MAP_FILE | MAP_SHARED, fd,
647 (off_t)(offset - pg_offset))) == (char *)-1) {
648 prterr("domapwrite: mmap");
651 memcpy(p + pg_offset, good_buf + offset, size);
652 if (msync(p, map_size, 0) != 0) {
653 prterr("domapwrite: msync");
656 if (munmap(p, map_size) != 0) {
657 prterr("domapwrite: munmap");
664 dotruncate(unsigned size)
666 int oldsize = file_size;
668 size -= size % truncbdy;
669 if (size > biggest) {
671 if (!quiet && testcalls > simulatedopcount)
672 prt("truncating to largest ever: 0x%x\n", size);
675 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
677 if (size > file_size)
678 memset(good_buf + file_size, '\0', size - file_size);
681 if (testcalls <= simulatedopcount)
684 if ((progressinterval && testcalls % progressinterval == 0) ||
685 (debug && (monitorstart == -1 || monitorend == -1 ||
686 size <= monitorend)))
687 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize,
689 if (ftruncate(fd, (off_t)size) == -1) {
690 prt("ftruncate1: %x\n", size);
691 prterr("dotruncate: ftruncate");
702 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
703 prterr("writefileimage: lseek");
706 iret = write(fd, good_buf, file_size);
707 if ((off_t)iret != file_size) {
709 prterr("writefileimage: write");
711 prt("short write: 0x%x bytes instead of 0x%llx\n",
712 iret, (unsigned long long)file_size);
715 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
716 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
717 prterr("writefileimage: ftruncate");
726 if (testcalls <= simulatedopcount)
730 prt("%lu close/open\n", testcalls);
732 prterr("docloseopen: close");
735 fd = open(fname, O_RDWR, 0);
737 prterr("docloseopen: open");
746 unsigned long offset;
747 unsigned long size = maxoplen;
748 unsigned long rv = random();
749 unsigned long op = rv % (3 + !lite + mapped_writes);
751 /* turn off the map read if necessary */
753 if (op == 2 && !mapped_reads)
756 if (simulatedopcount > 0 && testcalls == simulatedopcount)
762 closeopen = (rv >> 3) < (1 << 28) / closeprob;
764 if (debugstart > 0 && testcalls >= debugstart)
767 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
768 prt("%lu...\n", testcalls);
775 * MAPWRITE: op = 3 or 4
777 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
778 dotruncate(random() % maxfilelen);
781 size = random() % (maxoplen+1);
782 if (lite ? 0 : op == 3)
786 if (op == 1 || op == (lite ? 3 : 4)) {
787 offset %= maxfilelen;
788 if (offset + size > maxfilelen)
789 size = maxfilelen - offset;
791 domapwrite(offset, size);
793 dowrite(offset, size);
799 if (offset + size > file_size)
800 size = file_size - offset;
802 domapread(offset, size);
804 doread(offset, size);
808 if (sizechecks && testcalls > simulatedopcount)
820 prt("signal %d\n", sig);
821 prt("testcalls = %lu\n", testcalls);
829 fprintf(stdout, "usage: %s",
830 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m
831 start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t
832 truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed]
834 -b opnum: beginning operation number (default 1)\n\
835 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
836 -d: debug output for all operations\n\
837 -l flen: the upper bound on file size (default 262144)\n\
838 -m startop:endop: monitor (print debug output) specified byte range
839 (default 0:infinity)\n\
840 -n: no verifications of file size\n\
841 -o oplen: the upper bound on operation size (default 65536)\n\
842 -p progressinterval: debug output at specified operation interval\n\
843 -q: quieter operation\n\
844 -r readbdy: 4096 would make reads page aligned (default 1)\n\
845 -s style: 1 gives smaller truncates (default 0)\n\
846 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
847 -w writebdy: 4096 would make writes page aligned (default 1)\n\
848 -D startingop: debug output starting at specified operation\n\
849 -L: fsxLite - no file creations & no file size changes\n\
850 -N numops: total # operations to do (default infinity)\n\
851 -O: use oplen (see -o flag) for every op (default random)\n\
852 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
853 -S seed: for random # generator (default 1) 0 gets timestamp\n\
854 -W: mapped write operations DISabled\n\
855 -R: read() system calls only (mapped reads disabled)\n\
856 fname: this filename is REQUIRED (no default)\n");
862 getnum(char *s, char **e)
867 ret = strtol(s, e, 0);
896 main(int argc, char **argv)
906 page_size = getpagesize();
907 page_mask = page_size - 1;
909 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
911 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
915 simulatedopcount = getnum(optarg, &endp);
917 fprintf(stdout, "Will begin at operation
920 if (simulatedopcount == 0)
922 simulatedopcount -= 1;
925 closeprob = getnum(optarg, &endp);
928 "Chance of close/open is 1 in %d\n",
937 maxfilelen = getnum(optarg, &endp);
942 monitorstart = getnum(optarg, &endp);
943 if (monitorstart < 0)
945 if (!endp || *endp++ != ':')
947 monitorend = getnum(endp, &endp);
951 monitorend = -1; /* aka infinity */
957 maxoplen = getnum(optarg, &endp);
962 progressinterval = getnum(optarg, &endp);
963 if (progressinterval < 0)
970 readbdy = getnum(optarg, &endp);
975 style = getnum(optarg, &endp);
976 if (style < 0 || style > 1)
980 truncbdy = getnum(optarg, &endp);
985 writebdy = getnum(optarg, &endp);
990 debugstart = getnum(optarg, &endp);
998 numops = getnum(optarg, &endp);
1006 strncpy(goodfile, optarg, sizeof(goodfile));
1007 strcat(goodfile, "/");
1008 strncpy(logfile, optarg, sizeof(logfile));
1009 strcat(logfile, "/");
1015 seed = getnum(optarg, &endp);
1017 seed = time(0) % 10000;
1019 fprintf(stdout, "Seed set to %d\n", seed);
1026 fprintf(stdout, "mapped writes DISABLED\n");
1039 signal(SIGHUP, cleanup);
1040 signal(SIGINT, cleanup);
1041 signal(SIGPIPE, cleanup);
1042 signal(SIGALRM, cleanup);
1043 signal(SIGTERM, cleanup);
1044 signal(SIGXCPU, cleanup);
1045 signal(SIGXFSZ, cleanup);
1046 signal(SIGVTALRM, cleanup);
1047 signal(SIGUSR1, cleanup);
1048 signal(SIGUSR2, cleanup);
1050 initstate(seed, state, 256);
1052 fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1057 strncat(goodfile, fname, 256);
1058 strcat (goodfile, ".fsxgood");
1059 fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1060 if (fsxgoodfd < 0) {
1064 strncat(logfile, fname, 256);
1065 strcat (logfile, ".fsxlog");
1066 fsxlogf = fopen(logfile, "w");
1067 if (fsxlogf == NULL) {
1073 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1074 if (file_size == (off_t)-1) {
1076 warn("main: lseek eof");
1079 ret = lseek(fd, (off_t)0, SEEK_SET);
1080 if (ret == (off_t)-1) {
1082 warn("main: lseek 0");
1086 original_buf = (char *) malloc(maxfilelen);
1087 for (i = 0; i < maxfilelen; i++)
1088 original_buf[i] = random() % 256;
1089 good_buf = (char *) malloc(maxfilelen);
1090 memset(good_buf, '\0', maxfilelen);
1091 temp_buf = (char *) malloc(maxoplen);
1092 memset(temp_buf, '\0', maxoplen);
1093 if (lite) { /* zero entire existing file */
1096 written = write(fd, good_buf, (size_t)maxfilelen);
1097 if (written != maxfilelen) {
1098 if (written == -1) {
1100 warn("main: error on write");
1102 warn("main: short write, 0x%x bytes instead
1104 (unsigned)written, maxfilelen);
1110 while (numops == -1 || numops--)
1117 prt("All operations completed A-OK!\n");