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
This commit is contained in:
Tom Lane 2017-01-01 15:17:08 -05:00
parent 257d815720
commit 74baa1e3b8
1 changed files with 11 additions and 15 deletions

View File

@ -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);