2004-03-18 11:56:59 +08:00
|
|
|
/*
|
2011-01-22 08:27:25 +08:00
|
|
|
* pg_test_fsync.c
|
2011-01-16 00:54:43 +08:00
|
|
|
* tests all supported fsync() methods
|
2004-03-18 11:56:59 +08:00
|
|
|
*/
|
|
|
|
|
2011-01-23 04:01:26 +08:00
|
|
|
#include "postgres_fe.h"
|
2006-11-25 09:22:28 +08:00
|
|
|
|
2011-01-22 08:27:25 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-01-17 22:35:51 +08:00
|
|
|
#include "getopt_long.h"
|
2007-02-14 13:00:40 +08:00
|
|
|
#include "access/xlogdefs.h"
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2011-01-22 08:27:25 +08:00
|
|
|
/*
|
2011-01-16 00:54:43 +08:00
|
|
|
* put the temp files in the local directory
|
2011-01-22 08:27:25 +08:00
|
|
|
* unless the user specifies otherwise
|
2011-01-16 00:54:43 +08:00
|
|
|
*/
|
2011-01-22 08:27:25 +08:00
|
|
|
#define FSYNC_FILENAME "./pg_test_fsync.out"
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2011-01-26 11:18:24 +08:00
|
|
|
#define XLOG_BLCKSZ_K (XLOG_BLCKSZ / 1024)
|
2009-11-28 23:04:54 +08:00
|
|
|
|
2011-01-16 08:40:49 +08:00
|
|
|
#define LABEL_FORMAT " %-32s"
|
2011-03-11 09:25:41 +08:00
|
|
|
#define NA_FORMAT "%18s"
|
2011-01-16 21:36:43 +08:00
|
|
|
#define OPS_FORMAT "%9.3f ops/sec"
|
2011-01-16 00:54:43 +08:00
|
|
|
|
2011-01-23 04:01:26 +08:00
|
|
|
static const char *progname;
|
|
|
|
|
2011-04-10 23:42:00 +08:00
|
|
|
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 prepare_buf(void);
|
|
|
|
static void test_open(void);
|
|
|
|
static void test_non_sync(void);
|
|
|
|
static void test_sync(int writes_per_op);
|
|
|
|
static void test_open_syncs(void);
|
|
|
|
static void test_open_sync(const char *msg, int writes_size);
|
|
|
|
static void test_file_descriptor_sync(void);
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
#ifdef HAVE_FSYNC_WRITETHROUGH
|
|
|
|
static int pg_fsync_writethrough(int fd);
|
|
|
|
#endif
|
2011-04-10 23:42:00 +08:00
|
|
|
static void print_elapse(struct timeval start_t, struct timeval stop_t);
|
|
|
|
static void die(const char *str);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2004-03-18 23:26:27 +08:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2004-03-18 11:56:59 +08:00
|
|
|
{
|
2011-01-23 04:01:26 +08:00
|
|
|
progname = get_progname(argv[0]);
|
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
handle_args(argc, argv);
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
prepare_buf();
|
|
|
|
|
|
|
|
test_open();
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-26 11:18:24 +08:00
|
|
|
/* Test using 1 XLOG_BLCKSZ write */
|
2011-01-16 03:42:12 +08:00
|
|
|
test_sync(1);
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2011-01-26 11:18:24 +08:00
|
|
|
/* Test using 2 XLOG_BLCKSZ writes */
|
2011-01-16 03:42:12 +08:00
|
|
|
test_sync(2);
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
test_open_syncs();
|
|
|
|
|
|
|
|
test_file_descriptor_sync();
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-19 04:53:20 +08:00
|
|
|
test_non_sync();
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
unlink(filename);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-16 03:42:12 +08:00
|
|
|
handle_args(int argc, char *argv[])
|
|
|
|
{
|
2011-01-17 22:35:51 +08:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"filename", required_argument, NULL, 'f'},
|
|
|
|
{"ops-per-test", required_argument, NULL, 'o'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
int option; /* Command line option */
|
|
|
|
int optindex = 0; /* used by getopt_long */
|
|
|
|
|
|
|
|
if (argc > 1)
|
2011-01-16 03:42:12 +08:00
|
|
|
{
|
2011-01-17 22:35:51 +08:00
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
|
|
|
|
strcmp(argv[1], "-?") == 0)
|
|
|
|
{
|
2011-05-11 02:34:26 +08:00
|
|
|
printf("Usage: %s [-f FILENAME] [-o OPS-PER-TEST]\n", progname);
|
2011-01-17 22:35:51 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
2011-05-11 02:34:26 +08:00
|
|
|
puts("pg_test_fsync (PostgreSQL) " PG_VERSION);
|
2011-01-17 22:35:51 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2011-01-16 03:42:12 +08:00
|
|
|
}
|
2011-01-17 22:35:51 +08:00
|
|
|
|
|
|
|
while ((option = getopt_long(argc, argv, "f:o:",
|
2011-04-10 23:42:00 +08:00
|
|
|
long_options, &optindex)) != -1)
|
2004-03-19 03:54:00 +08:00
|
|
|
{
|
2011-01-17 22:35:51 +08:00
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'f':
|
|
|
|
filename = strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
ops_per_test = atoi(optarg);
|
|
|
|
break;
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-17 22:35:51 +08:00
|
|
|
default:
|
2011-01-23 04:01:26 +08:00
|
|
|
fprintf(stderr, "Try \"%s --help\" for more information.\n",
|
|
|
|
progname);
|
2011-01-17 22:35:51 +08:00
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
2004-03-19 03:54:00 +08:00
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2011-01-23 04:01:26 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-19 04:53:20 +08:00
|
|
|
printf("%d operations per test\n", ops_per_test);
|
2011-03-11 09:25:41 +08:00
|
|
|
#if PG_O_DIRECT != 0
|
|
|
|
printf("O_DIRECT supported on this platform for open_datasync and open_sync.\n");
|
|
|
|
#else
|
|
|
|
printf("Direct I/O is not supported on this platform.\n");
|
|
|
|
#endif
|
2011-01-16 03:42:12 +08:00
|
|
|
}
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-16 03:42:12 +08:00
|
|
|
prepare_buf(void)
|
|
|
|
{
|
|
|
|
int ops;
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
/* write random data into buffer */
|
|
|
|
for (ops = 0; ops < XLOG_SEG_SIZE; ops++)
|
|
|
|
full_buf[ops] = random();
|
|
|
|
|
|
|
|
buf = (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER, full_buf);
|
|
|
|
}
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-16 03:42:12 +08:00
|
|
|
test_open(void)
|
|
|
|
{
|
|
|
|
int tmpfile;
|
2004-03-19 03:54:00 +08:00
|
|
|
|
2011-01-22 08:27:25 +08:00
|
|
|
/*
|
|
|
|
* test if we can open the target file
|
2011-01-16 00:54:43 +08:00
|
|
|
*/
|
2010-10-03 06:40:28 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2006-11-25 09:22:28 +08:00
|
|
|
if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
|
|
|
|
die("write failed");
|
2011-01-16 03:42:12 +08:00
|
|
|
|
|
|
|
/* fsync now so that dirty buffers don't skew later tests */
|
2006-11-25 09:22:28 +08:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
close(tmpfile);
|
|
|
|
}
|
2006-11-25 09:22:28 +08:00
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-16 03:42:12 +08:00
|
|
|
test_sync(int writes_per_op)
|
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
int tmpfile,
|
|
|
|
ops,
|
|
|
|
writes;
|
2011-01-16 07:27:10 +08:00
|
|
|
bool fs_warning = false;
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-16 03:42:12 +08:00
|
|
|
if (writes_per_op == 1)
|
2011-01-27 05:21:19 +08:00
|
|
|
printf("\nCompare file sync methods using one %dkB write:\n", XLOG_BLCKSZ_K);
|
2011-01-16 03:42:12 +08:00
|
|
|
else
|
2011-01-27 05:21:19 +08:00
|
|
|
printf("\nCompare file sync methods using two %dkB writes:\n", XLOG_BLCKSZ_K);
|
2011-01-16 04:00:20 +08:00
|
|
|
printf("(in wal_sync_method preference order, except fdatasync\n");
|
|
|
|
printf("is Linux's default)\n");
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2011-01-16 00:54:43 +08:00
|
|
|
/*
|
|
|
|
* Test open_datasync if available
|
|
|
|
*/
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(LABEL_FORMAT, "open_datasync");
|
2010-07-14 01:00:50 +08:00
|
|
|
fflush(stdout);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2011-03-11 09:25:41 +08:00
|
|
|
#ifdef OPEN_DATASYNC_FLAG
|
2011-01-16 00:54:43 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
|
2011-01-16 07:27:10 +08:00
|
|
|
{
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a*\n");
|
2011-01-16 07:27:10 +08:00
|
|
|
fs_warning = true;
|
|
|
|
}
|
2011-01-16 00:54:43 +08:00
|
|
|
else
|
|
|
|
{
|
2011-03-11 09:25:41 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
|
|
|
|
die("could not open output file");
|
2011-01-16 00:54:43 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-16 03:42:12 +08:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2011-01-16 00:54:43 +08:00
|
|
|
{
|
2011-01-16 03:42:12 +08:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2011-01-16 03:42:12 +08:00
|
|
|
die("write failed");
|
2011-01-16 00:54:43 +08:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
close(tmpfile);
|
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
#else
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a\n");
|
2009-11-28 23:04:54 +08:00
|
|
|
#endif
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2011-01-16 00:54:43 +08:00
|
|
|
/*
|
|
|
|
* Test fdatasync if available
|
|
|
|
*/
|
2011-01-19 04:53:20 +08:00
|
|
|
printf(LABEL_FORMAT, "fdatasync");
|
2010-07-14 01:00:50 +08:00
|
|
|
fflush(stdout);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2011-03-11 09:25:41 +08:00
|
|
|
#ifdef HAVE_FDATASYNC
|
2009-11-28 23:04:54 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-16 03:42:12 +08:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2009-09-22 04:20:56 +08:00
|
|
|
{
|
2011-01-16 03:42:12 +08:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2011-01-16 03:42:12 +08:00
|
|
|
die("write failed");
|
2009-11-28 23:04:54 +08:00
|
|
|
fdatasync(tmpfile);
|
2009-09-22 04:20:56 +08:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
gettimeofday(&stop_t, NULL);
|
2004-03-18 23:26:27 +08:00
|
|
|
close(tmpfile);
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
#else
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a\n");
|
2009-11-28 23:04:54 +08:00
|
|
|
#endif
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2011-01-16 00:54:43 +08:00
|
|
|
/*
|
|
|
|
* Test fsync
|
|
|
|
*/
|
2011-01-19 04:53:20 +08:00
|
|
|
printf(LABEL_FORMAT, "fsync");
|
2010-07-14 01:00:50 +08:00
|
|
|
fflush(stdout);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-16 03:42:12 +08:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-19 03:54:00 +08:00
|
|
|
{
|
2011-01-16 03:42:12 +08:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2011-01-16 03:42:12 +08:00
|
|
|
die("write failed");
|
2009-11-28 23:04:54 +08:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
2009-09-22 04:20:56 +08:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
2004-03-19 03:54:00 +08:00
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
gettimeofday(&stop_t, NULL);
|
2004-03-18 23:26:27 +08:00
|
|
|
close(tmpfile);
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2011-01-22 08:27:25 +08:00
|
|
|
|
2011-01-16 00:54:43 +08:00
|
|
|
/*
|
|
|
|
* If fsync_writethrough is available, test as well
|
2011-01-22 08:27:25 +08:00
|
|
|
*/
|
2011-01-19 04:53:20 +08:00
|
|
|
printf(LABEL_FORMAT, "fsync_writethrough");
|
2011-01-16 00:54:43 +08:00
|
|
|
fflush(stdout);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2011-03-11 09:25:41 +08:00
|
|
|
#ifdef HAVE_FSYNC_WRITETHROUGH
|
2011-01-16 00:54:43 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2011-01-16 00:54:43 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-16 03:42:12 +08:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2011-01-16 00:54:43 +08:00
|
|
|
{
|
2011-01-16 03:42:12 +08:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2011-01-16 03:42:12 +08:00
|
|
|
die("write failed");
|
2011-01-22 08:44:53 +08:00
|
|
|
if (pg_fsync_writethrough(tmpfile) != 0)
|
2011-01-16 00:54:43 +08:00
|
|
|
die("fsync failed");
|
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
close(tmpfile);
|
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
#else
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a\n");
|
2011-01-16 00:54:43 +08:00
|
|
|
#endif
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2011-01-16 00:54:43 +08:00
|
|
|
/*
|
2011-01-16 03:42:12 +08:00
|
|
|
* Test open_sync if available
|
2011-01-16 00:54:43 +08:00
|
|
|
*/
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(LABEL_FORMAT, "open_sync");
|
2010-07-14 01:00:50 +08:00
|
|
|
fflush(stdout);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2011-03-11 09:25:41 +08:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2011-01-16 00:54:43 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
|
2011-01-16 07:27:10 +08:00
|
|
|
{
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a*\n");
|
2011-01-16 07:27:10 +08:00
|
|
|
fs_warning = true;
|
|
|
|
}
|
2011-01-16 03:42:12 +08:00
|
|
|
else
|
2011-01-16 00:54:43 +08:00
|
|
|
{
|
2011-01-16 03:42:12 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
|
|
|
{
|
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2011-01-16 03:42:12 +08:00
|
|
|
die("write failed");
|
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
close(tmpfile);
|
|
|
|
print_elapse(start_t, stop_t);
|
2011-01-16 00:54:43 +08:00
|
|
|
}
|
|
|
|
#else
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a\n");
|
2011-01-16 07:40:10 +08:00
|
|
|
#endif
|
|
|
|
|
2011-01-16 07:27:10 +08:00
|
|
|
if (fs_warning)
|
|
|
|
{
|
2011-03-11 09:25:41 +08:00
|
|
|
printf("* This file system and its mount options do not support direct\n");
|
2011-01-16 07:27:10 +08:00
|
|
|
printf("I/O, e.g. ext4 in journaled mode.\n");
|
|
|
|
}
|
2011-01-16 03:42:12 +08:00
|
|
|
}
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-16 03:42:12 +08:00
|
|
|
test_open_syncs(void)
|
|
|
|
{
|
2011-01-19 04:53:20 +08:00
|
|
|
printf("\nCompare open_sync with different write sizes:\n");
|
2011-01-27 05:21:19 +08:00
|
|
|
printf("(This is designed to compare the cost of writing 16kB\n");
|
2011-01-19 04:53:20 +08:00
|
|
|
printf("in different write open_sync sizes.)\n");
|
|
|
|
|
2011-01-27 05:21:19 +08:00
|
|
|
test_open_sync("16kB open_sync write", 16);
|
|
|
|
test_open_sync(" 8kB open_sync writes", 8);
|
|
|
|
test_open_sync(" 4kB open_sync writes", 4);
|
|
|
|
test_open_sync(" 2kB open_sync writes", 2);
|
|
|
|
test_open_sync(" 1kB open_sync writes", 1);
|
2011-01-19 04:53:20 +08:00
|
|
|
}
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2011-01-23 01:25:48 +08:00
|
|
|
/*
|
|
|
|
* Test open_sync with different size files
|
|
|
|
*/
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-19 04:53:20 +08:00
|
|
|
test_open_sync(const char *msg, int writes_size)
|
|
|
|
{
|
2011-04-20 01:01:51 +08:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2011-04-10 23:42:00 +08:00
|
|
|
int tmpfile,
|
|
|
|
ops,
|
|
|
|
writes;
|
2011-04-20 01:01:51 +08:00
|
|
|
#endif
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(LABEL_FORMAT, msg);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2011-01-16 08:40:49 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a*\n");
|
2011-01-16 08:40:49 +08:00
|
|
|
else
|
2004-03-19 03:54:00 +08:00
|
|
|
{
|
2011-01-16 08:40:49 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
|
|
|
{
|
2011-01-19 04:53:20 +08:00
|
|
|
for (writes = 0; writes < 16 / writes_size; writes++)
|
2011-01-25 08:42:32 +08:00
|
|
|
if (write(tmpfile, buf, writes_size * 1024) !=
|
|
|
|
writes_size * 1024)
|
2011-01-19 04:53:20 +08:00
|
|
|
die("write failed");
|
2011-01-16 08:40:49 +08:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
close(tmpfile);
|
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
#else
|
2011-03-11 09:25:41 +08:00
|
|
|
printf(NA_FORMAT, "n/a\n");
|
2006-10-13 22:19:29 +08:00
|
|
|
#endif
|
2011-01-16 03:42:12 +08:00
|
|
|
}
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-16 03:42:12 +08:00
|
|
|
test_file_descriptor_sync(void)
|
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
int tmpfile,
|
|
|
|
ops;
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Test whether fsync can sync data written on a different descriptor for
|
|
|
|
* the same file. This checks the efficiency of multi-process fsyncs
|
|
|
|
* against the same file. Possibly this should be done with writethrough
|
|
|
|
* on platforms which support it.
|
2009-11-28 23:04:54 +08:00
|
|
|
*/
|
2010-07-04 09:50:29 +08:00
|
|
|
printf("\nTest if fsync on non-write file descriptor is honored:\n");
|
2010-07-07 03:19:02 +08:00
|
|
|
printf("(If the times are similar, fsync() can sync data written\n");
|
2010-07-04 09:50:29 +08:00
|
|
|
printf("on a different descriptor.)\n");
|
2009-11-28 23:04:54 +08:00
|
|
|
|
2011-01-22 08:27:25 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* first write, fsync and close, which is the normal behavior without
|
|
|
|
* multiple descriptors
|
2011-01-16 00:54:43 +08:00
|
|
|
*/
|
2011-01-19 04:53:20 +08:00
|
|
|
printf(LABEL_FORMAT, "write, fsync, close");
|
2010-07-14 01:00:50 +08:00
|
|
|
fflush(stdout);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-16 03:42:12 +08:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-19 03:54:00 +08:00
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
2009-11-28 23:04:54 +08:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
|
|
|
close(tmpfile);
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2011-01-16 00:54:43 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* open and close the file again to be consistent with the following
|
|
|
|
* test
|
2011-01-16 00:54:43 +08:00
|
|
|
*/
|
2009-11-28 23:04:54 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2009-11-28 23:04:54 +08:00
|
|
|
close(tmpfile);
|
2004-03-19 03:54:00 +08:00
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2011-01-16 00:54:43 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Now open, write, close, open again and fsync This simulates processes
|
|
|
|
* fsyncing each other's writes.
|
2011-01-16 00:54:43 +08:00
|
|
|
*/
|
2011-01-22 08:27:25 +08:00
|
|
|
printf(LABEL_FORMAT, "write, close, fsync");
|
|
|
|
fflush(stdout);
|
2011-01-16 03:42:12 +08:00
|
|
|
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-16 03:42:12 +08:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-19 03:54:00 +08:00
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
2009-11-28 23:04:54 +08:00
|
|
|
close(tmpfile);
|
|
|
|
/* reopen file */
|
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2006-11-25 09:22:28 +08:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
2009-11-28 23:04:54 +08:00
|
|
|
close(tmpfile);
|
2004-03-19 03:54:00 +08:00
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 11:56:59 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-19 04:53:20 +08:00
|
|
|
test_non_sync(void)
|
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
int tmpfile,
|
|
|
|
ops;
|
2011-01-19 04:53:20 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test a simple write without fsync
|
|
|
|
*/
|
2011-01-27 05:21:19 +08:00
|
|
|
printf("\nNon-Sync'ed %dkB writes:\n", XLOG_BLCKSZ_K);
|
2011-01-19 04:53:20 +08:00
|
|
|
printf(LABEL_FORMAT, "write");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
gettimeofday(&start_t, NULL);
|
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
|
|
|
{
|
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2011-01-23 04:01:26 +08:00
|
|
|
die("could not open output file");
|
2011-01-25 09:07:05 +08:00
|
|
|
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2011-01-19 04:53:20 +08:00
|
|
|
die("write failed");
|
|
|
|
close(tmpfile);
|
|
|
|
}
|
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
}
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
#ifdef HAVE_FSYNC_WRITETHROUGH
|
|
|
|
|
|
|
|
static int
|
|
|
|
pg_fsync_writethrough(int fd)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return _commit(fd);
|
|
|
|
#elif defined(F_FULLFSYNC)
|
|
|
|
return (fcntl(fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-22 08:27:25 +08:00
|
|
|
/*
|
2011-01-16 00:54:43 +08:00
|
|
|
* print out the writes per second for tests
|
|
|
|
*/
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(struct timeval start_t, struct timeval stop_t)
|
2004-03-18 11:56:59 +08:00
|
|
|
{
|
2010-07-07 03:19:02 +08:00
|
|
|
double total_time = (stop_t.tv_sec - start_t.tv_sec) +
|
|
|
|
(stop_t.tv_usec - start_t.tv_usec) * 0.000001;
|
2011-01-16 03:42:12 +08:00
|
|
|
double per_second = ops_per_test / total_time;
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2011-01-16 21:36:43 +08:00
|
|
|
printf(OPS_FORMAT "\n", per_second);
|
2004-03-18 11:56:59 +08:00
|
|
|
}
|
|
|
|
|
2011-01-22 08:44:53 +08:00
|
|
|
static void
|
2011-01-23 04:01:26 +08:00
|
|
|
die(const char *str)
|
2004-03-18 11:56:59 +08:00
|
|
|
{
|
2011-01-23 04:01:26 +08:00
|
|
|
fprintf(stderr, "%s: %s\n", str, strerror(errno));
|
2004-03-18 11:56:59 +08:00
|
|
|
exit(1);
|
|
|
|
}
|