33 *tests all supported fsync() methods
44 */
55
6- #include "postgres .h"
6+ #include "postgres_fe .h"
77
8- #include <fcntl.h>
98#include <sys/stat.h>
109#include <sys/time.h>
1110#include <time.h>
1211#include <unistd.h>
1312
1413#include "getopt_long.h"
15- #include "access/xlog_internal.h"
16- #include "access/xlog.h"
1714#include "access/xlogdefs.h"
1815
1916
2926#define NA_FORMAT LABEL_FORMAT "%18s"
3027#define OPS_FORMAT "%9.3f ops/sec"
3128
32- int ops_per_test = 2000 ;
33- char full_buf [XLOG_SEG_SIZE ],* buf ,* filename = FSYNC_FILENAME ;
34- struct timeval start_t ,stop_t ;
29+ static const char * progname ;
30+
31+ static int ops_per_test = 2000 ;
32+ static char full_buf [XLOG_SEG_SIZE ],* buf ,* filename = FSYNC_FILENAME ;
33+ static struct timeval start_t ,stop_t ;
3534
3635
3736static void handle_args (int argc ,char * argv []);
@@ -46,12 +45,14 @@ static voidtest_file_descriptor_sync(void);
4645static int pg_fsync_writethrough (int fd );
4746#endif
4847static void print_elapse (struct timeval start_t ,struct timeval stop_t );
49- static void die (char * str );
48+ static void die (const char * str );
5049
5150
5251int
5352main (int argc ,char * argv [])
5453{
54+ progname = get_progname (argv [0 ]);
55+
5556handle_args (argc ,argv );
5657
5758prepare_buf ();
@@ -91,12 +92,12 @@ handle_args(int argc, char *argv[])
9192if (strcmp (argv [1 ],"--help" )== 0 || strcmp (argv [1 ],"-h" )== 0 ||
9293strcmp (argv [1 ],"-?" )== 0 )
9394{
94- fprintf (stderr ,"pg_test_fsync [-f filename] [-o ops-per-test]\n" );
95+ fprintf (stderr ,"%s [-f filename] [-o ops-per-test]\n" , progname );
9596exit (0 );
9697}
9798if (strcmp (argv [1 ],"--version" )== 0 || strcmp (argv [1 ],"-V" )== 0 )
9899{
99- fprintf (stderr ,"pg_test_fsync " PG_VERSION " \n" );
100+ fprintf (stderr ,"%s %s \n", progname , PG_VERSION );
100101exit (0 );
101102}
102103}
@@ -115,14 +116,23 @@ handle_args(int argc, char *argv[])
115116break ;
116117
117118default :
118- fprintf (stderr ,
119- "Try \"%s --help\" for more information.\n" ,
120- "pg_test_fsync" );
119+ fprintf (stderr ,"Try \"%s --help\" for more information.\n" ,
120+ progname );
121121exit (1 );
122122break ;
123123}
124124}
125125
126+ if (argc > optind )
127+ {
128+ fprintf (stderr ,
129+ "%s: too many command-line arguments (first is \"%s\")\n" ,
130+ progname ,argv [optind ]);
131+ fprintf (stderr ,"Try \"%s --help\" for more information.\n" ,
132+ progname );
133+ exit (1 );
134+ }
135+
126136printf ("%d operations per test\n" ,ops_per_test );
127137}
128138
@@ -147,7 +157,7 @@ test_open(void)
147157 * test if we can open the target file
148158 */
149159if ((tmpfile = open (filename ,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR ))== -1 )
150- die ("Cannot open output file. " );
160+ die ("could not open output file" );
151161if (write (tmpfile ,full_buf ,XLOG_SEG_SIZE )!= XLOG_SEG_SIZE )
152162die ("write failed" );
153163
@@ -183,7 +193,7 @@ test_sync(int writes_per_op)
183193fflush (stdout );
184194
185195if ((tmpfile = open (filename ,O_RDWR |O_DSYNC ,0 ))== -1 )
186- die ("Cannot open output file. " );
196+ die ("could not open output file" );
187197gettimeofday (& start_t ,NULL );
188198for (ops = 0 ;ops < ops_per_test ;ops ++ )
189199{
@@ -238,7 +248,7 @@ test_sync(int writes_per_op)
238248fflush (stdout );
239249
240250if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
241- die ("Cannot open output file. " );
251+ die ("could not open output file" );
242252gettimeofday (& start_t ,NULL );
243253for (ops = 0 ;ops < ops_per_test ;ops ++ )
244254{
@@ -263,7 +273,7 @@ test_sync(int writes_per_op)
263273fflush (stdout );
264274
265275if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
266- die ("Cannot open output file. " );
276+ die ("could not open output file" );
267277gettimeofday (& start_t ,NULL );
268278for (ops = 0 ;ops < ops_per_test ;ops ++ )
269279{
@@ -287,7 +297,7 @@ test_sync(int writes_per_op)
287297fflush (stdout );
288298
289299if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
290- die ("Cannot open output file. " );
300+ die ("could not open output file" );
291301gettimeofday (& start_t ,NULL );
292302for (ops = 0 ;ops < ops_per_test ;ops ++ )
293303{
@@ -318,7 +328,7 @@ test_sync(int writes_per_op)
318328fflush (stdout );
319329
320330if ((tmpfile = open (filename ,O_RDWR |OPEN_SYNC_FLAG ,0 ))== -1 )
321- die ("Cannot open output file. " );
331+ die ("could not open output file" );
322332gettimeofday (& start_t ,NULL );
323333for (ops = 0 ;ops < ops_per_test ;ops ++ )
324334{
@@ -453,7 +463,7 @@ test_file_descriptor_sync(void)
453463for (ops = 0 ;ops < ops_per_test ;ops ++ )
454464{
455465if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
456- die ("Cannot open output file. " );
466+ die ("could not open output file" );
457467if (write (tmpfile ,buf ,WRITE_SIZE )!= WRITE_SIZE )
458468die ("write failed" );
459469if (fsync (tmpfile )!= 0 )
@@ -464,7 +474,7 @@ test_file_descriptor_sync(void)
464474 * with the following test
465475 */
466476if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
467- die ("Cannot open output file. " );
477+ die ("could not open output file" );
468478close (tmpfile );
469479}
470480gettimeofday (& stop_t ,NULL );
@@ -482,13 +492,13 @@ test_file_descriptor_sync(void)
482492for (ops = 0 ;ops < ops_per_test ;ops ++ )
483493{
484494if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
485- die ("Cannot open output file. " );
495+ die ("could not open output file" );
486496if (write (tmpfile ,buf ,WRITE_SIZE )!= WRITE_SIZE )
487497die ("write failed" );
488498close (tmpfile );
489499/* reopen file */
490500if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
491- die ("Cannot open output file. " );
501+ die ("could not open output file" );
492502if (fsync (tmpfile )!= 0 )
493503die ("fsync failed" );
494504close (tmpfile );
@@ -514,7 +524,7 @@ test_non_sync(void)
514524for (ops = 0 ;ops < ops_per_test ;ops ++ )
515525{
516526if ((tmpfile = open (filename ,O_RDWR ,0 ))== -1 )
517- die ("Cannot open output file. " );
527+ die ("could not open output file" );
518528if (write (tmpfile ,buf ,WRITE_SIZE )!= WRITE_SIZE )
519529die ("write failed" );
520530close (tmpfile );
@@ -554,8 +564,8 @@ print_elapse(struct timeval start_t, struct timeval stop_t)
554564}
555565
556566static void
557- die (char * str )
567+ die (const char * str )
558568{
559- fprintf (stderr ,"%s\n" ,str );
569+ fprintf (stderr ,"%s: %s \n" ,str , strerror ( errno ) );
560570exit (1 );
561571}