#define NA_FORMAT "%18s"
#define OPS_FORMAT "%9.3f ops/sec"
+/* These are macros to avoid timing the function call overhead. */
+#define START_TIMER \
+do { \
+ alarm_triggered = false; \
+ alarm(secs_per_test); \
+ gettimeofday(&start_t, NULL); \
+} while (0)
+
+#define STOP_TIMER \
+do { \
+ gettimeofday(&stop_t, NULL); \
+ print_elapse(start_t, stop_t, ops); \
+} while (0)
+
+
static const char *progname;
-static int ops_per_test = 2000;
+static int secs_per_test = 2;
static int needs_unlink = 0;
static char full_buf[XLOG_SEG_SIZE],
*buf,
*filename = FSYNC_FILENAME;
static struct timeval start_t,
stop_t;
+static bool alarm_triggered = false;
static void handle_args(int argc, char *argv[]);
static void test_open_syncs(void);
static void test_open_sync(const char *msg, int writes_size);
static void test_file_descriptor_sync(void);
+static void process_alarm(int sig);
static void signal_cleanup(int sig);
#ifdef HAVE_FSYNC_WRITETHROUGH
static int pg_fsync_writethrough(int fd);
#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, int ops);
static void die(const char *str);
/* Prevent leaving behind the test file */
signal(SIGINT, signal_cleanup);
signal(SIGTERM, signal_cleanup);
+ signal(SIGALRM, process_alarm);
#ifdef SIGHUP
/* Not defined on win32 */
signal(SIGHUP, signal_cleanup);
{
static struct option long_options[] = {
{"filename", required_argument, NULL, 'f'},
- {"ops-per-test", required_argument, NULL, 'o'},
+ {"secs-per-test", required_argument, NULL, 's'},
{NULL, 0, NULL, 0}
};
int option; /* Command line option */
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "-?") == 0)
{
- printf("Usage: %s [-f FILENAME] [-o OPS-PER-TEST]\n", progname);
+ printf("Usage: %s [-f FILENAME] [-s SECS-PER-TEST]\n", progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
}
}
- while ((option = getopt_long(argc, argv, "f:o:",
+ while ((option = getopt_long(argc, argv, "f:s:",
long_options, &optindex)) != -1)
{
switch (option)
filename = strdup(optarg);
break;
- case 'o':
- ops_per_test = atoi(optarg);
+ case 's':
+ secs_per_test = atoi(optarg);
break;
default:
exit(1);
}
- printf("%d operations per test\n", ops_per_test);
+ printf("%d seconds per test\n", secs_per_test);
#if PG_O_DIRECT != 0
printf("O_DIRECT supported on this platform for open_datasync and open_sync.\n");
#else
{
if ((tmpfile = open(filename, O_RDWR | O_DSYNC | PG_O_DIRECT, 0)) == -1)
die("could not open output file");
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
if (lseek(tmpfile, 0, SEEK_SET) == -1)
die("seek failed");
}
- gettimeofday(&stop_t, NULL);
+ STOP_TIMER;
close(tmpfile);
- print_elapse(start_t, stop_t);
}
#else
printf(NA_FORMAT, "n/a\n");
#ifdef HAVE_FDATASYNC
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("could not open output file");
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
if (lseek(tmpfile, 0, SEEK_SET) == -1)
die("seek failed");
}
- gettimeofday(&stop_t, NULL);
+ STOP_TIMER;
close(tmpfile);
- print_elapse(start_t, stop_t);
#else
printf(NA_FORMAT, "n/a\n");
#endif
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("could not open output file");
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
if (lseek(tmpfile, 0, SEEK_SET) == -1)
die("seek failed");
}
- gettimeofday(&stop_t, NULL);
+ STOP_TIMER;
close(tmpfile);
- print_elapse(start_t, stop_t);
/*
* If fsync_writethrough is available, test as well
#ifdef HAVE_FSYNC_WRITETHROUGH
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("could not open output file");
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
if (lseek(tmpfile, 0, SEEK_SET) == -1)
die("seek failed");
}
- gettimeofday(&stop_t, NULL);
+ STOP_TIMER;
close(tmpfile);
- print_elapse(start_t, stop_t);
#else
printf(NA_FORMAT, "n/a\n");
#endif
}
else
{
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
for (writes = 0; writes < writes_per_op; writes++)
if (write(tmpfile, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
if (lseek(tmpfile, 0, SEEK_SET) == -1)
die("seek failed");
}
- gettimeofday(&stop_t, NULL);
+ STOP_TIMER;
close(tmpfile);
- print_elapse(start_t, stop_t);
}
#else
printf(NA_FORMAT, "n/a\n");
printf(NA_FORMAT, "n/a*\n");
else
{
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
for (writes = 0; writes < 16 / writes_size; writes++)
if (write(tmpfile, buf, writes_size * 1024) !=
if (lseek(tmpfile, 0, SEEK_SET) == -1)
die("seek failed");
}
- gettimeofday(&stop_t, NULL);
+ STOP_TIMER;
close(tmpfile);
- print_elapse(start_t, stop_t);
}
#else
printf(NA_FORMAT, "n/a\n");
printf(LABEL_FORMAT, "write, fsync, close");
fflush(stdout);
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("could not open output file");
die("could not open output file");
close(tmpfile);
}
- gettimeofday(&stop_t, NULL);
- print_elapse(start_t, stop_t);
+ STOP_TIMER;
/*
* Now open, write, close, open again and fsync This simulates processes
printf(LABEL_FORMAT, "write, close, fsync");
fflush(stdout);
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("could not open output file");
die("fsync failed");
close(tmpfile);
}
- gettimeofday(&stop_t, NULL);
- print_elapse(start_t, stop_t);
-
+ STOP_TIMER;
}
static void
printf(LABEL_FORMAT, "write");
fflush(stdout);
- gettimeofday(&start_t, NULL);
- for (ops = 0; ops < ops_per_test; ops++)
+ START_TIMER;
+ for (ops = 0; alarm_triggered == false; ops++)
{
if ((tmpfile = open(filename, O_RDWR, 0)) == -1)
die("could not open output file");
die("write failed");
close(tmpfile);
}
- gettimeofday(&stop_t, NULL);
- print_elapse(start_t, stop_t);
+ STOP_TIMER;
}
static void
* print out the writes per second for tests
*/
static void
-print_elapse(struct timeval start_t, struct timeval stop_t)
+print_elapse(struct timeval start_t, struct timeval stop_t, int ops)
{
double total_time = (stop_t.tv_sec - start_t.tv_sec) +
(stop_t.tv_usec - start_t.tv_usec) * 0.000001;
- double per_second = ops_per_test / total_time;
+ double per_second = ops / total_time;
printf(OPS_FORMAT "\n", per_second);
}
+static void
+process_alarm(int sig)
+{
+ alarm_triggered = true;
+}
+
static void
die(const char *str)
{