Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3bc2538

Browse files
committed
Move the "instr_time" typedef and associated macros into a new header
file portability/instr_time.h, and add a couple more macros to eliminate some abstraction leakage we formerly had. Also update psql to use this header instead of its own copy of nearly the same code. This commit in itself is just code cleanup and shouldn't change anything. It lays some groundwork for the upcoming function-stats patch, though.
1 parent 719a115 commit 3bc2538

File tree

8 files changed

+178
-131
lines changed

8 files changed

+178
-131
lines changed

src/backend/commands/explain.c

+2-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.174 2008/05/12 20:01:59 alvherre Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.175 2008/05/14 19:10:29 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -390,19 +390,7 @@ elapsed_time(instr_time *starttime)
390390
instr_time endtime;
391391

392392
INSTR_TIME_SET_CURRENT(endtime);
393-
394-
#ifndef WIN32
395-
endtime.tv_sec -= starttime->tv_sec;
396-
endtime.tv_usec -= starttime->tv_usec;
397-
while (endtime.tv_usec < 0)
398-
{
399-
endtime.tv_usec += 1000000;
400-
endtime.tv_sec--;
401-
}
402-
#else /* WIN32 */
403-
endtime.QuadPart -= starttime->QuadPart;
404-
#endif
405-
393+
INSTR_TIME_SUBTRACT(endtime, *starttime);
406394
return INSTR_TIME_GET_DOUBLE(endtime);
407395
}
408396

src/backend/executor/instrument.c

+2-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/executor/instrument.c,v 1.20 2008/01/01 19:45:49 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/executor/instrument.c,v 1.21 2008/05/14 19:10:29 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -55,25 +55,7 @@ InstrStopNode(Instrumentation *instr, double nTuples)
5555
}
5656

5757
INSTR_TIME_SET_CURRENT(endtime);
58-
59-
#ifndef WIN32
60-
instr->counter.tv_sec += endtime.tv_sec - instr->starttime.tv_sec;
61-
instr->counter.tv_usec += endtime.tv_usec - instr->starttime.tv_usec;
62-
63-
/* Normalize after each add to avoid overflow/underflow of tv_usec */
64-
while (instr->counter.tv_usec < 0)
65-
{
66-
instr->counter.tv_usec += 1000000;
67-
instr->counter.tv_sec--;
68-
}
69-
while (instr->counter.tv_usec >= 1000000)
70-
{
71-
instr->counter.tv_usec -= 1000000;
72-
instr->counter.tv_sec++;
73-
}
74-
#else /* WIN32 */
75-
instr->counter.QuadPart += (endtime.QuadPart - instr->starttime.QuadPart);
76-
#endif
58+
INSTR_TIME_ACCUM_DIFF(instr->counter, endtime, instr->starttime);
7759

7860
INSTR_TIME_SET_ZERO(instr->starttime);
7961

src/bin/psql/command.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.188 2008/05/08 17:04:26 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.189 2008/05/14 19:10:29 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -30,6 +30,8 @@
3030
#include <sys/stat.h> /* for stat() */
3131
#endif
3232

33+
#include "portability/instr_time.h"
34+
3335
#include "libpq-fe.h"
3436
#include "pqexpbuffer.h"
3537
#include "dumputils.h"
@@ -282,24 +284,22 @@ exec_command(const char *cmd,
282284
else if (pg_strcasecmp(cmd, "copy") == 0)
283285
{
284286
/* Default fetch-it-all-and-print mode */
285-
TimevalStruct before,
287+
instr_time before,
286288
after;
287-
double elapsed_msec = 0;
288289

289290
char *opt = psql_scan_slash_option(scan_state,
290291
OT_WHOLE_LINE, NULL, false);
291292

292293
if (pset.timing)
293-
GETTIMEOFDAY(&before);
294+
INSTR_TIME_SET_CURRENT(before);
294295

295296
success = do_copy(opt);
296297

297298
if (pset.timing && success)
298299
{
299-
GETTIMEOFDAY(&after);
300-
elapsed_msec = DIFF_MSEC(&after, &before);
301-
printf(_("Time: %.3f ms\n"), elapsed_msec);
302-
300+
INSTR_TIME_SET_CURRENT(after);
301+
INSTR_TIME_SUBTRACT(after, before);
302+
printf(_("Time: %.3f ms\n"), INSTR_TIME_GET_MILLISEC(after));
303303
}
304304

305305
free(opt);

src/bin/psql/common.c

+21-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.138 2008/01/01 19:45:55 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.139 2008/05/14 19:10:29 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -17,6 +17,8 @@
1717
#include <win32.h>
1818
#endif
1919

20+
#include "portability/instr_time.h"
21+
2022
#include "pqsignal.h"
2123

2224
#include "settings.h"
@@ -844,11 +846,11 @@ SendQuery(const char *query)
844846
if (pset.fetch_count <= 0 || !is_select_command(query))
845847
{
846848
/* Default fetch-it-all-and-print mode */
847-
TimevalStruct before,
849+
instr_time before,
848850
after;
849851

850852
if (pset.timing)
851-
GETTIMEOFDAY(&before);
853+
INSTR_TIME_SET_CURRENT(before);
852854

853855
results = PQexec(pset.db, query);
854856

@@ -858,8 +860,9 @@ SendQuery(const char *query)
858860

859861
if (pset.timing)
860862
{
861-
GETTIMEOFDAY(&after);
862-
elapsed_msec = DIFF_MSEC(&after, &before);
863+
INSTR_TIME_SET_CURRENT(after);
864+
INSTR_TIME_SUBTRACT(after, before);
865+
elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
863866
}
864867

865868
/* but printing results isn't: */
@@ -961,7 +964,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
961964
bool did_pager = false;
962965
int ntuples;
963966
char fetch_cmd[64];
964-
TimevalStruct before,
967+
instr_time before,
965968
after;
966969

967970
*elapsed_msec = 0;
@@ -972,7 +975,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
972975
my_popt.topt.prior_records = 0;
973976

974977
if (pset.timing)
975-
GETTIMEOFDAY(&before);
978+
INSTR_TIME_SET_CURRENT(before);
976979

977980
/* if we're not in a transaction, start one */
978981
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1001,8 +1004,9 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
10011004

10021005
if (pset.timing)
10031006
{
1004-
GETTIMEOFDAY(&after);
1005-
*elapsed_msec += DIFF_MSEC(&after, &before);
1007+
INSTR_TIME_SET_CURRENT(after);
1008+
INSTR_TIME_SUBTRACT(after, before);
1009+
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
10061010
}
10071011

10081012
snprintf(fetch_cmd, sizeof(fetch_cmd),
@@ -1028,15 +1032,16 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
10281032
for (;;)
10291033
{
10301034
if (pset.timing)
1031-
GETTIMEOFDAY(&before);
1035+
INSTR_TIME_SET_CURRENT(before);
10321036

10331037
/* get FETCH_COUNT tuples at a time */
10341038
results = PQexec(pset.db, fetch_cmd);
10351039

10361040
if (pset.timing)
10371041
{
1038-
GETTIMEOFDAY(&after);
1039-
*elapsed_msec += DIFF_MSEC(&after, &before);
1042+
INSTR_TIME_SET_CURRENT(after);
1043+
INSTR_TIME_SUBTRACT(after, before);
1044+
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
10401045
}
10411046

10421047
if (PQresultStatus(results) != PGRES_TUPLES_OK)
@@ -1112,7 +1117,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
11121117

11131118
cleanup:
11141119
if (pset.timing)
1115-
GETTIMEOFDAY(&before);
1120+
INSTR_TIME_SET_CURRENT(before);
11161121

11171122
/*
11181123
* We try to close the cursor on either success or failure, but on failure
@@ -1137,8 +1142,9 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
11371142

11381143
if (pset.timing)
11391144
{
1140-
GETTIMEOFDAY(&after);
1141-
*elapsed_msec += DIFF_MSEC(&after, &before);
1145+
INSTR_TIME_SET_CURRENT(after);
1146+
INSTR_TIME_SUBTRACT(after, before);
1147+
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
11421148
}
11431149

11441150
return OK;

src/bin/psql/common.h

+1-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.56 2008/01/01 19:45:55 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.57 2008/05/14 19:10:29 tgl Exp $
77
*/
88
#ifndef COMMON_H
99
#define COMMON_H
@@ -63,36 +63,4 @@ extern const char *session_username(void);
6363

6464
extern char *expand_tilde(char **filename);
6565

66-
#ifndef WIN32
67-
68-
#include <sys/time.h>
69-
70-
typedef struct timeval TimevalStruct;
71-
72-
#define GETTIMEOFDAY(T) gettimeofday(T, NULL)
73-
#define DIFF_MSEC(T, U) \
74-
((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 + \
75-
((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0)
76-
#else
77-
/*
78-
* To get good resolution (better than ~15ms) on Windows, use
79-
* the high resolution performance counters. They can't be used
80-
* to get absolute times, but are good for measuring differences.
81-
*/
82-
static __inline__ double
83-
GetTimerFrequency(void)
84-
{
85-
LARGE_INTEGER f;
86-
87-
QueryPerformanceFrequency(&f);
88-
return (double) f.QuadPart;
89-
}
90-
91-
typedef LARGE_INTEGER TimevalStruct;
92-
93-
#define GETTIMEOFDAY(T) QueryPerformanceCounter((T))
94-
#define DIFF_MSEC(T, U) \
95-
(((T)->QuadPart - (U)->QuadPart) * 1000.0 / GetTimerFrequency())
96-
#endif /* WIN32 */
97-
9866
#endif /* COMMON_H */

src/include/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# 'make install' installs whole contents of src/include.
66
#
7-
# $PostgreSQL: pgsql/src/include/Makefile,v 1.24 2008/03/17 19:44:41 petere Exp $
7+
# $PostgreSQL: pgsql/src/include/Makefile,v 1.25 2008/05/14 19:10:29 tgl Exp $
88
#
99
#-------------------------------------------------------------------------
1010

@@ -21,7 +21,8 @@ SUBDIRS = access bootstrap catalog commands executor lib libpq mb \
2121
nodes optimizer parser postmaster regex rewrite storage tcop \
2222
snowball snowball/libstemmer tsearch tsearch/dicts utils \
2323
port port/win32 port/win32_msvc port/win32_msvc/sys \
24-
port/win32/arpa port/win32/netinet port/win32/sys
24+
port/win32/arpa port/win32/netinet port/win32/sys \
25+
portability
2526

2627
# Install all headers
2728
install: all installdirs

src/include/executor/instrument.h

+2-39
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,14 @@
66
*
77
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
88
*
9-
* $PostgreSQL: pgsql/src/include/executor/instrument.h,v 1.18 2008/01/01 19:45:57 momjian Exp $
9+
* $PostgreSQL: pgsql/src/include/executor/instrument.h,v 1.19 2008/05/14 19:10:29 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
1313
#ifndef INSTRUMENT_H
1414
#define INSTRUMENT_H
1515

16-
#include <sys/time.h>
17-
18-
19-
/*
20-
* gettimeofday() does not have sufficient resolution on Windows,
21-
* so we must use QueryPerformanceCounter() instead. These macros
22-
* also give some breathing room to use other high-precision-timing APIs
23-
* on yet other platforms. (The macro-ization is not complete, however;
24-
* see subtraction code in instrument.c and explain.c.)
25-
*/
26-
#ifndef WIN32
27-
28-
typedef struct timeval instr_time;
29-
30-
#define INSTR_TIME_IS_ZERO(t) ((t).tv_sec == 0 && (t).tv_usec == 0)
31-
#define INSTR_TIME_SET_ZERO(t) ((t).tv_sec = 0, (t).tv_usec = 0)
32-
#define INSTR_TIME_SET_CURRENT(t) gettimeofday(&(t), NULL)
33-
#define INSTR_TIME_GET_DOUBLE(t) \
34-
(((double) (t).tv_sec) + ((double) (t).tv_usec) / 1000000.0)
35-
#else /* WIN32 */
36-
37-
typedef LARGE_INTEGER instr_time;
38-
39-
#define INSTR_TIME_IS_ZERO(t) ((t).QuadPart == 0)
40-
#define INSTR_TIME_SET_ZERO(t) ((t).QuadPart = 0)
41-
#define INSTR_TIME_SET_CURRENT(t) QueryPerformanceCounter(&(t))
42-
#define INSTR_TIME_GET_DOUBLE(t) \
43-
(((double) (t).QuadPart) / GetTimerFrequency())
44-
45-
static __inline__ double
46-
GetTimerFrequency(void)
47-
{
48-
LARGE_INTEGER f;
49-
50-
QueryPerformanceFrequency(&f);
51-
return (double) f.QuadPart;
52-
}
53-
#endif /* WIN32 */
16+
#include "portability/instr_time.h"
5417

5518

5619
typedef struct Instrumentation

0 commit comments

Comments
 (0)