31 #define FSYNC_FILENAME "./pg_test_fsync.out"
33 #define XLOG_BLCKSZ_K (XLOG_BLCKSZ / 1024)
35 #define LABEL_FORMAT " %-30s"
36 #define NA_FORMAT "%21s\n"
38 #define OPS_FORMAT gettext_noop("%13.3f ops/sec %6.0f usecs/op\n")
39 #define USECS_SEC 1000000
45 alarm_triggered = false; \
46 alarm(secs_per_test); \
47 gettimeofday(&start_t, NULL); \
53 alarm_triggered = false; \
54 if (CreateThread(NULL, 0, process_alarm, NULL, 0, NULL) == \
55 INVALID_HANDLE_VALUE) \
56 pg_fatal("could not create thread for alarm"); \
57 gettimeofday(&start_t, NULL); \
63 gettimeofday(&stop_t, NULL); \
64 print_elapse(start_t, stop_t, ops); \
75 static struct timeval start_t,
96 #ifdef HAVE_FSYNC_WRITETHROUGH
101 #define die(msg) pg_fatal("%s: %m", _(msg))
150 static struct option long_options[] = {
158 unsigned long optval;
163 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
168 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
170 puts(
"pg_test_fsync (PostgreSQL) " PG_VERSION);
176 long_options, &optindex)) != -1)
186 optval = strtoul(
optarg, &endptr, 10);
188 if (endptr ==
optarg || *endptr !=
'\0' ||
189 errno != 0 || optval != (
unsigned int) optval)
191 pg_log_error(
"invalid argument for option %s",
"--secs-per-test");
198 pg_fatal(
"%s must be in range %u..%u",
199 "--secs-per-test", 1, UINT_MAX);
211 pg_log_error(
"too many command-line arguments (first is \"%s\")",
218 "%u seconds per test\n",
221 #if defined(O_DIRECT)
222 printf(
_(
"O_DIRECT supported on this platform for open_datasync and open_sync.\n"));
223 #elif defined(F_NOCACHE)
224 printf(
_(
"F_NOCACHE supported on this platform for open_datasync and open_sync.\n"));
226 printf(
_(
"Direct I/O is not supported on this platform.\n"));
251 die(
"could not open output file");
258 if (
fsync(tmpfile) != 0)
273 fd = open(path, flags,
mode);
275 #if !defined(O_DIRECT) && defined(F_NOCACHE)
276 if (
fd >= 0 && fcntl(
fd, F_NOCACHE, 1) < 0)
278 int save_errno = errno;
295 bool fs_warning =
false;
297 if (writes_per_op == 1)
301 printf(
_(
"(in \"wal_sync_method\" preference order, except fdatasync is Linux's default)\n"));
320 for (writes = 0; writes < writes_per_op; writes++)
324 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
341 die(
"could not open output file");
345 for (writes = 0; writes < writes_per_op; writes++)
349 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
363 die(
"could not open output file");
367 for (writes = 0; writes < writes_per_op; writes++)
371 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
373 if (
fsync(tmpfile) != 0)
385 #ifdef HAVE_FSYNC_WRITETHROUGH
387 die(
"could not open output file");
391 for (writes = 0; writes < writes_per_op; writes++)
395 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
423 for (writes = 0; writes < writes_per_op; writes++)
427 writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
446 printf(
_(
"* This file system and its mount options do not support direct\n"
447 " I/O, e.g. ext4 in journaled mode.\n"));
454 printf(
_(
"\nCompare open_sync with different write sizes:\n"));
455 printf(
_(
"(This is designed to compare the cost of writing 16kB in different write\n"
456 "open_sync sizes.)\n"));
488 for (writes = 0; writes < 16 / writes_size; writes++)
492 writes * writes_size * 1024) !=
516 printf(
_(
"\nTest if fsync on non-write file descriptor is honored:\n"));
517 printf(
_(
"(If the times are similar, fsync() can sync data written on a different\n"
531 die(
"could not open output file");
532 if (
write(tmpfile,
buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
534 if (
fsync(tmpfile) != 0)
543 die(
"could not open output file");
559 die(
"could not open output file");
560 if (
write(tmpfile,
buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
565 die(
"could not open output file");
566 if (
fsync(tmpfile) != 0)
587 die(
"could not open output file");
591 if (
pg_pwrite(tmpfile,
buf, XLOG_BLCKSZ, 0) != XLOG_BLCKSZ)
612 #ifdef HAVE_FSYNC_WRITETHROUGH
617 #if defined(F_FULLFSYNC)
618 return (fcntl(
fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
632 double total_time = (
stop_t.tv_sec - start_t.tv_sec) +
633 (
stop_t.tv_usec - start_t.tv_usec) * 0.000001;
634 double per_second = ops / total_time;
635 double avg_op_time_us = (total_time / ops) *
USECS_SEC;
#define TYPEALIGN(ALIGNVAL, LEN)
#define ngettext(s, p, n)
#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
static void const char fflush(stdout)
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 char full_buf[DEFAULT_XLOG_SEG_SIZE]
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 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)
pqsigfunc pqsignal(int signo, pqsigfunc func)
static int fd(const char *x, int i)