Skip to content

Commit b2906c5

Browse files
committed
Add fsync tests:
Compare fsync before and after write's close: Compare one o_sync write to two: Compare file sync methods with one 8k write: Compare file sync methods with 2 8k writes:
1 parent 5ddecd1 commit b2906c5

File tree

1 file changed

+144
-49
lines changed

1 file changed

+144
-49
lines changed

src/tools/fsync/test_fsync.c

Lines changed: 144 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../../include/pg_config.h"
77

88
#include <sys/types.h>
9+
#include <sys/stat.h>
910
#include <fcntl.h>
1011
#include <stdio.h>
1112
#include <stdlib.h>
@@ -26,124 +27,217 @@
2627
#endif
2728
#endif
2829

29-
void die(char *str);
30-
void print_elapse(struct timeval start_t, struct timeval elapse_t);
30+
void die(char *str);
31+
void print_elapse(struct timeval start_t, struct timeval elapse_t);
3132

32-
int main(int argc, char *argv[])
33+
int
34+
main(int argc, char *argv[])
3335
{
3436
struct timeval start_t;
3537
struct timeval elapse_t;
36-
int tmpfile;
37-
char *strout = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
38+
int tmpfile,
39+
i;
40+
char *strout = (char *) malloc(65536);
41+
42+
for (i = 0; i < 65536; i++)
43+
strout[i] = 'a';
44+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
45+
die("can't open /var/tmp/test_fsync.out");
46+
write(tmpfile, strout, 65536);
47+
fsync(tmpfile); /* fsync so later fsync's don't have to do
48+
* it */
49+
close(tmpfile);
3850

3951
printf("Simple write timing:\n");
40-
/* write only */
52+
/* write only */
4153
gettimeofday(&start_t, NULL);
42-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT, 0600)) == -1)
54+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
4355
die("can't open /var/tmp/test_fsync.out");
44-
write(tmpfile, &strout, 200);
45-
close(tmpfile);
56+
write(tmpfile, strout, 8192);
57+
close(tmpfile);
4658
gettimeofday(&elapse_t, NULL);
47-
unlink("/var/tmp/test_fsync.out");
48-
printf("write ");
59+
printf("\twrite ");
4960
print_elapse(start_t, elapse_t);
5061
printf("\n\n");
5162

5263
printf("Compare fsync before and after write's close:\n");
64+
5365
/* write, fsync, close */
5466
gettimeofday(&start_t, NULL);
55-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT, 0600)) == -1)
67+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
5668
die("can't open /var/tmp/test_fsync.out");
57-
write(tmpfile, &strout, 200);
69+
write(tmpfile, strout, 8192);
5870
fsync(tmpfile);
59-
close(tmpfile);
71+
close(tmpfile);
6072
gettimeofday(&elapse_t, NULL);
61-
unlink("/var/tmp/test_fsync.out");
62-
printf("write, fsync, close ");
73+
printf("\twrite, fsync, close ");
6374
print_elapse(start_t, elapse_t);
6475
printf("\n");
6576

6677
/* write, close, fsync */
6778
gettimeofday(&start_t, NULL);
68-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT, 0600)) == -1)
79+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
6980
die("can't open /var/tmp/test_fsync.out");
70-
write(tmpfile, &strout, 200);
81+
write(tmpfile, strout, 8192);
7182
close(tmpfile);
7283
/* reopen file */
73-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT, 0600)) == -1)
84+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
7485
die("can't open /var/tmp/test_fsync.out");
7586
fsync(tmpfile);
76-
close(tmpfile);
87+
close(tmpfile);
7788
gettimeofday(&elapse_t, NULL);
78-
unlink("/var/tmp/test_fsync.out");
79-
printf("write, close, fsync ");
89+
printf("\twrite, close, fsync ");
8090
print_elapse(start_t, elapse_t);
8191
printf("\n");
8292

83-
printf("\nTest file sync methods:\n");
93+
printf("\nCompare one o_sync write to two:\n");
94+
95+
/* 16k o_sync write */
96+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | OPEN_SYNC_FLAG)) == -1)
97+
die("can't open /var/tmp/test_fsync.out");
98+
gettimeofday(&start_t, NULL);
99+
write(tmpfile, strout, 16384);
100+
gettimeofday(&elapse_t, NULL);
101+
close(tmpfile);
102+
printf("\tone 16k o_sync write ");
103+
print_elapse(start_t, elapse_t);
104+
printf("\n");
105+
106+
/* 2*8k o_sync writes */
107+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | OPEN_SYNC_FLAG)) == -1)
108+
die("can't open /var/tmp/test_fsync.out");
109+
gettimeofday(&start_t, NULL);
110+
write(tmpfile, strout, 8192);
111+
write(tmpfile, strout, 8192);
112+
gettimeofday(&elapse_t, NULL);
113+
close(tmpfile);
114+
printf("\ttwo 8k o_sync writes ");
115+
print_elapse(start_t, elapse_t);
116+
printf("\n");
117+
118+
printf("\nCompare file sync methods with one 8k write:\n");
84119

85120
#ifdef OPEN_DATASYNC_FLAG
86121
/* open_dsync, write */
122+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_DSYNC)) == -1)
123+
die("can't open /var/tmp/test_fsync.out");
87124
gettimeofday(&start_t, NULL);
88-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT | O_DSYNC, 0600)) == -1)
125+
write(tmpfile, strout, 8192);
126+
gettimeofday(&elapse_t, NULL);
127+
close(tmpfile);
128+
printf("\topen o_dsync, write ");
129+
print_elapse(start_t, elapse_t);
130+
#else
131+
printf("\t(o_dsync unavailable) ");
132+
#endif
133+
printf("\n");
134+
135+
/* open_fsync, write */
136+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | OPEN_SYNC_FLAG)) == -1)
89137
die("can't open /var/tmp/test_fsync.out");
90-
write(tmpfile, &strout, 200);
138+
gettimeofday(&start_t, NULL);
139+
write(tmpfile, strout, 8192);
140+
gettimeofday(&elapse_t, NULL);
91141
close(tmpfile);
142+
printf("\topen o_sync, write ");
143+
print_elapse(start_t, elapse_t);
144+
printf("\n");
145+
146+
#ifdef HAVE_FDATASYNC
147+
/* write, fdatasync */
148+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
149+
die("can't open /var/tmp/test_fsync.out");
150+
gettimeofday(&start_t, NULL);
151+
write(tmpfile, strout, 8192);
152+
fdatasync(tmpfile);
92153
gettimeofday(&elapse_t, NULL);
93-
unlink("/var/tmp/test_fsync.out");
94-
printf("open o_dsync, write ");
154+
close(tmpfile);
155+
printf("\twrite, fdatasync ");
95156
print_elapse(start_t, elapse_t);
96157
#else
97-
printf("o_dsync unavailable ");
158+
printf("\t(fdatasync unavailable)");
98159
#endif
99160
printf("\n");
100161

101-
/* open_fsync, write */
162+
/* write, fsync, close */
163+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
164+
die("can't open /var/tmp/test_fsync.out");
102165
gettimeofday(&start_t, NULL);
103-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT | OPEN_SYNC_FLAG, 0600)) == -1)
166+
write(tmpfile, strout, 8192);
167+
fsync(tmpfile);
168+
gettimeofday(&elapse_t, NULL);
169+
close(tmpfile);
170+
printf("\twrite, fsync, ");
171+
print_elapse(start_t, elapse_t);
172+
printf("\n");
173+
174+
printf("\nCompare file sync methods with 2 8k writes:\n");
175+
176+
#ifdef OPEN_DATASYNC_FLAG
177+
/* open_dsync, write */
178+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_DSYNC)) == -1)
104179
die("can't open /var/tmp/test_fsync.out");
105-
write(tmpfile, &strout, 200);
180+
gettimeofday(&start_t, NULL);
181+
write(tmpfile, strout, 8192);
182+
write(tmpfile, strout, 8192);
183+
gettimeofday(&elapse_t, NULL);
106184
close(tmpfile);
185+
printf("\topen o_dsync, write ");
186+
print_elapse(start_t, elapse_t);
187+
#else
188+
printf("\t(o_dsync unavailable) ");
189+
#endif
190+
printf("\n");
191+
192+
/* open_fsync, write */
193+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | OPEN_SYNC_FLAG)) == -1)
194+
die("can't open /var/tmp/test_fsync.out");
195+
gettimeofday(&start_t, NULL);
196+
write(tmpfile, strout, 8192);
197+
write(tmpfile, strout, 8192);
107198
gettimeofday(&elapse_t, NULL);
108-
unlink("/var/tmp/test_fsync.out");
109-
printf("open o_fsync, write ");
199+
close(tmpfile);
200+
printf("\topen o_sync, write ");
110201
print_elapse(start_t, elapse_t);
111202
printf("\n");
112203

113204
#ifdef HAVE_FDATASYNC
114205
/* write, fdatasync */
115-
gettimeofday(&start_t, NULL);
116-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT, 0600)) == -1)
206+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
117207
die("can't open /var/tmp/test_fsync.out");
118-
write(tmpfile, &strout, 200);
208+
gettimeofday(&start_t, NULL);
209+
write(tmpfile, strout, 8192);
210+
write(tmpfile, strout, 8192);
119211
fdatasync(tmpfile);
120-
close(tmpfile);
121212
gettimeofday(&elapse_t, NULL);
122-
unlink("/var/tmp/test_fsync.out");
123-
printf("write, fdatasync ");
213+
close(tmpfile);
214+
printf("\twrite, fdatasync ");
124215
print_elapse(start_t, elapse_t);
125216
#else
126-
printf("fdatasync unavailable ");
217+
printf("\t(fdatasync unavailable)");
127218
#endif
128219
printf("\n");
129220

130221
/* write, fsync, close */
131-
gettimeofday(&start_t, NULL);
132-
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR | O_CREAT, 0600)) == -1)
222+
if ((tmpfile = open("/var/tmp/test_fsync.out", O_RDWR)) == -1)
133223
die("can't open /var/tmp/test_fsync.out");
134-
write(tmpfile, &strout, 200);
224+
gettimeofday(&start_t, NULL);
225+
write(tmpfile, strout, 8192);
226+
write(tmpfile, strout, 8192);
135227
fsync(tmpfile);
136-
close(tmpfile);
137228
gettimeofday(&elapse_t, NULL);
138-
unlink("/var/tmp/test_fsync.out");
139-
printf("write, fsync, ");
229+
close(tmpfile);
230+
printf("\twrite, fsync, ");
140231
print_elapse(start_t, elapse_t);
141232
printf("\n");
142233

234+
unlink("/var/tmp/test_fsync.out");
235+
143236
return 0;
144237
}
145238

146-
void print_elapse(struct timeval start_t, struct timeval elapse_t)
239+
void
240+
print_elapse(struct timeval start_t, struct timeval elapse_t)
147241
{
148242
if (elapse_t.tv_usec < start_t.tv_usec)
149243
{
@@ -152,10 +246,11 @@ void print_elapse(struct timeval start_t, struct timeval elapse_t)
152246
}
153247

154248
printf("%ld.%06ld", (long) (elapse_t.tv_sec - start_t.tv_sec),
155-
(long) (elapse_t.tv_usec - start_t.tv_usec));
249+
(long) (elapse_t.tv_usec - start_t.tv_usec));
156250
}
157251

158-
void die(char *str)
252+
void
253+
die(char *str)
159254
{
160255
fprintf(stderr, "%s", str);
161256
exit(1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy