From 74baa1e3b89c1651ade1afeffc715cac24041e2f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 1 Jan 2017 15:17:08 -0500 Subject: [PATCH] Avoid assuming that instr_time == struct timeval in pgbench logging. This code was presuming undue familiarity with the contents of the instr_time struct. That was already broken on Windows, and it's about to get broken on most other platforms as well. The simplest solution that preserves the current output definition is to just do our own gettimeofday() call here. Realistically, the extra cost is probably negligible in comparison to everything else that's going on in a pgbench transaction, so it's not worth sweating over. On Windows, the precision delivered by gettimeofday() is lower than one could wish, but this is still a big improvement over printing zeroes, as the code did before. Discussion: https://postgr.es/m/8837.1483216839@sss.pgh.pa.us --- src/bin/pgbench/pgbench.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index fbb0c2bda4..1f05650c66 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -2483,27 +2483,23 @@ doLog(TState *thread, CState *st, instr_time *now, else { /* no, print raw transactions */ -#ifndef WIN32 + struct timeval tv; - /* This is more than we really ought to know about instr_time */ + /* + * We print the current system timestamp in the log, so that entries + * can be correlated against other logs. On some platforms this is + * available in *now, but rather than get entangled with that, we just + * eat the cost of an extra syscall in all cases. + */ + gettimeofday(&tv, NULL); if (skipped) fprintf(logfile, "%d " INT64_FORMAT " skipped %d %ld %ld", st->id, st->cnt, st->use_file, - (long) now->tv_sec, (long) now->tv_usec); + (long) tv.tv_sec, (long) tv.tv_usec); else fprintf(logfile, "%d " INT64_FORMAT " %.0f %d %ld %ld", st->id, st->cnt, latency, st->use_file, - (long) now->tv_sec, (long) now->tv_usec); -#else - - /* On Windows, instr_time doesn't provide a timestamp anyway */ - if (skipped) - fprintf(logfile, "%d " INT64_FORMAT " skipped %d 0 0", - st->id, st->cnt, st->use_file); - else - fprintf(logfile, "%d " INT64_FORMAT " %.0f %d 0 0", - st->id, st->cnt, latency, st->use_file); -#endif + (long) tv.tv_sec, (long) tv.tv_usec); if (throttle_delay) fprintf(logfile, " %.0f", lag); fputc('\n', logfile); @@ -4402,7 +4398,7 @@ threadRun(void *arg) if (use_log) { char logpath[MAXPGPATH]; - char *prefix = logfile_prefix ? logfile_prefix : "pgbench_log"; + char *prefix = logfile_prefix ? logfile_prefix : "pgbench_log"; if (thread->tid == 0) snprintf(logpath, sizeof(logpath), "%s.%d", prefix, main_pid);