Commit 169516ad authored by Bruce Momjian's avatar Bruce Momjian

Restructure test_fync to use modular C so there is less duplicate code

and it can be enhanced easier.
parent 3866ff61
...@@ -3,7 +3,7 @@ test_fsync ...@@ -3,7 +3,7 @@ test_fsync
This program tests fsync. The tests are described as part of the program output. This program tests fsync. The tests are described as part of the program output.
Usage: test_fsync [-f filename] [loops] Usage: test_fsync [-f filename] [ops_per_test]
test_fsync is intended to give you a reasonable idea of what the fastest test_fsync is intended to give you a reasonable idea of what the fastest
fsync_method is on your specific system, as well as supplying diagnostic fsync_method is on your specific system, as well as supplying diagnostic
...@@ -16,5 +16,6 @@ The output filename defaults to test_fsync.out in the current directory. ...@@ -16,5 +16,6 @@ The output filename defaults to test_fsync.out in the current directory.
test_fsync should be run in the same filesystem as your transaction log test_fsync should be run in the same filesystem as your transaction log
directory (pg_xlog). directory (pg_xlog).
Loops default to 2000. Increase this to get more accurate measurements. Ops-per-test defaults to 2000. Increase this to get more accurate
measurements.
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
/* /*
* put the temp files in the local directory * put the temp files in the local directory
* unless the user specifies otherwise * unless the user specifies otherwise
...@@ -34,21 +35,59 @@ ...@@ -34,21 +35,59 @@
#define NA_FORMAT LABEL_FORMAT "%18s" #define NA_FORMAT LABEL_FORMAT "%18s"
int loops = 2000; int ops_per_test = 2000;
char full_buf[XLOG_SEG_SIZE], *buf, *filename = FSYNC_FILENAME;
struct timeval start_t, stop_t;
void die(char *str);
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);
void print_elapse(struct timeval start_t, struct timeval stop_t); void print_elapse(struct timeval start_t, struct timeval stop_t);
void die(char *str);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct timeval start_t, stop_t; handle_args(argc, argv);
int tmpfile, i;
char *full_buf = (char *) malloc(XLOG_SEG_SIZE), prepare_buf();
*buf, *filename = FSYNC_FILENAME;
test_open();
test_non_sync();
/* Test using 1 8k write */
test_sync(1);
/* 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);
}
/* /*
* arguments: loops and filename (optional) * arguments: ops_per_test and filename (optional)
*/ */
if (argc > 2 && strcmp(argv[1], "-f") == 0) if (argc > 2 && strcmp(argv[1], "-f") == 0)
{ {
...@@ -58,10 +97,27 @@ main(int argc, char *argv[]) ...@@ -58,10 +97,27 @@ main(int argc, char *argv[])
} }
if (argc > 1) if (argc > 1)
loops = atoi(argv[1]); ops_per_test = atoi(argv[1]);
printf("Ops-per-test = %d\n\n", ops_per_test);
}
for (i = 0; i < XLOG_SEG_SIZE; i++) void
full_buf[i] = random(); prepare_buf(void)
{
int ops;
/* 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;
/* /*
* test if we can open the target file * test if we can open the target file
...@@ -70,25 +126,28 @@ main(int argc, char *argv[]) ...@@ -70,25 +126,28 @@ main(int argc, char *argv[])
die("Cannot open output file."); die("Cannot 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");
/*
* fsync now so that dirty buffers don't skew later tests /* fsync now so that dirty buffers don't skew later tests */
*/
if (fsync(tmpfile) != 0) if (fsync(tmpfile) != 0)
die("fsync failed"); die("fsync failed");
close(tmpfile);
buf = (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER, full_buf); close(tmpfile);
}
printf("Loops = %d\n\n", loops); void
test_non_sync(void)
{
int tmpfile, ops;
/* /*
* Test a simple write without fsync * Test a simple write without fsync
*/ */
printf("Simple write:\n"); printf("Simple non-sync'ed write:\n");
printf(LABEL_FORMAT, "8k write"); printf(LABEL_FORMAT, "8k write");
fflush(stdout); fflush(stdout);
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) 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("Cannot open output file.");
...@@ -98,23 +157,35 @@ main(int argc, char *argv[]) ...@@ -98,23 +157,35 @@ main(int argc, char *argv[])
} }
gettimeofday(&stop_t, NULL); gettimeofday(&stop_t, NULL);
print_elapse(start_t, stop_t); print_elapse(start_t, stop_t);
}
/* void
* Test all fsync methods using single 8k writes test_sync(int writes_per_op)
*/ {
int tmpfile, ops, writes;
if (writes_per_op == 1)
printf("\nCompare file sync methods using one write:\n"); printf("\nCompare file sync methods using one write:\n");
else
printf("\nCompare file sync methods using two writes:\n");
printf("(in wal_sync_method preference order)\n");
/* /*
* Test open_datasync if available * Test open_datasync if available
*/ */
#ifdef OPEN_DATASYNC_FLAG #ifdef OPEN_DATASYNC_FLAG
if (writes_per_op == 1)
printf(LABEL_FORMAT, "open_datasync 8k write"); printf(LABEL_FORMAT, "open_datasync 8k write");
else
printf(LABEL_FORMAT, "2 open_datasync 8k writes");
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("Cannot open output file.");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
if (lseek(tmpfile, 0, SEEK_SET) == -1) if (lseek(tmpfile, 0, SEEK_SET) == -1)
...@@ -128,65 +199,20 @@ main(int argc, char *argv[]) ...@@ -128,65 +199,20 @@ main(int argc, char *argv[])
* If O_DIRECT is enabled, test that with open_datasync * If O_DIRECT is enabled, test that with open_datasync
*/ */
#if PG_O_DIRECT != 0 #if PG_O_DIRECT != 0
fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
printf(NA_FORMAT, "o_direct", "n/a on this filesystem\n"); printf(NA_FORMAT, "o_direct", "n/a on this filesystem\n");
else else
{ {
if (writes_per_op == 1)
printf(LABEL_FORMAT, "open_datasync 8k direct I/O write"); printf(LABEL_FORMAT, "open_datasync 8k direct I/O write");
gettimeofday(&start_t, NULL); else
for (i = 0; i < loops; i++) printf(LABEL_FORMAT, "2 open_datasync 8k direct I/O 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);
}
#else
printf(NA_FORMAT, "o_direct", "n/a\n");
#endif
#else
printf(NA_FORMAT, "open_datasync", "n/a\n");
#endif
/*
* Test open_sync if available
*/
#ifdef OPEN_SYNC_FLAG
printf(LABEL_FORMAT, "open_sync 8k write");
fflush(stdout); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
die("Cannot open output file.");
gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++)
{
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);
/*
* If O_DIRECT is enabled, test that with open_sync
*/
#if PG_O_DIRECT != 0
printf(LABEL_FORMAT, "open_sync 8k direct I/O write");
fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
printf(NA_FORMAT, "o_direct", "n/a on this filesystem\n");
else
{
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
if (lseek(tmpfile, 0, SEEK_SET) == -1) if (lseek(tmpfile, 0, SEEK_SET) == -1)
...@@ -201,20 +227,25 @@ main(int argc, char *argv[]) ...@@ -201,20 +227,25 @@ main(int argc, char *argv[])
#endif #endif
#else #else
printf(NA_FORMAT, "open_sync", "n/a\n"); printf(NA_FORMAT, "open_datasync", "n/a\n");
#endif #endif
/* /*
* Test fdatasync if available * Test fdatasync if available
*/ */
#ifdef HAVE_FDATASYNC #ifdef HAVE_FDATASYNC
if (writes_per_op == 1)
printf(LABEL_FORMAT, "8k write, fdatasync"); printf(LABEL_FORMAT, "8k write, fdatasync");
else
printf(LABEL_FORMAT, "8k write, 8k write, fdatasync");
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("Cannot open output file.");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
fdatasync(tmpfile); fdatasync(tmpfile);
...@@ -231,13 +262,18 @@ main(int argc, char *argv[]) ...@@ -231,13 +262,18 @@ main(int argc, char *argv[])
/* /*
* Test fsync * Test fsync
*/ */
if (writes_per_op == 1)
printf(LABEL_FORMAT, "8k write, fsync"); printf(LABEL_FORMAT, "8k write, fsync");
else
printf(LABEL_FORMAT, "8k write, 8k write, fsync");
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("Cannot open output file.");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
for (writes = 0; writes < writes_per_op; writes++)
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)
...@@ -253,13 +289,18 @@ main(int argc, char *argv[]) ...@@ -253,13 +289,18 @@ main(int argc, char *argv[])
* If fsync_writethrough is available, test as well * If fsync_writethrough is available, test as well
*/ */
#ifdef HAVE_FSYNC_WRITETHROUGH #ifdef HAVE_FSYNC_WRITETHROUGH
if (writes_per_op == 1)
printf(LABEL_FORMAT, "8k write, fsync_writethrough"); printf(LABEL_FORMAT, "8k write, fsync_writethrough");
else
printf(LABEL_FORMAT, "8k write, 8k write, fsync_writethrough");
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("Cannot open output file.");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
if (fcntl(tmpfile, F_FULLFSYNC ) != 0) if (fcntl(tmpfile, F_FULLFSYNC ) != 0)
...@@ -274,73 +315,22 @@ main(int argc, char *argv[]) ...@@ -274,73 +315,22 @@ main(int argc, char *argv[])
printf(NA_FORMAT, "fsync_writethrough", "n/a\n"); printf(NA_FORMAT, "fsync_writethrough", "n/a\n");
#endif #endif
/*
* Compare some of the file sync methods with
* two 8k writes to see if timing is different
*/
printf("\nCompare file sync methods using two writes:\n");
/*
* Test open_datasync with and without o_direct
*/
#ifdef OPEN_DATASYNC_FLAG
printf(LABEL_FORMAT, "2 open_datasync 8k writes");
fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
die("Cannot open output file.");
gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++)
{
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);
#if PG_O_DIRECT != 0
printf(LABEL_FORMAT, "2 open_datasync direct I/O 8k writes");
fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
die("Cannot open output file.");
gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++)
{
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);
#else
printf(NA_FORMAT, "o_direct" "n/a\n");
#endif
#else
printf(NA_FORMAT, "open_datasync", "n/a\n");
#endif
/* /*
* Test open_sync with and without o_direct * Test open_sync if available
*/ */
#ifdef OPEN_SYNC_FLAG #ifdef OPEN_SYNC_FLAG
if (writes_per_op == 1)
printf(LABEL_FORMAT, "open_sync 8k write");
else
printf(LABEL_FORMAT, "2 open_sync 8k writes"); printf(LABEL_FORMAT, "2 open_sync 8k writes");
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("Cannot open output file.");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) for (writes = 0; writes < writes_per_op; writes++)
die("write failed");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
if (lseek(tmpfile, 0, SEEK_SET) == -1) if (lseek(tmpfile, 0, SEEK_SET) == -1)
...@@ -350,16 +340,24 @@ main(int argc, char *argv[]) ...@@ -350,16 +340,24 @@ main(int argc, char *argv[])
close(tmpfile); close(tmpfile);
print_elapse(start_t, stop_t); print_elapse(start_t, stop_t);
/*
* If O_DIRECT is enabled, test that with open_sync
*/
#if PG_O_DIRECT != 0 #if PG_O_DIRECT != 0
printf(LABEL_FORMAT, "2 open_sync direct I/O 8k writes"); 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); fflush(stdout);
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1) if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG | PG_O_DIRECT, 0)) == -1)
die("Cannot open output file."); printf(NA_FORMAT, "o_direct", "n/a on this filesystem\n");
else
{
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) for (writes = 0; writes < writes_per_op; writes++)
die("write failed");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
if (lseek(tmpfile, 0, SEEK_SET) == -1) if (lseek(tmpfile, 0, SEEK_SET) == -1)
...@@ -368,6 +366,7 @@ main(int argc, char *argv[]) ...@@ -368,6 +366,7 @@ main(int argc, char *argv[])
gettimeofday(&stop_t, NULL); gettimeofday(&stop_t, NULL);
close(tmpfile); close(tmpfile);
print_elapse(start_t, stop_t); print_elapse(start_t, stop_t);
}
#else #else
printf(NA_FORMAT, "o_direct", "n/a\n"); printf(NA_FORMAT, "o_direct", "n/a\n");
#endif #endif
...@@ -375,82 +374,12 @@ main(int argc, char *argv[]) ...@@ -375,82 +374,12 @@ main(int argc, char *argv[])
#else #else
printf(NA_FORMAT, "open_sync", "n/a\n"); printf(NA_FORMAT, "open_sync", "n/a\n");
#endif #endif
}
/* void
* Test fdatasync test_open_syncs(void)
*/ {
#ifdef HAVE_FDATASYNC int tmpfile, ops;
printf(LABEL_FORMAT, "8k write, 8k write, fdatasync");
fflush(stdout);
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file.");
gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++)
{
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed");
fdatasync(tmpfile);
if (lseek(tmpfile, 0, SEEK_SET) == -1)
die("seek failed");
}
gettimeofday(&stop_t, NULL);
close(tmpfile);
print_elapse(start_t, stop_t);
#else
printf(NA_FORMAT, "fdatasync", "n/a\n");
#endif
/*
* Test basic fsync
*/
printf(LABEL_FORMAT, "8k write, 8k write, fsync");
fflush(stdout);
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file.");
gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++)
{
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed");
if (fsync(tmpfile) != 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);
/*
* Test fsync_writethrough if available
*/
#ifdef HAVE_FSYNC_WRITETHROUGH
printf(LABEL_FORMAT, "8k write, 8k write, fsync_writethrough");
fflush(stdout);
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("Cannot open output file.");
gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++)
{
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed");
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed");
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
printf(NA_FORMAT, "fsync_writethrough", "n/a\n");
#endif
/* /*
* Compare 1 to 2 writes * Compare 1 to 2 writes
...@@ -465,10 +394,11 @@ main(int argc, char *argv[]) ...@@ -465,10 +394,11 @@ main(int argc, char *argv[])
#ifdef OPEN_SYNC_FLAG #ifdef OPEN_SYNC_FLAG
printf(LABEL_FORMAT, "open_sync 16k write"); printf(LABEL_FORMAT, "open_sync 16k write");
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("Cannot open output file.");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
if (write(tmpfile, buf, WRITE_SIZE * 2) != WRITE_SIZE * 2) if (write(tmpfile, buf, WRITE_SIZE * 2) != WRITE_SIZE * 2)
die("write failed"); die("write failed");
...@@ -481,10 +411,11 @@ main(int argc, char *argv[]) ...@@ -481,10 +411,11 @@ main(int argc, char *argv[])
printf(LABEL_FORMAT, "2 open_sync 8k writes"); printf(LABEL_FORMAT, "2 open_sync 8k writes");
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("Cannot open output file.");
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) for (ops = 0; ops < ops_per_test; ops++)
{ {
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE) if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
die("write failed"); die("write failed");
...@@ -499,6 +430,12 @@ main(int argc, char *argv[]) ...@@ -499,6 +430,12 @@ main(int argc, char *argv[])
#else #else
printf(NA_FORMAT, "open_sync", "n/a\n"); printf(NA_FORMAT, "open_sync", "n/a\n");
#endif #endif
}
void
test_file_descriptor_sync(void)
{
int tmpfile, ops;
/* /*
* Test whether fsync can sync data written on a different * Test whether fsync can sync data written on a different
...@@ -517,8 +454,9 @@ main(int argc, char *argv[]) ...@@ -517,8 +454,9 @@ main(int argc, char *argv[])
*/ */
printf(LABEL_FORMAT, "8k write, fsync, close"); printf(LABEL_FORMAT, "8k write, fsync, close");
fflush(stdout); fflush(stdout);
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) 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("Cannot open output file.");
...@@ -545,8 +483,9 @@ main(int argc, char *argv[]) ...@@ -545,8 +483,9 @@ main(int argc, char *argv[])
*/ */
printf(LABEL_FORMAT, "8k write, close, fsync"); printf(LABEL_FORMAT, "8k write, close, fsync");
fflush(stdout); fflush(stdout);
gettimeofday(&start_t, NULL); gettimeofday(&start_t, NULL);
for (i = 0; i < loops; i++) 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("Cannot open output file.");
...@@ -563,13 +502,6 @@ main(int argc, char *argv[]) ...@@ -563,13 +502,6 @@ main(int argc, char *argv[])
gettimeofday(&stop_t, NULL); gettimeofday(&stop_t, NULL);
print_elapse(start_t, stop_t); print_elapse(start_t, stop_t);
/*
* cleanup
*/
free(full_buf);
unlink(filename);
return 0;
} }
/* /*
...@@ -580,7 +512,7 @@ print_elapse(struct timeval start_t, struct timeval stop_t) ...@@ -580,7 +512,7 @@ print_elapse(struct timeval start_t, struct timeval stop_t)
{ {
double total_time = (stop_t.tv_sec - start_t.tv_sec) + double total_time = (stop_t.tv_sec - start_t.tv_sec) +
(stop_t.tv_usec - start_t.tv_usec) * 0.000001; (stop_t.tv_usec - start_t.tv_usec) * 0.000001;
double per_second = loops / total_time; double per_second = ops_per_test / total_time;
printf("%9.3f ops/sec\n", per_second); printf("%9.3f ops/sec\n", per_second);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment