1414#include <sys/time.h>
1515#include <unistd.h>
1616
17+ #define FSYNC_FILENAME "/var/tmp/test_fsync.out"
18+
1719/* O_SYNC and O_FSYNC are the same */
1820#if defined(O_SYNC )
1921#define OPEN_SYNC_FLAG O_SYNC
@@ -36,12 +38,25 @@ main(int argc, char *argv[])
3638struct timeval start_t ;
3739struct timeval elapse_t ;
3840int tmpfile ,
39- i ;
41+ i ,
42+ loops = 1000 ;
4043char * strout = (char * )malloc (65536 );
44+ char * filename = FSYNC_FILENAME ;
4145
46+ if (argc > 2 && strcmp (argv [1 ],"-f" )== 0 )
47+ {
48+ filename = argv [2 ];
49+ argv += 2 ;
50+ argc -= 2 ;
51+ }
52+
53+ if (argc > 1 )
54+ loops = atoi (argv [1 ]);
55+
4256for (i = 0 ;i < 65536 ;i ++ )
4357strout [i ]= 'a' ;
44- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR ))== -1 )
58+
59+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR ))== -1 )
4560die ("can't open /var/tmp/test_fsync.out" );
4661write (tmpfile ,strout ,65536 );
4762fsync (tmpfile );/* fsync so later fsync's don't have to do
@@ -51,40 +66,54 @@ main(int argc, char *argv[])
5166printf ("Simple write timing:\n" );
5267/* write only */
5368gettimeofday (& start_t ,NULL );
54- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
55- die ("can't open /var/tmp/test_fsync.out" );
56- write (tmpfile ,strout ,8192 );
57- close (tmpfile );
69+ for (i = 0 ;i < loops ;i ++ )
70+ {
71+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
72+ die ("can't open /var/tmp/test_fsync.out" );
73+ write (tmpfile ,strout ,8192 );
74+ close (tmpfile );
75+ }
5876gettimeofday (& elapse_t ,NULL );
5977printf ("\twrite " );
6078print_elapse (start_t ,elapse_t );
61- printf ("\n\n " );
79+ printf ("\n" );
6280
63- printf ("Compare fsync before and after write's close:\n" );
81+ printf ("\nCompare fsync times on write() and non-write() descriptor:\n" );
82+ printf ("(If the times are similar, fsync() can sync data written\n on a different descriptor.)\n" );
6483
6584/* write, fsync, close */
6685gettimeofday (& start_t ,NULL );
67- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
68- die ("can't open /var/tmp/test_fsync.out" );
69- write (tmpfile ,strout ,8192 );
70- fsync (tmpfile );
71- close (tmpfile );
86+ for (i = 0 ;i < loops ;i ++ )
87+ {
88+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
89+ die ("can't open /var/tmp/test_fsync.out" );
90+ write (tmpfile ,strout ,8192 );
91+ fsync (tmpfile );
92+ close (tmpfile );
93+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
94+ die ("can't open /var/tmp/test_fsync.out" );
95+ /* do nothing but the open/close the tests are consistent. */
96+ close (tmpfile );
97+ }
7298gettimeofday (& elapse_t ,NULL );
7399printf ("\twrite, fsync, close " );
74100print_elapse (start_t ,elapse_t );
75101printf ("\n" );
76102
77103/* write, close, fsync */
78104gettimeofday (& start_t ,NULL );
79- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
80- die ("can't open /var/tmp/test_fsync.out" );
81- write (tmpfile ,strout ,8192 );
82- close (tmpfile );
83- /* reopen file */
84- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
85- die ("can't open /var/tmp/test_fsync.out" );
86- fsync (tmpfile );
87- close (tmpfile );
105+ for (i = 0 ;i < loops ;i ++ )
106+ {
107+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
108+ die ("can't open /var/tmp/test_fsync.out" );
109+ write (tmpfile ,strout ,8192 );
110+ close (tmpfile );
111+ /* reopen file */
112+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
113+ die ("can't open /var/tmp/test_fsync.out" );
114+ fsync (tmpfile );
115+ close (tmpfile );
116+ }
88117gettimeofday (& elapse_t ,NULL );
89118printf ("\twrite, close, fsync " );
90119print_elapse (start_t ,elapse_t );
@@ -93,22 +122,26 @@ main(int argc, char *argv[])
93122printf ("\nCompare one o_sync write to two:\n" );
94123
95124/* 16k o_sync write */
96- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
125+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
97126die ("can't open /var/tmp/test_fsync.out" );
98127gettimeofday (& start_t ,NULL );
99- write (tmpfile ,strout ,16384 );
128+ for (i = 0 ;i < loops ;i ++ )
129+ write (tmpfile ,strout ,16384 );
100130gettimeofday (& elapse_t ,NULL );
101131close (tmpfile );
102132printf ("\tone 16k o_sync write " );
103133print_elapse (start_t ,elapse_t );
104134printf ("\n" );
105135
106136/* 2*8k o_sync writes */
107- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
137+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
108138die ("can't open /var/tmp/test_fsync.out" );
109139gettimeofday (& start_t ,NULL );
110- write (tmpfile ,strout ,8192 );
111- write (tmpfile ,strout ,8192 );
140+ for (i = 0 ;i < loops ;i ++ )
141+ {
142+ write (tmpfile ,strout ,8192 );
143+ write (tmpfile ,strout ,8192 );
144+ }
112145gettimeofday (& elapse_t ,NULL );
113146close (tmpfile );
114147printf ("\ttwo 8k o_sync writes " );
@@ -119,10 +152,11 @@ main(int argc, char *argv[])
119152
120153#ifdef OPEN_DATASYNC_FLAG
121154/* open_dsync, write */
122- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR |O_DSYNC ))== -1 )
155+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR |O_DSYNC ))== -1 )
123156die ("can't open /var/tmp/test_fsync.out" );
124157gettimeofday (& start_t ,NULL );
125- write (tmpfile ,strout ,8192 );
158+ for (i = 0 ;i < loops ;i ++ )
159+ write (tmpfile ,strout ,8192 );
126160gettimeofday (& elapse_t ,NULL );
127161close (tmpfile );
128162printf ("\topen o_dsync, write " );
@@ -133,10 +167,11 @@ main(int argc, char *argv[])
133167printf ("\n" );
134168
135169/* open_fsync, write */
136- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
170+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
137171die ("can't open /var/tmp/test_fsync.out" );
138172gettimeofday (& start_t ,NULL );
139- write (tmpfile ,strout ,8192 );
173+ for (i = 0 ;i < loops ;i ++ )
174+ write (tmpfile ,strout ,8192 );
140175gettimeofday (& elapse_t ,NULL );
141176close (tmpfile );
142177printf ("\topen o_sync, write " );
@@ -145,11 +180,14 @@ main(int argc, char *argv[])
145180
146181#ifdef HAVE_FDATASYNC
147182/* write, fdatasync */
148- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
183+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
149184die ("can't open /var/tmp/test_fsync.out" );
150185gettimeofday (& start_t ,NULL );
151- write (tmpfile ,strout ,8192 );
152- fdatasync (tmpfile );
186+ for (i = 0 ;i < loops ;i ++ )
187+ {
188+ write (tmpfile ,strout ,8192 );
189+ fdatasync (tmpfile );
190+ }
153191gettimeofday (& elapse_t ,NULL );
154192close (tmpfile );
155193printf ("\twrite, fdatasync " );
@@ -160,11 +198,14 @@ main(int argc, char *argv[])
160198printf ("\n" );
161199
162200/* write, fsync, close */
163- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
201+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
164202die ("can't open /var/tmp/test_fsync.out" );
165203gettimeofday (& start_t ,NULL );
166- write (tmpfile ,strout ,8192 );
167- fsync (tmpfile );
204+ for (i = 0 ;i < loops ;i ++ )
205+ {
206+ write (tmpfile ,strout ,8192 );
207+ fsync (tmpfile );
208+ }
168209gettimeofday (& elapse_t ,NULL );
169210close (tmpfile );
170211printf ("\twrite, fsync, " );
@@ -176,11 +217,14 @@ main(int argc, char *argv[])
176217
177218#ifdef OPEN_DATASYNC_FLAG
178219/* open_dsync, write */
179- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR |O_DSYNC ))== -1 )
220+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR |O_DSYNC ))== -1 )
180221die ("can't open /var/tmp/test_fsync.out" );
181222gettimeofday (& start_t ,NULL );
182- write (tmpfile ,strout ,8192 );
183- write (tmpfile ,strout ,8192 );
223+ for (i = 0 ;i < loops ;i ++ )
224+ {
225+ write (tmpfile ,strout ,8192 );
226+ write (tmpfile ,strout ,8192 );
227+ }
184228gettimeofday (& elapse_t ,NULL );
185229close (tmpfile );
186230printf ("\topen o_dsync, write " );
@@ -191,11 +235,14 @@ main(int argc, char *argv[])
191235printf ("\n" );
192236
193237/* open_fsync, write */
194- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
238+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR |OPEN_SYNC_FLAG ))== -1 )
195239die ("can't open /var/tmp/test_fsync.out" );
196240gettimeofday (& start_t ,NULL );
197- write (tmpfile ,strout ,8192 );
198- write (tmpfile ,strout ,8192 );
241+ for (i = 0 ;i < loops ;i ++ )
242+ {
243+ write (tmpfile ,strout ,8192 );
244+ write (tmpfile ,strout ,8192 );
245+ }
199246gettimeofday (& elapse_t ,NULL );
200247close (tmpfile );
201248printf ("\topen o_sync, write " );
@@ -204,12 +251,15 @@ main(int argc, char *argv[])
204251
205252#ifdef HAVE_FDATASYNC
206253/* write, fdatasync */
207- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
254+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
208255die ("can't open /var/tmp/test_fsync.out" );
209256gettimeofday (& start_t ,NULL );
210- write (tmpfile ,strout ,8192 );
211- write (tmpfile ,strout ,8192 );
212- fdatasync (tmpfile );
257+ for (i = 0 ;i < loops ;i ++ )
258+ {
259+ write (tmpfile ,strout ,8192 );
260+ write (tmpfile ,strout ,8192 );
261+ fdatasync (tmpfile );
262+ }
213263gettimeofday (& elapse_t ,NULL );
214264close (tmpfile );
215265printf ("\twrite, fdatasync " );
@@ -220,19 +270,22 @@ main(int argc, char *argv[])
220270printf ("\n" );
221271
222272/* write, fsync, close */
223- if ((tmpfile = open ("/var/tmp/test_fsync.out" ,O_RDWR ))== -1 )
273+ if ((tmpfile = open (FSYNC_FILENAME ,O_RDWR ))== -1 )
224274die ("can't open /var/tmp/test_fsync.out" );
225275gettimeofday (& start_t ,NULL );
226- write (tmpfile ,strout ,8192 );
227- write (tmpfile ,strout ,8192 );
228- fsync (tmpfile );
276+ for (i = 0 ;i < loops ;i ++ )
277+ {
278+ write (tmpfile ,strout ,8192 );
279+ write (tmpfile ,strout ,8192 );
280+ fsync (tmpfile );
281+ }
229282gettimeofday (& elapse_t ,NULL );
230283close (tmpfile );
231284printf ("\twrite, fsync, " );
232285print_elapse (start_t ,elapse_t );
233286printf ("\n" );
234287
235- unlink ("/var/tmp/test_fsync.out" );
288+ unlink (FSYNC_FILENAME );
236289
237290return 0 ;
238291}
@@ -246,7 +299,7 @@ print_elapse(struct timeval start_t, struct timeval elapse_t)
246299elapse_t .tv_usec += 1000000 ;
247300}
248301
249- printf ("%ld .%06ld" , (long ) (elapse_t .tv_sec - start_t .tv_sec ),
302+ printf ("%3ld .%06ld" , (long ) (elapse_t .tv_sec - start_t .tv_sec ),
250303 (long ) (elapse_t .tv_usec - start_t .tv_usec ));
251304}
252305