3
3
* tests all supported fsync() methods
4
4
*/
5
5
6
- #include "postgres .h"
6
+ #include "postgres_fe .h"
7
7
8
- #include <fcntl.h>
9
8
#include <sys/stat.h>
10
9
#include <sys/time.h>
11
10
#include <time.h>
12
11
#include <unistd.h>
13
12
14
13
#include "getopt_long.h"
15
- #include "access/xlog_internal.h"
16
- #include "access/xlog.h"
17
14
#include "access/xlogdefs.h"
18
15
19
16
29
26
#define NA_FORMAT LABEL_FORMAT "%18s"
30
27
#define OPS_FORMAT "%9.3f ops/sec"
31
28
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 ;
35
34
36
35
37
36
static void handle_args (int argc , char * argv []);
@@ -46,12 +45,14 @@ static void test_file_descriptor_sync(void);
46
45
static int pg_fsync_writethrough (int fd );
47
46
#endif
48
47
static void print_elapse (struct timeval start_t , struct timeval stop_t );
49
- static void die (char * str );
48
+ static void die (const char * str );
50
49
51
50
52
51
int
53
52
main (int argc , char * argv [])
54
53
{
54
+ progname = get_progname (argv [0 ]);
55
+
55
56
handle_args (argc , argv );
56
57
57
58
prepare_buf ();
@@ -91,12 +92,12 @@ handle_args(int argc, char *argv[])
91
92
if (strcmp (argv [1 ], "--help" ) == 0 || strcmp (argv [1 ], "-h" ) == 0 ||
92
93
strcmp (argv [1 ], "-?" ) == 0 )
93
94
{
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 );
95
96
exit (0 );
96
97
}
97
98
if (strcmp (argv [1 ], "--version" ) == 0 || strcmp (argv [1 ], "-V" ) == 0 )
98
99
{
99
- fprintf (stderr ,"pg_test_fsync " PG_VERSION " \n" );
100
+ fprintf (stderr , "%s %s \n", progname , PG_VERSION );
100
101
exit (0 );
101
102
}
102
103
}
@@ -115,14 +116,23 @@ handle_args(int argc, char *argv[])
115
116
break ;
116
117
117
118
default :
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 );
121
121
exit (1 );
122
122
break ;
123
123
}
124
124
}
125
125
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
+
126
136
printf ("%d operations per test\n" , ops_per_test );
127
137
}
128
138
@@ -147,7 +157,7 @@ test_open(void)
147
157
* test if we can open the target file
148
158
*/
149
159
if ((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" );
151
161
if (write (tmpfile , full_buf , XLOG_SEG_SIZE ) != XLOG_SEG_SIZE )
152
162
die ("write failed" );
153
163
@@ -183,7 +193,7 @@ test_sync(int writes_per_op)
183
193
fflush (stdout );
184
194
185
195
if ((tmpfile = open (filename , O_RDWR | O_DSYNC , 0 )) == -1 )
186
- die ("Cannot open output file. " );
196
+ die ("could not open output file" );
187
197
gettimeofday (& start_t , NULL );
188
198
for (ops = 0 ; ops < ops_per_test ; ops ++ )
189
199
{
@@ -238,7 +248,7 @@ test_sync(int writes_per_op)
238
248
fflush (stdout );
239
249
240
250
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
241
- die ("Cannot open output file. " );
251
+ die ("could not open output file" );
242
252
gettimeofday (& start_t , NULL );
243
253
for (ops = 0 ; ops < ops_per_test ; ops ++ )
244
254
{
@@ -263,7 +273,7 @@ test_sync(int writes_per_op)
263
273
fflush (stdout );
264
274
265
275
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
266
- die ("Cannot open output file. " );
276
+ die ("could not open output file" );
267
277
gettimeofday (& start_t , NULL );
268
278
for (ops = 0 ; ops < ops_per_test ; ops ++ )
269
279
{
@@ -287,7 +297,7 @@ test_sync(int writes_per_op)
287
297
fflush (stdout );
288
298
289
299
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
290
- die ("Cannot open output file. " );
300
+ die ("could not open output file" );
291
301
gettimeofday (& start_t , NULL );
292
302
for (ops = 0 ; ops < ops_per_test ; ops ++ )
293
303
{
@@ -318,7 +328,7 @@ test_sync(int writes_per_op)
318
328
fflush (stdout );
319
329
320
330
if ((tmpfile = open (filename , O_RDWR | OPEN_SYNC_FLAG , 0 )) == -1 )
321
- die ("Cannot open output file. " );
331
+ die ("could not open output file" );
322
332
gettimeofday (& start_t , NULL );
323
333
for (ops = 0 ; ops < ops_per_test ; ops ++ )
324
334
{
@@ -453,7 +463,7 @@ test_file_descriptor_sync(void)
453
463
for (ops = 0 ; ops < ops_per_test ; ops ++ )
454
464
{
455
465
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
456
- die ("Cannot open output file. " );
466
+ die ("could not open output file" );
457
467
if (write (tmpfile , buf , WRITE_SIZE ) != WRITE_SIZE )
458
468
die ("write failed" );
459
469
if (fsync (tmpfile ) != 0 )
@@ -464,7 +474,7 @@ test_file_descriptor_sync(void)
464
474
* with the following test
465
475
*/
466
476
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
467
- die ("Cannot open output file. " );
477
+ die ("could not open output file" );
468
478
close (tmpfile );
469
479
}
470
480
gettimeofday (& stop_t , NULL );
@@ -482,13 +492,13 @@ test_file_descriptor_sync(void)
482
492
for (ops = 0 ; ops < ops_per_test ; ops ++ )
483
493
{
484
494
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
485
- die ("Cannot open output file. " );
495
+ die ("could not open output file" );
486
496
if (write (tmpfile , buf , WRITE_SIZE ) != WRITE_SIZE )
487
497
die ("write failed" );
488
498
close (tmpfile );
489
499
/* reopen file */
490
500
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
491
- die ("Cannot open output file. " );
501
+ die ("could not open output file" );
492
502
if (fsync (tmpfile ) != 0 )
493
503
die ("fsync failed" );
494
504
close (tmpfile );
@@ -514,7 +524,7 @@ test_non_sync(void)
514
524
for (ops = 0 ; ops < ops_per_test ; ops ++ )
515
525
{
516
526
if ((tmpfile = open (filename , O_RDWR , 0 )) == -1 )
517
- die ("Cannot open output file. " );
527
+ die ("could not open output file" );
518
528
if (write (tmpfile , buf , WRITE_SIZE ) != WRITE_SIZE )
519
529
die ("write failed" );
520
530
close (tmpfile );
@@ -554,8 +564,8 @@ print_elapse(struct timeval start_t, struct timeval stop_t)
554
564
}
555
565
556
566
static void
557
- die (char * str )
567
+ die (const char * str )
558
568
{
559
- fprintf (stderr , "%s\n" , str );
569
+ fprintf (stderr , "%s: %s \n" , str , strerror ( errno ) );
560
570
exit (1 );
561
571
}
0 commit comments