2 * $PostgreSQL: pgsql/src/tools/fsync/test_fsync.c,v 1.22 2009/05/08 14:06:27 momjian Exp $
6 * test various fsync() methods
11 #include "access/xlog_internal.h"
12 #include "access/xlog.h"
13 #include "access/xlogdefs.h"
15 #include <sys/types.h>
27 #define FSYNC_FILENAME "./test_fsync.out"
29 /* /tmp might be a memory file system */
30 #define FSYNC_FILENAME "/var/tmp/test_fsync.out"
33 #define WRITE_SIZE (16 * 1024)
36 void print_elapse(struct timeval start_t, struct timeval elapse_t);
39 main(int argc, char *argv[])
41 struct timeval start_t;
42 struct timeval elapse_t;
46 char *full_buf = (char *) malloc(XLOG_SEG_SIZE),
48 char *filename = FSYNC_FILENAME;
50 if (argc > 2 && strcmp(argv[1], "-f") == 0)
58 loops = atoi(argv[1]);
60 for (i = 0; i < XLOG_SEG_SIZE; i++)
63 if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, 0)) == -1)
64 die("Cannot open output file.");
65 if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
67 /* fsync so later fsync's don't have to do it */
68 if (fsync(tmpfile) != 0)
72 buf = (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER, full_buf);
74 printf("Simple write timing:\n");
76 gettimeofday(&start_t, NULL);
77 for (i = 0; i < loops; i++)
79 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
80 die("Cannot open output file.");
81 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
85 gettimeofday(&elapse_t, NULL);
87 print_elapse(start_t, elapse_t);
90 printf("\nCompare fsync times on write() and non-write() descriptor:\n");
91 printf("(If the times are similar, fsync() can sync data written\n on a different descriptor.)\n");
93 /* write, fsync, close */
94 gettimeofday(&start_t, NULL);
95 for (i = 0; i < loops; i++)
97 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
98 die("Cannot open output file.");
99 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
101 if (fsync(tmpfile) != 0)
104 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
105 die("Cannot open output file.");
106 /* do nothing but the open/close the tests are consistent. */
109 gettimeofday(&elapse_t, NULL);
110 printf("\twrite, fsync, close ");
111 print_elapse(start_t, elapse_t);
114 /* write, close, fsync */
115 gettimeofday(&start_t, NULL);
116 for (i = 0; i < loops; i++)
118 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
119 die("Cannot open output file.");
120 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
124 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
125 die("Cannot open output file.");
126 if (fsync(tmpfile) != 0)
130 gettimeofday(&elapse_t, NULL);
131 printf("\twrite, close, fsync ");
132 print_elapse(start_t, elapse_t);
135 printf("\nCompare one o_sync write to two:\n");
137 #ifdef OPEN_SYNC_FLAG
138 /* 16k o_sync write */
139 if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
140 die("Cannot open output file.");
141 gettimeofday(&start_t, NULL);
142 for (i = 0; i < loops; i++)
143 if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
145 gettimeofday(&elapse_t, NULL);
147 printf("\tone 16k o_sync write ");
148 print_elapse(start_t, elapse_t);
151 /* 2*8k o_sync writes */
152 if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
153 die("Cannot open output file.");
154 gettimeofday(&start_t, NULL);
155 for (i = 0; i < loops; i++)
157 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
159 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
162 gettimeofday(&elapse_t, NULL);
164 printf("\ttwo 8k o_sync writes ");
165 print_elapse(start_t, elapse_t);
168 printf("\nCompare file sync methods with one 8k write:\n");
170 printf("\t(o_sync unavailable) ");
174 #ifdef OPEN_DATASYNC_FLAG
175 /* open_dsync, write */
176 if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
177 die("Cannot open output file.");
178 gettimeofday(&start_t, NULL);
179 for (i = 0; i < loops; i++)
180 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
182 gettimeofday(&elapse_t, NULL);
184 printf("\topen o_dsync, write ");
185 print_elapse(start_t, elapse_t);
187 #ifdef OPEN_SYNC_FLAG
188 /* open_fsync, write */
189 if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
190 die("Cannot open output file.");
191 gettimeofday(&start_t, NULL);
192 for (i = 0; i < loops; i++)
193 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
195 gettimeofday(&elapse_t, NULL);
197 printf("\topen o_sync, write ");
198 print_elapse(start_t, elapse_t);
201 printf("\t(o_dsync unavailable) ");
205 #ifdef HAVE_FDATASYNC
206 /* write, fdatasync */
207 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
208 die("Cannot open output file.");
209 gettimeofday(&start_t, NULL);
210 for (i = 0; i < loops; i++)
212 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
216 gettimeofday(&elapse_t, NULL);
218 printf("\twrite, fdatasync ");
219 print_elapse(start_t, elapse_t);
221 printf("\t(fdatasync unavailable)");
225 /* write, fsync, close */
226 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
227 die("Cannot open output file.");
228 gettimeofday(&start_t, NULL);
229 for (i = 0; i < loops; i++)
231 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
233 if (fsync(tmpfile) != 0)
236 gettimeofday(&elapse_t, NULL);
238 printf("\twrite, fsync, ");
239 print_elapse(start_t, elapse_t);
242 printf("\nCompare file sync methods with 2 8k writes:\n");
244 #ifdef OPEN_DATASYNC_FLAG
245 /* open_dsync, write */
246 if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
247 die("Cannot open output file.");
248 gettimeofday(&start_t, NULL);
249 for (i = 0; i < loops; i++)
251 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
253 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
256 gettimeofday(&elapse_t, NULL);
258 printf("\topen o_dsync, write ");
259 print_elapse(start_t, elapse_t);
261 printf("\t(o_dsync unavailable) ");
265 #ifdef OPEN_SYNC_FLAG
266 /* open_fsync, write */
267 if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
268 die("Cannot open output file.");
269 gettimeofday(&start_t, NULL);
270 for (i = 0; i < loops; i++)
272 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
274 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
277 gettimeofday(&elapse_t, NULL);
279 printf("\topen o_sync, write ");
280 print_elapse(start_t, elapse_t);
284 #ifdef HAVE_FDATASYNC
285 /* write, fdatasync */
286 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
287 die("Cannot open output file.");
288 gettimeofday(&start_t, NULL);
289 for (i = 0; i < loops; i++)
291 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
293 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
297 gettimeofday(&elapse_t, NULL);
299 printf("\twrite, fdatasync ");
300 print_elapse(start_t, elapse_t);
302 printf("\t(fdatasync unavailable)");
306 /* write, fsync, close */
307 if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
308 die("Cannot open output file.");
309 gettimeofday(&start_t, NULL);
310 for (i = 0; i < loops; i++)
312 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
314 if (write(tmpfile, buf, WRITE_SIZE / 2) != WRITE_SIZE / 2)
316 if (fsync(tmpfile) != 0)
319 gettimeofday(&elapse_t, NULL);
321 printf("\twrite, fsync, ");
322 print_elapse(start_t, elapse_t);
332 print_elapse(struct timeval start_t, struct timeval elapse_t)
334 if (elapse_t.tv_usec < start_t.tv_usec)
337 elapse_t.tv_usec += 1000000;
340 printf("%3ld.%06ld", (long) (elapse_t.tv_sec - start_t.tv_sec),
341 (long) (elapse_t.tv_usec - start_t.tv_usec));
347 fprintf(stderr, "%s\n", str);