30#define FSYNC_FILENAME "./pg_test_fsync.out"
32#define XLOG_BLCKSZ_K (XLOG_BLCKSZ / 1024)
34#define LABEL_FORMAT " %-30s"
35#define NA_FORMAT "%21s\n"
37#define OPS_FORMAT gettext_noop("%13.3f ops/sec %6.0f usecs/op\n")
38#define USECS_SEC 1000000
44 alarm_triggered = false; \
45 alarm(secs_per_test); \
46 gettimeofday(&start_t, NULL); \
52 alarm_triggered = false; \
53 if (CreateThread(NULL, 0, process_alarm, NULL, 0, NULL) == \
54 INVALID_HANDLE_VALUE) \
55 pg_fatal("could not create thread for alarm"); \
56 gettimeofday(&start_t, NULL); \
62 gettimeofday(&stop_t, NULL); \
63 print_elapse(start_t, stop_t, ops); \
73static struct timeval start_t,
94#ifdef HAVE_FSYNC_WRITETHROUGH
99#define die(msg) pg_fatal("%s: %m", _(msg))
147 static struct option long_options[] = {
155 unsigned long optval;
160 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
165 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
167 puts(
"pg_test_fsync (PostgreSQL) " PG_VERSION);
173 long_options, &optindex)) != -1)
183 optval = strtoul(
optarg, &endptr, 10);
185 if (endptr ==
optarg || *endptr !=
'\0' ||
186 errno != 0 || optval != (
unsigned int) optval)
188 pg_log_error(
"invalid argument for option %s",
"--secs-per-test");
195 pg_fatal(
"%s must be in range %u..%u",
196 "--secs-per-test", 1, UINT_MAX);
208 pg_log_error(
"too many command-line arguments (first is \"%s\")",
215 "%u seconds per test\n",
219 printf(
_(
"O_DIRECT supported on this platform for open_datasync and open_sync.\n"));
220#elif defined(F_NOCACHE)
221 printf(
_(
"F_NOCACHE supported on this platform for open_datasync and open_sync.\n"));
223 printf(
_(
"Direct I/O is not supported on this platform.\n"));
246 die(
"could not open output file");
253 if (
fsync(tmpfile) != 0)
268 fd = open(path, flags,
mode);
270#if !defined(O_DIRECT) && defined(F_NOCACHE)
271 if (
fd >= 0 && fcntl(
fd, F_NOCACHE, 1) < 0)
273 int save_errno = errno;
290 bool fs_warning =
false;
292 if (writes_per_op == 1)
296 printf(
_(
"(in \"wal_sync_method\" preference order, except fdatasync is Linux's default)\n"));
315 for (writes = 0; writes < writes_per_op; writes++)
319 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
336 die(
"could not open output file");
340 for (writes = 0; writes < writes_per_op; writes++)
344 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
358 die(
"could not open output file");
362 for (writes = 0; writes < writes_per_op; writes++)
366 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
368 if (
fsync(tmpfile) != 0)
380#ifdef HAVE_FSYNC_WRITETHROUGH
382 die(
"could not open output file");
386 for (writes = 0; writes < writes_per_op; writes++)
390 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
418 for (writes = 0; writes < writes_per_op; writes++)
422 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
441 printf(
_(
"* This file system and its mount options do not support direct\n"
442 " I/O, e.g. ext4 in journaled mode.\n"));
449 printf(
_(
"\nCompare open_sync with different write sizes:\n"));
450 printf(
_(
"(This is designed to compare the cost of writing 16kB in different write\n"
451 "open_sync sizes.)\n"));
483 for (writes = 0; writes < 16 / writes_size; writes++)
487 writes * writes_size * 1024) !=
511 printf(
_(
"\nTest if fsync on non-write file descriptor is honored:\n"));
512 printf(
_(
"(If the times are similar, fsync() can sync data written on a different\n"
526 die(
"could not open output file");
527 if (
write(tmpfile,
buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
529 if (
fsync(tmpfile) != 0)
538 die(
"could not open output file");
554 die(
"could not open output file");
555 if (
write(tmpfile,
buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
560 die(
"could not open output file");
561 if (
fsync(tmpfile) != 0)
582 die(
"could not open output file");
586 if (
pg_pwrite(tmpfile,
buf, XLOG_BLCKSZ, 0) != XLOG_BLCKSZ)
607#ifdef HAVE_FSYNC_WRITETHROUGH
612#if defined(F_FULLFSYNC)
613 return (fcntl(
fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
627 double total_time = (
stop_t.tv_sec - start_t.tv_sec) +
628 (
stop_t.tv_usec - start_t.tv_usec) * 0.000001;
629 double per_second = ops / total_time;
630 double avg_op_time_us = (total_time / ops) *
USECS_SEC;
#define ngettext(s, p, n)
struct PGAlignedXLogBlock PGAlignedXLogBlock
#define PG_TEXTDOMAIN(domain)
int fdatasync(int fildes)
void set_pglocale_pgservice(const char *argv0, const char *app)
int pg_fsync_writethrough(int fd)
char * pg_strdup(const char *in)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
static PgChecksumMode mode
#define DEFAULT_XLOG_SEG_SIZE
PGDLLIMPORT char * optarg
int32 pg_prng_int32(pg_prng_state *state)
void pg_prng_seed(pg_prng_state *state, uint64 seed)
pg_prng_state pg_global_prng_state
static void test_file_descriptor_sync(void)
int main(int argc, char *argv[])
static void prepare_buf(void)
static void print_elapse(struct timeval start_t, struct timeval stop_t, int ops)
static void test_open_syncs(void)
static void handle_args(int argc, char *argv[])
static int open_direct(const char *path, int flags, mode_t mode)
static char buf[DEFAULT_XLOG_SEG_SIZE]
static void test_sync(int writes_per_op)
static void test_open(void)
static void test_non_sync(void)
static struct timeval start_t stop_t
static void test_open_sync(const char *msg, int writes_size)
static const char * progname
static void process_alarm(SIGNAL_ARGS)
static sig_atomic_t alarm_triggered
static unsigned int secs_per_test
static void signal_cleanup(SIGNAL_ARGS)
const char * get_progname(const char *argv0)
static int fd(const char *x, int i)