2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <sys/types.h>
30 #define O_DIRECT 040000
35 #define ALIGNMENT 16384
36 #define TRUNCSIZE 1000
38 /* write data to disk - buffered/sync or direct
39 * write different buffered data to disk
41 * direct read back, see if server puts stale data down
50 char *buf = NULL, *goodbuf = NULL;
52 char *filename="testfile";
56 printf("Usage: trunc -f testfilename\n");
60 while((c=getopt(argc,argv,"f:"))!=EOF) {
66 fprintf(stderr,"Usage: trunc -f filename\n");
71 err = posix_memalign((void **)&buf, ALIGNMENT, BUFSIZE);
73 fprintf(stderr, "posix_memalign failed: %s\n", strerror(err));
75 err = posix_memalign((void **)&goodbuf, ALIGNMENT, BUFSIZE);
77 fprintf(stderr, "posix_memalign failed: %s\n", strerror(err));
79 err = unlink(filename);
80 /* if (err < 0) perror("unlink failed");*/
82 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
83 if (fd < 0) perror("direct open failed");
85 memset(buf, 1, BUFSIZE);
87 printf("direct write of 1's into file\n");
88 err = write(fd, buf, BUFSIZE);
89 if (err < 0) perror("direct write failed");
93 fd = open(filename, O_CREAT|O_RDWR, 0666);
94 if (fd < 0) perror("buffered open failed");
98 memset(buf, 2, BUFSIZE);
99 memset(goodbuf, 2, BUFSIZE);
101 printf("buffered write of 2's into file\n");
102 err = write(fd, buf, BUFSIZE);
103 if (err < 0) perror("buffered write failed");
105 /* 1 now on disk, but 2 data is buffered */
107 printf("truncate file\n");
108 err = ftruncate(fd, TRUNCSIZE);
109 if (err < 0) perror("ftruncate failed");
110 starttime = time(NULL);
112 printf("sync buffered data (2's)\n");
114 if (err < 0) perror("fdatasync failed");
116 /* during truncate server may have read/modified/written last block */
120 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
121 if (fd < 0) perror("direct open failed");
123 /* read what's really on disk now */
125 printf("iterate direct reads for %ds or until failure...\n", WAITTIME);
127 while ((elapsed = (time(NULL) - starttime)) <= WAITTIME) {
132 err = lseek(fd, 0, SEEK_SET);
133 if (err < 0) perror("lseek failed");
135 err = read(fd, buf, BUFSIZE);
136 if (err < 0) perror("read failed");
138 err = memcmp(buf, goodbuf, 100);
140 printf("\nFailed after %d secs: read %d's\n", elapsed, buf[0]);