2004-03-18 06:56:59 +03:00
|
|
|
/*
|
2010-09-21 00:08:53 +04:00
|
|
|
* src/tools/fsync/test_fsync.c
|
2008-05-17 05:28:26 +04:00
|
|
|
*
|
|
|
|
*
|
2004-03-18 06:56:59 +03:00
|
|
|
* test_fsync.c
|
2011-01-15 19:54:43 +03:00
|
|
|
* tests all supported fsync() methods
|
2004-03-18 06:56:59 +03:00
|
|
|
*/
|
|
|
|
|
2006-11-25 04:22:28 +03:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/xlog_internal.h"
|
|
|
|
#include "access/xlog.h"
|
2007-02-14 08:00:40 +03:00
|
|
|
#include "access/xlogdefs.h"
|
2004-03-18 06:56:59 +03:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2004-03-18 18:26:27 +03:00
|
|
|
#include <sys/stat.h>
|
2004-03-18 06:56:59 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2004-03-18 07:04:36 +03:00
|
|
|
#include <sys/time.h>
|
2004-03-18 06:56:59 +03:00
|
|
|
#include <unistd.h>
|
2006-11-25 04:22:28 +03:00
|
|
|
#include <string.h>
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* put the temp files in the local directory
|
|
|
|
* unless the user specifies otherwise
|
|
|
|
*/
|
2006-11-25 04:22:28 +03:00
|
|
|
#define FSYNC_FILENAME "./test_fsync.out"
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2010-02-26 05:01:40 +03:00
|
|
|
#define WRITE_SIZE (8 * 1024) /* 8k */
|
2009-11-28 18:04:54 +03:00
|
|
|
|
2011-01-16 03:40:49 +03:00
|
|
|
#define LABEL_FORMAT " %-32s"
|
|
|
|
#define NA_FORMAT LABEL_FORMAT "%18s"
|
2011-01-16 16:36:43 +03:00
|
|
|
#define OPS_FORMAT "%9.3f ops/sec"
|
2011-01-15 19:54:43 +03:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
int ops_per_test = 2000;
|
|
|
|
char full_buf[XLOG_SEG_SIZE], *buf, *filename = FSYNC_FILENAME;
|
|
|
|
struct timeval start_t, stop_t;
|
2010-07-04 05:50:29 +04:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
|
|
|
|
void handle_args(int argc, char *argv[]);
|
|
|
|
void prepare_buf(void);
|
|
|
|
void test_open(void);
|
|
|
|
void test_non_sync(void);
|
|
|
|
void test_sync(int writes_per_op);
|
|
|
|
void test_open_syncs(void);
|
|
|
|
void test_file_descriptor_sync(void);
|
2009-11-28 18:04:54 +03:00
|
|
|
void print_elapse(struct timeval start_t, struct timeval stop_t);
|
2011-01-15 22:42:12 +03:00
|
|
|
void die(char *str);
|
|
|
|
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2004-03-18 18:26:27 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2004-03-18 06:56:59 +03:00
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
handle_args(argc, argv);
|
|
|
|
|
|
|
|
prepare_buf();
|
|
|
|
|
|
|
|
test_open();
|
|
|
|
|
|
|
|
test_non_sync();
|
|
|
|
|
|
|
|
/* Test using 1 8k write */
|
|
|
|
test_sync(1);
|
2004-03-18 18:26:27 +03:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
/* Test using 2 8k writes */
|
|
|
|
test_sync(2);
|
|
|
|
|
|
|
|
test_open_syncs();
|
|
|
|
|
|
|
|
test_file_descriptor_sync();
|
|
|
|
|
|
|
|
unlink(filename);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_args(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc > 1 && strcmp(argv[1], "-h") == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "test_fsync [-f filename] [ops-per-test]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
2011-01-15 22:42:12 +03:00
|
|
|
* arguments: ops_per_test and filename (optional)
|
2011-01-15 19:54:43 +03:00
|
|
|
*/
|
2004-08-29 09:07:03 +04:00
|
|
|
if (argc > 2 && strcmp(argv[1], "-f") == 0)
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
|
|
|
filename = argv[2];
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
}
|
2004-08-29 09:07:03 +04:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
if (argc > 1)
|
2011-01-15 22:42:12 +03:00
|
|
|
ops_per_test = atoi(argv[1]);
|
|
|
|
|
|
|
|
printf("Ops-per-test = %d\n\n", ops_per_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prepare_buf(void)
|
|
|
|
{
|
|
|
|
int ops;
|
2004-08-29 09:07:03 +04:00
|
|
|
|
2011-01-15 22:42:12 +03: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_open(void)
|
|
|
|
{
|
|
|
|
int tmpfile;
|
2004-03-18 22:54:00 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* test if we can open the target file
|
|
|
|
*/
|
2010-10-03 02:40:28 +04:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
|
2005-11-16 06:32:04 +03:00
|
|
|
die("Cannot open output file.");
|
2006-11-25 04:22:28 +03:00
|
|
|
if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
|
|
|
|
die("write failed");
|
2011-01-15 22:42:12 +03:00
|
|
|
|
|
|
|
/* fsync now so that dirty buffers don't skew later tests */
|
2006-11-25 04:22:28 +03:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
close(tmpfile);
|
|
|
|
}
|
2006-11-25 04:22:28 +03:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
void
|
|
|
|
test_non_sync(void)
|
|
|
|
{
|
|
|
|
int tmpfile, ops;
|
2010-07-04 05:50:29 +04:00
|
|
|
|
2009-08-10 22:19:06 +04:00
|
|
|
/*
|
2011-01-15 19:54:43 +03:00
|
|
|
* Test a simple write without fsync
|
2009-08-10 22:19:06 +04:00
|
|
|
*/
|
2011-01-15 22:42:12 +03:00
|
|
|
printf("Simple non-sync'ed write:\n");
|
2010-07-13 21:00:50 +04:00
|
|
|
printf(LABEL_FORMAT, "8k write");
|
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2004-03-18 06:56:59 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
2007-11-05 20:10:26 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 06:32:04 +03:00
|
|
|
die("Cannot open output file.");
|
2009-11-28 18:04:54 +03:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 04:22:28 +03:00
|
|
|
die("write failed");
|
2004-03-18 22:54:00 +03:00
|
|
|
close(tmpfile);
|
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
print_elapse(start_t, stop_t);
|
2011-01-15 22:42:12 +03:00
|
|
|
}
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
void
|
|
|
|
test_sync(int writes_per_op)
|
|
|
|
{
|
|
|
|
int tmpfile, ops, writes;
|
2011-01-16 02:27:10 +03:00
|
|
|
bool fs_warning = false;
|
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
if (writes_per_op == 1)
|
|
|
|
printf("\nCompare file sync methods using one write:\n");
|
|
|
|
else
|
|
|
|
printf("\nCompare file sync methods using two writes:\n");
|
2011-01-15 23:00:20 +03:00
|
|
|
printf("(in wal_sync_method preference order, except fdatasync\n");
|
|
|
|
printf("is Linux's default)\n");
|
2004-03-18 18:26:27 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* Test open_datasync if available
|
|
|
|
*/
|
2009-11-28 18:04:54 +03:00
|
|
|
#ifdef OPEN_DATASYNC_FLAG
|
2011-01-15 22:42:12 +03:00
|
|
|
if (writes_per_op == 1)
|
2011-01-16 02:27:10 +03:00
|
|
|
printf(LABEL_FORMAT, "open_datasync 8k write"
|
|
|
|
#if PG_O_DIRECT != 0
|
2011-01-16 02:40:10 +03:00
|
|
|
"*"
|
2011-01-16 02:27:10 +03:00
|
|
|
#endif
|
|
|
|
);
|
2011-01-15 22:42:12 +03:00
|
|
|
else
|
2011-01-16 02:27:10 +03:00
|
|
|
printf(LABEL_FORMAT, "2 open_datasync 8k writes"
|
|
|
|
#if PG_O_DIRECT != 0
|
2011-01-16 02:40:10 +03:00
|
|
|
"*"
|
2011-01-16 02:27:10 +03:00
|
|
|
#endif
|
|
|
|
);
|
2010-07-13 21:00:50 +04:00
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2009-11-28 18:04:54 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
2004-03-18 06:56:59 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
2009-11-28 18:04:54 +03:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
2004-03-18 22:54:00 +03:00
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
close(tmpfile);
|
|
|
|
print_elapse(start_t, stop_t);
|
2011-01-15 19:54:43 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If O_DIRECT is enabled, test that with open_datasync
|
|
|
|
*/
|
|
|
|
#if PG_O_DIRECT != 0
|
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
|
2011-01-16 02:27:10 +03:00
|
|
|
{
|
2011-01-16 02:40:10 +03:00
|
|
|
printf(NA_FORMAT, "o_direct", "n/a**\n");
|
2011-01-16 02:27:10 +03:00
|
|
|
fs_warning = true;
|
|
|
|
}
|
2011-01-15 19:54:43 +03:00
|
|
|
else
|
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
if (writes_per_op == 1)
|
|
|
|
printf(LABEL_FORMAT, "open_datasync 8k direct I/O write");
|
|
|
|
else
|
|
|
|
printf(LABEL_FORMAT, "2 open_datasync 8k direct I/O writes");
|
|
|
|
fflush(stdout);
|
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2011-01-15 19:54:43 +03:00
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
2011-01-15 19:54:43 +03:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
close(tmpfile);
|
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
}
|
|
|
|
#else
|
2011-01-15 20:24:05 +03:00
|
|
|
printf(NA_FORMAT, "o_direct", "n/a\n");
|
2011-01-15 19:54:43 +03:00
|
|
|
#endif
|
|
|
|
|
2009-11-28 18:04:54 +03:00
|
|
|
#else
|
2011-01-15 20:24:05 +03:00
|
|
|
printf(NA_FORMAT, "open_datasync", "n/a\n");
|
2009-11-28 18:04:54 +03:00
|
|
|
#endif
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* Test fdatasync if available
|
|
|
|
*/
|
2009-11-28 18:04:54 +03:00
|
|
|
#ifdef HAVE_FDATASYNC
|
2011-01-15 22:42:12 +03:00
|
|
|
if (writes_per_op == 1)
|
|
|
|
printf(LABEL_FORMAT, "8k write, fdatasync");
|
|
|
|
else
|
|
|
|
printf(LABEL_FORMAT, "8k write, 8k write, fdatasync");
|
2010-07-13 21:00:50 +04:00
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2009-11-28 18:04:54 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 06:32:04 +03:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 18:26:27 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2009-09-22 00:20:56 +04:00
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
2009-11-28 18:04:54 +03:00
|
|
|
fdatasync(tmpfile);
|
2009-09-22 00:20:56 +04:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
gettimeofday(&stop_t, NULL);
|
2004-03-18 18:26:27 +03:00
|
|
|
close(tmpfile);
|
2009-11-28 18:04:54 +03:00
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
#else
|
2011-01-15 20:24:05 +03:00
|
|
|
printf(NA_FORMAT, "fdatasync", "n/a\n");
|
2009-11-28 18:04:54 +03:00
|
|
|
#endif
|
2004-03-18 18:26:27 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* Test fsync
|
|
|
|
*/
|
2011-01-15 22:42:12 +03:00
|
|
|
if (writes_per_op == 1)
|
|
|
|
printf(LABEL_FORMAT, "8k write, fsync");
|
|
|
|
else
|
|
|
|
printf(LABEL_FORMAT, "8k write, 8k write, fsync");
|
2010-07-13 21:00:50 +04:00
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2009-11-28 18:04:54 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 06:32:04 +03:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 18:26:27 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
2009-11-28 18:04:54 +03:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
2009-09-22 00:20:56 +04:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
2004-03-18 22:54:00 +03:00
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
gettimeofday(&stop_t, NULL);
|
2004-03-18 18:26:27 +03:00
|
|
|
close(tmpfile);
|
2009-11-28 18:04:54 +03:00
|
|
|
print_elapse(start_t, stop_t);
|
2011-01-15 19:54:43 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If fsync_writethrough is available, test as well
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_FSYNC_WRITETHROUGH
|
2011-01-15 22:42:12 +03:00
|
|
|
if (writes_per_op == 1)
|
|
|
|
printf(LABEL_FORMAT, "8k write, fsync_writethrough");
|
|
|
|
else
|
|
|
|
printf(LABEL_FORMAT, "8k write, 8k write, fsync_writethrough");
|
2011-01-15 19:54:43 +03:00
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2011-01-15 19:54:43 +03:00
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
2011-01-15 19:54:43 +03:00
|
|
|
if (fcntl(tmpfile, F_FULLFSYNC ) != 0)
|
|
|
|
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-01-15 20:24:05 +03:00
|
|
|
printf(NA_FORMAT, "fsync_writethrough", "n/a\n");
|
2011-01-15 19:54:43 +03:00
|
|
|
#endif
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
2011-01-15 22:42:12 +03:00
|
|
|
* Test open_sync if available
|
2011-01-15 19:54:43 +03:00
|
|
|
*/
|
2006-10-13 18:19:29 +04:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2011-01-15 22:42:12 +03:00
|
|
|
if (writes_per_op == 1)
|
2011-01-16 02:27:10 +03:00
|
|
|
printf(LABEL_FORMAT, "open_sync 8k write"
|
|
|
|
#if PG_O_DIRECT != 0
|
2011-01-16 02:40:10 +03:00
|
|
|
"*"
|
2011-01-16 02:27:10 +03:00
|
|
|
#endif
|
|
|
|
);
|
2011-01-15 22:42:12 +03:00
|
|
|
else
|
2011-01-16 02:27:10 +03:00
|
|
|
printf(LABEL_FORMAT, "2 open_sync 8k writes"
|
|
|
|
#if PG_O_DIRECT != 0
|
2011-01-16 02:40:10 +03:00
|
|
|
"*"
|
2011-01-16 02:27:10 +03:00
|
|
|
#endif
|
|
|
|
);
|
2010-07-13 21:00:50 +04:00
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2007-11-05 20:10:26 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
|
2005-11-16 06:32:04 +03:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 18:26:27 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2009-09-22 00:20:56 +04:00
|
|
|
{
|
2011-01-15 22:42:12 +03:00
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
2009-09-22 00:20:56 +04:00
|
|
|
if (lseek(tmpfile, 0, SEEK_SET) == -1)
|
|
|
|
die("seek failed");
|
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
gettimeofday(&stop_t, NULL);
|
2004-03-18 06:56:59 +03:00
|
|
|
close(tmpfile);
|
2009-11-28 18:04:54 +03:00
|
|
|
print_elapse(start_t, stop_t);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If O_DIRECT is enabled, test that with open_sync
|
|
|
|
*/
|
2011-01-15 19:54:43 +03:00
|
|
|
#if PG_O_DIRECT != 0
|
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
|
2011-01-16 02:27:10 +03:00
|
|
|
{
|
2011-01-16 02:40:10 +03:00
|
|
|
printf(NA_FORMAT, "o_direct", "n/a**\n");
|
2011-01-16 02:27:10 +03:00
|
|
|
fs_warning = true;
|
|
|
|
}
|
2011-01-15 22:42:12 +03:00
|
|
|
else
|
2011-01-15 19:54:43 +03:00
|
|
|
{
|
2011-01-16 03:40:49 +03:00
|
|
|
if (writes_per_op == 1)
|
|
|
|
printf(LABEL_FORMAT, "open_sync 8k direct I/O write");
|
|
|
|
else
|
|
|
|
printf(LABEL_FORMAT, "2 open_sync 8k direct I/O writes");
|
|
|
|
fflush(stdout);
|
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
|
|
|
{
|
|
|
|
for (writes = 0; writes < writes_per_op; writes++)
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
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-15 19:54:43 +03:00
|
|
|
}
|
|
|
|
#else
|
2011-01-15 20:24:05 +03:00
|
|
|
printf(NA_FORMAT, "o_direct", "n/a\n");
|
2011-01-15 19:54:43 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
2011-01-15 20:24:05 +03:00
|
|
|
printf(NA_FORMAT, "open_sync", "n/a\n");
|
2006-10-13 18:19:29 +04:00
|
|
|
#endif
|
2011-01-16 02:27:10 +03:00
|
|
|
|
2011-01-16 02:40:10 +03:00
|
|
|
#if defined(OPEN_DATASYNC_FLAG) || defined(OPEN_SYNC_FLAG)
|
|
|
|
if (PG_O_DIRECT != 0)
|
|
|
|
printf("* This non-direct I/O option is not used by Postgres.\n");
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 02:27:10 +03:00
|
|
|
if (fs_warning)
|
|
|
|
{
|
2011-01-16 02:40:10 +03:00
|
|
|
printf("** This file system and its mount options do not support direct\n");
|
2011-01-16 02:27:10 +03:00
|
|
|
printf("I/O, e.g. ext4 in journaled mode.\n");
|
|
|
|
}
|
2011-01-15 22:42:12 +03:00
|
|
|
}
|
2004-03-18 18:26:27 +03:00
|
|
|
|
2011-01-15 22:42:12 +03:00
|
|
|
void
|
|
|
|
test_open_syncs(void)
|
|
|
|
{
|
|
|
|
int tmpfile, ops;
|
2004-03-18 18:26:27 +03:00
|
|
|
|
2009-08-10 22:19:06 +04:00
|
|
|
/*
|
2010-02-26 05:01:40 +03:00
|
|
|
* Compare 1 to 2 writes
|
2009-08-10 22:19:06 +04:00
|
|
|
*/
|
2010-07-04 17:42:51 +04:00
|
|
|
printf("\nCompare open_sync with different sizes:\n");
|
2011-01-15 19:54:43 +03:00
|
|
|
printf("(This is designed to compare the cost of one large\n");
|
|
|
|
printf("sync'ed write and two smaller sync'ed writes.)\n");
|
2004-03-18 18:26:27 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* Test open_sync with different size files
|
|
|
|
*/
|
2009-11-28 18:04:54 +03:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2011-01-16 03:40:49 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
|
|
|
|
printf(NA_FORMAT, "o_direct", "n/a**\n");
|
|
|
|
else
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
2011-01-16 03:40:49 +03:00
|
|
|
printf(LABEL_FORMAT, "open_sync 16k write");
|
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2011-01-16 03:40:49 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
|
|
|
{
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE * 2) != WRITE_SIZE * 2)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
|
|
|
|
printf(NA_FORMAT, "n/a**\n");
|
|
|
|
else
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
2011-01-16 03:40:49 +03:00
|
|
|
printf(LABEL_FORMAT, "2 open_sync 8k writes");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
gettimeofday(&start_t, NULL);
|
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
|
|
|
{
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
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);
|
2004-03-18 22:54:00 +03:00
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
#else
|
2011-01-15 20:24:05 +03:00
|
|
|
printf(NA_FORMAT, "open_sync", "n/a\n");
|
2006-10-13 18:19:29 +04:00
|
|
|
#endif
|
2011-01-15 22:42:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_file_descriptor_sync(void)
|
|
|
|
{
|
|
|
|
int tmpfile, ops;
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2009-11-28 18:04:54 +03:00
|
|
|
/*
|
2011-01-15 19:54:43 +03: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 18:04:54 +03:00
|
|
|
*/
|
2010-07-04 05:50:29 +04:00
|
|
|
printf("\nTest if fsync on non-write file descriptor is honored:\n");
|
2010-07-06 23:19:02 +04:00
|
|
|
printf("(If the times are similar, fsync() can sync data written\n");
|
2010-07-04 05:50:29 +04:00
|
|
|
printf("on a different descriptor.)\n");
|
2009-11-28 18:04:54 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* first write, fsync and close, which is the
|
|
|
|
* normal behavior without multiple descriptors
|
|
|
|
*/
|
2010-07-13 21:00:50 +04:00
|
|
|
printf(LABEL_FORMAT, "8k write, fsync, close");
|
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2004-03-18 18:26:27 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
2009-11-28 18:04:54 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 04:22:28 +03:00
|
|
|
die("write failed");
|
2009-11-28 18:04:54 +03:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
|
|
|
close(tmpfile);
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* open and close the file again to be consistent
|
|
|
|
* with the following test
|
|
|
|
*/
|
2009-11-28 18:04:54 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
|
|
|
close(tmpfile);
|
2004-03-18 22:54:00 +03:00
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 06:56:59 +03:00
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* Now open, write, close, open again and fsync
|
|
|
|
* This simulates processes fsyncing each other's
|
|
|
|
* writes.
|
|
|
|
*/
|
2010-07-13 21:00:50 +04:00
|
|
|
printf(LABEL_FORMAT, "8k write, close, fsync");
|
|
|
|
fflush(stdout);
|
2011-01-15 22:42:12 +03:00
|
|
|
|
2004-03-18 18:26:27 +03:00
|
|
|
gettimeofday(&start_t, NULL);
|
2011-01-15 22:42:12 +03:00
|
|
|
for (ops = 0; ops < ops_per_test; ops++)
|
2004-03-18 22:54:00 +03:00
|
|
|
{
|
2009-11-28 18:04:54 +03:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 04:22:28 +03:00
|
|
|
die("write failed");
|
2009-11-28 18:04:54 +03:00
|
|
|
close(tmpfile);
|
|
|
|
/* reopen file */
|
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
2006-11-25 04:22:28 +03:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
2009-11-28 18:04:54 +03:00
|
|
|
close(tmpfile);
|
2004-03-18 22:54:00 +03:00
|
|
|
}
|
2009-11-28 18:04:54 +03:00
|
|
|
gettimeofday(&stop_t, NULL);
|
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 06:56:59 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-15 19:54:43 +03:00
|
|
|
/*
|
|
|
|
* print out the writes per second for tests
|
|
|
|
*/
|
2004-03-18 18:26:27 +03:00
|
|
|
void
|
2009-11-28 18:04:54 +03:00
|
|
|
print_elapse(struct timeval start_t, struct timeval stop_t)
|
2004-03-18 06:56:59 +03:00
|
|
|
{
|
2010-07-06 23:19:02 +04:00
|
|
|
double total_time = (stop_t.tv_sec - start_t.tv_sec) +
|
|
|
|
(stop_t.tv_usec - start_t.tv_usec) * 0.000001;
|
2011-01-15 22:42:12 +03:00
|
|
|
double per_second = ops_per_test / total_time;
|
2010-07-06 23:19:02 +04:00
|
|
|
|
2011-01-16 16:36:43 +03:00
|
|
|
printf(OPS_FORMAT "\n", per_second);
|
2004-03-18 06:56:59 +03:00
|
|
|
}
|
|
|
|
|
2004-03-18 18:26:27 +03:00
|
|
|
void
|
|
|
|
die(char *str)
|
2004-03-18 06:56:59 +03:00
|
|
|
{
|
2005-11-16 06:32:04 +03:00
|
|
|
fprintf(stderr, "%s\n", str);
|
2004-03-18 06:56:59 +03:00
|
|
|
exit(1);
|
|
|
|
}
|