2004-03-18 11:56:59 +08:00
|
|
|
/*
|
2010-07-04 09:50:29 +08:00
|
|
|
* $PostgreSQL: pgsql/src/tools/fsync/test_fsync.c,v 1.28 2010/07/04 01:50:29 momjian Exp $
|
2008-05-17 09:28:26 +08:00
|
|
|
*
|
|
|
|
*
|
2004-03-18 11:56:59 +08:00
|
|
|
* test_fsync.c
|
2006-11-25 09:22:28 +08:00
|
|
|
* test various fsync() methods
|
2004-03-18 11:56:59 +08:00
|
|
|
*/
|
|
|
|
|
2006-11-25 09:22:28 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/xlog_internal.h"
|
|
|
|
#include "access/xlog.h"
|
2007-02-14 13:00:40 +08:00
|
|
|
#include "access/xlogdefs.h"
|
2004-03-18 11:56:59 +08:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2004-03-18 23:26:27 +08:00
|
|
|
#include <sys/stat.h>
|
2004-03-18 11:56:59 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2004-03-18 12:04:36 +08:00
|
|
|
#include <sys/time.h>
|
2004-03-18 11:56:59 +08:00
|
|
|
#include <unistd.h>
|
2006-11-25 09:22:28 +08:00
|
|
|
#include <string.h>
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2006-11-25 09:22:28 +08:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define FSYNC_FILENAME "./test_fsync.out"
|
|
|
|
#else
|
|
|
|
/* /tmp might be a memory file system */
|
|
|
|
#define FSYNC_FILENAME "/var/tmp/test_fsync.out"
|
2004-03-18 11:56:59 +08:00
|
|
|
#endif
|
|
|
|
|
2010-02-26 10:01:40 +08:00
|
|
|
#define WRITE_SIZE (8 * 1024) /* 8k */
|
2009-11-28 23:04:54 +08:00
|
|
|
|
|
|
|
#define LABEL_FORMAT "\t%-30s"
|
2004-03-19 04:09:33 +08:00
|
|
|
|
2010-07-04 09:50:29 +08:00
|
|
|
int loops = 10000;
|
|
|
|
|
2004-03-18 23:26:27 +08:00
|
|
|
void die(char *str);
|
2009-11-28 23:04:54 +08:00
|
|
|
void print_elapse(struct timeval start_t, struct timeval stop_t);
|
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
|
|
|
{
|
|
|
|
struct timeval start_t;
|
2009-11-28 23:04:54 +08:00
|
|
|
struct timeval stop_t;
|
2004-03-18 23:26:27 +08:00
|
|
|
int tmpfile,
|
2010-07-04 09:50:29 +08:00
|
|
|
i;
|
2007-11-16 05:14:46 +08:00
|
|
|
char *full_buf = (char *) malloc(XLOG_SEG_SIZE),
|
|
|
|
*buf;
|
2004-03-19 03:54:00 +08:00
|
|
|
char *filename = FSYNC_FILENAME;
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (argc > 2 && strcmp(argv[1], "-f") == 0)
|
2004-03-19 03:54:00 +08:00
|
|
|
{
|
|
|
|
filename = argv[2];
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2004-03-19 03:54:00 +08:00
|
|
|
if (argc > 1)
|
2004-08-29 13:07:03 +08:00
|
|
|
loops = atoi(argv[1]);
|
|
|
|
|
2006-11-25 09:22:28 +08:00
|
|
|
for (i = 0; i < XLOG_SEG_SIZE; i++)
|
2009-11-28 23:04:54 +08:00
|
|
|
full_buf[i] = random();
|
2004-03-19 03:54:00 +08:00
|
|
|
|
2009-05-08 22:06:27 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot 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");
|
2009-11-28 23:04:54 +08:00
|
|
|
/* fsync now so later fsync's don't have to do it */
|
2006-11-25 09:22:28 +08:00
|
|
|
if (fsync(tmpfile) != 0)
|
|
|
|
die("fsync failed");
|
2004-03-18 23:26:27 +08:00
|
|
|
close(tmpfile);
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
buf = (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER, full_buf);
|
2006-11-25 09:22:28 +08:00
|
|
|
|
2010-07-04 09:50:29 +08:00
|
|
|
printf("Loops = %d\n\n", loops);
|
|
|
|
|
2009-08-11 02:19:06 +08:00
|
|
|
/*
|
2010-02-26 10:01:40 +08:00
|
|
|
* Simple write
|
2009-08-11 02:19:06 +08:00
|
|
|
*/
|
2010-07-04 09:50:29 +08:00
|
|
|
printf("Simple write timing:\n");
|
2004-03-18 23:26:27 +08:00
|
|
|
/* write only */
|
2004-03-18 11:56:59 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2007-11-06 01:10:26 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
2004-03-19 03:54:00 +08:00
|
|
|
close(tmpfile);
|
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
gettimeofday(&stop_t, NULL);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "8k write");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2009-08-11 02:19:06 +08:00
|
|
|
/*
|
2010-02-26 10:01:40 +08:00
|
|
|
* Compare file sync methods with one 8k write
|
2009-08-11 02:19:06 +08:00
|
|
|
*/
|
2010-07-04 09:50:29 +08:00
|
|
|
printf("\nCompare file sync methods using one write:\n");
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
#ifdef OPEN_DATASYNC_FLAG
|
|
|
|
/* open_dsync, write */
|
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
2004-03-18 11:56:59 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
2009-11-28 23:04:54 +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);
|
|
|
|
close(tmpfile);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "open_datasync 8k write");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
#else
|
|
|
|
printf("\t(open_datasync unavailable)\n");
|
|
|
|
#endif
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
|
|
|
/* open_fsync, write */
|
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
2004-03-18 11:56:59 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
2009-11-28 23:04:54 +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);
|
|
|
|
close(tmpfile);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "open_sync 8k write");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
#else
|
|
|
|
printf("\t(open_sync unavailable)\n");
|
|
|
|
#endif
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
#ifdef HAVE_FDATASYNC
|
|
|
|
/* write, fdatasync */
|
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
2009-09-22 04:20:56 +08:00
|
|
|
{
|
2006-11-25 09:22:28 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
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);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "8k write, fdatasync");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
#else
|
|
|
|
printf("\t(fdatasync unavailable)\n");
|
|
|
|
#endif
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
/* write, fsync, close */
|
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
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");
|
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);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "8k write, fsync");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2009-08-11 02:19:06 +08:00
|
|
|
/*
|
2010-02-26 10:01:40 +08:00
|
|
|
* Compare file sync methods with two 8k write
|
2009-08-11 02:19:06 +08:00
|
|
|
*/
|
2010-07-04 09:50:29 +08:00
|
|
|
printf("\nCompare file sync methods using two writes:\n");
|
2009-08-11 02:19:06 +08:00
|
|
|
|
2004-03-18 11:56:59 +08:00
|
|
|
#ifdef OPEN_DATASYNC_FLAG
|
|
|
|
/* open_dsync, write */
|
2007-11-06 01:10:26 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | O_DSYNC, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 11:56:59 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
2009-09-22 04:20:56 +08:00
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
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);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "2 open_datasync 8k writes");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2009-08-11 02:19:06 +08:00
|
|
|
#else
|
2009-11-28 23:04:54 +08:00
|
|
|
printf("\t(open_datasync unavailable)\n");
|
2009-08-11 02:19:06 +08:00
|
|
|
#endif
|
|
|
|
|
2006-10-13 22:19:29 +08:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2004-03-18 23:26:27 +08:00
|
|
|
/* open_fsync, write */
|
2007-11-06 01:10:26 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
2009-09-22 04:20:56 +08:00
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
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 11:56:59 +08:00
|
|
|
close(tmpfile);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "2 open_sync 8k writes");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2006-10-13 22:19:29 +08:00
|
|
|
#endif
|
2004-03-18 23:26:27 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_FDATASYNC
|
|
|
|
/* write, fdatasync */
|
2007-11-06 01:10:26 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
2004-03-19 03:54:00 +08:00
|
|
|
fdatasync(tmpfile);
|
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);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "8k write, 8k write, fdatasync");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 22:02:58 +08:00
|
|
|
#else
|
2009-11-28 23:04:54 +08:00
|
|
|
printf("\t(fdatasync unavailable)\n");
|
2004-03-18 11:56:59 +08:00
|
|
|
#endif
|
|
|
|
|
2004-03-18 23:26:27 +08:00
|
|
|
/* write, fsync, close */
|
2007-11-06 01:10:26 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 11:56:59 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
|
|
|
die("write failed");
|
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
|
|
|
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);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "8k write, 8k write, fsync");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2009-08-11 02:19:06 +08:00
|
|
|
/*
|
2010-02-26 10:01:40 +08:00
|
|
|
* Compare 1 to 2 writes
|
2009-08-11 02:19:06 +08:00
|
|
|
*/
|
2009-11-28 23:04:54 +08:00
|
|
|
printf("\nCompare open_sync sizes:\n");
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
|
|
|
/* 16k open_sync write */
|
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE * 2) != WRITE_SIZE * 2)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write 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 11:56:59 +08:00
|
|
|
close(tmpfile);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "open_sync 16k write");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
/* Two 8k open_sync writes */
|
2007-11-06 01:10:26 +08:00
|
|
|
if ((tmpfile = open(filename, O_RDWR | OPEN_SYNC_FLAG, 0)) == -1)
|
2005-11-16 11:32:04 +08:00
|
|
|
die("Cannot open output file.");
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write failed");
|
2009-11-28 23:04:54 +08:00
|
|
|
if (write(tmpfile, buf, WRITE_SIZE) != WRITE_SIZE)
|
2006-11-25 09:22:28 +08:00
|
|
|
die("write 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);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "2 open_sync 8k writes");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
|
|
|
#else
|
|
|
|
printf("\t(open_sync unavailable)\n");
|
2006-10-13 22:19:29 +08:00
|
|
|
#endif
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
/*
|
2010-02-26 10:01:40 +08:00
|
|
|
* Fsync another file descriptor?
|
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");
|
|
|
|
printf("(If the times are similar, fsync() can sync data written\n");
|
|
|
|
printf("on a different descriptor.)\n");
|
2009-11-28 23:04:54 +08:00
|
|
|
|
|
|
|
/* write, fsync, close */
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08: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 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);
|
|
|
|
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
|
|
|
|
die("Cannot open output file.");
|
|
|
|
/* do nothing but the open/close the tests are consistent. */
|
|
|
|
close(tmpfile);
|
2004-03-19 03:54:00 +08:00
|
|
|
}
|
2009-11-28 23:04:54 +08:00
|
|
|
gettimeofday(&stop_t, NULL);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "8k write, fsync, close");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
/* write, close, fsync */
|
2004-03-18 23:26:27 +08:00
|
|
|
gettimeofday(&start_t, NULL);
|
2004-03-19 03:54:00 +08:00
|
|
|
for (i = 0; i < loops; i++)
|
|
|
|
{
|
2009-11-28 23:04:54 +08: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 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)
|
|
|
|
die("Cannot 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);
|
2010-07-04 09:50:29 +08:00
|
|
|
printf(LABEL_FORMAT, "8k write, close, fsync");
|
2009-11-28 23:04:54 +08:00
|
|
|
print_elapse(start_t, stop_t);
|
2004-03-18 11:56:59 +08:00
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
/* cleanup */
|
2006-11-25 09:22:28 +08:00
|
|
|
free(full_buf);
|
2005-11-16 09:31:07 +08:00
|
|
|
unlink(filename);
|
2004-03-18 23:26:27 +08:00
|
|
|
|
2004-03-18 11:56:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-18 23:26:27 +08:00
|
|
|
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-04 09:50:29 +08:00
|
|
|
double total_time, per_second;
|
|
|
|
|
2009-11-28 23:04:54 +08:00
|
|
|
if (stop_t.tv_usec < start_t.tv_usec)
|
2004-03-18 11:56:59 +08:00
|
|
|
{
|
2009-11-28 23:04:54 +08:00
|
|
|
stop_t.tv_sec--;
|
|
|
|
stop_t.tv_usec += 1000000;
|
2004-03-18 11:56:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-04 09:50:29 +08:00
|
|
|
total_time = (stop_t.tv_sec - start_t.tv_sec) +
|
|
|
|
(stop_t.tv_usec - start_t.tv_usec) * 0.000001;
|
|
|
|
per_second = loops / total_time;
|
|
|
|
|
|
|
|
printf("%9.3f/second\n", per_second);
|
2004-03-18 11:56:59 +08:00
|
|
|
}
|
|
|
|
|
2004-03-18 23:26:27 +08:00
|
|
|
void
|
|
|
|
die(char *str)
|
2004-03-18 11:56:59 +08:00
|
|
|
{
|
2005-11-16 11:32:04 +08:00
|
|
|
fprintf(stderr, "%s\n", str);
|
2004-03-18 11:56:59 +08:00
|
|
|
exit(1);
|
|
|
|
}
|