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>
27 #define O_DIRECT 040000
31 #define ALIGNMENT 16384
32 #define TRUNCSIZE 1000
34 /* write data to disk - buffered/sync or direct
35 * write different buffered data to disk
37 * direct read back, see if server puts stale data down
48 char ch, *filename="testfile";
52 { printf("Usage: trunc -f testfilename\n");
56 while((c=getopt(argc,argv,"f:"))!=EOF) {
63 fprintf(stderr,"Usage: trunc -f filename\n");
68 err = posix_memalign(&buf, ALIGNMENT, BUFSIZE);
69 if (err < 0) perror("posix_memalign failed");
71 err = posix_memalign(&goodbuf, ALIGNMENT, BUFSIZE);
72 if (err < 0) perror("posix_memalign failed");
74 err = unlink(filename);
75 /* if (err < 0) perror("unlink failed");*/
77 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
78 if (fd < 0) perror("direct open failed");
80 memset(buf, 1, BUFSIZE);
82 printf("direct write of 1's into file\n");
83 err = write(fd, buf, BUFSIZE);
84 if (err < 0) perror("buffered write failed");
88 fd = open(filename, O_CREAT|O_RDWR, 0666);
89 if (fd < 0) perror("buffered open failed");
93 memset(buf, 2, BUFSIZE);
94 memset(goodbuf, 2, BUFSIZE);
96 printf("buffered write of 2's into file\n");
97 err = write(fd, buf, BUFSIZE);
98 if (err < 0) perror("direct write failed");
100 /* 1 now on disk, but 2 data is buffered */
102 printf("truncate file\n");
103 err = ftruncate(fd, TRUNCSIZE);
104 if (err < 0) perror("ftruncate failed");
105 starttime = time(NULL);
107 printf("sync buffered data (2's)\n");
109 if (err < 0) perror("fdatasync failed");
111 /* during truncate server may have read/modified/written last block */
115 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
116 if (fd < 0) perror("direct open failed");
118 /* read what's really on disk now */
120 printf("iterate direct reads for %ds or until failure...\n", WAITTIME);
122 while ((elapsed = (time(NULL) - starttime)) <= WAITTIME) {
127 err = lseek(fd, 0, SEEK_SET);
128 if (err < 0) perror("lseek failed");
130 err = read(fd, buf, BUFSIZE);
131 if (err < 0) perror("read failed");
133 err = memcmp(buf, goodbuf, 100);
135 printf("\nFailed after %d secs: read %d's\n", elapsed, buf[0]);