Skip to content

Commit 37eb2cd

Browse files
committed
More pg_test_fsync fixups.
Reduce #includes to minimum actually needed; in particular include postgres_fe.h not postgres.h, so as to stop build failures on some platforms. Use get_progname() instead of hardwired program name; improve error checking for command line syntax; bring error messages into line with style guidelines; include strerror result in die() cases.
1 parent 3ae28ce commit 37eb2cd

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

contrib/pg_test_fsync/pg_test_fsync.c

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
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

@@ -29,9 +26,11 @@
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

3736
static void handle_args(int argc, char *argv[]);
@@ -46,12 +45,14 @@ static void test_file_descriptor_sync(void);
4645
static int pg_fsync_writethrough(int fd);
4746
#endif
4847
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);
5049

5150

5251
int
5352
main(int argc, char *argv[])
5453
{
54+
progname = get_progname(argv[0]);
55+
5556
handle_args(argc, argv);
5657

5758
prepare_buf();
@@ -91,12 +92,12 @@ handle_args(int argc, char *argv[])
9192
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
9293
strcmp(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);
9596
exit(0);
9697
}
9798
if (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);
100101
exit(0);
101102
}
102103
}
@@ -115,14 +116,23 @@ handle_args(int argc, char *argv[])
115116
break;
116117

117118
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);
121121
exit(1);
122122
break;
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+
126136
printf("%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
*/
149159
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");
151161
if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
152162
die("write failed");
153163

@@ -183,7 +193,7 @@ test_sync(int writes_per_op)
183193
fflush(stdout);
184194

185195
if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
186-
die("Cannot open output file.");
196+
die("could not open output file");
187197
gettimeofday(&start_t, NULL);
188198
for (ops = 0; ops < ops_per_test; ops++)
189199
{
@@ -238,7 +248,7 @@ test_sync(int writes_per_op)
238248
fflush(stdout);
239249

240250
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
241-
die("Cannot open output file.");
251+
die("could not open output file");
242252
gettimeofday(&start_t, NULL);
243253
for (ops = 0; ops < ops_per_test; ops++)
244254
{
@@ -263,7 +273,7 @@ test_sync(int writes_per_op)
263273
fflush(stdout);
264274

265275
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
266-
die("Cannot open output file.");
276+
die("could not open output file");
267277
gettimeofday(&start_t, NULL);
268278
for (ops = 0; ops < ops_per_test; ops++)
269279
{
@@ -287,7 +297,7 @@ test_sync(int writes_per_op)
287297
fflush(stdout);
288298

289299
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
290-
die("Cannot open output file.");
300+
die("could not open output file");
291301
gettimeofday(&start_t, NULL);
292302
for (ops = 0; ops < ops_per_test; ops++)
293303
{
@@ -318,7 +328,7 @@ test_sync(int writes_per_op)
318328
fflush(stdout);
319329

320330
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
321-
die("Cannot open output file.");
331+
die("could not open output file");
322332
gettimeofday(&start_t, NULL);
323333
for (ops = 0; ops < ops_per_test; ops++)
324334
{
@@ -453,7 +463,7 @@ test_file_descriptor_sync(void)
453463
for (ops = 0; ops < ops_per_test; ops++)
454464
{
455465
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
456-
die("Cannot open output file.");
466+
die("could not open output file");
457467
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
458468
die("write failed");
459469
if (fsync(tmpfile) != 0)
@@ -464,7 +474,7 @@ test_file_descriptor_sync(void)
464474
* with the following test
465475
*/
466476
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
467-
die("Cannot open output file.");
477+
die("could not open output file");
468478
close(tmpfile);
469479
}
470480
gettimeofday(&stop_t, NULL);
@@ -482,13 +492,13 @@ test_file_descriptor_sync(void)
482492
for (ops = 0; ops < ops_per_test; ops++)
483493
{
484494
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
485-
die("Cannot open output file.");
495+
die("could not open output file");
486496
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
487497
die("write failed");
488498
close(tmpfile);
489499
/* reopen file */
490500
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
491-
die("Cannot open output file.");
501+
die("could not open output file");
492502
if (fsync(tmpfile) != 0)
493503
die("fsync failed");
494504
close(tmpfile);
@@ -514,7 +524,7 @@ test_non_sync(void)
514524
for (ops = 0; ops < ops_per_test; ops++)
515525
{
516526
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
517-
die("Cannot open output file.");
527+
die("could not open output file");
518528
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
519529
die("write failed");
520530
close(tmpfile);
@@ -554,8 +564,8 @@ print_elapse(struct timeval start_t, struct timeval stop_t)
554564
}
555565

556566
static 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));
560570
exit(1);
561571
}

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