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>
29 #define O_DIRECT 040000
33 #define ALIGNMENT 16384
34 #define TRUNCSIZE 1000
36 /* write data to disk - buffered/sync or direct
37 * write different buffered data to disk
39 * direct read back, see if server puts stale data down
50 char ch, *filename="testfile";
54 { printf("Usage: trunc -f testfilename\n");
58 while((c=getopt(argc,argv,"f:"))!=EOF) {
65 fprintf(stderr,"Usage: trunc -f filename\n");
70 err = posix_memalign((void **)&buf, ALIGNMENT, BUFSIZE);
71 if (err < 0) perror("posix_memalign failed");
73 err = posix_memalign((void **)&goodbuf, ALIGNMENT, BUFSIZE);
74 if (err < 0) perror("posix_memalign failed");
76 err = unlink(filename);
77 /* if (err < 0) perror("unlink failed");*/
79 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
80 if (fd < 0) perror("direct open failed");
82 memset(buf, 1, BUFSIZE);
84 printf("direct write of 1's into file\n");
85 err = write(fd, buf, BUFSIZE);
86 if (err < 0) perror("buffered write failed");
90 fd = open(filename, O_CREAT|O_RDWR, 0666);
91 if (fd < 0) perror("buffered open failed");
95 memset(buf, 2, BUFSIZE);
96 memset(goodbuf, 2, BUFSIZE);
98 printf("buffered write of 2's into file\n");
99 err = write(fd, buf, BUFSIZE);
100 if (err < 0) perror("direct write failed");
102 /* 1 now on disk, but 2 data is buffered */
104 printf("truncate file\n");
105 err = ftruncate(fd, TRUNCSIZE);
106 if (err < 0) perror("ftruncate failed");
107 starttime = time(NULL);
109 printf("sync buffered data (2's)\n");
111 if (err < 0) perror("fdatasync failed");
113 /* during truncate server may have read/modified/written last block */
117 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
118 if (fd < 0) perror("direct open failed");
120 /* read what's really on disk now */
122 printf("iterate direct reads for %ds or until failure...\n", WAITTIME);
124 while ((elapsed = (time(NULL) - starttime)) <= WAITTIME) {
129 err = lseek(fd, 0, SEEK_SET);
130 if (err < 0) perror("lseek failed");
132 err = read(fd, buf, BUFSIZE);
133 if (err < 0) perror("read failed");
135 err = memcmp(buf, goodbuf, 100);
137 printf("\nFailed after %d secs: read %d's\n", elapsed, buf[0]);