6
6
#include "../../include/pg_config.h"
7
7
8
8
#include <sys/types.h>
9
+ #include <sys/stat.h>
9
10
#include <fcntl.h>
10
11
#include <stdio.h>
11
12
#include <stdlib.h>
26
27
#endif
27
28
#endif
28
29
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 );
31
32
32
- int main (int argc , char * argv [])
33
+ int
34
+ main (int argc , char * argv [])
33
35
{
34
36
struct timeval start_t ;
35
37
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 );
38
50
39
51
printf ("Simple write timing:\n" );
40
- /* write only */
52
+ /* write only */
41
53
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 )
43
55
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 );
46
58
gettimeofday (& elapse_t , NULL );
47
- unlink ("/var/tmp/test_fsync.out" );
48
- printf ("write " );
59
+ printf ("\twrite " );
49
60
print_elapse (start_t , elapse_t );
50
61
printf ("\n\n" );
51
62
52
63
printf ("Compare fsync before and after write's close:\n" );
64
+
53
65
/* write, fsync, close */
54
66
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 )
56
68
die ("can't open /var/tmp/test_fsync.out" );
57
- write (tmpfile , & strout , 200 );
69
+ write (tmpfile , strout , 8192 );
58
70
fsync (tmpfile );
59
- close (tmpfile );
71
+ close (tmpfile );
60
72
gettimeofday (& elapse_t , NULL );
61
- unlink ("/var/tmp/test_fsync.out" );
62
- printf ("write, fsync, close " );
73
+ printf ("\twrite, fsync, close " );
63
74
print_elapse (start_t , elapse_t );
64
75
printf ("\n" );
65
76
66
77
/* write, close, fsync */
67
78
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 )
69
80
die ("can't open /var/tmp/test_fsync.out" );
70
- write (tmpfile , & strout , 200 );
81
+ write (tmpfile , strout , 8192 );
71
82
close (tmpfile );
72
83
/* 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 )
74
85
die ("can't open /var/tmp/test_fsync.out" );
75
86
fsync (tmpfile );
76
- close (tmpfile );
87
+ close (tmpfile );
77
88
gettimeofday (& elapse_t , NULL );
78
- unlink ("/var/tmp/test_fsync.out" );
79
- printf ("write, close, fsync " );
89
+ printf ("\twrite, close, fsync " );
80
90
print_elapse (start_t , elapse_t );
81
91
printf ("\n" );
82
92
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" );
84
119
85
120
#ifdef OPEN_DATASYNC_FLAG
86
121
/* 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" );
87
124
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 )
89
137
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 );
91
141
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 );
92
153
gettimeofday (& elapse_t , NULL );
93
- unlink ( "/var/tmp/test_fsync.out" );
94
- printf ("open o_dsync, write " );
154
+ close ( tmpfile );
155
+ printf ("\twrite, fdatasync " );
95
156
print_elapse (start_t , elapse_t );
96
157
#else
97
- printf ("o_dsync unavailable " );
158
+ printf ("\t(fdatasync unavailable) " );
98
159
#endif
99
160
printf ("\n" );
100
161
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" );
102
165
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 )
104
179
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 );
106
184
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 );
107
198
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 " );
110
201
print_elapse (start_t , elapse_t );
111
202
printf ("\n" );
112
203
113
204
#ifdef HAVE_FDATASYNC
114
205
/* 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 )
117
207
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 );
119
211
fdatasync (tmpfile );
120
- close (tmpfile );
121
212
gettimeofday (& elapse_t , NULL );
122
- unlink ( "/var/tmp/test_fsync.out" );
123
- printf ("write , fdatasync " );
213
+ close ( tmpfile );
214
+ printf ("\twrite , fdatasync " );
124
215
print_elapse (start_t , elapse_t );
125
216
#else
126
- printf ("fdatasync unavailable " );
217
+ printf ("\t( fdatasync unavailable) " );
127
218
#endif
128
219
printf ("\n" );
129
220
130
221
/* 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 )
133
223
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 );
135
227
fsync (tmpfile );
136
- close (tmpfile );
137
228
gettimeofday (& elapse_t , NULL );
138
- unlink ( "/var/tmp/test_fsync.out" );
139
- printf ("write , fsync, " );
229
+ close ( tmpfile );
230
+ printf ("\twrite , fsync, " );
140
231
print_elapse (start_t , elapse_t );
141
232
printf ("\n" );
142
233
234
+ unlink ("/var/tmp/test_fsync.out" );
235
+
143
236
return 0 ;
144
237
}
145
238
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 )
147
241
{
148
242
if (elapse_t .tv_usec < start_t .tv_usec )
149
243
{
@@ -152,10 +246,11 @@ void print_elapse(struct timeval start_t, struct timeval elapse_t)
152
246
}
153
247
154
248
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 ));
156
250
}
157
251
158
- void die (char * str )
252
+ void
253
+ die (char * str )
159
254
{
160
255
fprintf (stderr , "%s" , str );
161
256
exit (1 );
0 commit comments