agora inbox for [email protected]
help / color / mirror / Atom feedCFH: Mariposa, distributed DB
275+ messages / 6 participants
[nested] [flat]
* CFH: Mariposa, distributed DB
@ 2000-02-07 21:11 Ross J. Reedstrom <[email protected]>
0 siblings, 2 replies; 275+ messages in thread
From: Ross J. Reedstrom @ 2000-02-07 21:11 UTC (permalink / raw)
To: pgsql-hackers
This is a Call For Hackers:
Some time ago, I floated a little discussion on this list about doing
some distributed database work with PostgreSQL. The project got back
burnered at work, but now has a timeline for needing a solution "this
summer." Recent discussions on this list about Postgres's historical
object roots got me back to the Berkeley db sites, and reminded me about
Mariposa, which is Stonebraker's take on distributed DBs.
http://s2k-ftp.cs.berkeley.edu:8000:8000/mariposa/
StoneBraker has gone on to commercialize Mariposa as Cohera, which seems
to be one of those Enterprise Scale products where if you need to ask
how much a license costs, you can't afford it ;-)
Sounds like now would be a good time to re-visit Mariposa, and see what
good ideas can be folded over into PostgreSQL. Mariposa was funded by
ARPA and ARO, and was used by NASA as the database part of the Sequoia
Project, which became Big Sur, looking to unify the various kinds of
geophysical data collected by earth observing missions.
The code is an offshoot of Postgres95, with lots of nasty '#ifdef P95's
scattered around. The split predates lots of good work by the PostgreSQL
team to clean up years of academic cruft that had accumulated, so merging
is not trivial.
Anyway, anyone interested in taking a look at this with me? I think the
place to start (i.e., where I'm starting) is to get the June-1996 alpha
release of Mariposa to compile on a current system (I'm running Linux
myself.) I've been doing a compare-and-contrast, staring at source code,
but I think I need a running system to decide how the parts fit together.
Then, plan what features to 'fold' into pgsql, and run a proposal past
this list, some time later in the 7.x series, perhaps in a couple of
months (you guys will probably be on 8.x by then!) Hopefully, not take-up
too much of the core developers time until we're talking integration.
Anyone else interested, I'm using the tarball from:
ftp://epoch.cs.berkeley.edu/pub/mariposa/src/alpha-1/mariposa-alpha-1.tar.gz
If this really takes off, I can host CVS of the mariposa and pgsql
sources, as well as web pages, mailing list, whatever. If it's just a
couple of us (or me all by myself ;-) we'll keep it simple.
Ross
--
Ross J. Reedstrom, Ph.D., <[email protected]>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 21:23 Bruce Momjian <[email protected]>
parent: Ross J. Reedstrom <[email protected]>
1 sibling, 3 replies; 275+ messages in thread
From: Bruce Momjian @ 2000-02-07 21:23 UTC (permalink / raw)
To: Ross J. Reedstrom <[email protected]>; +Cc: pgsql-hackers
> This is a Call For Hackers:
>
> Some time ago, I floated a little discussion on this list about doing
> some distributed database work with PostgreSQL. The project got back
> burnered at work, but now has a timeline for needing a solution "this
> summer." Recent discussions on this list about Postgres's historical
> object roots got me back to the Berkeley db sites, and reminded me about
> Mariposa, which is Stonebraker's take on distributed DBs.
>
> http://s2k-ftp.cs.berkeley.edu:8000:8000/mariposa/
>
I have looked at the code. I have files that show all the diffs they
made to it and they have some new files. It was hard for me to see what
they were doing. Looks like they hacked up the executor and put in some
translation layer to talk to some databroker. It seems like an awfully
complicated way to do it. I would not bother getting it to run, but
figure out what they were trying to do, and why, and see how we can
implement it. My guess is that they had one central server for each
table, and you went to that server to get information.
--
Bruce Momjian | http://www.op.net/~candle
[email protected] | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 21:44 Hannu Krosing <[email protected]>
parent: Ross J. Reedstrom <[email protected]>
1 sibling, 1 reply; 275+ messages in thread
From: Hannu Krosing @ 2000-02-07 21:44 UTC (permalink / raw)
To: Ross J. Reedstrom <[email protected]>; +Cc: pgsql-hackers
"Ross J. Reedstrom" wrote:
>
>
> Anyone else interested, I'm using the tarball from:
>
> ftp://epoch.cs.berkeley.edu/pub/mariposa/src/alpha-1/mariposa-alpha-1.tar.gz
>
Is mariposa licence compatible with ours ?
------------------
Hannu
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 21:50 Ross J. Reedstrom <[email protected]>
parent: Bruce Momjian <[email protected]>
2 siblings, 0 replies; 275+ messages in thread
From: Ross J. Reedstrom @ 2000-02-07 21:50 UTC (permalink / raw)
To: Bruce Momjian <[email protected]>; +Cc: pgsql-hackers
On Mon, Feb 07, 2000 at 04:23:06PM -0500, Bruce Momjian wrote:
> > This is a Call For Hackers:
> >
> > Some time ago, I floated a little discussion on this list about doing
> > some distributed database work with PostgreSQL. The project got back
> > burnered at work, but now has a timeline for needing a solution "this
> > summer." Recent discussions on this list about Postgres's historical
> > object roots got me back to the Berkeley db sites, and reminded me about
> > Mariposa, which is Stonebraker's take on distributed DBs.
> >
> > http://s2k-ftp.cs.berkeley.edu:8000:8000/mariposa/
> >
>
> I have looked at the code. I have files that show all the diffs they
> made to it and they have some new files. It was hard for me to see what
> they were doing. Looks like they hacked up the executor and put in some
> translation layer to talk to some databroker. It seems like an awfully
> complicated way to do it. I would not bother getting it to run, but
> figure out what they were trying to do, and why, and see how we can
> implement it. My guess is that they had one central server for each
> table, and you went to that server to get information.
>
Actually, this being an academic project, there's lots of design
documents about how it's _supposed_ to work. Stonebraker calls in an
'agoric' distributed database, as in agora, market. The various db
servers offer tables (or even specific views on tables) 'for sale', and
bid against/with each other to provide the data to clients requesting
it. The idea behind it is to us a micro-economic market model to do
your distributed optimizations for you, rather than have the DBAs decide
what tables go where, what tables need to be shadowed, etc. The win is
supposedly massive scaleability: they Cohera site talks about 10000s
of servers.
As I said, I've been doing the compare existing source code thing,
but thought working code might be more revealing, and give my project
manager something to see progress on ;-) Your right, though, that the
most productive way to go, in the long run, might be to reimplement what
they've described, in the current pgsql tree, using the Mariposa source
as an example implementation.
Ross
--
Ross J. Reedstrom, Ph.D., <[email protected]>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 21:56 Ross J. Reedstrom <[email protected]>
parent: Hannu Krosing <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Ross J. Reedstrom @ 2000-02-07 21:56 UTC (permalink / raw)
To: Hannu Krosing <[email protected]>; +Cc: pgsql-hackers
On Mon, Feb 07, 2000 at 11:44:14PM +0200, Hannu Krosing wrote:
> "Ross J. Reedstrom" wrote:
> >
> >
> > Anyone else interested, I'm using the tarball from:
> >
> > ftp://epoch.cs.berkeley.edu/pub/mariposa/src/alpha-1/mariposa-alpha-1.tar.gz
> >
>
> Is mariposa licence compatible with ours ?
It better be, it's the same license ;-) That is, Mariposa is a branch off
the Postgres95 tree. Actually, it's a good question: the PG95 license
would have let them put just about any license on Mariposa they wanted.
After running both COPYRIGHT files throught fmt, here's the diff output:
wallace$ diff COPYRIGHT COPYRIGHT.pgsql
1c1,2
< Mariposa Distributed Data Base Management System
---
> PostgreSQL Data Base Management System (formerly known as Postgres,
> then as Postgres95).
3c4
< Copyright (c) 1994-6 Regents of the University of California
---
> Copyright (c) 1994-7 Regents of the University of California
21d21
<
wallace$
So, it is word for word the PostgreSQL license.
Ross
--
Ross J. Reedstrom, Ph.D., <[email protected]>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 22:04 Hannu Krosing <[email protected]>
parent: Bruce Momjian <[email protected]>
2 siblings, 0 replies; 275+ messages in thread
From: Hannu Krosing @ 2000-02-07 22:04 UTC (permalink / raw)
To: Bruce Momjian <[email protected]>; +Cc: Ross J. Reedstrom <[email protected]>; pgsql-hackers
Bruce Momjian wrote:
>
> > This is a Call For Hackers:
> >
> > Some time ago, I floated a little discussion on this list about doing
> > some distributed database work with PostgreSQL. The project got back
> > burnered at work, but now has a timeline for needing a solution "this
> > summer." Recent discussions on this list about Postgres's historical
> > object roots got me back to the Berkeley db sites, and reminded me about
> > Mariposa, which is Stonebraker's take on distributed DBs.
> >
> > http://s2k-ftp.cs.berkeley.edu:8000:8000/mariposa/
It has a nice concept of simulating free market for distributed query
optimisation. Auctions, brokers and all ...
>
> I have looked at the code. I have files that show all the diffs they
> made to it and they have some new files. It was hard for me to see what
> they were doing. Looks like they hacked up the executor and put in some
> translation layer to talk to some databroker.
The broker was for determining where to get the data from - as each table
could be queried from several sites there had to be a mechanism for the
planner to figure out the cheapest (or fastest if "money" was not a problem)
> It seems like an awfully
> complicated way to do it. I would not bother getting it to run, but
> figure out what they were trying to do, and why, and see how we can
> implement it. My guess is that they had one central server for each
> table, and you went to that server to get information.
They would not have needed the broker for such a simple scheme
IIRC they had no central table, but they doubled the length of oid and
made it to include the site id of the site that created the tuple.
It could be that they restricted changing a tuple to that site ?
The site to go for information was determined by an auction where each site
offered speed and cost for looking up the data. Usually the didn't also
quarantee the latest data, just the "best effort".
-------------------
Hannu
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 22:19 Don Baccus <[email protected]>
parent: Bruce Momjian <[email protected]>
2 siblings, 2 replies; 275+ messages in thread
From: Don Baccus @ 2000-02-07 22:19 UTC (permalink / raw)
To: Hannu Krosing <[email protected]>; Bruce Momjian <[email protected]>; +Cc: Ross J. Reedstrom <[email protected]>; pgsql-hackers
At 12:04 AM 2/8/00 +0200, Hannu Krosing wrote:
>The site to go for information was determined by an auction where each site
>offered speed and cost for looking up the data. Usually the didn't also
>quarantee the latest data, just the "best effort".
I just glanced at the website. They explicitly mention that they don't
require global synchronization, because it would slow down response time
for many things (with thousands of server, that sounds like an
understatement).
So, yes, it would appear they don't guarantee the latest data.
- Don Baccus, Portland OR <[email protected]>
Nature photos, on-line guides, Pacific Northwest
Rare Bird Alert Service and other goodies at
http://donb.photo.net.
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 22:57 Ross J. Reedstrom <[email protected]>
parent: Don Baccus <[email protected]>
1 sibling, 0 replies; 275+ messages in thread
From: Ross J. Reedstrom @ 2000-02-07 22:57 UTC (permalink / raw)
To: pgsql-hackers
Seems there was more than just going back to the Berkeley site that
reminded me of Mariposa. A principle new functionality in Mariposa is
the ability to 'fragment' a class, based on a user-defined partitioning
function. The example used is a widgets class, which is partitioned on
the 'location' field (i.e., the warehouse the widget is stored in)
CREATE TABLE widgets (
part_no int4,
location char16,
on_hand int4,
on_order int4,
commited int4
) PARTITION ON LOCATION USING btchar16cmp;
Then, the table is filled with tuples, all containing locations of either
'Miami' or 'New York'.
SELECT * from widgets;
works as expected.
Later, this table is fragmented:
SPLIT FRAGMENT widgets INTO widgets_mi, widgets_ny AT 'Miami';
Now, the original table widgets is _empty_: all the tuples with location <=
'Miami' go to widgets_mi, location > 'Miami' go to widgets_ny.
SELECT * from widgets;
Still returns all the tuples! So, this works sort of the way Chris Bitmead
has implemented subclasses: widgets_mi and widgets_ny are subclasses of
the widgets class, so selects return everything below. They differ in
that only PARTITIONed classes can be FRAGMENTed.
The distributed part comes in with the MOVE FRAGMENT command. This
transfers the 'master' copy of a table to the designated host, so future
access to that FRAGMENT will go over the network.
There's also a COPY FRAGMENT command, that sets up a local cache of a
fragment, with a periodic update time. These copies may be either
READONLY, or (default) READ/WRITE. Seems updates are timed only (simple
extension would be to implement write through behavior)
All this is coming from the Mariposa User's Manual, which is an extended
version of the Postgres95 User's Manual.
As to latest vs. best effort: One defines a BidCurve, who's dimensions are
Cost and Time. A flat curve should get you that latest data. And, since
the DataBroker and Bidder are both implemented as Tcl scripts, so it
would be possible to define a bid policy that only buys the latest data,
regardless of how long it's going to take.
Oh, BTW, yes that does put _two_ interpreted Tcl scripts on the execution
path for every query. Wonder what _that'll_ do for execution time. However,
it's like planning/optimization time, in that it's spent per query, rather
than per tuple.
Ross
--
Ross J. Reedstrom, Ph.D., <[email protected]>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005
On Mon, Feb 07, 2000 at 02:19:56PM -0800, Don Baccus wrote:
> At 12:04 AM 2/8/00 +0200, Hannu Krosing wrote:
>
> >The site to go for information was determined by an auction where each site
> >offered speed and cost for looking up the data. Usually the didn't also
> >quarantee the latest data, just the "best effort".
>
> I just glanced at the website. They explicitly mention that they don't
> require global synchronization, because it would slow down response time
> for many things (with thousands of server, that sounds like an
> understatement).
>
> So, yes, it would appear they don't guarantee the latest data.
>
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-07 23:18 Don Baccus <[email protected]>
parent: Don Baccus <[email protected]>
1 sibling, 1 reply; 275+ messages in thread
From: Don Baccus @ 2000-02-07 23:18 UTC (permalink / raw)
To: Ross J. Reedstrom <[email protected]>; pgsql-hackers
At 04:57 PM 2/7/00 -0600, Ross J. Reedstrom wrote:
>CREATE TABLE widgets (
> part_no int4,
> location char16,
> on_hand int4,
> on_order int4,
> commited int4
>) PARTITION ON LOCATION USING btchar16cmp;
Oracle's partitioning is fixed, in other words once you choose a
condition to split on, you can't change it. In other words, in
your example:
>Then, the table is filled with tuples, all containing locations of either
>'Miami' or 'New York'.
After splitting the table into ">'Miami'" and "<='Miami" fragments,
I've been told that you can't (say) change it to ">'Boston'" and
have the proper rows move automatically.
In practice, partioning is often used to split tables on dates. You
might want to partion off your old tax data at the 7-yr old mark, and
each year as you do your taxes move the oldest tax data in your
"recent taxes" table split off to your "older taxes" table.
Apparently, Informix is smart enough to do this for you.
Since a couple of the people associated with the project are Informix
people, do you have any idea if Mariposa is able to do this?
>
>SELECT * from widgets;
>
>works as expected.
>
>Later, this table is fragmented:
>
>SPLIT FRAGMENT widgets INTO widgets_mi, widgets_ny AT 'Miami';
In other words some sort of "update the two tables AT <some new criteria>"
Whatever the answer to my question, Mariposa certainly looks interesting.
It's functionality that folks who do data warehousing really need.
>Oh, BTW, yes that does put _two_ interpreted Tcl scripts on the execution
>path for every query. Wonder what _that'll_ do for execution time. However,
>it's like planning/optimization time, in that it's spent per query, rather
>than per tuple.
Probably not as bad as you think, if they're simple and short. Once
someone has this up and running and integrated with PostgreSQL and
robust and reliable we can measure it and change to something else if
necessary :)
- Don Baccus, Portland OR <[email protected]>
Nature photos, on-line guides, Pacific Northwest
Rare Bird Alert Service and other goodies at
http://donb.photo.net.
^ permalink raw reply [nested|flat] 275+ messages in thread
* Re: [HACKERS] CFH: Mariposa, distributed DB
@ 2000-02-08 15:44 Karel Zak - Zakkr <[email protected]>
parent: Don Baccus <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Karel Zak - Zakkr @ 2000-02-08 15:44 UTC (permalink / raw)
To: Don Baccus <[email protected]>; +Cc: Ross J. Reedstrom <[email protected]>; pgsql-hackers
Hi,
the Mariposa db distribution is interesting, but it is very specific. If I
good understand it is not real-time and global synchronized DB replication.
But for a lot of users (and me) is probably interestion on-line DB replication
and synchronization. How much users have 10K servers?
I explore current PG's source and is probably possible create support for
on-line replication. My idea is replicate data on a heap_ layout. The parser,
planer and executor run on local backend and replicate straight-out tuples
to the others servers (nodes). It needs synchronize PG's locks too.
In near future I want start project for PG on-line replication. Or works on
this anyone now? Comments?
Karel
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
0 siblings, 0 replies; 275+ messages in thread
From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)
This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
src/include/portability/instr_time.h | 24 ++++++++++++++------
src/backend/access/transam/xlog.c | 11 +++-------
src/backend/storage/buffer/bufmgr.c | 16 +++++---------
src/backend/storage/file/buffile.c | 16 +++++---------
src/backend/storage/ipc/latch.c | 8 +++----
src/bin/psql/common.c | 33 +++++++++++++---------------
6 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
*
* INSTR_TIME_IS_LT(x, y) x < y
*
+ * INSTR_TIME_ZERO() an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT() an instr_time set to current time
+ *
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
#define PG_INSTR_CLOCK CLOCK_REALTIME
#endif
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_clock_gettime_ns(void)
{
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
return now;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+ pg_clock_gettime_ns()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
/* Use QueryPerformanceCounter() */
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
static inline instr_time
pg_query_performance_counter(void)
{
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
return (double) f.QuadPart;
}
-#define INSTR_TIME_SET_CURRENT(t) \
- ((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+ pg_query_performance_counter()
#define INSTR_TIME_SET_SECONDS(t, s) \
((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
#define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks)
-#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
+#define INSTR_TIME_ZERO(t) (instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+ (t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+ ((t) = INSTR_TIME_ZERO())
#define INSTR_TIME_SET_CURRENT_LAZY(t) \
(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Size nbytes;
Size nleft;
int written;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
/* OK to write the page(s) */
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
/* Measure I/O timing to write WAL data */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
*/
if (track_wal_io_timing)
{
- instr_time duration;
+ instr_time duration = INSTR_TIME_CURRENT();
- INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
}
@@ -8137,7 +8134,7 @@ void
issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
{
char *msg = NULL;
- instr_time start;
+ instr_time start = INSTR_TIME_ZERO();
Assert(tli != 0);
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
/* Measure I/O timing to sync the WAL file */
if (track_wal_io_timing)
INSTR_TIME_SET_CURRENT(start);
- else
- INSTR_TIME_SET_ZERO(start);
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
MemSet((char *) bufBlock, 0, BLCKSZ);
else
{
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
XLogRecPtr recptr;
ErrorContextCallback errcallback;
- instr_time io_start,
- io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
Block bufBlock;
char *bufToWrite;
uint32 buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
BufFileLoadBuffer(BufFile *file)
{
File thisfile;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
/*
* Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
while (wpos < file->nbytes)
{
off_t availbytes;
- instr_time io_start;
- instr_time io_time;
+ instr_time io_start = INSTR_TIME_ZERO();
/*
* Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
INSTR_TIME_SET_CURRENT(io_start);
- else
- INSTR_TIME_SET_ZERO(io_start);
bytestowrite = FileWrite(thisfile,
file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
if (track_io_timing)
{
- INSTR_TIME_SET_CURRENT(io_time);
+ instr_time io_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(io_time, io_start);
INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
uint32 wait_event_info)
{
int returned_events = 0;
- instr_time start_time;
- instr_time cur_time;
+ instr_time start_time = INSTR_TIME_ZERO();
long cur_timeout = -1;
Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
}
- else
- INSTR_TIME_SET_ZERO(start_time);
pgstat_report_wait_start(wait_event_info);
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* If we're not done, update cur_timeout for next iteration */
if (returned_events == 0 && timeout >= 0)
{
- INSTR_TIME_SET_CURRENT(cur_time);
+ instr_time cur_time = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
bool timing = pset.timing;
PGresult *result;
bool OK;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
*elapsed_msec = 0;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/*
* To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
{
bool timing = pset.timing;
bool success;
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
PGresult *result;
FILE *gfile_fout = NULL;
bool gfile_is_pipe = false;
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
if (pset.bind_flag)
success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
*/
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
int ntuples;
int fetch_count;
char fetch_cmd[64];
- instr_time before,
- after;
+ instr_time before = INSTR_TIME_ZERO();
int flush_error;
*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
INSTR_TIME_SET_CURRENT(before);
- else
- INSTR_TIME_SET_ZERO(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
@@ -1926,7 +1922,8 @@ cleanup:
if (timing)
{
- INSTR_TIME_SET_CURRENT(after);
+ instr_time after = INSTR_TIME_CURRENT();
+
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
--
2.38.0
--vt42kbp2j7rjcapp--
^ permalink raw reply [nested|flat] 275+ messages in thread
end of thread, other threads:[~2023-01-21 00:09 UTC | newest]
Thread overview: 275+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-02-07 21:11 CFH: Mariposa, distributed DB Ross J. Reedstrom <[email protected]>
2000-02-07 21:23 ` Bruce Momjian <[email protected]>
2000-02-07 21:50 ` Ross J. Reedstrom <[email protected]>
2000-02-07 22:04 ` Hannu Krosing <[email protected]>
2000-02-07 22:19 ` Don Baccus <[email protected]>
2000-02-07 22:57 ` Ross J. Reedstrom <[email protected]>
2000-02-07 23:18 ` Don Baccus <[email protected]>
2000-02-08 15:44 ` Karel Zak - Zakkr <[email protected]>
2000-02-07 21:44 ` Hannu Krosing <[email protected]>
2000-02-07 21:56 ` Ross J. Reedstrom <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox