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.
This commit is contained in:
Tom Lane 2011-01-22 15:01:26 -05:00
parent 3ae28ce8c4
commit 37eb2cd4ad
1 changed files with 36 additions and 26 deletions

View File

@ -3,17 +3,14 @@
* tests all supported fsync() methods * tests all supported fsync() methods
*/ */
#include "postgres.h" #include "postgres_fe.h"
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "getopt_long.h" #include "getopt_long.h"
#include "access/xlog_internal.h"
#include "access/xlog.h"
#include "access/xlogdefs.h" #include "access/xlogdefs.h"
@ -29,9 +26,11 @@
#define NA_FORMAT LABEL_FORMAT "%18s" #define NA_FORMAT LABEL_FORMAT "%18s"
#define OPS_FORMAT "%9.3f ops/sec" #define OPS_FORMAT "%9.3f ops/sec"
int ops_per_test = 2000; static const char *progname;
char full_buf[XLOG_SEG_SIZE], *buf, *filename = FSYNC_FILENAME;
struct timeval start_t, stop_t; static int ops_per_test = 2000;
static char full_buf[XLOG_SEG_SIZE], *buf, *filename = FSYNC_FILENAME;
static struct timeval start_t, stop_t;
static void handle_args(int argc, char *argv[]); static void handle_args(int argc, char *argv[]);
@ -46,12 +45,14 @@ static void test_file_descriptor_sync(void);
static int pg_fsync_writethrough(int fd); static int pg_fsync_writethrough(int fd);
#endif #endif
static void print_elapse(struct timeval start_t, struct timeval stop_t); static void print_elapse(struct timeval start_t, struct timeval stop_t);
static void die(char *str); static void die(const char *str);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
progname = get_progname(argv[0]);
handle_args(argc, argv); handle_args(argc, argv);
prepare_buf(); prepare_buf();
@ -91,12 +92,12 @@ handle_args(int argc, char *argv[])
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 || if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "-?") == 0) strcmp(argv[1], "-?") == 0)
{ {
fprintf(stderr, "pg_test_fsync [-f filename] [-o ops-per-test]\n"); fprintf(stderr, "%s [-f filename] [-o ops-per-test]\n", progname);
exit(0); exit(0);
} }
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{ {
fprintf(stderr,"pg_test_fsync " PG_VERSION "\n"); fprintf(stderr, "%s %s\n", progname, PG_VERSION);
exit(0); exit(0);
} }
} }
@ -115,14 +116,23 @@ handle_args(int argc, char *argv[])
break; break;
default: default:
fprintf(stderr, fprintf(stderr, "Try \"%s --help\" for more information.\n",
"Try \"%s --help\" for more information.\n", progname);
"pg_test_fsync");
exit(1); exit(1);
break; break;
} }
} }
if (argc > optind)
{
fprintf(stderr,
"%s: too many command-line arguments (first is \"%s\")\n",
progname, argv[optind]);
fprintf(stderr, "Try \"%s --help\" for more information.\n",
progname);
exit(1);
}
printf("%d operations per test\n", ops_per_test); printf("%d operations per test\n", ops_per_test);
} }
@ -147,7 +157,7 @@ test_open(void)
* test if we can open the target file * test if we can open the target file
*/ */
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1) if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
die("Cannot open output file."); die("could not open output file");
if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE) if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
die("write failed"); die("write failed");
@ -183,7 +193,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
@ -238,7 +248,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
@ -263,7 +273,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
@ -287,7 +297,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
@ -318,7 +328,7 @@ test_sync(int writes_per_op)
fflush(stdout); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
@ -453,7 +463,7 @@ test_file_descriptor_sync(void)
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
if (fsync(tmpfile) != 0) if (fsync(tmpfile) != 0)
@ -464,7 +474,7 @@ test_file_descriptor_sync(void)
* with the following test * with the following test
*/ */
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
close(tmpfile); close(tmpfile);
} }
gettimeofday(&stop_t, NULL); gettimeofday(&stop_t, NULL);
@ -482,13 +492,13 @@ test_file_descriptor_sync(void)
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
close(tmpfile); close(tmpfile);
/* reopen file */ /* reopen file */
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
if (fsync(tmpfile) != 0) if (fsync(tmpfile) != 0)
die("fsync failed"); die("fsync failed");
close(tmpfile); close(tmpfile);
@ -514,7 +524,7 @@ test_non_sync(void)
for (ops = 0; ops < ops_per_test; ops++) for (ops = 0; ops < ops_per_test; ops++)
{ {
if ((tmpfile = open(filename, O_RDWR, 0)) == -1) if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file."); die("could not open output file");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
close(tmpfile); close(tmpfile);
@ -554,8 +564,8 @@ print_elapse(struct timeval start_t, struct timeval stop_t)
} }
static void static void
die(char *str) die(const char *str)
{ {
fprintf(stderr, "%s\n", str); fprintf(stderr, "%s: %s\n", str, strerror(errno));
exit(1); exit(1);
} }